mirror of
https://github.com/mpv-player/mpv
synced 2025-01-03 13:32:16 +00:00
threads: add function to calculate deadline for timed waits
Usually, you have to call pthread_cond_timedwait() in a loop (because it can wake up sporadically). If this function is used by another higher level function, which uses a relative timeout, we actually have to reduce the timeout on each iteration - or, simpler, compute the "deadline" at the beginning of the function, and always pass the same absolute time to the waiting function. Might be unsafe if the system time is changed. On the other hand, this is a fundamental race condition with these APIs.
This commit is contained in:
parent
a17be5576f
commit
d8dd9a6725
@ -46,13 +46,22 @@ static void timespec_add_seconds(struct timespec *ts, double seconds)
|
||||
ts->tv_nsec += nsecs;
|
||||
}
|
||||
|
||||
// Call pthread_cond_timedwait() with a relative timeout in seconds
|
||||
int mpthread_cond_timed_wait(pthread_cond_t *cond, pthread_mutex_t *mutex,
|
||||
double timeout)
|
||||
// Return the argument to pass to e.g. pthread_cond_timedwait().
|
||||
// (Note that pthread_cond_t supports multiple clocks; this function computes
|
||||
// the time value needed by the default clock.)
|
||||
struct timespec mpthread_get_deadline(double timeout)
|
||||
{
|
||||
struct timespec ts;
|
||||
get_pthread_time(&ts);
|
||||
timespec_add_seconds(&ts, timeout);
|
||||
return ts;
|
||||
}
|
||||
|
||||
// Call pthread_cond_timedwait() with a relative timeout in seconds
|
||||
int mpthread_cond_timed_wait(pthread_cond_t *cond, pthread_mutex_t *mutex,
|
||||
double timeout)
|
||||
{
|
||||
struct timespec ts = mpthread_get_deadline(timeout);
|
||||
return pthread_cond_timedwait(cond, mutex, &ts);
|
||||
}
|
||||
|
||||
|
@ -3,6 +3,8 @@
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
struct timespec mpthread_get_deadline(double timeout);
|
||||
|
||||
int mpthread_cond_timed_wait(pthread_cond_t *cond, pthread_mutex_t *mutex,
|
||||
double timeout);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user