Annotation of parser3/src/main/pa_os.C, revision 1.26
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.26 ! moko 11: volatile const char * IDENT_PA_OS_C="$Id: pa_os.C,v 1.25 2024/11/04 17:47:46 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);
1.23 moko 22: #define ERRNO pa_errno()
1.22 moko 23:
24: int pa_flock(int fd, int operation) {
25: HANDLE hFile = (HANDLE)_get_osfhandle(fd);
26: if (hFile == INVALID_HANDLE_VALUE) {
27: return -1;
28: }
29:
30: OVERLAPPED overlapped = {0};
31:
32: if (operation == PA_ULOCK) {
33: return UnlockFileEx(hFile, 0, MAXDWORD, MAXDWORD, &overlapped) ? 0 : -1;
34: } else {
35: DWORD flags = LOCKFILE_FAIL_IMMEDIATELY;
36: if (operation == PA_EX_LOCK) {
37: flags |= LOCKFILE_EXCLUSIVE_LOCK;
38: }
39: return LockFileEx(hFile, flags, 0, MAXDWORD, MAXDWORD, &overlapped) ? 0 : -1;
40: }
41: }
1.23 moko 42:
43: int pa_errno() {
44: switch(GetLastError()) {
1.25 moko 45: case ERROR_LOCK_VIOLATION: // real case: returning the same error as with _locking
46: return EACCES;
1.23 moko 47: case ERROR_IO_PENDING:
48: return EAGAIN;
49: case ERROR_INVALID_HANDLE:
50: return EBADF;
51: case ERROR_NOT_LOCKED:
1.24 moko 52: return ENOLCK;
1.23 moko 53: }
1.24 moko 54: return EACCES;
1.23 moko 55: }
56:
1.22 moko 57: #else
1.1 paf 58:
1.23 moko 59: #define ERRNO errno
60:
1.1 paf 61: #ifdef HAVE_FLOCK
62:
1.9 misha 63: #define PA_SH_LOCK LOCK_SH|LOCK_NB
64: #define PA_EX_LOCK LOCK_EX|LOCK_NB
65: #define PA_ULOCK LOCK_UN
66: #define FLOCK(operation) int status=flock(fd, operation);
1.1 paf 67:
68: #else
69: #ifdef HAVE_FCNTL
70:
1.9 misha 71: #define PA_SH_LOCK F_RDLCK
72: #define PA_EX_LOCK F_WRLCK
73: #define PA_ULOCK F_UNLCK
74: #define FLOCK(operation) struct flock ls={operation, SEEK_SET}; int status=fcntl(fd, F_SETLK, &ls);
1.1 paf 75:
76: #else
77: #ifdef HAVE_LOCKF
78:
1.9 misha 79: #define PA_SH_LOCK F_TLOCK
80: #define PA_EX_LOCK F_TLOCK
81: #define PA_ULOCK F_ULOCK
82: #define FLOCK(operation) lseek(fd, 0, SEEK_SET); int status=lockf(fd, operation, 1);
1.1 paf 83:
84: #else
85:
86: #error unable to find file locking func
87:
88: #endif
89: #endif
90: #endif
1.22 moko 91:
1.1 paf 92: #endif
93:
1.9 misha 94: int pa_lock(int fd, int attempts, int operation){
95: while(true){
96: FLOCK(operation);
1.16 moko 97: if(status==0)
98: return 0;
99: if(--attempts<=0)
1.23 moko 100: return ERRNO;
1.10 misha 101: pa_sleep(PA_LOCK_WAIT_TIMEOUT_SECS, PA_LOCK_WAIT_TIMEOUT_USECS);
1.9 misha 102: }
103: };
104:
105: int pa_lock_shared_blocking(int fd) {
1.17 moko 106: return pa_lock(fd, pa_lock_attempts, PA_SH_LOCK);
1.9 misha 107: }
108:
109: int pa_lock_exclusive_blocking(int fd) {
1.17 moko 110: return pa_lock(fd, pa_lock_attempts, PA_EX_LOCK);
1.9 misha 111: }
112:
113: int pa_lock_exclusive_nonblocking(int fd) {
114: return pa_lock(fd, 1, PA_EX_LOCK);
115: }
116:
117: int pa_unlock(int fd) {
118: return pa_lock(fd, 1, PA_ULOCK);
119: }
120:
121:
1.1 paf 122: int pa_sleep(unsigned long secs, unsigned long usecs) {
1.11 moko 123: if(usecs >= 1000000){
124: secs += usecs/1000000;
125: usecs = usecs%1000000;
126: }
1.1 paf 127:
1.14 moko 128: #ifdef _MSC_VER
1.1 paf 129: Sleep(secs * 1000 + usecs / 1000);
130: return 0;
131: #else
132: struct timeval t;
133: t.tv_sec = secs;
134: t.tv_usec = usecs;
1.6 paf 135: return (select(0, NULL, NULL, NULL, &t)<0 ? errno : 0);
1.1 paf 136: #endif
137: }
E-mail: