]> code.delx.au - pulseaudio/blob - src/pulsecore/thread-posix.c
Also wrap yield functionality so that it can be platform independent.
[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 <sched.h>
29
30 #include <atomic_ops.h>
31
32 #include <pulse/xmalloc.h>
33
34 #include "thread.h"
35
36 #define ASSERT_SUCCESS(x) do { \
37 int _r = (x); \
38 assert(_r == 0); \
39 } while(0)
40
41 struct pa_thread {
42 pthread_t id;
43 pa_thread_func_t thread_func;
44 void *userdata;
45 AO_t running;
46 };
47
48 struct pa_tls {
49 pthread_key_t key;
50 };
51
52 static pa_tls *thread_tls;
53 static pthread_once_t thread_tls_once = PTHREAD_ONCE_INIT;
54
55 static void thread_tls_once_func(void) {
56 thread_tls = pa_tls_new(NULL);
57 assert(thread_tls);
58 }
59
60 static void* internal_thread_func(void *userdata) {
61 pa_thread *t = userdata;
62 assert(t);
63
64 t->id = pthread_self();
65
66 ASSERT_SUCCESS(pthread_once(&thread_tls_once, thread_tls_once_func));
67 pa_tls_set(thread_tls, t);
68
69 AO_fetch_and_add1_full(&t->running);
70 t->thread_func(t->userdata);
71 AO_fetch_and_add_full(&t->running, (AO_t) -2);
72
73 return NULL;
74 }
75
76 pa_thread* pa_thread_new(pa_thread_func_t thread_func, void *userdata) {
77 pa_thread *t;
78
79 t = pa_xnew(pa_thread, 1);
80 t->thread_func = thread_func;
81 t->userdata = userdata;
82 AO_store_full(&t->running, 0);
83
84 if (pthread_create(&t->id, NULL, internal_thread_func, t) < 0) {
85 pa_xfree(t);
86 return NULL;
87 }
88
89 AO_fetch_and_add1_full(&t->running);
90
91 return t;
92 }
93
94 int pa_thread_is_running(pa_thread *t) {
95 AO_t r;
96 assert(t);
97
98 r = AO_load_full(&t->running);
99 return r == 1 || r == 2;
100 }
101
102 void pa_thread_free(pa_thread *t) {
103 assert(t);
104
105 pa_thread_join(t);
106 pa_xfree(t);
107 }
108
109 int pa_thread_join(pa_thread *t) {
110 assert(t);
111
112 return pthread_join(t->id, NULL);
113 }
114
115 pa_thread* pa_thread_self(void) {
116 ASSERT_SUCCESS(pthread_once(&thread_tls_once, thread_tls_once_func));
117 return pa_tls_get(thread_tls);
118 }
119
120 void pa_thread_yield(void) {
121 #ifdef HAVE_PTHREAD_YIELD
122 pthread_yield();
123 #else
124 sched_yield();
125 #endif
126 }
127
128 pa_tls* pa_tls_new(pa_free_cb_t free_cb) {
129 pa_tls *t;
130
131 t = pa_xnew(pa_tls, 1);
132
133 if (pthread_key_create(&t->key, free_cb) < 0) {
134 pa_xfree(t);
135 return NULL;
136 }
137
138 return t;
139 }
140
141 void pa_tls_free(pa_tls *t) {
142 assert(t);
143
144 ASSERT_SUCCESS(pthread_key_delete(t->key));
145 pa_xfree(t);
146 }
147
148 void *pa_tls_get(pa_tls *t) {
149 assert(t);
150
151 return pthread_getspecific(t->key);
152 }
153
154 void *pa_tls_set(pa_tls *t, void *userdata) {
155 void *r;
156
157 r = pthread_getspecific(t->key);
158 ASSERT_SUCCESS(pthread_setspecific(t->key, userdata));
159 return r;
160 }
161