From 6d532029eaac08a3b60a28752f23f0586f895168 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Sat, 9 Sep 2006 22:59:17 +0000 Subject: update for newer APIs: replace direct usage of libatomic_ops by usage of our own atomic.h; remove pa_once implementation; always use our pa_once implementation instead of the POSIX version git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@1386 fefdeb5f-60dc-0310-8127-8f9354f1896f --- src/pulsecore/thread-posix.c | 51 +++++++++++--------------------------------- src/pulsecore/thread.h | 5 ----- 2 files changed, 13 insertions(+), 43 deletions(-) diff --git a/src/pulsecore/thread-posix.c b/src/pulsecore/thread-posix.c index a2cb9b56..d69790a5 100644 --- a/src/pulsecore/thread-posix.c +++ b/src/pulsecore/thread-posix.c @@ -28,10 +28,10 @@ #include #include -#include - #include #include +#include +#include #include "thread.h" @@ -44,7 +44,7 @@ struct pa_thread { pthread_t id; pa_thread_func_t thread_func; void *userdata; - AO_t running; + pa_atomic_int_t running; }; struct pa_tls { @@ -52,10 +52,7 @@ struct pa_tls { }; static pa_tls *thread_tls; -static pthread_once_t thread_tls_once = PTHREAD_ONCE_INIT; - -static pa_mutex *once_mutex; -static pthread_once_t thread_once_once = PTHREAD_ONCE_INIT; +static pa_once_t thread_tls_once = PA_ONCE_INIT; static void tls_free_cb(void *p) { pa_thread *t = p; @@ -78,12 +75,13 @@ static void* internal_thread_func(void *userdata) { t->id = pthread_self(); - ASSERT_SUCCESS(pthread_once(&thread_tls_once, thread_tls_once_func)); + pa_once(&thread_tls_once, thread_tls_once_func); + pa_tls_set(thread_tls, t); - AO_fetch_and_add1_full(&t->running); + pa_atomic_inc(&t->running); t->thread_func(t->userdata); - AO_fetch_and_add_full(&t->running, (AO_t) -2); + pa_atomic_add(&t->running, -2); return NULL; } @@ -96,20 +94,19 @@ pa_thread* pa_thread_new(pa_thread_func_t thread_func, void *userdata) { t = pa_xnew(pa_thread, 1); t->thread_func = thread_func; t->userdata = userdata; - AO_store_full(&t->running, 0); + pa_atomic_store(&t->running, 0); if (pthread_create(&t->id, NULL, internal_thread_func, t) < 0) { pa_xfree(t); return NULL; } - AO_fetch_and_add1_full(&t->running); + pa_atomic_inc(&t->running); return t; } int pa_thread_is_running(pa_thread *t) { - AO_t r; assert(t); if (!t->thread_func) { @@ -123,8 +120,7 @@ int pa_thread_is_running(pa_thread *t) { return pthread_getschedparam(t->id, &policy, ¶m) >= 0 || errno != ESRCH; } - r = AO_load_full(&t->running); - return r == 1 || r == 2; + return pa_atomic_load(&t->running) > 0; } void pa_thread_free(pa_thread *t) { @@ -143,7 +139,7 @@ int pa_thread_join(pa_thread *t) { pa_thread* pa_thread_self(void) { pa_thread *t; - ASSERT_SUCCESS(pthread_once(&thread_tls_once, thread_tls_once_func)); + pa_once(&thread_tls_once, thread_tls_once_func); if ((t = pa_tls_get(thread_tls))) return t; @@ -155,7 +151,7 @@ pa_thread* pa_thread_self(void) { t->id = pthread_self(); t->thread_func = NULL; t->userdata = NULL; - AO_store_full(&t->running, 1); + pa_atomic_store(&t->running, 2); pa_tls_set(thread_tls, t); @@ -182,27 +178,6 @@ void pa_thread_yield(void) { #endif } -static void thread_once_once_func(void) { - once_mutex = pa_mutex_new(0); - assert(once_mutex); -} - -void pa_thread_once(pa_thread_once_t *control, pa_thread_once_func_t once_func) { - assert(control); - assert(once_func); - - ASSERT_SUCCESS(pthread_once(&thread_once_once, thread_once_once_func)); - - pa_mutex_lock(once_mutex); - - if (*control == PA_THREAD_ONCE_INIT) { - *control = ~PA_THREAD_ONCE_INIT; - pa_mutex_unlock(once_mutex); - once_func(); - } else - pa_mutex_unlock(once_mutex); -} - pa_tls* pa_tls_new(pa_free_cb_t free_cb) { pa_tls *t; diff --git a/src/pulsecore/thread.h b/src/pulsecore/thread.h index e50a707f..d08990a2 100644 --- a/src/pulsecore/thread.h +++ b/src/pulsecore/thread.h @@ -24,13 +24,9 @@ #include -#define PA_THREAD_ONCE_INIT 0 - typedef struct pa_thread pa_thread; typedef void (*pa_thread_func_t) (void *userdata); -typedef void (*pa_thread_once_func_t) (void); -typedef unsigned int pa_thread_once_t; pa_thread* pa_thread_new(pa_thread_func_t thread_func, void *userdata); void pa_thread_free(pa_thread *t); @@ -38,7 +34,6 @@ int pa_thread_join(pa_thread *t); int pa_thread_is_running(pa_thread *t); pa_thread *pa_thread_self(void); void pa_thread_yield(void); -void pa_thread_once(pa_thread_once_t *control, pa_thread_once_func_t once_func); void* pa_thread_get_data(pa_thread *t); void pa_thread_set_data(pa_thread *t, void *userdata); -- cgit