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
This commit is contained in:
Lennart Poettering 2006-09-09 22:59:17 +00:00
parent 3426a399cc
commit 6d532029ea
2 changed files with 13 additions and 43 deletions

View file

@ -28,10 +28,10 @@
#include <sched.h> #include <sched.h>
#include <errno.h> #include <errno.h>
#include <atomic_ops.h>
#include <pulse/xmalloc.h> #include <pulse/xmalloc.h>
#include <pulsecore/mutex.h> #include <pulsecore/mutex.h>
#include <pulsecore/once.h>
#include <pulsecore/atomic.h>
#include "thread.h" #include "thread.h"
@ -44,7 +44,7 @@ struct pa_thread {
pthread_t id; pthread_t id;
pa_thread_func_t thread_func; pa_thread_func_t thread_func;
void *userdata; void *userdata;
AO_t running; pa_atomic_int_t running;
}; };
struct pa_tls { struct pa_tls {
@ -52,10 +52,7 @@ struct pa_tls {
}; };
static pa_tls *thread_tls; static pa_tls *thread_tls;
static pthread_once_t thread_tls_once = PTHREAD_ONCE_INIT; static pa_once_t thread_tls_once = PA_ONCE_INIT;
static pa_mutex *once_mutex;
static pthread_once_t thread_once_once = PTHREAD_ONCE_INIT;
static void tls_free_cb(void *p) { static void tls_free_cb(void *p) {
pa_thread *t = p; pa_thread *t = p;
@ -78,12 +75,13 @@ static void* internal_thread_func(void *userdata) {
t->id = pthread_self(); 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); pa_tls_set(thread_tls, t);
AO_fetch_and_add1_full(&t->running); pa_atomic_inc(&t->running);
t->thread_func(t->userdata); t->thread_func(t->userdata);
AO_fetch_and_add_full(&t->running, (AO_t) -2); pa_atomic_add(&t->running, -2);
return NULL; 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 = pa_xnew(pa_thread, 1);
t->thread_func = thread_func; t->thread_func = thread_func;
t->userdata = userdata; 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) { if (pthread_create(&t->id, NULL, internal_thread_func, t) < 0) {
pa_xfree(t); pa_xfree(t);
return NULL; return NULL;
} }
AO_fetch_and_add1_full(&t->running); pa_atomic_inc(&t->running);
return t; return t;
} }
int pa_thread_is_running(pa_thread *t) { int pa_thread_is_running(pa_thread *t) {
AO_t r;
assert(t); assert(t);
if (!t->thread_func) { if (!t->thread_func) {
@ -123,8 +120,7 @@ int pa_thread_is_running(pa_thread *t) {
return pthread_getschedparam(t->id, &policy, &param) >= 0 || errno != ESRCH; return pthread_getschedparam(t->id, &policy, &param) >= 0 || errno != ESRCH;
} }
r = AO_load_full(&t->running); return pa_atomic_load(&t->running) > 0;
return r == 1 || r == 2;
} }
void pa_thread_free(pa_thread *t) { 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* pa_thread_self(void) {
pa_thread *t; 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))) if ((t = pa_tls_get(thread_tls)))
return t; return t;
@ -155,7 +151,7 @@ pa_thread* pa_thread_self(void) {
t->id = pthread_self(); t->id = pthread_self();
t->thread_func = NULL; t->thread_func = NULL;
t->userdata = NULL; t->userdata = NULL;
AO_store_full(&t->running, 1); pa_atomic_store(&t->running, 2);
pa_tls_set(thread_tls, t); pa_tls_set(thread_tls, t);
@ -182,27 +178,6 @@ void pa_thread_yield(void) {
#endif #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* pa_tls_new(pa_free_cb_t free_cb) {
pa_tls *t; pa_tls *t;

View file

@ -24,13 +24,9 @@
#include <pulse/def.h> #include <pulse/def.h>
#define PA_THREAD_ONCE_INIT 0
typedef struct pa_thread pa_thread; typedef struct pa_thread pa_thread;
typedef void (*pa_thread_func_t) (void *userdata); 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); pa_thread* pa_thread_new(pa_thread_func_t thread_func, void *userdata);
void pa_thread_free(pa_thread *t); 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); int pa_thread_is_running(pa_thread *t);
pa_thread *pa_thread_self(void); pa_thread *pa_thread_self(void);
void pa_thread_yield(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_get_data(pa_thread *t);
void pa_thread_set_data(pa_thread *t, void *userdata); void pa_thread_set_data(pa_thread *t, void *userdata);