condition: migrate from pthread to glib threads

This commit is contained in:
Thomas Jansen 2008-12-28 22:09:42 +01:00
parent 248cd50a20
commit ce5c22f4f4
2 changed files with 27 additions and 36 deletions

View File

@ -26,53 +26,47 @@
void cond_init(struct condition *cond) void cond_init(struct condition *cond)
{ {
int err; cond->mutex = g_mutex_new();
if ((err = pthread_mutex_init(&cond->mutex, NULL))) if (cond->mutex == NULL)
FATAL("failed to init mutex: %s\n", strerror(err)); FATAL("g_mutex_new failed");
if ((err = pthread_cond_init(&cond->cond, NULL)))
FATAL("failed to init cond: %s\n", strerror(err)); cond->cond = g_cond_new();
if (cond->cond == NULL)
FATAL("g_cond_new() failed");
} }
void cond_enter(struct condition *cond) void cond_enter(struct condition *cond)
{ {
pthread_mutex_lock(&cond->mutex); g_mutex_lock(cond->mutex);
} }
void cond_leave(struct condition *cond) void cond_leave(struct condition *cond)
{ {
pthread_mutex_unlock(&cond->mutex); g_mutex_unlock(cond->mutex);
} }
void cond_wait(struct condition *cond) void cond_wait(struct condition *cond)
{ {
pthread_cond_wait(&cond->cond, &cond->mutex); g_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;
} }
int cond_timedwait(struct condition *cond, const long sec) int cond_timedwait(struct condition *cond, const long sec)
{ {
struct timespec ts; GTimeVal t;
int ret = pthread_cond_timedwait(&cond->cond, &cond->mutex,
ts_timeout(&ts, sec)); g_get_current_time(&t);
if (!ret || ret == ETIMEDOUT) g_time_val_add(&t, sec * 1000000);
return ret;
FATAL("cond_timedwait: %s\n", strerror(ret)); if (g_cond_timed_wait(cond->cond, cond->mutex, &t) == FALSE)
return ret; return ETIMEDOUT;
return 0;
} }
int cond_signal_async(struct condition *cond) int cond_signal_async(struct condition *cond)
{ {
if (!pthread_mutex_trylock(&cond->mutex)) { if (g_mutex_trylock(cond->mutex) == FALSE) {
pthread_cond_signal(&cond->cond); g_cond_signal(cond->cond);
pthread_mutex_unlock(&cond->mutex); g_mutex_unlock(cond->mutex);
return 0; return 0;
} }
return EBUSY; return EBUSY;
@ -80,14 +74,11 @@ int cond_signal_async(struct condition *cond)
void cond_signal_sync(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) void cond_destroy(struct condition *cond)
{ {
int err; g_mutex_free(cond->mutex);
if ((err = pthread_cond_destroy(&cond->cond))) g_cond_free(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));
} }

View File

@ -20,11 +20,11 @@
#ifndef MPD_CONDITION_H #ifndef MPD_CONDITION_H
#define MPD_CONDITION_H #define MPD_CONDITION_H
#include <pthread.h> #include <glib.h>
struct condition { struct condition {
pthread_mutex_t mutex; GMutex *mutex;
pthread_cond_t cond; GCond *cond;
}; };
void cond_init(struct condition *cond); void cond_init(struct condition *cond);