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