Annotation of parser3/src/main/pa_os.C, revision 1.10

1.1       paf         1: /** @file
                      2:        Parser: commonly functions.
                      3: 
1.8       paf         4:        Copyright(c) 2001-2005 ArtLebedev Group (http://www.artlebedev.com)
1.1       paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
                      6: */
                      7: 
1.10    ! misha       8: static const char * const IDENT_COMMON_C="$Date: 2007/05/24 10:27:18 $"; 
1.1       paf         9: 
                     10: #include "pa_config_includes.h"
                     11: #include "pa_os.h"
                     12: 
                     13: #      if defined(WIN32)
                     14: #              include <windows.h>
                     15: #      endif
                     16: 
                     17: #ifdef HAVE_FLOCK
                     18: 
1.9       misha      19: #define PA_SH_LOCK LOCK_SH|LOCK_NB
                     20: #define PA_EX_LOCK LOCK_EX|LOCK_NB
                     21: #define PA_ULOCK LOCK_UN
                     22: #define FLOCK(operation) int status=flock(fd, operation);
1.1       paf        23: 
                     24: #else
                     25: #ifdef HAVE__LOCKING
                     26: 
1.9       misha      27: #define PA_SH_LOCK _LK_NBLCK
                     28: #define PA_EX_LOCK _LK_NBLCK
                     29: #define PA_ULOCK _LK_UNLCK
                     30: #define FLOCK(operation) lseek(fd, 0, SEEK_SET); int status=_locking(fd, operation, 1);
1.1       paf        31: 
                     32: #else
                     33: #ifdef HAVE_FCNTL
                     34: 
1.9       misha      35: #define PA_SH_LOCK F_RDLCK
                     36: #define PA_EX_LOCK F_WRLCK
                     37: #define PA_ULOCK F_UNLCK
                     38: #define FLOCK(operation) struct flock ls={operation, SEEK_SET}; int status=fcntl(fd, F_SETLK, &ls); 
1.1       paf        39: 
                     40: #else
                     41: #ifdef HAVE_LOCKF
                     42: 
1.9       misha      43: #define PA_SH_LOCK F_TLOCK
                     44: #define PA_EX_LOCK F_TLOCK
                     45: #define PA_ULOCK F_ULOCK
                     46: #define FLOCK(operation) lseek(fd, 0, SEEK_SET); int status=lockf(fd, operation, 1);
1.1       paf        47: 
                     48: #else
                     49: 
                     50: #error unable to find file locking func
                     51: 
                     52: #endif
                     53: #endif
                     54: #endif
                     55: #endif
                     56: 
1.9       misha      57: int pa_lock(int fd, int attempts, int operation){
                     58:        while(true){
                     59:                FLOCK(operation);
                     60:                attempts--;
                     61:                if(status==0 || attempts<=0){
                     62:                        return status;
                     63:                }
1.10    ! misha      64:                pa_sleep(PA_LOCK_WAIT_TIMEOUT_SECS, PA_LOCK_WAIT_TIMEOUT_USECS);
1.9       misha      65:        }
                     66: };
                     67: 
                     68: int pa_lock_shared_blocking(int fd) {
                     69:        return pa_lock(fd, PA_LOCK_ATTEMPTS, PA_SH_LOCK);
                     70: }
                     71: 
                     72: int pa_lock_exclusive_blocking(int fd) {
                     73:        return pa_lock(fd, PA_LOCK_ATTEMPTS, PA_EX_LOCK);
                     74: }
                     75: 
                     76: int pa_lock_exclusive_nonblocking(int fd) {
                     77:        return pa_lock(fd, 1, PA_EX_LOCK);
                     78: }
                     79: 
                     80: int pa_unlock(int fd) {
                     81:        return pa_lock(fd, 1, PA_ULOCK);
                     82: }
                     83: 
                     84: 
1.1       paf        85: int pa_sleep(unsigned long secs, unsigned long usecs) {
                     86:        for (;  usecs >= 1000000; ++secs, usecs -= 1000000); 
                     87: 
                     88: #ifdef WIN32
                     89:        Sleep(secs * 1000 + usecs / 1000); 
                     90:        return 0;
                     91: #else
                     92:        struct timeval t;
                     93:        t.tv_sec = secs;
                     94:        t.tv_usec = usecs;
1.6       paf        95:        return (select(0, NULL, NULL, NULL, &t)<0 ? errno : 0); 
1.1       paf        96: #endif
                     97: }

E-mail: