diff options
author | Matt Oliver <protogonoi@gmail.com> | 2016-12-07 16:55:35 +1100 |
---|---|---|
committer | Marton Balint <cus@passwd.hu> | 2020-01-21 22:34:15 +0100 |
commit | fc6fde22c34ac9ae39f16494238140ba40456efd (patch) | |
tree | 3cdec30db06d618a115dddbd7cce2d8d72b097cb /compat | |
parent | 00447b6f523e5b86bec6c3944ad917edea4ed50c (diff) | |
download | ffmpeg-fc6fde22c34ac9ae39f16494238140ba40456efd.tar.gz |
avutil/thread: Add pthread_cond_timedwait function
v2: fix calculating milisecond times and use SleepConditionVariableSRW.
Signed-off-by: Matt Oliver <protogonoi@gmail.com>
Diffstat (limited to 'compat')
-rw-r--r-- | compat/os2threads.h | 25 | ||||
-rw-r--r-- | compat/w32pthreads.h | 18 |
2 files changed, 43 insertions, 0 deletions
diff --git a/compat/os2threads.h b/compat/os2threads.h index 2177a033ec..eec6f40ae7 100644 --- a/compat/os2threads.h +++ b/compat/os2threads.h @@ -31,11 +31,14 @@ #undef __STRICT_ANSI__ /* for _beginthread() */ #include <stdlib.h> +#include <time.h> #include <sys/builtin.h> #include <sys/fmutex.h> #include "libavutil/attributes.h" +#include "libavutil/common.h" +#include "libavutil/time.h" typedef struct { TID tid; @@ -163,6 +166,28 @@ static av_always_inline int pthread_cond_broadcast(pthread_cond_t *cond) return 0; } +static av_always_inline int pthread_cond_timedwait(pthread_cond_t *cond, + pthread_mutex_t *mutex, + const struct timespec *abstime) +{ + int64_t abs_milli = abstime->tv_sec * 1000LL + abstime->tv_nsec / 1000000; + ULONG t = av_clip64(abs_milli - av_gettime() / 1000, 0, ULONG_MAX); + + __atomic_increment(&cond->wait_count); + + pthread_mutex_unlock(mutex); + + APIRET ret = DosWaitEventSem(cond->event_sem, t); + + __atomic_decrement(&cond->wait_count); + + DosPostEventSem(cond->ack_sem); + + pthread_mutex_lock(mutex); + + return (ret == ERROR_TIMEOUT) ? ETIMEDOUT : 0; +} + static av_always_inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) { diff --git a/compat/w32pthreads.h b/compat/w32pthreads.h index 21acfd2ba1..7df33b7da4 100644 --- a/compat/w32pthreads.h +++ b/compat/w32pthreads.h @@ -38,11 +38,13 @@ #define WIN32_LEAN_AND_MEAN #include <windows.h> #include <process.h> +#include <time.h> #include "libavutil/attributes.h" #include "libavutil/common.h" #include "libavutil/internal.h" #include "libavutil/mem.h" +#include "libavutil/time.h" typedef struct pthread_t { void *handle; @@ -156,6 +158,22 @@ static inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex return 0; } +static inline int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, + const struct timespec *abstime) +{ + int64_t abs_milli = abstime->tv_sec * 1000LL + abstime->tv_nsec / 1000000; + DWORD t = av_clip64(abs_milli - av_gettime() / 1000, 0, UINT32_MAX); + + if (!SleepConditionVariableSRW(cond, mutex, t, 0)) { + DWORD err = GetLastError(); + if (err == ERROR_TIMEOUT) + return ETIMEDOUT; + else + return EINVAL; + } + return 0; +} + static inline int pthread_cond_signal(pthread_cond_t *cond) { WakeConditionVariable(cond); |