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.)
This commit is contained in:
wm4 2015-05-11 23:44:36 +02:00
parent ca9964a4fb
commit 92b9d75d72
9 changed files with 33 additions and 34 deletions

View File

@ -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");

View File

@ -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);
}

View File

@ -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;

View File

@ -4,15 +4,6 @@
#include <pthread.h>
#include <inttypes.h>
// 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);

View File

@ -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 <stdio.h>
#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);

View File

@ -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 */

View File

@ -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);

View File

@ -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);

View File

@ -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);
}