]> code.delx.au - pulseaudio/blob - src/pulsecore/asyncq.c
Merge dead branch 'ossman'
[pulseaudio] / src / pulsecore / asyncq.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2006-2008 Lennart Poettering
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
8 published by the Free Software Foundation; either version 2.1 of the
9 License, 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 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License 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 <unistd.h>
27 #include <errno.h>
28
29 #include <pulsecore/atomic.h>
30 #include <pulsecore/log.h>
31 #include <pulsecore/thread.h>
32 #include <pulsecore/macro.h>
33 #include <pulsecore/core-util.h>
34 #include <pulsecore/llist.h>
35 #include <pulsecore/flist.h>
36 #include <pulse/xmalloc.h>
37
38 #include "asyncq.h"
39 #include "fdsem.h"
40
41 #define ASYNCQ_SIZE 256
42
43 /* For debugging purposes we can define _Y to put an extra thread
44 * yield between each operation. */
45
46 /* #define PROFILE */
47
48 #ifdef PROFILE
49 #define _Y pa_thread_yield()
50 #else
51 #define _Y do { } while(0)
52 #endif
53
54 struct localq {
55 void *data;
56 PA_LLIST_FIELDS(struct localq);
57 };
58
59 struct pa_asyncq {
60 unsigned size;
61 unsigned read_idx;
62 unsigned write_idx;
63 pa_fdsem *read_fdsem, *write_fdsem;
64
65 PA_LLIST_HEAD(struct localq, localq);
66 struct localq *last_localq;
67 pa_bool_t waiting_for_post;
68 };
69
70 PA_STATIC_FLIST_DECLARE(localq, 0, pa_xfree);
71
72 #define PA_ASYNCQ_CELLS(x) ((pa_atomic_ptr_t*) ((uint8_t*) (x) + PA_ALIGN(sizeof(struct pa_asyncq))))
73
74 static int reduce(pa_asyncq *l, int value) {
75 return value & (unsigned) (l->size - 1);
76 }
77
78 pa_asyncq *pa_asyncq_new(unsigned size) {
79 pa_asyncq *l;
80
81 if (!size)
82 size = ASYNCQ_SIZE;
83
84 pa_assert(pa_is_power_of_two(size));
85
86 l = pa_xmalloc0(PA_ALIGN(sizeof(pa_asyncq)) + (sizeof(pa_atomic_ptr_t) * size));
87
88 l->size = size;
89
90 PA_LLIST_HEAD_INIT(struct localq, l->localq);
91 l->last_localq = NULL;
92 l->waiting_for_post = FALSE;
93
94 if (!(l->read_fdsem = pa_fdsem_new())) {
95 pa_xfree(l);
96 return NULL;
97 }
98
99 if (!(l->write_fdsem = pa_fdsem_new())) {
100 pa_fdsem_free(l->read_fdsem);
101 pa_xfree(l);
102 return NULL;
103 }
104
105 return l;
106 }
107
108 void pa_asyncq_free(pa_asyncq *l, pa_free_cb_t free_cb) {
109 struct localq *q;
110 pa_assert(l);
111
112 if (free_cb) {
113 void *p;
114
115 while ((p = pa_asyncq_pop(l, 0)))
116 free_cb(p);
117 }
118
119 while ((q = l->localq)) {
120 if (free_cb)
121 free_cb(q->data);
122
123 PA_LLIST_REMOVE(struct localq, l->localq, q);
124
125 if (pa_flist_push(PA_STATIC_FLIST_GET(localq), q) < 0)
126 pa_xfree(q);
127 }
128
129 pa_fdsem_free(l->read_fdsem);
130 pa_fdsem_free(l->write_fdsem);
131 pa_xfree(l);
132 }
133
134 static int push(pa_asyncq*l, void *p, pa_bool_t wait) {
135 int idx;
136 pa_atomic_ptr_t *cells;
137
138 pa_assert(l);
139 pa_assert(p);
140
141 cells = PA_ASYNCQ_CELLS(l);
142
143 _Y;
144 idx = reduce(l, l->write_idx);
145
146 if (!pa_atomic_ptr_cmpxchg(&cells[idx], NULL, p)) {
147
148 if (!wait)
149 return -1;
150
151 /* pa_log("sleeping on push"); */
152
153 do {
154 pa_fdsem_wait(l->read_fdsem);
155 } while (!pa_atomic_ptr_cmpxchg(&cells[idx], NULL, p));
156 }
157
158 _Y;
159 l->write_idx++;
160
161 pa_fdsem_post(l->write_fdsem);
162
163 return 0;
164 }
165
166 static pa_bool_t flush_postq(pa_asyncq *l) {
167 struct localq *q;
168
169 pa_assert(l);
170
171 while ((q = l->last_localq)) {
172
173 if (push(l, q->data, FALSE) < 0)
174 return FALSE;
175
176 l->last_localq = q->prev;
177
178 PA_LLIST_REMOVE(struct localq, l->localq, q);
179
180 if (pa_flist_push(PA_STATIC_FLIST_GET(localq), q) < 0)
181 pa_xfree(q);
182 }
183
184 return TRUE;
185 }
186
187 int pa_asyncq_push(pa_asyncq*l, void *p, pa_bool_t wait) {
188 pa_assert(l);
189
190 if (!flush_postq(l))
191 return -1;
192
193 return push(l, p, wait);
194 }
195
196 void pa_asyncq_post(pa_asyncq*l, void *p) {
197 struct localq *q;
198
199 pa_assert(l);
200 pa_assert(p);
201
202 if (pa_asyncq_push(l, p, FALSE) >= 0)
203 return;
204
205 /* OK, we couldn't push anything in the queue. So let's queue it
206 * locally and push it later */
207
208 pa_log("q overrun, queuing locally");
209
210 if (!(q = pa_flist_pop(PA_STATIC_FLIST_GET(localq))))
211 q = pa_xnew(struct localq, 1);
212
213 q->data = p;
214 PA_LLIST_PREPEND(struct localq, l->localq, q);
215
216 if (!l->last_localq)
217 l->last_localq = q;
218
219 return;
220 }
221
222 void* pa_asyncq_pop(pa_asyncq*l, pa_bool_t wait) {
223 int idx;
224 void *ret;
225 pa_atomic_ptr_t *cells;
226
227 pa_assert(l);
228
229 cells = PA_ASYNCQ_CELLS(l);
230
231 _Y;
232 idx = reduce(l, l->read_idx);
233
234 if (!(ret = pa_atomic_ptr_load(&cells[idx]))) {
235
236 if (!wait)
237 return NULL;
238
239 /* pa_log("sleeping on pop"); */
240
241 do {
242 pa_fdsem_wait(l->write_fdsem);
243 } while (!(ret = pa_atomic_ptr_load(&cells[idx])));
244 }
245
246 pa_assert(ret);
247
248 /* Guaranteed to succeed if we only have a single reader */
249 pa_assert_se(pa_atomic_ptr_cmpxchg(&cells[idx], ret, NULL));
250
251 _Y;
252 l->read_idx++;
253
254 pa_fdsem_post(l->read_fdsem);
255
256 return ret;
257 }
258
259 int pa_asyncq_read_fd(pa_asyncq *q) {
260 pa_assert(q);
261
262 return pa_fdsem_get(q->write_fdsem);
263 }
264
265 int pa_asyncq_read_before_poll(pa_asyncq *l) {
266 int idx;
267 pa_atomic_ptr_t *cells;
268
269 pa_assert(l);
270
271 cells = PA_ASYNCQ_CELLS(l);
272
273 _Y;
274 idx = reduce(l, l->read_idx);
275
276 for (;;) {
277 if (pa_atomic_ptr_load(&cells[idx]))
278 return -1;
279
280 if (pa_fdsem_before_poll(l->write_fdsem) >= 0)
281 return 0;
282 }
283
284 return 0;
285 }
286
287 void pa_asyncq_read_after_poll(pa_asyncq *l) {
288 pa_assert(l);
289
290 pa_fdsem_after_poll(l->write_fdsem);
291 }
292
293 int pa_asyncq_write_fd(pa_asyncq *q) {
294 pa_assert(q);
295
296 return pa_fdsem_get(q->read_fdsem);
297 }
298
299 void pa_asyncq_write_before_poll(pa_asyncq *l) {
300 pa_assert(l);
301
302 for (;;) {
303
304 if (flush_postq(l))
305 break;
306
307 if (pa_fdsem_before_poll(l->read_fdsem) >= 0) {
308 l->waiting_for_post = TRUE;
309 break;
310 }
311 }
312 }
313
314 void pa_asyncq_write_after_poll(pa_asyncq *l) {
315 pa_assert(l);
316
317 if (l->waiting_for_post) {
318 pa_fdsem_after_poll(l->read_fdsem);
319 l->waiting_for_post = FALSE;
320 }
321 }