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