--- parser3/src/main/pa_os.C 2004/07/15 06:38:10 1.7 +++ parser3/src/main/pa_os.C 2017/01/29 19:41:17 1.17 @@ -1,52 +1,51 @@ /** @file Parser: commonly functions. - Copyright(c) 2001-2004 ArtLebedev Group (http://www.artlebedev.com) + Copyright (c) 2001-2015 Art. Lebedev Studio (http://www.artlebedev.com) Author: Alexandr Petrosian (http://paf.design.ru) */ -static const char * const IDENT_COMMON_C="$Date: 2004/07/15 06:38:10 $"; - #include "pa_config_includes.h" #include "pa_os.h" -# if defined(WIN32) -# include -# endif +volatile const char * IDENT_PA_OS_C="$Id: pa_os.C,v 1.17 2017/01/29 19:41:17 moko Exp $" IDENT_PA_OS_H; + +unsigned int pa_lock_attempts=PA_LOCK_ATTEMPTS; + +#ifdef _MSC_VER +#include +#endif #ifdef HAVE_FLOCK -int pa_lock_shared_blocking(int fd) { return flock(fd, LOCK_SH); } -int pa_lock_exclusive_blocking(int fd) { return flock(fd, LOCK_EX); } -int pa_lock_exclusive_nonblocking(int fd) { return flock(fd, LOCK_EX || LOCK_NB); } -int pa_unlock(int fd) { return flock(fd, LOCK_UN); } +#define PA_SH_LOCK LOCK_SH|LOCK_NB +#define PA_EX_LOCK LOCK_EX|LOCK_NB +#define PA_ULOCK LOCK_UN +#define FLOCK(operation) int status=flock(fd, operation); #else #ifdef HAVE__LOCKING -#define FLOCK(operation) lseek(fd, 0, SEEK_SET); return _locking(fd, operation, 1) -int pa_lock_shared_blocking(int fd) { FLOCK(_LK_LOCK); } -int pa_lock_exclusive_blocking(int fd) { FLOCK(_LK_LOCK); } -int pa_lock_exclusive_nonblocking(int fd) { FLOCK(_LK_NBLCK); } -int pa_unlock(int fd) { FLOCK(_LK_UNLCK); } +#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 -#define FLOCK(cmd, arg) struct flock ls={arg, SEEK_SET}; return fcntl(fd, cmd, &ls) -int pa_lock_shared_blocking(int fd) { FLOCK(F_SETLKW, F_RDLCK); } -int pa_lock_exclusive_blocking(int fd) { FLOCK(F_SETLKW, F_WRLCK); } -int pa_lock_exclusive_nonblocking(int fd) { FLOCK(F_SETLK, F_RDLCK); } -int pa_unlock(int fd) { FLOCK(F_SETLK, F_UNLCK); } +#define PA_SH_LOCK F_RDLCK +#define PA_EX_LOCK F_WRLCK +#define PA_ULOCK F_UNLCK +#define FLOCK(operation) struct flock ls={operation, SEEK_SET}; int status=fcntl(fd, F_SETLK, &ls); #else #ifdef HAVE_LOCKF -#define FLOCK(fd, operation) lseek(fd, 0, SEEK_SET); return lockf(fd, operation, 1) -int pa_lock_shared_blocking(int fd) { FLOCK(F_LOCK); } // on intel solaris man doesn't have doc on shared blocking -int pa_lock_exclusive_blocking(int fd) { FLOCK(F_LOCK); } -int pa_lock_exclusive_nonblocking(int fd) { FLOCK(F_TLOCK); } -int pa_unlock(int fd) { FLOCK(F_TLOCK); } +#define PA_SH_LOCK F_TLOCK +#define PA_EX_LOCK F_TLOCK +#define PA_ULOCK F_ULOCK +#define FLOCK(operation) lseek(fd, 0, SEEK_SET); int status=lockf(fd, operation, 1); #else @@ -57,10 +56,41 @@ int pa_unlock(int fd) { FLOCK(F_TLOCK); #endif #endif +int pa_lock(int fd, int attempts, int operation){ + while(true){ + FLOCK(operation); + if(status==0) + return 0; + if(--attempts<=0) + return errno; + pa_sleep(PA_LOCK_WAIT_TIMEOUT_SECS, PA_LOCK_WAIT_TIMEOUT_USECS); + } +}; + +int pa_lock_shared_blocking(int fd) { + return pa_lock(fd, pa_lock_attempts, PA_SH_LOCK); +} + +int pa_lock_exclusive_blocking(int fd) { + return pa_lock(fd, pa_lock_attempts, PA_EX_LOCK); +} + +int pa_lock_exclusive_nonblocking(int fd) { + return pa_lock(fd, 1, PA_EX_LOCK); +} + +int pa_unlock(int fd) { + return pa_lock(fd, 1, PA_ULOCK); +} + + 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); return 0; #else