nostrdb

an unfairly fast embedded nostr database backed by lmdb
git clone git://jb55.com/nostrdb
Log | Files | Refs | Submodules | README | LICENSE

thread.h (1719B)


      1 #ifndef NDB_THREAD_H
      2 #define NDB_THREAD_H
      3 
      4 #ifdef _WIN32
      5   #include <windows.h>
      6 
      7   #define     ErrCode()       GetLastError()
      8 // Define POSIX-like thread types
      9 typedef HANDLE pthread_t;
     10 typedef CRITICAL_SECTION pthread_mutex_t;
     11 typedef CONDITION_VARIABLE pthread_cond_t;
     12 
     13 #define ErrCode() GetLastError()
     14 
     15 // Mutex functions
     16 #define pthread_mutex_init(mutex, attr) \
     17     (InitializeCriticalSection(mutex), 0)
     18 
     19 #define pthread_mutex_destroy(mutex) \
     20     (DeleteCriticalSection(mutex), 0)
     21 
     22 #define pthread_mutex_lock(mutex) \
     23     (EnterCriticalSection(mutex), 0)
     24 
     25 #define pthread_mutex_unlock(mutex) \
     26     (LeaveCriticalSection(mutex), 0)
     27 
     28 // Condition variable functions
     29 #define pthread_cond_init(cond, attr) \
     30     (InitializeConditionVariable(cond), 0)
     31 
     32 #define pthread_cond_destroy(cond)
     33 
     34 #define pthread_cond_signal(cond) \
     35     (WakeConditionVariable(cond), 0)
     36 
     37 #define pthread_cond_wait(cond, mutex) \
     38     (SleepConditionVariableCS(cond, mutex, INFINITE) ? 0 : ErrCode())
     39 
     40 // Thread functions
     41 #define THREAD_CREATE(thr, start, arg) \
     42     (((thr = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)start, arg, 0, NULL)) != NULL) ? 0 : ErrCode())
     43 
     44 #define THREAD_FINISH(thr) \
     45     (WaitForSingleObject(thr, INFINITE), CloseHandle(thr), 0)
     46 
     47 #define THREAD_TERMINATE(thr) \
     48     (TerminateThread(thr, 0) ? ErrCode() : 0)
     49 
     50 #else // _WIN32
     51   #include <pthread.h>
     52 
     53   //#define     ErrCode()       errno
     54   #define THREAD_CREATE(thr,start,arg)	pthread_create(&thr,NULL,start,arg)
     55   #define THREAD_FINISH(thr)	pthread_join(thr,NULL)
     56   #define THREAD_TERMINATE(thr)	pthread_exit(&thr)
     57   
     58   #define LOCK_MUTEX(mutex)	pthread_mutex_lock(mutex)
     59   #define UNLOCK_MUTEX(mutex)	pthread_mutex_unlock(mutex)
     60 
     61 #endif
     62 
     63 #endif // NDB_THREAD_H