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

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.33    ! moko       11: volatile const char * IDENT_PA_OS_C="$Id: pa_os.C,v 1.32 2026/10/29 11:42:27 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.33    ! moko       57:         case ERROR_NOT_SAME_DEVICE: // moving to another disk
        !            58:             return EXDEV;
1.23      moko       59:     }
1.24      moko       60:     return EACCES;
1.23      moko       61: }
                     62: 
1.27      moko       63: int pa_set_errno(int status) {
                     64:     if (!status)
                     65:         errno = pa_errno();
                     66:     return status;
                     67: }
                     68: 
1.22      moko       69: #else
1.1       paf        70: 
                     71: #ifdef HAVE_FLOCK
                     72: 
1.9       misha      73: #define PA_SH_LOCK LOCK_SH|LOCK_NB
                     74: #define PA_EX_LOCK LOCK_EX|LOCK_NB
                     75: #define PA_ULOCK LOCK_UN
                     76: #define FLOCK(operation) int status=flock(fd, operation);
1.1       paf        77: 
                     78: #else
                     79: #ifdef HAVE_FCNTL
                     80: 
1.9       misha      81: #define PA_SH_LOCK F_RDLCK
                     82: #define PA_EX_LOCK F_WRLCK
                     83: #define PA_ULOCK F_UNLCK
                     84: #define FLOCK(operation) struct flock ls={operation, SEEK_SET}; int status=fcntl(fd, F_SETLK, &ls); 
1.1       paf        85: 
                     86: #else
                     87: #ifdef HAVE_LOCKF
                     88: 
1.9       misha      89: #define PA_SH_LOCK F_TLOCK
                     90: #define PA_EX_LOCK F_TLOCK
                     91: #define PA_ULOCK F_ULOCK
                     92: #define FLOCK(operation) lseek(fd, 0, SEEK_SET); int status=lockf(fd, operation, 1);
1.1       paf        93: 
                     94: #else
                     95: 
                     96: #error unable to find file locking func
                     97: 
                     98: #endif
                     99: #endif
                    100: #endif
1.22      moko      101: 
1.1       paf       102: #endif
                    103: 
1.9       misha     104: int pa_lock(int fd, int attempts, int operation){
                    105:        while(true){
                    106:                FLOCK(operation);
1.16      moko      107:                if(status==0)
                    108:                        return 0;
                    109:                if(--attempts<=0)
1.27      moko      110:                        return errno;
1.10      misha     111:                pa_sleep(PA_LOCK_WAIT_TIMEOUT_SECS, PA_LOCK_WAIT_TIMEOUT_USECS);
1.9       misha     112:        }
                    113: };
                    114: 
                    115: int pa_lock_shared_blocking(int fd) {
1.17      moko      116:        return pa_lock(fd, pa_lock_attempts, PA_SH_LOCK);
1.9       misha     117: }
                    118: 
                    119: int pa_lock_exclusive_blocking(int fd) {
1.17      moko      120:        return pa_lock(fd, pa_lock_attempts, PA_EX_LOCK);
1.9       misha     121: }
                    122: 
                    123: int pa_lock_exclusive_nonblocking(int fd) {
                    124:        return pa_lock(fd, 1, PA_EX_LOCK);
                    125: }
                    126: 
                    127: int pa_unlock(int fd) {
                    128:        return pa_lock(fd, 1, PA_ULOCK);
                    129: }
                    130: 
                    131: 
1.1       paf       132: int pa_sleep(unsigned long secs, unsigned long usecs) {
1.11      moko      133:        if(usecs >= 1000000){
                    134:                        secs += usecs/1000000;
                    135:                        usecs = usecs%1000000;
                    136:        }
1.1       paf       137: 
1.14      moko      138: #ifdef _MSC_VER
1.1       paf       139:        Sleep(secs * 1000 + usecs / 1000); 
                    140:        return 0;
                    141: #else
1.28      moko      142:        struct timespec t;
1.1       paf       143:        t.tv_sec = secs;
1.28      moko      144:        t.tv_nsec = usecs * 1000;
                    145:        // GC stops the threads with signals, the sleep goes on for the time left
                    146:        int result;
                    147:        while((result=nanosleep(&t, &t))<0 && errno==EINTR);
                    148:        return result<0 ? errno : 0;
1.1       paf       149: #endif
                    150: }
1.29      moko      151: 
                    152: const char *pa_ctime(time_t t) {
                    153: #ifdef _MSC_VER
                    154:        const char *result=ctime(&t); // the buffer is per thread
                    155: #else
                    156:        static THREAD_LOCAL char buffer[32];
                    157:        const char *result=ctime_r(&t, buffer);
                    158: #endif
                    159:        return result ? result : "";
                    160: }
                    161: 
1.31      moko      162: #ifdef PA_RESPONSE_TIMEOUT
1.29      moko      163: 
1.31      moko      164: #include "pa_threads.h"
                    165: 
                    166: #define PA_TIMER_SIGNAL SIGVTALRM
1.29      moko      167: 
                    168: #ifndef sigev_notify_thread_id
                    169: #define sigev_notify_thread_id _sigev_un._tid
                    170: #endif
                    171: 
                    172: bool pa_timer_start(pa_timer *timer, int secs, int interval, void (*handler)(int)) {
                    173:        struct sigaction action;
                    174:        memset(&action, 0, sizeof(action));
                    175:        action.sa_handler=handler; // no SA_RESTART: the signal interrupts the blocking calls
1.31      moko      176:        sigaction(PA_TIMER_SIGNAL, &action, 0);
1.29      moko      177: 
                    178:        // the signal is sent to the calling thread only
                    179:        struct sigevent event;
                    180:        memset(&event, 0, sizeof(event));
                    181:        event.sigev_notify=SIGEV_THREAD_ID;
1.31      moko      182:        event.sigev_signo=PA_TIMER_SIGNAL;
                    183:        event.sigev_notify_thread_id=pa_get_thread_id();
1.32      moko      184:        timer_t id;
                    185:        if(timer_create(CLOCK_MONOTONIC, &event, &id))
1.29      moko      186:                return false;
                    187: 
                    188:        struct itimerspec spec;
                    189:        memset(&spec, 0, sizeof(spec));
                    190:        spec.it_value.tv_sec=secs;
                    191:        spec.it_interval.tv_sec=interval;
1.32      moko      192:        if(timer_settime(id, 0, &spec, 0)){
                    193:                timer_delete(id);
1.29      moko      194:                return false;
                    195:        }
1.32      moko      196:        *timer=(pa_timer)id;
1.29      moko      197:        return true;
                    198: }
                    199: 
                    200: void pa_timer_stop(pa_timer timer) {
1.32      moko      201:        timer_delete((timer_t)timer);
1.29      moko      202: }
                    203: 
                    204: #else
                    205: 
                    206: bool pa_timer_start(pa_timer *, int, int, void (*)(int)) { return false; }
                    207: void pa_timer_stop(pa_timer) {}
                    208: 
                    209: #endif

E-mail: