Annotation of parser3/src/main/pa_os.C, revision 1.28
1.1 paf 1: /** @file
2: Parser: commonly functions.
3:
1.26 moko 4: Copyright (c) 2001-2026 Art. Lebedev Studio (https://www.artlebedev.com)
1.20 moko 5: Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru>
1.1 paf 6: */
7:
8: #include "pa_config_includes.h"
9: #include "pa_os.h"
10:
1.28 ! moko 11: volatile const char * IDENT_PA_OS_C="$Id: pa_os.C,v 1.27 2026/10/02 19:57:10 moko Exp $" IDENT_PA_OS_H;
1.17 moko 12:
13: unsigned int pa_lock_attempts=PA_LOCK_ATTEMPTS;
1.12 moko 14:
1.13 moko 15: #ifdef _MSC_VER
16: #include <windows.h>
1.22 moko 17:
18: #define PA_SH_LOCK 2
19: #define PA_EX_LOCK 1
20: #define PA_ULOCK 0
21: #define FLOCK(operation) int status=pa_flock(fd, operation);
22:
23: int pa_flock(int fd, int operation) {
24: HANDLE hFile = (HANDLE)_get_osfhandle(fd);
25: if (hFile == INVALID_HANDLE_VALUE) {
26: return -1;
27: }
28:
29: OVERLAPPED overlapped = {0};
30:
31: if (operation == PA_ULOCK) {
1.27 moko 32: return pa_set_errno(UnlockFileEx(hFile, 0, MAXDWORD, MAXDWORD, &overlapped)) ? 0 : -1;
1.22 moko 33: } else {
34: DWORD flags = LOCKFILE_FAIL_IMMEDIATELY;
35: if (operation == PA_EX_LOCK) {
36: flags |= LOCKFILE_EXCLUSIVE_LOCK;
37: }
1.27 moko 38: return pa_set_errno(LockFileEx(hFile, flags, 0, MAXDWORD, MAXDWORD, &overlapped)) ? 0 : -1;
1.22 moko 39: }
40: }
1.23 moko 41:
1.27 moko 42: static int pa_errno() {
1.23 moko 43: switch(GetLastError()) {
1.25 moko 44: case ERROR_LOCK_VIOLATION: // real case: returning the same error as with _locking
45: return EACCES;
1.23 moko 46: case ERROR_IO_PENDING:
47: return EAGAIN;
48: case ERROR_INVALID_HANDLE:
49: return EBADF;
50: case ERROR_NOT_LOCKED:
1.24 moko 51: return ENOLCK;
1.27 moko 52: case ERROR_FILE_NOT_FOUND: // the same errors as with opendir
53: case ERROR_PATH_NOT_FOUND:
54: return ENOENT;
55: case ERROR_DIRECTORY:
56: return ENOTDIR;
1.23 moko 57: }
1.24 moko 58: return EACCES;
1.23 moko 59: }
60:
1.27 moko 61: int pa_set_errno(int status) {
62: if (!status)
63: errno = pa_errno();
64: return status;
65: }
66:
1.22 moko 67: #else
1.1 paf 68:
69: #ifdef HAVE_FLOCK
70:
1.9 misha 71: #define PA_SH_LOCK LOCK_SH|LOCK_NB
72: #define PA_EX_LOCK LOCK_EX|LOCK_NB
73: #define PA_ULOCK LOCK_UN
74: #define FLOCK(operation) int status=flock(fd, operation);
1.1 paf 75:
76: #else
77: #ifdef HAVE_FCNTL
78:
1.9 misha 79: #define PA_SH_LOCK F_RDLCK
80: #define PA_EX_LOCK F_WRLCK
81: #define PA_ULOCK F_UNLCK
82: #define FLOCK(operation) struct flock ls={operation, SEEK_SET}; int status=fcntl(fd, F_SETLK, &ls);
1.1 paf 83:
84: #else
85: #ifdef HAVE_LOCKF
86:
1.9 misha 87: #define PA_SH_LOCK F_TLOCK
88: #define PA_EX_LOCK F_TLOCK
89: #define PA_ULOCK F_ULOCK
90: #define FLOCK(operation) lseek(fd, 0, SEEK_SET); int status=lockf(fd, operation, 1);
1.1 paf 91:
92: #else
93:
94: #error unable to find file locking func
95:
96: #endif
97: #endif
98: #endif
1.22 moko 99:
1.1 paf 100: #endif
101:
1.9 misha 102: int pa_lock(int fd, int attempts, int operation){
103: while(true){
104: FLOCK(operation);
1.16 moko 105: if(status==0)
106: return 0;
107: if(--attempts<=0)
1.27 moko 108: return errno;
1.10 misha 109: pa_sleep(PA_LOCK_WAIT_TIMEOUT_SECS, PA_LOCK_WAIT_TIMEOUT_USECS);
1.9 misha 110: }
111: };
112:
113: int pa_lock_shared_blocking(int fd) {
1.17 moko 114: return pa_lock(fd, pa_lock_attempts, PA_SH_LOCK);
1.9 misha 115: }
116:
117: int pa_lock_exclusive_blocking(int fd) {
1.17 moko 118: return pa_lock(fd, pa_lock_attempts, PA_EX_LOCK);
1.9 misha 119: }
120:
121: int pa_lock_exclusive_nonblocking(int fd) {
122: return pa_lock(fd, 1, PA_EX_LOCK);
123: }
124:
125: int pa_unlock(int fd) {
126: return pa_lock(fd, 1, PA_ULOCK);
127: }
128:
129:
1.1 paf 130: int pa_sleep(unsigned long secs, unsigned long usecs) {
1.11 moko 131: if(usecs >= 1000000){
132: secs += usecs/1000000;
133: usecs = usecs%1000000;
134: }
1.1 paf 135:
1.14 moko 136: #ifdef _MSC_VER
1.1 paf 137: Sleep(secs * 1000 + usecs / 1000);
138: return 0;
139: #else
1.28 ! moko 140: struct timespec t;
1.1 paf 141: t.tv_sec = secs;
1.28 ! moko 142: t.tv_nsec = usecs * 1000;
! 143: // GC stops the threads with signals, the sleep goes on for the time left
! 144: int result;
! 145: while((result=nanosleep(&t, &t))<0 && errno==EINTR);
! 146: return result<0 ? errno : 0;
1.1 paf 147: #endif
148: }
E-mail: