]> code.delx.au - pulseaudio/blob - src/pulsecore/thread-posix.c
add a threading primitive API
[pulseaudio] / src / pulsecore / thread-posix.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
10
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <assert.h>
27 #include <pthread.h>
28 #include <atomic_ops.h>
29
30 #include <pulse/xmalloc.h>
31
32 #include "thread.h"
33
34 #define ASSERT_SUCCESS(x) do { \
35 int _r = (x); \
36 assert(_r == 0); \
37 } while(0)
38
39 struct pa_thread {
40 pthread_t id;
41 pa_thread_func_t thread_func;
42 void *userdata;
43 AO_t running;
44 };
45
46 struct pa_tls {
47 pthread_key_t key;
48 };
49
50 static pa_tls *thread_tls;
51 static pthread_once_t thread_tls_once = PTHREAD_ONCE_INIT;
52
53 static void thread_tls_once_func(void) {
54 thread_tls = pa_tls_new(NULL);
55 assert(thread_tls);
56 }
57
58 static void* internal_thread_func(void *userdata) {
59 pa_thread *t = userdata;
60 assert(t);
61
62 t->id = pthread_self();
63
64 ASSERT_SUCCESS(pthread_once(&thread_tls_once, thread_tls_once_func));
65 pa_tls_set(thread_tls, t);
66
67 AO_store_release_write(&t->running, 1);
68 t->thread_func(t->userdata);
69 AO_store_release_write(&t->running, 0);
70
71 return NULL;
72 }
73
74 pa_thread* pa_thread_new(pa_thread_func_t thread_func, void *userdata) {
75 pa_thread *t;
76
77 t = pa_xnew(pa_thread, 1);
78 t->thread_func = thread_func;
79 t->userdata = userdata;
80
81 if (pthread_create(&t->id, NULL, internal_thread_func, t) < 0) {
82 pa_xfree(t);
83 return NULL;
84 }
85
86 return t;
87 }
88
89 int pa_thread_is_running(pa_thread *t) {
90 assert(t);
91
92 return !!AO_load_acquire_read(&t->running);
93 }
94
95 void pa_thread_free(pa_thread *t) {
96 assert(t);
97
98 pa_thread_join(t);
99 pa_xfree(t);
100 }
101
102 int pa_thread_join(pa_thread *t) {
103 assert(t);
104
105 return pthread_join(t->id, NULL);
106 }
107
108 pa_thread* pa_thread_self(void) {
109 ASSERT_SUCCESS(pthread_once(&thread_tls_once, thread_tls_once_func));
110 return pa_tls_get(thread_tls);
111 }
112
113 pa_tls* pa_tls_new(pa_free_cb_t free_cb) {
114 pa_tls *t;
115
116 t = pa_xnew(pa_tls, 1);
117
118 if (pthread_key_create(&t->key, free_cb) < 0) {
119 pa_xfree(t);
120 return NULL;
121 }
122
123 return t;
124 }
125
126 void pa_tls_free(pa_tls *t) {
127 assert(t);
128
129 ASSERT_SUCCESS(pthread_key_delete(t->key));
130 pa_xfree(t);
131 }
132
133 void *pa_tls_get(pa_tls *t) {
134 assert(t);
135
136 return pthread_getspecific(t->key);
137 }
138
139 void *pa_tls_set(pa_tls *t, void *userdata) {
140 void *r;
141
142 r = pthread_getspecific(t->key);
143 ASSERT_SUCCESS(pthread_setspecific(t->key, userdata));
144 return r;
145 }
146