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

1.1       paf         1: /** @file
                      2:        Parser: commonly functions.
                      3: 
1.18    ! moko        4:        Copyright (c) 2001-2017 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.18    ! moko       11: 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; 
1.17      moko       12: 
                     13: unsigned int pa_lock_attempts=PA_LOCK_ATTEMPTS;
1.12      moko       14: 
1.13      moko       15: #ifdef _MSC_VER
                     16: #include <windows.h>
                     17: #endif
1.1       paf        18: 
                     19: #ifdef HAVE_FLOCK
                     20: 
1.9       misha      21: #define PA_SH_LOCK LOCK_SH|LOCK_NB
                     22: #define PA_EX_LOCK LOCK_EX|LOCK_NB
                     23: #define PA_ULOCK LOCK_UN
                     24: #define FLOCK(operation) int status=flock(fd, operation);
1.1       paf        25: 
                     26: #else
                     27: #ifdef HAVE__LOCKING
                     28: 
1.9       misha      29: #define PA_SH_LOCK _LK_NBLCK
                     30: #define PA_EX_LOCK _LK_NBLCK
                     31: #define PA_ULOCK _LK_UNLCK
                     32: #define FLOCK(operation) lseek(fd, 0, SEEK_SET); int status=_locking(fd, operation, 1);
1.1       paf        33: 
                     34: #else
                     35: #ifdef HAVE_FCNTL
                     36: 
1.9       misha      37: #define PA_SH_LOCK F_RDLCK
                     38: #define PA_EX_LOCK F_WRLCK
                     39: #define PA_ULOCK F_UNLCK
                     40: #define FLOCK(operation) struct flock ls={operation, SEEK_SET}; int status=fcntl(fd, F_SETLK, &ls); 
1.1       paf        41: 
                     42: #else
                     43: #ifdef HAVE_LOCKF
                     44: 
1.9       misha      45: #define PA_SH_LOCK F_TLOCK
                     46: #define PA_EX_LOCK F_TLOCK
                     47: #define PA_ULOCK F_ULOCK
                     48: #define FLOCK(operation) lseek(fd, 0, SEEK_SET); int status=lockf(fd, operation, 1);
1.1       paf        49: 
                     50: #else
                     51: 
                     52: #error unable to find file locking func
                     53: 
                     54: #endif
                     55: #endif
                     56: #endif
                     57: #endif
                     58: 
1.9       misha      59: int pa_lock(int fd, int attempts, int operation){
                     60:        while(true){
                     61:                FLOCK(operation);
1.16      moko       62:                if(status==0)
                     63:                        return 0;
                     64:                if(--attempts<=0)
                     65:                        return errno;
1.10      misha      66:                pa_sleep(PA_LOCK_WAIT_TIMEOUT_SECS, PA_LOCK_WAIT_TIMEOUT_USECS);
1.9       misha      67:        }
                     68: };
                     69: 
                     70: int pa_lock_shared_blocking(int fd) {
1.17      moko       71:        return pa_lock(fd, pa_lock_attempts, PA_SH_LOCK);
1.9       misha      72: }
                     73: 
                     74: int pa_lock_exclusive_blocking(int fd) {
1.17      moko       75:        return pa_lock(fd, pa_lock_attempts, PA_EX_LOCK);
1.9       misha      76: }
                     77: 
                     78: int pa_lock_exclusive_nonblocking(int fd) {
                     79:        return pa_lock(fd, 1, PA_EX_LOCK);
                     80: }
                     81: 
                     82: int pa_unlock(int fd) {
                     83:        return pa_lock(fd, 1, PA_ULOCK);
                     84: }
                     85: 
                     86: 
1.1       paf        87: int pa_sleep(unsigned long secs, unsigned long usecs) {
1.11      moko       88:        if(usecs >= 1000000){
                     89:                        secs += usecs/1000000;
                     90:                        usecs = usecs%1000000;
                     91:        }
1.1       paf        92: 
1.14      moko       93: #ifdef _MSC_VER
1.1       paf        94:        Sleep(secs * 1000 + usecs / 1000); 
                     95:        return 0;
                     96: #else
                     97:        struct timeval t;
                     98:        t.tv_sec = secs;
                     99:        t.tv_usec = usecs;
1.6       paf       100:        return (select(0, NULL, NULL, NULL, &t)<0 ? errno : 0); 
1.1       paf       101: #endif
                    102: }

E-mail: