diff options
Diffstat (limited to 'misc/thread_tools.c')
-rw-r--r-- | misc/thread_tools.c | 83 |
1 files changed, 45 insertions, 38 deletions
diff --git a/misc/thread_tools.c b/misc/thread_tools.c index 91b774eb93..0f7fe8f77a 100644 --- a/misc/thread_tools.c +++ b/misc/thread_tools.c @@ -14,10 +14,11 @@ */ #include <assert.h> +#include <errno.h> +#include <stdatomic.h> #include <string.h> #include <sys/types.h> #include <unistd.h> -#include <errno.h> #ifdef __MINGW32__ #include <windows.h> @@ -27,7 +28,6 @@ #include "common/common.h" #include "misc/linked_list.h" -#include "osdep/atomic.h" #include "osdep/io.h" #include "osdep/timer.h" @@ -35,10 +35,10 @@ uintptr_t mp_waiter_wait(struct mp_waiter *waiter) { - pthread_mutex_lock(&waiter->lock); + mp_mutex_lock(&waiter->lock); while (!waiter->done) - pthread_cond_wait(&waiter->wakeup, &waiter->lock); - pthread_mutex_unlock(&waiter->lock); + mp_cond_wait(&waiter->wakeup, &waiter->lock); + mp_mutex_unlock(&waiter->lock); uintptr_t ret = waiter->value; @@ -50,8 +50,8 @@ uintptr_t mp_waiter_wait(struct mp_waiter *waiter) // following functions will do nearly nothing. This is true for Windows // and Linux. But some lesser OSes still might allocate kernel objects // when initializing mutexes, so destroy them here. - pthread_mutex_destroy(&waiter->lock); - pthread_cond_destroy(&waiter->wakeup); + mp_mutex_destroy(&waiter->lock); + mp_cond_destroy(&waiter->wakeup); memset(waiter, 0xCA, sizeof(*waiter)); // for debugging @@ -60,25 +60,25 @@ uintptr_t mp_waiter_wait(struct mp_waiter *waiter) void mp_waiter_wakeup(struct mp_waiter *waiter, uintptr_t value) { - pthread_mutex_lock(&waiter->lock); + mp_mutex_lock(&waiter->lock); assert(!waiter->done); waiter->done = true; waiter->value = value; - pthread_cond_signal(&waiter->wakeup); - pthread_mutex_unlock(&waiter->lock); + mp_cond_signal(&waiter->wakeup); + mp_mutex_unlock(&waiter->lock); } bool mp_waiter_poll(struct mp_waiter *waiter) { - pthread_mutex_lock(&waiter->lock); + mp_mutex_lock(&waiter->lock); bool r = waiter->done; - pthread_mutex_unlock(&waiter->lock); + mp_mutex_unlock(&waiter->lock); return r; } struct mp_cancel { - pthread_mutex_t lock; - pthread_cond_t wakeup; + mp_mutex lock; + mp_cond wakeup; // Semaphore state and "mirrors". atomic_bool triggered; @@ -117,8 +117,8 @@ static void cancel_destroy(void *p) CloseHandle(c->win32_event); #endif - pthread_mutex_destroy(&c->lock); - pthread_cond_destroy(&c->wakeup); + mp_mutex_destroy(&c->lock); + mp_cond_destroy(&c->wakeup); } struct mp_cancel *mp_cancel_new(void *talloc_ctx) @@ -126,11 +126,11 @@ struct mp_cancel *mp_cancel_new(void *talloc_ctx) struct mp_cancel *c = talloc_ptrtype(talloc_ctx, c); talloc_set_destructor(c, cancel_destroy); *c = (struct mp_cancel){ - .triggered = ATOMIC_VAR_INIT(false), + .triggered = false, .wakeup_pipe = {-1, -1}, }; - pthread_mutex_init(&c->lock, NULL); - pthread_cond_init(&c->wakeup, NULL); + mp_mutex_init(&c->lock); + mp_cond_init(&c->wakeup); return c; } @@ -138,7 +138,7 @@ static void trigger_locked(struct mp_cancel *c) { atomic_store(&c->triggered, true); - pthread_cond_broadcast(&c->wakeup); // condition bound to c->triggered + mp_cond_broadcast(&c->wakeup); // condition bound to c->triggered if (c->cb) c->cb(c->cb_ctx); @@ -157,14 +157,14 @@ static void trigger_locked(struct mp_cancel *c) void mp_cancel_trigger(struct mp_cancel *c) { - pthread_mutex_lock(&c->lock); + mp_mutex_lock(&c->lock); trigger_locked(c); - pthread_mutex_unlock(&c->lock); + mp_mutex_unlock(&c->lock); } void mp_cancel_reset(struct mp_cancel *c) { - pthread_mutex_lock(&c->lock); + mp_mutex_lock(&c->lock); atomic_store(&c->triggered, false); @@ -182,7 +182,7 @@ void mp_cancel_reset(struct mp_cancel *c) ResetEvent(c->win32_event); #endif - pthread_mutex_unlock(&c->lock); + mp_mutex_unlock(&c->lock); } bool mp_cancel_test(struct mp_cancel *c) @@ -192,13 +192,13 @@ bool mp_cancel_test(struct mp_cancel *c) bool mp_cancel_wait(struct mp_cancel *c, double timeout) { - struct timespec ts = mp_rel_time_to_timespec(timeout); - pthread_mutex_lock(&c->lock); + int64_t wait_until = mp_time_ns_add(mp_time_ns(), timeout); + mp_mutex_lock(&c->lock); while (!mp_cancel_test(c)) { - if (pthread_cond_timedwait(&c->wakeup, &c->lock, &ts)) + if (mp_cond_timedwait_until(&c->wakeup, &c->lock, wait_until)) break; } - pthread_mutex_unlock(&c->lock); + mp_mutex_unlock(&c->lock); return mp_cancel_test(c); } @@ -213,11 +213,11 @@ static void retrigger_locked(struct mp_cancel *c) void mp_cancel_set_cb(struct mp_cancel *c, void (*cb)(void *ctx), void *ctx) { - pthread_mutex_lock(&c->lock); + mp_mutex_lock(&c->lock); c->cb = cb; c->cb_ctx = ctx; retrigger_locked(c); - pthread_mutex_unlock(&c->lock); + mp_mutex_unlock(&c->lock); } void mp_cancel_set_parent(struct mp_cancel *slave, struct mp_cancel *parent) @@ -228,27 +228,34 @@ void mp_cancel_set_parent(struct mp_cancel *slave, struct mp_cancel *parent) if (slave->parent == parent) return; if (slave->parent) { - pthread_mutex_lock(&slave->parent->lock); + mp_mutex_lock(&slave->parent->lock); LL_REMOVE(siblings, &slave->parent->slaves, slave); - pthread_mutex_unlock(&slave->parent->lock); + mp_mutex_unlock(&slave->parent->lock); } slave->parent = parent; if (slave->parent) { - pthread_mutex_lock(&slave->parent->lock); + mp_mutex_lock(&slave->parent->lock); LL_APPEND(siblings, &slave->parent->slaves, slave); retrigger_locked(slave->parent); - pthread_mutex_unlock(&slave->parent->lock); + mp_mutex_unlock(&slave->parent->lock); } } int mp_cancel_get_fd(struct mp_cancel *c) { - pthread_mutex_lock(&c->lock); + mp_mutex_lock(&c->lock); if (c->wakeup_pipe[0] < 0) { +#if defined(__GNUC__) && !defined(__clang__) +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wstringop-overflow=" +#endif mp_make_wakeup_pipe(c->wakeup_pipe); +#if defined(__GNUC__) && !defined(__clang__) +# pragma GCC diagnostic pop +#endif retrigger_locked(c); } - pthread_mutex_unlock(&c->lock); + mp_mutex_unlock(&c->lock); return c->wakeup_pipe[0]; @@ -257,12 +264,12 @@ int mp_cancel_get_fd(struct mp_cancel *c) #ifdef __MINGW32__ void *mp_cancel_get_event(struct mp_cancel *c) { - pthread_mutex_lock(&c->lock); + mp_mutex_lock(&c->lock); if (!c->win32_event) { c->win32_event = CreateEventW(NULL, TRUE, FALSE, NULL); retrigger_locked(c); } - pthread_mutex_unlock(&c->lock); + mp_mutex_unlock(&c->lock); return c->win32_event; } |