From 44d9f62f34e0561d83ea32941f0ea1b529b1490d Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sat, 16 Aug 2008 09:28:15 -0700 Subject: core rewrite (decode,player,outputBuffer,playlist) This is a huge refactoring of the core mpd process. The queueing/buffering mechanism is heavily reworked. The player.c code has been merged into outputBuffer (the actual ring buffering logic is handled by ringbuf.c); and decode.c actually handles decoding stuff. The end result is several hundreds of lines shorter, even though we still have a lot of DEBUG statements left in there for tracing and a lot of assertions, too. --- src/condition.c | 54 ++++++++++++++++++++++-------------------------------- 1 file changed, 22 insertions(+), 32 deletions(-) (limited to 'src/condition.c') 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) -- cgit v1.2.3