diff options
-rw-r--r-- | src/thread.c | 75 | ||||
-rw-r--r-- | src/thread.h | 11 |
2 files changed, 86 insertions, 0 deletions
diff --git a/src/thread.c b/src/thread.c index eb535ab..796ea0b 100644 --- a/src/thread.c +++ b/src/thread.c @@ -67,6 +67,8 @@ int thread_join(THREAD_T thread) int thread_alive(THREAD_T thread) { + if (!thread) + return 0; #ifdef WIN32 return WaitForSingleObject(thread, 0) == WAIT_TIMEOUT; #else @@ -138,3 +140,76 @@ void thread_once(thread_once_t *once_control, void (*init_routine)(void)) pthread_once(once_control, init_routine); #endif } + +void cond_init(cond_t* cond) +{ +#ifdef WIN32 + cond->sem = CreateSemaphore(NULL, 0, 32767, NULL); +#else + pthread_cond_init(cond, NULL); +#endif +} + +void cond_destroy(cond_t* cond) +{ +#ifdef WIN32 + CloseHandle(cond->sem); +#else + pthread_cond_destroy(cond); +#endif +} + +int cond_signal(cond_t* cond) +{ +#ifdef WIN32 + int result = 0; + if (!ReleaseSemaphore(cond->sem, 1, NULL)) { + result = -1; + } + return result; +#else + return pthread_cond_signal(cond); +#endif +} + +int cond_wait(cond_t* cond, mutex_t* mutex) +{ +#ifdef WIN32 + mutex_unlock(mutex); + DWORD res = WaitForSingleObject(cond->sem, INFINITE); + switch (res) { + case WAIT_OBJECT_0: + return 0; + default: + return -1; + } +#else + return pthread_cond_wait(cond, mutex); +#endif +} + +int cond_wait_timeout(cond_t* cond, mutex_t* mutex, unsigned int timeout_ms) +{ +#ifdef WIN32 + mutex_unlock(mutex); + DWORD res = WaitForSingleObject(cond->sem, timeout_ms); + switch (res) { + case WAIT_OBJECT_0: + case WAIT_TIMEOUT: + return 0; + default: + return -1; + } +#else + struct timespec ts; + struct timeval now; + gettimeofday(&now, NULL); + + ts.tv_sec = now.tv_sec + timeout_ms / 1000; + ts.tv_nsec = now.tv_usec * 1000 + 1000 * 1000 * (timeout_ms % 1000); + ts.tv_sec += ts.tv_nsec / (1000 * 1000 * 1000); + ts.tv_nsec %= (1000 * 1000 * 1000); + + return pthread_cond_timedwait(cond, mutex, &ts); +#endif +} diff --git a/src/thread.h b/src/thread.h index 23e4510..2aadc6e 100644 --- a/src/thread.h +++ b/src/thread.h @@ -28,6 +28,9 @@ #include <windows.h> typedef HANDLE THREAD_T; typedef CRITICAL_SECTION mutex_t; +typedef struct { + HANDLE sem; +} cond_t; typedef volatile struct { LONG lock; int state; @@ -38,8 +41,10 @@ typedef volatile struct { #else #include <pthread.h> #include <signal.h> +#include <sys/time.h> typedef pthread_t THREAD_T; typedef pthread_mutex_t mutex_t; +typedef pthread_cond_t cond_t; typedef pthread_once_t thread_once_t; #define THREAD_ONCE_INIT PTHREAD_ONCE_INIT #define THREAD_ID pthread_self() @@ -73,4 +78,10 @@ void mutex_unlock(mutex_t* mutex); void thread_once(thread_once_t *once_control, void (*init_routine)(void)); +void cond_init(cond_t* cond); +void cond_destroy(cond_t* cond); +int cond_signal(cond_t* cond); +int cond_wait(cond_t* cond, mutex_t* mutex); +int cond_wait_timeout(cond_t* cond, mutex_t* mutex, unsigned int timeout_ms); + #endif |