Annotation of parser3/src/main/pa_os.C, revision 1.24
1.1 paf 1: /** @file
2: Parser: commonly functions.
3:
1.21 moko 4: Copyright (c) 2001-2024 Art. Lebedev Studio (http://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.24 ! moko 11: volatile const char * IDENT_PA_OS_C="$Id: pa_os.C,v 1.23 2024/11/04 16:38:47 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()) {
45: case ERROR_LOCK_VIOLATION:
46: case ERROR_SHARING_VIOLATION:
1.24 ! moko 47: return EBUSY;
1.23 moko 48: case ERROR_IO_PENDING:
49: return EAGAIN;
50: case ERROR_INVALID_HANDLE:
51: return EBADF;
52: case ERROR_NOT_LOCKED:
1.24 ! moko 53: return ENOLCK;
1.23 moko 54: }
1.24 ! moko 55: return EACCES;
1.23 moko 56: }
57:
1.22 moko 58: #else
1.1 paf 59:
1.23 moko 60: #define ERRNO errno
61:
1.1 paf 62: #ifdef HAVE_FLOCK
63:
1.9 misha 64: #define PA_SH_LOCK LOCK_SH|LOCK_NB
65: #define PA_EX_LOCK LOCK_EX|LOCK_NB
66: #define PA_ULOCK LOCK_UN
67: #define FLOCK(operation) int status=flock(fd, operation);
1.1 paf 68:
69: #else
70: #ifdef HAVE_FCNTL
71:
1.9 misha 72: #define PA_SH_LOCK F_RDLCK
73: #define PA_EX_LOCK F_WRLCK
74: #define PA_ULOCK F_UNLCK
75: #define FLOCK(operation) struct flock ls={operation, SEEK_SET}; int status=fcntl(fd, F_SETLK, &ls);
1.1 paf 76:
77: #else
78: #ifdef HAVE_LOCKF
79:
1.9 misha 80: #define PA_SH_LOCK F_TLOCK
81: #define PA_EX_LOCK F_TLOCK
82: #define PA_ULOCK F_ULOCK
83: #define FLOCK(operation) lseek(fd, 0, SEEK_SET); int status=lockf(fd, operation, 1);
1.1 paf 84:
85: #else
86:
87: #error unable to find file locking func
88:
89: #endif
90: #endif
91: #endif
1.22 moko 92:
1.1 paf 93: #endif
94:
1.9 misha 95: int pa_lock(int fd, int attempts, int operation){
96: while(true){
97: FLOCK(operation);
1.16 moko 98: if(status==0)
99: return 0;
100: if(--attempts<=0)
1.23 moko 101: return ERRNO;
1.10 misha 102: pa_sleep(PA_LOCK_WAIT_TIMEOUT_SECS, PA_LOCK_WAIT_TIMEOUT_USECS);
1.9 misha 103: }
104: };
105:
106: int pa_lock_shared_blocking(int fd) {
1.17 moko 107: return pa_lock(fd, pa_lock_attempts, PA_SH_LOCK);
1.9 misha 108: }
109:
110: int pa_lock_exclusive_blocking(int fd) {
1.17 moko 111: return pa_lock(fd, pa_lock_attempts, PA_EX_LOCK);
1.9 misha 112: }
113:
114: int pa_lock_exclusive_nonblocking(int fd) {
115: return pa_lock(fd, 1, PA_EX_LOCK);
116: }
117:
118: int pa_unlock(int fd) {
119: return pa_lock(fd, 1, PA_ULOCK);
120: }
121:
122:
1.1 paf 123: int pa_sleep(unsigned long secs, unsigned long usecs) {
1.11 moko 124: if(usecs >= 1000000){
125: secs += usecs/1000000;
126: usecs = usecs%1000000;
127: }
1.1 paf 128:
1.14 moko 129: #ifdef _MSC_VER
1.1 paf 130: Sleep(secs * 1000 + usecs / 1000);
131: return 0;
132: #else
133: struct timeval t;
134: t.tv_sec = secs;
135: t.tv_usec = usecs;
1.6 paf 136: return (select(0, NULL, NULL, NULL, &t)<0 ? errno : 0);
1.1 paf 137: #endif
138: }
E-mail: