2 This file is part of PulseAudio.
4 Copyright 2006-2008 Lennart Poettering
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.
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.
16 You should have received a copy of the GNU Lesser General Public
17 License along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
27 #include <pulse/xmalloc.h>
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 <pulsecore/fdsem.h>
40 #define ASYNCQ_SIZE 256
42 /* For debugging purposes we can define _Y to put an extra thread
43 * yield between each operation. */
48 #define _Y pa_thread_yield()
50 #define _Y do { } while(0)
55 PA_LLIST_FIELDS(struct localq);
62 pa_fdsem *read_fdsem, *write_fdsem;
64 PA_LLIST_HEAD(struct localq, localq);
65 struct localq *last_localq;
66 bool waiting_for_post;
69 PA_STATIC_FLIST_DECLARE(localq, 0, pa_xfree);
71 #define PA_ASYNCQ_CELLS(x) ((pa_atomic_ptr_t*) ((uint8_t*) (x) + PA_ALIGN(sizeof(struct pa_asyncq))))
73 static unsigned reduce(pa_asyncq *l, unsigned value) {
74 return value & (unsigned) (l->size - 1);
77 pa_asyncq *pa_asyncq_new(unsigned size) {
83 pa_assert(pa_is_power_of_two(size));
85 l = pa_xmalloc0(PA_ALIGN(sizeof(pa_asyncq)) + (sizeof(pa_atomic_ptr_t) * size));
89 PA_LLIST_HEAD_INIT(struct localq, l->localq);
90 l->last_localq = NULL;
91 l->waiting_for_post = false;
93 if (!(l->read_fdsem = pa_fdsem_new())) {
98 if (!(l->write_fdsem = pa_fdsem_new())) {
99 pa_fdsem_free(l->read_fdsem);
107 void pa_asyncq_free(pa_asyncq *l, pa_free_cb_t free_cb) {
114 while ((p = pa_asyncq_pop(l, 0)))
118 while ((q = l->localq)) {
122 PA_LLIST_REMOVE(struct localq, l->localq, q);
124 if (pa_flist_push(PA_STATIC_FLIST_GET(localq), q) < 0)
128 pa_fdsem_free(l->read_fdsem);
129 pa_fdsem_free(l->write_fdsem);
133 static int push(pa_asyncq*l, void *p, bool wait_op) {
135 pa_atomic_ptr_t *cells;
140 cells = PA_ASYNCQ_CELLS(l);
143 idx = reduce(l, l->write_idx);
145 if (!pa_atomic_ptr_cmpxchg(&cells[idx], NULL, p)) {
150 /* pa_log("sleeping on push"); */
153 pa_fdsem_wait(l->read_fdsem);
154 } while (!pa_atomic_ptr_cmpxchg(&cells[idx], NULL, p));
160 pa_fdsem_post(l->write_fdsem);
165 static bool flush_postq(pa_asyncq *l, bool wait_op) {
170 while ((q = l->last_localq)) {
172 if (push(l, q->data, wait_op) < 0)
175 l->last_localq = q->prev;
177 PA_LLIST_REMOVE(struct localq, l->localq, q);
179 if (pa_flist_push(PA_STATIC_FLIST_GET(localq), q) < 0)
186 int pa_asyncq_push(pa_asyncq*l, void *p, bool wait_op) {
189 if (!flush_postq(l, wait_op))
192 return push(l, p, wait_op);
195 void pa_asyncq_post(pa_asyncq*l, void *p) {
201 if (flush_postq(l, false))
202 if (pa_asyncq_push(l, p, false) >= 0)
205 /* OK, we couldn't push anything in the queue. So let's queue it
206 * locally and push it later */
208 if (pa_log_ratelimit(PA_LOG_WARN))
209 pa_log_warn("q overrun, queuing locally");
211 if (!(q = pa_flist_pop(PA_STATIC_FLIST_GET(localq))))
212 q = pa_xnew(struct localq, 1);
215 PA_LLIST_PREPEND(struct localq, l->localq, q);
223 void* pa_asyncq_pop(pa_asyncq*l, bool wait_op) {
226 pa_atomic_ptr_t *cells;
230 cells = PA_ASYNCQ_CELLS(l);
233 idx = reduce(l, l->read_idx);
235 if (!(ret = pa_atomic_ptr_load(&cells[idx]))) {
240 /* pa_log("sleeping on pop"); */
243 pa_fdsem_wait(l->write_fdsem);
244 } while (!(ret = pa_atomic_ptr_load(&cells[idx])));
249 /* Guaranteed to succeed if we only have a single reader */
250 pa_assert_se(pa_atomic_ptr_cmpxchg(&cells[idx], ret, NULL));
255 pa_fdsem_post(l->read_fdsem);
260 int pa_asyncq_read_fd(pa_asyncq *q) {
263 return pa_fdsem_get(q->write_fdsem);
266 int pa_asyncq_read_before_poll(pa_asyncq *l) {
268 pa_atomic_ptr_t *cells;
272 cells = PA_ASYNCQ_CELLS(l);
275 idx = reduce(l, l->read_idx);
278 if (pa_atomic_ptr_load(&cells[idx]))
281 if (pa_fdsem_before_poll(l->write_fdsem) >= 0)
286 void pa_asyncq_read_after_poll(pa_asyncq *l) {
289 pa_fdsem_after_poll(l->write_fdsem);
292 int pa_asyncq_write_fd(pa_asyncq *q) {
295 return pa_fdsem_get(q->read_fdsem);
298 void pa_asyncq_write_before_poll(pa_asyncq *l) {
303 if (flush_postq(l, false))
306 if (pa_fdsem_before_poll(l->read_fdsem) >= 0) {
307 l->waiting_for_post = true;
313 void pa_asyncq_write_after_poll(pa_asyncq *l) {
316 if (l->waiting_for_post) {
317 pa_fdsem_after_poll(l->read_fdsem);
318 l->waiting_for_post = false;