diff options
author | Eric Wong <normalperson@yhbt.net> | 2008-08-20 01:31:51 -0700 |
---|---|---|
committer | Eric Wong <normalperson@yhbt.net> | 2008-08-20 01:31:51 -0700 |
commit | 1b197e65232e1a51b853db53985e8eda61f1b196 (patch) | |
tree | 2d85ff67ba79e32256a20d2d2bee7222352007fd /src/condition.c | |
parent | 5a0216af3dc7c9dedc4dacb708191f0fd380bb73 (diff) | |
parent | 508ae1c18d3bdc99a1bb06181762e5ec859cf072 (diff) | |
download | mpd-1b197e65232e1a51b853db53985e8eda61f1b196.tar.gz mpd-1b197e65232e1a51b853db53985e8eda61f1b196.tar.xz mpd-1b197e65232e1a51b853db53985e8eda61f1b196.zip |
Merge branch 'core-rewrite' of git://git.musicpd.org/normalperson/mpd
* 'core-rewrite' of git://git.musicpd.org/normalperson/mpd:
Remove ob_wait_sync and cleanup triggering in playlist
fix output buffer deadlock when daemonizing
log.c: thread-safety for warning log
core rewrite (decode,player,outputBuffer,playlist)
Diffstat (limited to '')
-rw-r--r-- | src/condition.c | 54 |
1 files changed, 22 insertions, 32 deletions
diff --git a/src/condition.c b/src/condition.c index e71dcdd88..ca63ba537 100644 --- a/src/condition.c +++ b/src/condition.c @@ -27,54 +27,44 @@ void cond_init(struct condition *cond) xpthread_cond_init(&cond->cond, NULL); } -void cond_enter(struct condition *cond) -{ - pthread_mutex_lock(&cond->mutex); -} - -void cond_leave(struct condition *cond) -{ - pthread_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) +int cond_timedwait(struct condition *cond, const long msec) { + static const long nsec_per_msec = 1000000; + static const long nsec_per_usec = 1000; + static const long nsec_per_sec = 1000000000; + struct timespec ts; struct timeval tv; + int ret; + gettimeofday(&tv, NULL); - ts->tv_sec = tv.tv_sec + sec; - ts->tv_nsec = tv.tv_usec * 1000; - return ts; -} + ts.tv_sec = tv.tv_sec; + ts.tv_nsec = (tv.tv_usec * nsec_per_usec) + (msec * nsec_per_msec); + if (ts.tv_nsec >= nsec_per_sec) { + ts.tv_nsec -= nsec_per_sec; + ts.tv_sec++; + } -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)); + ret = pthread_cond_timedwait(&cond->cond, &cond->mutex, &ts); if (!ret || ret == ETIMEDOUT) return ret; FATAL("cond_timedwait: %s\n", strerror(ret)); return ret; } -int cond_signal_async(struct condition *cond) +int cond_signal_trysync(struct condition *cond) { - if (!pthread_mutex_trylock(&cond->mutex)) { - pthread_cond_signal(&cond->cond); - pthread_mutex_unlock(&cond->mutex); - return 0; - } - return EBUSY; + if (pthread_mutex_trylock(&cond->mutex) == EBUSY) + return EBUSY; + pthread_cond_signal(&cond->cond); + pthread_mutex_unlock(&cond->mutex); + return 0; } void cond_signal_sync(struct condition *cond) { + pthread_mutex_lock(&cond->mutex); pthread_cond_signal(&cond->cond); + pthread_mutex_unlock(&cond->mutex); } void cond_destroy(struct condition *cond) |