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

1.1       paf         1: /** @file
                      2:        Parser: commonly functions.
                      3: 
1.12    ! moko        4:        Copyright (c) 2001-2012 Art. Lebedev Studio (http://www.artlebedev.com)
1.1       paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
                      6: */
                      7: 
                      8: #include "pa_config_includes.h"
                      9: #include "pa_os.h"
                     10: 
1.12    ! moko       11: volatile const char * IDENT_PA_OS_C="$Id: 2010-11-23 19:32:08 $" IDENT_PA_OS_H; 
        !            12: 
1.1       paf        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) {
1.11      moko       86:        if(usecs >= 1000000){
                     87:                        secs += usecs/1000000;
                     88:                        usecs = usecs%1000000;
                     89:        }
1.1       paf        90: 
                     91: #ifdef WIN32
                     92:        Sleep(secs * 1000 + usecs / 1000); 
                     93:        return 0;
                     94: #else
                     95:        struct timeval t;
                     96:        t.tv_sec = secs;
                     97:        t.tv_usec = usecs;
1.6       paf        98:        return (select(0, NULL, NULL, NULL, &t)<0 ? errno : 0); 
1.1       paf        99: #endif
                    100: }

E-mail: