|
|
| version 1.16, 2016/11/30 17:01:01 | version 1.25, 2024/11/04 17:47:46 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: commonly functions. | Parser: commonly functions. |
| Copyright (c) 2001-2015 Art. Lebedev Studio (http://www.artlebedev.com) | Copyright (c) 2001-2024 Art. Lebedev Studio (http://www.artlebedev.com) |
| Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) | Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru> |
| */ | */ |
| #include "pa_config_includes.h" | #include "pa_config_includes.h" |
| Line 10 | Line 10 |
| volatile const char * IDENT_PA_OS_C="$Id$" IDENT_PA_OS_H; | volatile const char * IDENT_PA_OS_C="$Id$" IDENT_PA_OS_H; |
| unsigned int pa_lock_attempts=PA_LOCK_ATTEMPTS; | |
| #ifdef _MSC_VER | #ifdef _MSC_VER |
| #include <windows.h> | #include <windows.h> |
| #endif | |
| #define PA_SH_LOCK 2 | |
| #define PA_EX_LOCK 1 | |
| #define PA_ULOCK 0 | |
| #define FLOCK(operation) int status=pa_flock(fd, operation); | |
| #define ERRNO pa_errno() | |
| int pa_flock(int fd, int operation) { | |
| HANDLE hFile = (HANDLE)_get_osfhandle(fd); | |
| if (hFile == INVALID_HANDLE_VALUE) { | |
| return -1; | |
| } | |
| OVERLAPPED overlapped = {0}; | |
| if (operation == PA_ULOCK) { | |
| return UnlockFileEx(hFile, 0, MAXDWORD, MAXDWORD, &overlapped) ? 0 : -1; | |
| } else { | |
| DWORD flags = LOCKFILE_FAIL_IMMEDIATELY; | |
| if (operation == PA_EX_LOCK) { | |
| flags |= LOCKFILE_EXCLUSIVE_LOCK; | |
| } | |
| return LockFileEx(hFile, flags, 0, MAXDWORD, MAXDWORD, &overlapped) ? 0 : -1; | |
| } | |
| } | |
| int pa_errno() { | |
| switch(GetLastError()) { | |
| case ERROR_LOCK_VIOLATION: // real case: returning the same error as with _locking | |
| return EACCES; | |
| case ERROR_IO_PENDING: | |
| return EAGAIN; | |
| case ERROR_INVALID_HANDLE: | |
| return EBADF; | |
| case ERROR_NOT_LOCKED: | |
| return ENOLCK; | |
| } | |
| return EACCES; | |
| } | |
| #else | |
| #define ERRNO errno | |
| #ifdef HAVE_FLOCK | #ifdef HAVE_FLOCK |
| Line 22 volatile const char * IDENT_PA_OS_C="$Id | Line 66 volatile const char * IDENT_PA_OS_C="$Id |
| #define FLOCK(operation) int status=flock(fd, operation); | #define FLOCK(operation) int status=flock(fd, operation); |
| #else | #else |
| #ifdef HAVE__LOCKING | |
| #define PA_SH_LOCK _LK_NBLCK | |
| #define PA_EX_LOCK _LK_NBLCK | |
| #define PA_ULOCK _LK_UNLCK | |
| #define FLOCK(operation) lseek(fd, 0, SEEK_SET); int status=_locking(fd, operation, 1); | |
| #else | |
| #ifdef HAVE_FCNTL | #ifdef HAVE_FCNTL |
| #define PA_SH_LOCK F_RDLCK | #define PA_SH_LOCK F_RDLCK |
| Line 52 volatile const char * IDENT_PA_OS_C="$Id | Line 88 volatile const char * IDENT_PA_OS_C="$Id |
| #endif | #endif |
| #endif | #endif |
| #endif | #endif |
| #endif | #endif |
| int pa_lock(int fd, int attempts, int operation){ | int pa_lock(int fd, int attempts, int operation){ |
| Line 60 int pa_lock(int fd, int attempts, int op | Line 97 int pa_lock(int fd, int attempts, int op |
| if(status==0) | if(status==0) |
| return 0; | return 0; |
| if(--attempts<=0) | if(--attempts<=0) |
| return errno; | return ERRNO; |
| pa_sleep(PA_LOCK_WAIT_TIMEOUT_SECS, PA_LOCK_WAIT_TIMEOUT_USECS); | pa_sleep(PA_LOCK_WAIT_TIMEOUT_SECS, PA_LOCK_WAIT_TIMEOUT_USECS); |
| } | } |
| }; | }; |
| int pa_lock_shared_blocking(int fd) { | int pa_lock_shared_blocking(int fd) { |
| return pa_lock(fd, PA_LOCK_ATTEMPTS, PA_SH_LOCK); | return pa_lock(fd, pa_lock_attempts, PA_SH_LOCK); |
| } | } |
| int pa_lock_exclusive_blocking(int fd) { | int pa_lock_exclusive_blocking(int fd) { |
| return pa_lock(fd, PA_LOCK_ATTEMPTS, PA_EX_LOCK); | return pa_lock(fd, pa_lock_attempts, PA_EX_LOCK); |
| } | } |
| int pa_lock_exclusive_nonblocking(int fd) { | int pa_lock_exclusive_nonblocking(int fd) { |