diff options
author | wm4 <wm4@nowhere> | 2014-05-18 16:36:08 +0200 |
---|---|---|
committer | wm4 <wm4@nowhere> | 2014-05-18 19:20:32 +0200 |
commit | f47a4fc3d900e14653bc059717e2805ad4964a67 (patch) | |
tree | a8ca8944a10c15bbb46239db62a56940a0d3f7b1 /osdep/threads.c | |
parent | e209e44ca2fef86ec2ec5513bbb63d4d68ff97b5 (diff) | |
download | mpv-f47a4fc3d900e14653bc059717e2805ad4964a67.tar.bz2 mpv-f47a4fc3d900e14653bc059717e2805ad4964a67.tar.xz |
threads: use mpv time for mpthread_cond_timedwait wrapper
Use the time as returned by mp_time_us() for mpthread_cond_timedwait(),
instead of calculating the struct timespec value based on a timeout.
This (probably) makes it easier to wait for a specific deadline.
Diffstat (limited to 'osdep/threads.c')
-rw-r--r-- | osdep/threads.c | 55 |
1 files changed, 8 insertions, 47 deletions
diff --git a/osdep/threads.c b/osdep/threads.c index 84a65feddd..8cb03045e4 100644 --- a/osdep/threads.c +++ b/osdep/threads.c @@ -15,61 +15,22 @@ * with mpv. If not, see <http://www.gnu.org/licenses/>. */ -#include <time.h> -#include <unistd.h> -#include <sys/time.h> -#include <limits.h> - -#include "common/common.h" #include "threads.h" +#include "timer.h" -static void get_pthread_time(struct timespec *out_ts) -{ -#if defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0 - clock_gettime(CLOCK_REALTIME, out_ts); -#else - // OSX - struct timeval tv; - gettimeofday(&tv, NULL); - out_ts->tv_sec = tv.tv_sec; - out_ts->tv_nsec = tv.tv_usec * 1000UL; -#endif -} - -static void timespec_add_seconds(struct timespec *ts, double seconds) -{ - // clamp to 1 week to avoid tv_sec overflows - seconds = MPMIN(seconds, 60 * 60 * 24 * 7); - unsigned long secs = (int)seconds; - unsigned long nsecs = (seconds - secs) * 1000000000UL; - if (nsecs + ts->tv_nsec >= 1000000000UL) { - secs += 1; - nsecs -= 1000000000UL; - } - ts->tv_sec += secs; - ts->tv_nsec += nsecs; -} - -// 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) +int mpthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, + int64_t abstime) { - struct timespec ts; - get_pthread_time(&ts); - timespec_add_seconds(&ts, timeout); - return ts; + struct timespec ts = mp_time_us_to_timespec(abstime); + return pthread_cond_timedwait(cond, mutex, &ts); } -// Call pthread_cond_timedwait() with a relative timeout in seconds -int mpthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, - double timeout) +int mpthread_cond_timedwait_rel(pthread_cond_t *cond, pthread_mutex_t *mutex, + double s) { - struct timespec ts = mpthread_get_deadline(timeout); - return pthread_cond_timedwait(cond, mutex, &ts); + return mpthread_cond_timedwait(cond, mutex, mp_add_timeout(mp_time_us(), s)); } -// Helper to reduce boiler plate. int mpthread_mutex_init_recursive(pthread_mutex_t *mutex) { pthread_mutexattr_t attr; |