|
|
| version 1.10, 2007/05/24 10:32:57 | version 1.22, 2024/11/04 15:42:31 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: commonly functions. | Parser: commonly functions. |
| Copyright(c) 2001-2005 ArtLebedev Group (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> |
| */ | */ |
| static const char * const IDENT_COMMON_C="$Date$"; | |
| #include "pa_config_includes.h" | #include "pa_config_includes.h" |
| #include "pa_os.h" | #include "pa_os.h" |
| # if defined(WIN32) | volatile const char * IDENT_PA_OS_C="$Id$" IDENT_PA_OS_H; |
| # include <windows.h> | |
| # endif | unsigned int pa_lock_attempts=PA_LOCK_ATTEMPTS; |
| #ifdef _MSC_VER | |
| #include <windows.h> | |
| #define PA_SH_LOCK 2 | |
| #define PA_EX_LOCK 1 | |
| #define PA_ULOCK 0 | |
| #define FLOCK(operation) int status=pa_flock(fd, operation); | |
| 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; | |
| } | |
| } | |
| #else | |
| #ifdef HAVE_FLOCK | #ifdef HAVE_FLOCK |
| Line 22 static const char * const IDENT_COMMON_C | Line 48 static const char * const IDENT_COMMON_C |
| #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 static const char * const IDENT_COMMON_C | Line 70 static const char * const IDENT_COMMON_C |
| #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){ |
| while(true){ | while(true){ |
| FLOCK(operation); | FLOCK(operation); |
| attempts--; | if(status==0) |
| if(status==0 || attempts<=0){ | return 0; |
| return status; | if(--attempts<=0) |
| } | 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) { |
| Line 83 int pa_unlock(int fd) { | Line 102 int pa_unlock(int fd) { |
| int pa_sleep(unsigned long secs, unsigned long usecs) { | int pa_sleep(unsigned long secs, unsigned long usecs) { |
| for (; usecs >= 1000000; ++secs, usecs -= 1000000); | if(usecs >= 1000000){ |
| secs += usecs/1000000; | |
| usecs = usecs%1000000; | |
| } | |
| #ifdef WIN32 | #ifdef _MSC_VER |
| Sleep(secs * 1000 + usecs / 1000); | Sleep(secs * 1000 + usecs / 1000); |
| return 0; | return 0; |
| #else | #else |