From 37ad3e79dd03cecc4be3a7341fdf69f96873d2eb Mon Sep 17 00:00:00 2001 From: wm4 Date: Mon, 11 May 2015 23:44:36 +0200 Subject: threads: use utility+POSIX functions instead of weird wrappers There is not much of a reason to have these wrappers around. Use POSIX standard functions directly, and use a separate utility function to take care of the timespec calculations. (Course POSIX for using this weird format for time values.) (cherry picked from commit 92b9d75d7256be71d8c8b18438af9494b78f0e96) --- audio/out/push.c | 9 ++++++--- misc/dispatch.c | 3 ++- osdep/threads.c | 13 ------------- osdep/threads.h | 9 --------- osdep/timer.c | 8 +++++++- osdep/timer.h | 4 ++++ player/client.c | 6 ++++-- stream/cache.c | 9 ++++++--- video/out/vo.c | 6 ++++-- 9 files changed, 33 insertions(+), 34 deletions(-) diff --git a/audio/out/push.c b/audio/out/push.c index 8e3d2f87b0..beafd36a13 100644 --- a/audio/out/push.c +++ b/audio/out/push.c @@ -340,7 +340,8 @@ static void *playthread(void *arg) pthread_cond_signal(&p->wakeup); // for draining if (p->still_playing && timeout > 0) { - mpthread_cond_timedwait_rel(&p->wakeup, &p->lock, timeout); + struct timespec ts = mp_rel_time_to_timespec(timeout); + pthread_cond_timedwait(&p->wakeup, &p->lock, &ts); } else { pthread_cond_wait(&p->wakeup, &p->lock); } @@ -352,8 +353,10 @@ static void *playthread(void *arg) if (ao->driver->get_delay) timeout = ao->driver->get_delay(ao); timeout *= 0.25; // wake up if 25% played - if (!p->need_wakeup) - mpthread_cond_timedwait_rel(&p->wakeup, &p->lock, timeout); + if (!p->need_wakeup) { + struct timespec ts = mp_rel_time_to_timespec(timeout); + pthread_cond_timedwait(&p->wakeup, &p->lock, &ts); + } } } MP_STATS(ao, "end audio wait"); diff --git a/misc/dispatch.c b/misc/dispatch.c index 6391caa063..502742b835 100644 --- a/misc/dispatch.c +++ b/misc/dispatch.c @@ -206,7 +206,8 @@ void mp_dispatch_queue_process(struct mp_dispatch_queue *queue, double timeout) } } else { if (wait > 0) { - mpthread_cond_timedwait(&queue->cond, &queue->lock, wait); + struct timespec ts = mp_time_us_to_timespec(wait); + pthread_cond_timedwait(&queue->cond, &queue->lock, &ts); } else { pthread_cond_wait(&queue->cond, &queue->lock); } diff --git a/osdep/threads.c b/osdep/threads.c index 4f5b39d504..cccce85d1c 100644 --- a/osdep/threads.c +++ b/osdep/threads.c @@ -27,19 +27,6 @@ #include "threads.h" #include "timer.h" -int mpthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, - int64_t abstime) -{ - struct timespec ts = mp_time_us_to_timespec(abstime); - return pthread_cond_timedwait(cond, mutex, &ts); -} - -int mpthread_cond_timedwait_rel(pthread_cond_t *cond, pthread_mutex_t *mutex, - double s) -{ - return mpthread_cond_timedwait(cond, mutex, mp_add_timeout(mp_time_us(), s)); -} - int mpthread_mutex_init_recursive(pthread_mutex_t *mutex) { pthread_mutexattr_t attr; diff --git a/osdep/threads.h b/osdep/threads.h index 2277fa65a1..8633618009 100644 --- a/osdep/threads.h +++ b/osdep/threads.h @@ -4,15 +4,6 @@ #include #include -// Call pthread_cond_timedwait() with an absolute timeout using the same -// time source/unit as mp_time_us() (microseconds). -int mpthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, - int64_t abstime); - -// Wait by a relative amount of time in seconds. -int mpthread_cond_timedwait_rel(pthread_cond_t *cond, pthread_mutex_t *mutex, - double seconds); - // Helper to reduce boiler plate. int mpthread_mutex_init_recursive(pthread_mutex_t *mutex); diff --git a/osdep/timer.c b/osdep/timer.c index 643d7cbb9b..053d12dd7c 100644 --- a/osdep/timer.c +++ b/osdep/timer.c @@ -116,6 +116,11 @@ struct timespec mp_time_us_to_timespec(int64_t time_us) return ts; } +struct timespec mp_rel_time_to_timespec(double timeout_sec) +{ + return mp_time_us_to_timespec(mp_add_timeout(mp_time_us(), timeout_sec)); +} + #if 0 #include #include "threads.h" @@ -138,7 +143,8 @@ int main(void) { #if TEST_SLEEP mp_sleep_us(delay); #else - mpthread_cond_timedwait(&cnd, &mtx, r + delay); + struct timespec ts = mp_time_us_to_timespec(r + delay); + pthread_cond_timedwait(&cnd, &mtx, &ts); #endif j = (mp_time_us() - r) - delay; printf("sleep time: t=%"PRId64" sleep=%8i err=%5i\n", r, delay, (int)j); diff --git a/osdep/timer.h b/osdep/timer.h index 547323943d..427bcc3ccb 100644 --- a/osdep/timer.h +++ b/osdep/timer.h @@ -52,4 +52,8 @@ int64_t mp_add_timeout(int64_t time_us, double timeout_sec); // Convert the mp time in microseconds to a timespec using CLOCK_REALTIME. struct timespec mp_time_us_to_timespec(int64_t time_us); +// Convert the relative timeout in seconds to a timespec. +// The timespec is absolute, using CLOCK_REALTIME. +struct timespec mp_rel_time_to_timespec(double timeout_sec); + #endif /* MPLAYER_TIMER_H */ diff --git a/player/client.c b/player/client.c index 43c7c07c60..b6c3ade9ef 100644 --- a/player/client.c +++ b/player/client.c @@ -284,8 +284,10 @@ static int wait_wakeup(struct mpv_handle *ctx, int64_t end) int r = 0; pthread_mutex_unlock(&ctx->lock); pthread_mutex_lock(&ctx->wakeup_lock); - if (!ctx->need_wakeup) - r = mpthread_cond_timedwait(&ctx->wakeup, &ctx->wakeup_lock, end); + if (!ctx->need_wakeup) { + struct timespec ts = mp_time_us_to_timespec(end); + r = pthread_cond_timedwait(&ctx->wakeup, &ctx->wakeup_lock, &ts); + } if (r == 0) ctx->need_wakeup = false; pthread_mutex_unlock(&ctx->wakeup_lock); diff --git a/stream/cache.c b/stream/cache.c index 0ffa3927be..ede1e1fe83 100644 --- a/stream/cache.c +++ b/stream/cache.c @@ -134,7 +134,8 @@ static void cache_wakeup_and_wait(struct priv *s, double *retry_time) } pthread_cond_signal(&s->wakeup); - mpthread_cond_timedwait_rel(&s->wakeup, &s->mutex, CACHE_WAIT_TIME); + struct timespec ts = mp_rel_time_to_timespec(CACHE_WAIT_TIME); + pthread_cond_timedwait(&s->wakeup, &s->mutex, &ts); if (*retry_time >= 0) *retry_time += mp_time_sec() - start; @@ -465,8 +466,10 @@ static void *cache_thread(void *arg) pthread_cond_signal(&s->wakeup); s->control = CACHE_CTRL_NONE; } - if (s->idle && s->control == CACHE_CTRL_NONE) - mpthread_cond_timedwait_rel(&s->wakeup, &s->mutex, CACHE_IDLE_SLEEP_TIME); + if (s->idle && s->control == CACHE_CTRL_NONE) { + struct timespec ts = mp_rel_time_to_timespec(CACHE_IDLE_SLEEP_TIME); + pthread_cond_timedwait(&s->wakeup, &s->mutex, &ts); + } } pthread_cond_signal(&s->wakeup); pthread_mutex_unlock(&s->mutex); diff --git a/video/out/vo.c b/video/out/vo.c index 0b197763a0..be9f8a3088 100644 --- a/video/out/vo.c +++ b/video/out/vo.c @@ -471,8 +471,10 @@ static void wait_vo(struct vo *vo, int64_t until_time) pthread_mutex_unlock(&in->lock); } else { pthread_mutex_lock(&in->lock); - if (!in->need_wakeup) - mpthread_cond_timedwait(&in->wakeup, &in->lock, until_time); + if (!in->need_wakeup) { + struct timespec ts = mp_time_us_to_timespec(until_time); + pthread_cond_timedwait(&in->wakeup, &in->lock, &ts); + } in->need_wakeup = false; pthread_mutex_unlock(&in->lock); } -- cgit v1.2.3