diff options
author | Chris Robinson <[email protected]> | 2014-05-26 03:52:55 -0700 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2014-05-26 03:52:55 -0700 |
commit | b6e1042e8c641a86b57b616497d2128cfbedbe54 (patch) | |
tree | 82e2ab8a1ad67630217f05a79aec3e689feea579 | |
parent | fd5e7f1466267a66ef17a03698d1e3b1323cb73d (diff) |
Implement condition variables (POSIX only!)
Windows requires Vista or newer to get the CONDITION_VARIABNLE API, but we
currently only require XP.
-rw-r--r-- | common/threads.c | 48 | ||||
-rw-r--r-- | include/threads.h | 11 |
2 files changed, 55 insertions, 4 deletions
diff --git a/common/threads.c b/common/threads.c index d45efc91..b2db1682 100644 --- a/common/threads.c +++ b/common/threads.c @@ -170,7 +170,8 @@ int althrd_join(althrd_t thr, int *res) GetExitCodeThread(hdl, &code); CloseHandle(hdl); - *res = (int)code; + if(res != NULL) + *res = (int)code; return althrd_success; } @@ -412,11 +413,10 @@ int althrd_join(althrd_t thr, int *res) { void *code; - if(!res) return althrd_error; - if(pthread_join(thr, &code) != 0) return althrd_error; - *res = (int)(intptr_t)code; + if(res != NULL) + *res = (int)(intptr_t)code; return althrd_success; } @@ -495,6 +495,46 @@ int almtx_timedlock(almtx_t *mtx, const struct timespec *ts) #endif } +int alcnd_init(alcnd_t *cond) +{ + if(pthread_cond_init(cond, NULL) == 0) + return althrd_success; + return althrd_error; +} + +int alcnd_signal(alcnd_t *cond) +{ + if(pthread_cond_signal(cond) == 0) + return althrd_success; + return althrd_error; +} + +int alcnd_broadcast(alcnd_t *cond) +{ + if(pthread_cond_broadcast(cond) == 0) + return althrd_success; + return althrd_error; +} + +int alcnd_wait(alcnd_t *cond, almtx_t *mtx) +{ + if(pthread_cond_wait(cond, mtx) == 0) + return althrd_success; + return althrd_error; +} + +int alcnd_timedwait(alcnd_t *cond, almtx_t *mtx, const struct timespec *time_point) +{ + if(pthread_cond_timedwait(cond, mtx, time_point) == 0) + return althrd_success; + return althrd_error; +} + +void alcnd_destroy(alcnd_t *cond) +{ + pthread_cond_destroy(cond); +} + int altss_create(altss_t *tss_id, altss_dtor_t callback) { diff --git a/include/threads.h b/include/threads.h index 40f523fa..8b633f33 100644 --- a/include/threads.h +++ b/include/threads.h @@ -113,12 +113,23 @@ inline int altss_set(altss_t tss_id, void *val) typedef pthread_t althrd_t; typedef pthread_mutex_t almtx_t; +typedef pthread_cond_t alcnd_t; typedef pthread_key_t altss_t; typedef pthread_once_t alonce_flag; #define AL_ONCE_FLAG_INIT PTHREAD_ONCE_INIT +/* NOTE: Condition variables are POSIX-only at the moment, as Windows requires + * Vista or newer for them (without slow, hacky work-arounds). */ +int alcnd_init(alcnd_t *cond); +int alcnd_signal(alcnd_t *cond); +int alcnd_broadcast(alcnd_t *cond); +int alcnd_wait(alcnd_t *cond, almtx_t *mtx); +int alcnd_timedwait(alcnd_t *cond, almtx_t *mtx, const struct timespec *time_point); +void alcnd_destroy(alcnd_t *cond); + + inline althrd_t althrd_current(void) { return pthread_self(); |