From ce5c22f4f4b463c11c58de2990b8565c061cf58b Mon Sep 17 00:00:00 2001 From: Thomas Jansen Date: Sun, 28 Dec 2008 22:09:42 +0100 Subject: condition: migrate from pthread to glib threads --- src/condition.c | 57 ++++++++++++++++++++++++--------------------------------- src/condition.h | 6 +++--- 2 files changed, 27 insertions(+), 36 deletions(-) (limited to 'src') diff --git a/src/condition.c b/src/condition.c index b23bed51e..ed4f538d5 100644 --- a/src/condition.c +++ b/src/condition.c @@ -26,53 +26,47 @@ void cond_init(struct condition *cond) { - int err; - if ((err = pthread_mutex_init(&cond->mutex, NULL))) - FATAL("failed to init mutex: %s\n", strerror(err)); - if ((err = pthread_cond_init(&cond->cond, NULL))) - FATAL("failed to init cond: %s\n", strerror(err)); + cond->mutex = g_mutex_new(); + if (cond->mutex == NULL) + FATAL("g_mutex_new failed"); + + cond->cond = g_cond_new(); + if (cond->cond == NULL) + FATAL("g_cond_new() failed"); } void cond_enter(struct condition *cond) { - pthread_mutex_lock(&cond->mutex); + g_mutex_lock(cond->mutex); } void cond_leave(struct condition *cond) { - pthread_mutex_unlock(&cond->mutex); + g_mutex_unlock(cond->mutex); } void cond_wait(struct condition *cond) { - pthread_cond_wait(&cond->cond, &cond->mutex); -} - -static struct timespec * ts_timeout(struct timespec *ts, const long sec) -{ - struct timeval tv; - gettimeofday(&tv, NULL); - ts->tv_sec = tv.tv_sec + sec; - ts->tv_nsec = tv.tv_usec * 1000; - return ts; + g_cond_wait(cond->cond, cond->mutex); } int cond_timedwait(struct condition *cond, const long sec) { - struct timespec ts; - int ret = pthread_cond_timedwait(&cond->cond, &cond->mutex, - ts_timeout(&ts, sec)); - if (!ret || ret == ETIMEDOUT) - return ret; - FATAL("cond_timedwait: %s\n", strerror(ret)); - return ret; + GTimeVal t; + + g_get_current_time(&t); + g_time_val_add(&t, sec * 1000000); + + if (g_cond_timed_wait(cond->cond, cond->mutex, &t) == FALSE) + return ETIMEDOUT; + return 0; } int cond_signal_async(struct condition *cond) { - if (!pthread_mutex_trylock(&cond->mutex)) { - pthread_cond_signal(&cond->cond); - pthread_mutex_unlock(&cond->mutex); + if (g_mutex_trylock(cond->mutex) == FALSE) { + g_cond_signal(cond->cond); + g_mutex_unlock(cond->mutex); return 0; } return EBUSY; @@ -80,14 +74,11 @@ int cond_signal_async(struct condition *cond) void cond_signal_sync(struct condition *cond) { - pthread_cond_signal(&cond->cond); + g_cond_signal(cond->cond); } void cond_destroy(struct condition *cond) { - int err; - if ((err = pthread_cond_destroy(&cond->cond))) - FATAL("failed to destroy cond: %s\n", strerror(err)); - if ((err = pthread_mutex_destroy(&cond->mutex))) - FATAL("failed to destroy mutex: %s\n", strerror(err)); + g_mutex_free(cond->mutex); + g_cond_free(cond->cond); } diff --git a/src/condition.h b/src/condition.h index 48d8735ee..521e61daa 100644 --- a/src/condition.h +++ b/src/condition.h @@ -20,11 +20,11 @@ #ifndef MPD_CONDITION_H #define MPD_CONDITION_H -#include +#include struct condition { - pthread_mutex_t mutex; - pthread_cond_t cond; + GMutex *mutex; + GCond *cond; }; void cond_init(struct condition *cond); -- cgit v1.2.3