Annotation of parser3/src/main/pa_os.C, revision 1.22
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.22 ! moko 11: volatile const char * IDENT_PA_OS_C="$Id: pa_os.C,v 1.21 2024/11/04 03:53:25 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) {
! 32: return UnlockFileEx(hFile, 0, MAXDWORD, MAXDWORD, &overlapped) ? 0 : -1;
! 33: } else {
! 34: DWORD flags = LOCKFILE_FAIL_IMMEDIATELY;
! 35: if (operation == PA_EX_LOCK) {
! 36: flags |= LOCKFILE_EXCLUSIVE_LOCK;
! 37: }
! 38: return LockFileEx(hFile, flags, 0, MAXDWORD, MAXDWORD, &overlapped) ? 0 : -1;
! 39: }
! 40: }
! 41: #else
1.1 paf 42:
43: #ifdef HAVE_FLOCK
44:
1.9 misha 45: #define PA_SH_LOCK LOCK_SH|LOCK_NB
46: #define PA_EX_LOCK LOCK_EX|LOCK_NB
47: #define PA_ULOCK LOCK_UN
48: #define FLOCK(operation) int status=flock(fd, operation);
1.1 paf 49:
50: #else
51: #ifdef HAVE_FCNTL
52:
1.9 misha 53: #define PA_SH_LOCK F_RDLCK
54: #define PA_EX_LOCK F_WRLCK
55: #define PA_ULOCK F_UNLCK
56: #define FLOCK(operation) struct flock ls={operation, SEEK_SET}; int status=fcntl(fd, F_SETLK, &ls);
1.1 paf 57:
58: #else
59: #ifdef HAVE_LOCKF
60:
1.9 misha 61: #define PA_SH_LOCK F_TLOCK
62: #define PA_EX_LOCK F_TLOCK
63: #define PA_ULOCK F_ULOCK
64: #define FLOCK(operation) lseek(fd, 0, SEEK_SET); int status=lockf(fd, operation, 1);
1.1 paf 65:
66: #else
67:
68: #error unable to find file locking func
69:
70: #endif
71: #endif
72: #endif
1.22 ! moko 73:
1.1 paf 74: #endif
75:
1.9 misha 76: int pa_lock(int fd, int attempts, int operation){
77: while(true){
78: FLOCK(operation);
1.16 moko 79: if(status==0)
80: return 0;
81: if(--attempts<=0)
82: return errno;
1.10 misha 83: pa_sleep(PA_LOCK_WAIT_TIMEOUT_SECS, PA_LOCK_WAIT_TIMEOUT_USECS);
1.9 misha 84: }
85: };
86:
87: int pa_lock_shared_blocking(int fd) {
1.17 moko 88: return pa_lock(fd, pa_lock_attempts, PA_SH_LOCK);
1.9 misha 89: }
90:
91: int pa_lock_exclusive_blocking(int fd) {
1.17 moko 92: return pa_lock(fd, pa_lock_attempts, PA_EX_LOCK);
1.9 misha 93: }
94:
95: int pa_lock_exclusive_nonblocking(int fd) {
96: return pa_lock(fd, 1, PA_EX_LOCK);
97: }
98:
99: int pa_unlock(int fd) {
100: return pa_lock(fd, 1, PA_ULOCK);
101: }
102:
103:
1.1 paf 104: int pa_sleep(unsigned long secs, unsigned long usecs) {
1.11 moko 105: if(usecs >= 1000000){
106: secs += usecs/1000000;
107: usecs = usecs%1000000;
108: }
1.1 paf 109:
1.14 moko 110: #ifdef _MSC_VER
1.1 paf 111: Sleep(secs * 1000 + usecs / 1000);
112: return 0;
113: #else
114: struct timeval t;
115: t.tv_sec = secs;
116: t.tv_usec = usecs;
1.6 paf 117: return (select(0, NULL, NULL, NULL, &t)<0 ? errno : 0);
1.1 paf 118: #endif
119: }
E-mail: