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

1.1       paf         1: /** @file
                      2:        Parser: commonly functions.
                      3: 
                      4:        Copyright(c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com)
                      5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
                      6: */
                      7: 
1.2     ! paf         8: const char* IDENT_COMMON_C="$Date: 2003/11/06 08:50:26 $"; 
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: // locking constants
                     18: //#define PA_DEBUG_NO_LOCKING
                     19: 
                     20: #ifdef PA_DEBUG_NO_LOCKING
                     21: 
                     22: #ifdef HAVE_FLOCK
                     23: 
1.2     ! paf        24: int pa_lock_shared_blocking(int fd) { return flock(fd, LOCK_SH); }
        !            25: int pa_lock_exclusive_blocking(int fd) { return flock(fd, LOCK_EX); }
        !            26: int pa_lock_exclusive_nonblocking(int fd) { return flock(fd, LOCK_EX || LOCK_NB); }
        !            27: int pa_unlock(int fd) { return flock(fd, LOCK_UN); }
1.1       paf        28: 
                     29: #else
                     30: #ifdef HAVE__LOCKING
                     31: 
                     32: #define FLOCK(operation) lseek(fd, 0, SEEK_SET);  return _locking(fd, operation, 1)
1.2     ! paf        33: int pa_lock_shared_blocking(int fd) { FLOCK(_LK_LOCK); }
        !            34: int pa_lock_exclusive_blocking(int fd) { FLOCK(_LK_LOCK); }
        !            35: int pa_lock_exclusive_nonblocking(int fd) { FLOCK(_LK_NBLCK); }
        !            36: int pa_unlock(int fd) { FLOCK(_LK_UNLCK); }
1.1       paf        37: 
                     38: #else
                     39: #ifdef HAVE_FCNTL
                     40: 
                     41: #define FLOCK(cmd, arg) struct flock ls={arg, SEEK_SET};  return fcntl(fd, cmd, &ls)
1.2     ! paf        42: int pa_lock_shared_blocking(int fd) { FLOCK(F_SETLKW, F_RDLCK); }
        !            43: int pa_lock_exclusive_blocking(int fd) { FLOCK(F_SETLKW, F_WRLCK); }
        !            44: int pa_lock_exclusive_nonblocking(int fd) { FLOCK(F_SETLK, F_RDLCK); }
        !            45: int pa_unlock(int fd) { FLOCK(F_SETLK, F_UNLCK); }
1.1       paf        46: 
                     47: #else
                     48: #ifdef HAVE_LOCKF
                     49: 
                     50: #define FLOCK(fd, operation) lseek(fd, 0, SEEK_SET);  return lockf(fd, operation, 1)
1.2     ! paf        51: int pa_lock_shared_blocking(int fd) { FLOCK(F_LOCK); } // on intel solaris man doesn't have doc on shared blocking
        !            52: int pa_lock_exclusive_blocking(int fd) { FLOCK(F_LOCK); }
        !            53: int pa_lock_exclusive_nonblocking(int fd) { FLOCK(F_TLOCK); }
        !            54: int pa_unlock(int fd) { FLOCK(F_TLOCK); }
1.1       paf        55: 
                     56: #else
                     57: 
                     58: #error unable to find file locking func
                     59: 
                     60: #endif
                     61: #endif
                     62: #endif
                     63: #endif
                     64: 
                     65: #else
1.2     ! paf        66: int pa_lock_shared_blocking(int fd) { return 0; }
        !            67: int pa_lock_exclusive_blocking(int fd) { return 0; }
        !            68: int pa_lock_exclusive_nonblocking(int fd) { return 0; }
        !            69: int pa_unlock(int fd) { return 0; }
1.1       paf        70: 
                     71: #endif
                     72: 
                     73: int pa_sleep(unsigned long secs, unsigned long usecs) {
                     74:        for (;  usecs >= 1000000; ++secs, usecs -= 1000000); 
                     75: 
                     76: #ifdef WIN32
                     77:        Sleep(secs * 1000 + usecs / 1000); 
                     78:        return 0;
                     79: #else
                     80:        struct timeval t;
                     81:        t.tv_sec = secs;
                     82:        t.tv_usec = usecs;
                     83:        return (select(0, NULL, NULL, NULL, &t) == -1 ? errno : 0); 
                     84: #endif
                     85: }

E-mail: