diff options
Diffstat (limited to 'common')
| -rw-r--r-- | common/thread.c | 16 | ||||
| -rw-r--r-- | common/thread.h | 9 |
2 files changed, 25 insertions, 0 deletions
diff --git a/common/thread.c b/common/thread.c index 2cf4321..d6d6c1a 100644 --- a/common/thread.c +++ b/common/thread.c | |||
| @@ -81,3 +81,19 @@ void mutex_unlock(mutex_t* mutex) | |||
| 81 | pthread_mutex_unlock(mutex); | 81 | pthread_mutex_unlock(mutex); |
| 82 | #endif | 82 | #endif |
| 83 | } | 83 | } |
| 84 | |||
| 85 | void thread_once(thread_once_t *once_control, void (*init_routine)(void)) | ||
| 86 | { | ||
| 87 | #ifdef WIN32 | ||
| 88 | while (InterlockedExchange(&(once_control->lock), 1) != 0) { | ||
| 89 | Sleep(1); | ||
| 90 | } | ||
| 91 | if (!once_control->state) { | ||
| 92 | once_control->state = 1; | ||
| 93 | init_routine(); | ||
| 94 | } | ||
| 95 | InterlockedExchange(&(once_control->lock), 0); | ||
| 96 | #else | ||
| 97 | pthread_once(once_control, init_routine); | ||
| 98 | #endif | ||
| 99 | } | ||
diff --git a/common/thread.h b/common/thread.h index e74ee74..5d20083 100644 --- a/common/thread.h +++ b/common/thread.h | |||
| @@ -26,10 +26,17 @@ | |||
| 26 | #include <windows.h> | 26 | #include <windows.h> |
| 27 | typedef HANDLE thread_t; | 27 | typedef HANDLE thread_t; |
| 28 | typedef CRITICAL_SECTION mutex_t; | 28 | typedef CRITICAL_SECTION mutex_t; |
| 29 | typedef volatile struct { | ||
| 30 | LONG lock; | ||
| 31 | int state; | ||
| 32 | } thread_once_t; | ||
| 33 | #define THREAD_ONCE_INIT {0, 0} | ||
| 29 | #else | 34 | #else |
| 30 | #include <pthread.h> | 35 | #include <pthread.h> |
| 31 | typedef pthread_t thread_t; | 36 | typedef pthread_t thread_t; |
| 32 | typedef pthread_mutex_t mutex_t; | 37 | typedef pthread_mutex_t mutex_t; |
| 38 | typedef pthread_once_t thread_once_t; | ||
| 39 | #define THREAD_ONCE_INIT PTHREAD_ONCE_INIT | ||
| 33 | #endif | 40 | #endif |
| 34 | 41 | ||
| 35 | typedef void* (*thread_func_t)(void* data); | 42 | typedef void* (*thread_func_t)(void* data); |
| @@ -42,4 +49,6 @@ void mutex_destroy(mutex_t* mutex); | |||
| 42 | void mutex_lock(mutex_t* mutex); | 49 | void mutex_lock(mutex_t* mutex); |
| 43 | void mutex_unlock(mutex_t* mutex); | 50 | void mutex_unlock(mutex_t* mutex); |
| 44 | 51 | ||
| 52 | void thread_once(thread_once_t *once_control, void (*init_routine)(void)); | ||
| 53 | |||
| 45 | #endif | 54 | #endif |
