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

1.1       paf         1: /** @file
                      2:        Parser: commonly functions.
                      3: 
1.26      moko        4:        Copyright (c) 2001-2026 Art. Lebedev Studio (https://www.artlebedev.com)
1.20      moko        5:        Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru>
1.1       paf         6: */
                      7: 
                      8: #include "pa_config_includes.h"
                      9: #include "pa_os.h"
                     10: 
1.29    ! moko       11: volatile const char * IDENT_PA_OS_C="$Id: pa_os.C,v 1.28 2026/10/17 22:58:09 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>
1.22      moko       17: 
                     18: #define PA_SH_LOCK 2
                     19: #define PA_EX_LOCK 1
                     20: #define PA_ULOCK 0
                     21: #define FLOCK(operation) int status=pa_flock(fd, operation);
                     22: 
                     23: int pa_flock(int fd, int operation) {
                     24:     HANDLE hFile = (HANDLE)_get_osfhandle(fd);
                     25:     if (hFile == INVALID_HANDLE_VALUE) {
                     26:         return -1;
                     27:     }
                     28: 
                     29:     OVERLAPPED overlapped = {0};
                     30: 
                     31:     if (operation == PA_ULOCK) {
1.27      moko       32:         return pa_set_errno(UnlockFileEx(hFile, 0, MAXDWORD, MAXDWORD, &overlapped)) ? 0 : -1;
1.22      moko       33:     } else {
                     34:         DWORD flags = LOCKFILE_FAIL_IMMEDIATELY;
                     35:         if (operation == PA_EX_LOCK) {
                     36:             flags |= LOCKFILE_EXCLUSIVE_LOCK;
                     37:         }
1.27      moko       38:         return pa_set_errno(LockFileEx(hFile, flags, 0, MAXDWORD, MAXDWORD, &overlapped)) ? 0 : -1;
1.22      moko       39:     }
                     40: }
1.23      moko       41: 
1.27      moko       42: static int pa_errno() {
1.23      moko       43:     switch(GetLastError()) {
1.25      moko       44:         case ERROR_LOCK_VIOLATION: // real case: returning the same error as with _locking
                     45:             return EACCES;
1.23      moko       46:         case ERROR_IO_PENDING:
                     47:             return EAGAIN;
                     48:         case ERROR_INVALID_HANDLE:
                     49:             return EBADF;
                     50:         case ERROR_NOT_LOCKED:
1.24      moko       51:             return ENOLCK;
1.27      moko       52:         case ERROR_FILE_NOT_FOUND: // the same errors as with opendir
                     53:         case ERROR_PATH_NOT_FOUND:
                     54:             return ENOENT;
                     55:         case ERROR_DIRECTORY:
                     56:             return ENOTDIR;
1.23      moko       57:     }
1.24      moko       58:     return EACCES;
1.23      moko       59: }
                     60: 
1.27      moko       61: int pa_set_errno(int status) {
                     62:     if (!status)
                     63:         errno = pa_errno();
                     64:     return status;
                     65: }
                     66: 
1.22      moko       67: #else
1.1       paf        68: 
                     69: #ifdef HAVE_FLOCK
                     70: 
1.9       misha      71: #define PA_SH_LOCK LOCK_SH|LOCK_NB
                     72: #define PA_EX_LOCK LOCK_EX|LOCK_NB
                     73: #define PA_ULOCK LOCK_UN
                     74: #define FLOCK(operation) int status=flock(fd, operation);
1.1       paf        75: 
                     76: #else
                     77: #ifdef HAVE_FCNTL
                     78: 
1.9       misha      79: #define PA_SH_LOCK F_RDLCK
                     80: #define PA_EX_LOCK F_WRLCK
                     81: #define PA_ULOCK F_UNLCK
                     82: #define FLOCK(operation) struct flock ls={operation, SEEK_SET}; int status=fcntl(fd, F_SETLK, &ls); 
1.1       paf        83: 
                     84: #else
                     85: #ifdef HAVE_LOCKF
                     86: 
1.9       misha      87: #define PA_SH_LOCK F_TLOCK
                     88: #define PA_EX_LOCK F_TLOCK
                     89: #define PA_ULOCK F_ULOCK
                     90: #define FLOCK(operation) lseek(fd, 0, SEEK_SET); int status=lockf(fd, operation, 1);
1.1       paf        91: 
                     92: #else
                     93: 
                     94: #error unable to find file locking func
                     95: 
                     96: #endif
                     97: #endif
                     98: #endif
1.22      moko       99: 
1.1       paf       100: #endif
                    101: 
1.9       misha     102: int pa_lock(int fd, int attempts, int operation){
                    103:        while(true){
                    104:                FLOCK(operation);
1.16      moko      105:                if(status==0)
                    106:                        return 0;
                    107:                if(--attempts<=0)
1.27      moko      108:                        return errno;
1.10      misha     109:                pa_sleep(PA_LOCK_WAIT_TIMEOUT_SECS, PA_LOCK_WAIT_TIMEOUT_USECS);
1.9       misha     110:        }
                    111: };
                    112: 
                    113: int pa_lock_shared_blocking(int fd) {
1.17      moko      114:        return pa_lock(fd, pa_lock_attempts, PA_SH_LOCK);
1.9       misha     115: }
                    116: 
                    117: int pa_lock_exclusive_blocking(int fd) {
1.17      moko      118:        return pa_lock(fd, pa_lock_attempts, PA_EX_LOCK);
1.9       misha     119: }
                    120: 
                    121: int pa_lock_exclusive_nonblocking(int fd) {
                    122:        return pa_lock(fd, 1, PA_EX_LOCK);
                    123: }
                    124: 
                    125: int pa_unlock(int fd) {
                    126:        return pa_lock(fd, 1, PA_ULOCK);
                    127: }
                    128: 
                    129: 
1.1       paf       130: int pa_sleep(unsigned long secs, unsigned long usecs) {
1.11      moko      131:        if(usecs >= 1000000){
                    132:                        secs += usecs/1000000;
                    133:                        usecs = usecs%1000000;
                    134:        }
1.1       paf       135: 
1.14      moko      136: #ifdef _MSC_VER
1.1       paf       137:        Sleep(secs * 1000 + usecs / 1000); 
                    138:        return 0;
                    139: #else
1.28      moko      140:        struct timespec t;
1.1       paf       141:        t.tv_sec = secs;
1.28      moko      142:        t.tv_nsec = usecs * 1000;
                    143:        // GC stops the threads with signals, the sleep goes on for the time left
                    144:        int result;
                    145:        while((result=nanosleep(&t, &t))<0 && errno==EINTR);
                    146:        return result<0 ? errno : 0;
1.1       paf       147: #endif
                    148: }
1.29    ! moko      149: 
        !           150: const char *pa_ctime(time_t t) {
        !           151: #ifdef _MSC_VER
        !           152:        const char *result=ctime(&t); // the buffer is per thread
        !           153: #else
        !           154:        static THREAD_LOCAL char buffer[32];
        !           155:        const char *result=ctime_r(&t, buffer);
        !           156: #endif
        !           157:        return result ? result : "";
        !           158: }
        !           159: 
        !           160: #ifdef __linux__
        !           161: 
        !           162: #include <sys/syscall.h>
        !           163: 
        !           164: #ifndef sigev_notify_thread_id
        !           165: #define sigev_notify_thread_id _sigev_un._tid
        !           166: #endif
        !           167: 
        !           168: bool pa_timer_start(pa_timer *timer, int secs, int interval, void (*handler)(int)) {
        !           169:        struct sigaction action;
        !           170:        memset(&action, 0, sizeof(action));
        !           171:        action.sa_handler=handler; // no SA_RESTART: the signal interrupts the blocking calls
        !           172:        sigaction(SIGUSR1, &action, 0);
        !           173: 
        !           174:        // the signal is sent to the calling thread only
        !           175:        struct sigevent event;
        !           176:        memset(&event, 0, sizeof(event));
        !           177:        event.sigev_notify=SIGEV_THREAD_ID;
        !           178:        event.sigev_signo=SIGUSR1;
        !           179:        event.sigev_notify_thread_id=(pid_t)syscall(SYS_gettid);
        !           180:        if(timer_create(CLOCK_MONOTONIC, &event, timer))
        !           181:                return false;
        !           182: 
        !           183:        struct itimerspec spec;
        !           184:        memset(&spec, 0, sizeof(spec));
        !           185:        spec.it_value.tv_sec=secs;
        !           186:        spec.it_interval.tv_sec=interval;
        !           187:        if(timer_settime(*timer, 0, &spec, 0)){
        !           188:                timer_delete(*timer);
        !           189:                return false;
        !           190:        }
        !           191:        return true;
        !           192: }
        !           193: 
        !           194: void pa_timer_stop(pa_timer timer) {
        !           195:        timer_delete(timer);
        !           196: }
        !           197: 
        !           198: #else
        !           199: 
        !           200: bool pa_timer_start(pa_timer *, int, int, void (*)(int)) { return false; }
        !           201: void pa_timer_stop(pa_timer) {}
        !           202: 
        !           203: #endif

E-mail: