summaryrefslogtreecommitdiffstats
path: root/src/pulsecore/asyncmsgq.c
blob: 408416c918045e3a525a29cba982816a692c906f (plain)
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/***
  This file is part of PulseAudio.

  Copyright 2006 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, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  USA.
***/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <unistd.h>
#include <errno.h>

#include <pulse/xmalloc.h>

#include <pulsecore/macro.h>
#include <pulsecore/log.h>
#include <pulsecore/semaphore.h>
#include <pulsecore/macro.h>
#include <pulsecore/mutex.h>
#include <pulsecore/flist.h>

#include "asyncmsgq.h"

PA_STATIC_FLIST_DECLARE(asyncmsgq, 0, pa_xfree);
PA_STATIC_FLIST_DECLARE(semaphores, 0, (void(*)(void*)) pa_semaphore_free);

struct asyncmsgq_item {
    int code;
    pa_msgobject *object;
    void *userdata;
    pa_free_cb_t free_cb;
    int64_t offset;
    pa_memchunk memchunk;
    pa_semaphore *semaphore;
    int ret;
};

struct pa_asyncmsgq {
    PA_REFCNT_DECLARE;
    pa_asyncq *asyncq;
    pa_mutex *mutex; /* only for the writer side */

    struct asyncmsgq_item *current;
};

pa_asyncmsgq *pa_asyncmsgq_new(unsigned size) {
    pa_asyncmsgq *a;

    a = pa_xnew(pa_asyncmsgq, 1);

    PA_REFCNT_INIT(a);
    pa_assert_se(a->asyncq = pa_asyncq_new(size));
    pa_assert_se(a->mutex = pa_mutex_new(FALSE, TRUE));
    a->current = NULL;

    return a;
}

static void asyncmsgq_free(pa_asyncmsgq *a) {
    struct asyncmsgq_item *i;
    pa_assert(a);

    while ((i = pa_asyncq_pop(a->asyncq, FALSE))) {

        pa_assert(!i->semaphore);

        if (i->object)
            pa_msgobject_unref(i->object);

        if (i->memchunk.memblock)
            pa_memblock_unref(i->memchunk.memblock);

        if (i->free_cb)
            i->free_cb(i->userdata);

        if (pa_flist_push(PA_STATIC_FLIST_GET(asyncmsgq), i) < 0)
            pa_xfree(i);
    }

    pa_asyncq_free(a->asyncq, NULL);
    pa_mutex_free(a->mutex);
    pa_xfree(a);
}

pa_asyncmsgq* pa_asyncmsgq_ref(pa_asyncmsgq *q) {
    pa_assert(PA_REFCNT_VALUE(q) > 0);

    PA_REFCNT_INC(q);
    return q;
}

void pa_asyncmsgq_unref(pa_asyncmsgq* q) {
    pa_assert(PA_REFCNT_VALUE(q) > 0);

    if (PA_REFCNT_DEC(q) <= 0)
        asyncmsgq_free(q);
}

void pa_asyncmsgq_post(pa_asyncmsgq *a, pa_msgobject *object, int code, const void *userdata, int64_t offset, const pa_memchunk *chunk, pa_free_cb_t free_cb) {
    struct asyncmsgq_item *i;
    pa_assert(PA_REFCNT_VALUE(a) > 0);

    if (!(i = pa_flist_pop(PA_STATIC_FLIST_GET(asyncmsgq))))
        i = pa_xnew(struct asyncmsgq_item, 1);

    i->code = code;
    i->object = object ? pa_msgobject_ref(object) : NULL;
    i->userdata = (void*) userdata;
    i->free_cb = free_cb;
    i->offset = offset;
    if (chunk) {
        pa_assert(chunk->memblock);
        i->memchunk = *chunk;
        pa_memblock_ref(i->memchunk.memblock);
    } else
        pa_memchunk_reset(&i->memchunk);
    i->semaphore = NULL;

    /* This mutex makes the queue multiple-writer safe. This lock is only used on the writing side */
    pa_mutex_lock(a->mutex);
    pa_asyncq_post(a->asyncq, i);
    pa_mutex_unlock(a->mutex);
}

int pa_asyncmsgq_send(pa_asyncmsgq *a, pa_msgobject *object, int code, const void *userdata, int64_t offset, const pa_memchunk *chunk) {
    struct asyncmsgq_item i;
    pa_assert(PA_REFCNT_VALUE(a) > 0);

    i.code = code;
    i.object = object;
    i.userdata = (void*) userdata;
    i.free_cb = NULL;
    i.ret = -1;
    i.offset = offset;
    if (chunk) {
        pa_assert(chunk->memblock);
        i.memchunk = *chunk;
    } else
        pa_memchunk_reset(&i.memchunk);

    if (!(i.semaphore = pa_flist_pop(PA_STATIC_FLIST_GET(semaphores))))
        i.semaphore = pa_semaphore_new(0);

    pa_assert_se(i.semaphore);

    /* This mutex makes the queue multiple-writer safe. This lock is only used on the writing side */
    pa_mutex_lock(a->mutex);
    pa_assert_se(pa_asyncq_push(a->asyncq, &i, TRUE) == 0);
    pa_mutex_unlock(a->mutex);

    pa_semaphore_wait(i.semaphore);

    if (pa_flist_push(PA_STATIC_FLIST_GET(semaphores), i.semaphore) < 0)
        pa_semaphore_free(i.semaphore);

    return i.ret;
}

int pa_asyncmsgq_get(pa_asyncmsgq *a, pa_msgobject **object, int *code, void **userdata, int64_t *offset, pa_memchunk *chunk, pa_bool_t wait_op) {
    pa_assert(PA_REFCNT_VALUE(a) > 0);
    pa_assert(!a->current);

    if (!(a->current = pa_asyncq_pop(a->asyncq, wait_op))) {
/*         pa_log("failure"); */
        return -1;
    }

/*     pa_log("success"); */

    if (code)
        *code = a->current->code;
    if (userdata)
        *userdata = a->current->userdata;
    if (offset)
        *offset = a->current->offset;
    if (object) {
        if ((*object = a->current->object))
            pa_msgobject_assert_ref(*object);
    }
    if (chunk)
        *chunk = a->current->memchunk;

/*     pa_log_debug("Get q=%p object=%p (%s) code=%i data=%p chunk.length=%lu", */
/*                  (void*) a, */
/*                  (void*) a->current->object, */
/*                  a->current->object ? a->current->object->parent.type_name : NULL, */
/*                  a->current->code, */
/*                  (void*) a->current->userdata, */
/*                  (unsigned long) a->current->memchunk.length); */

    return 0;
}

void pa_asyncmsgq_done(pa_asyncmsgq *a, int ret) {
    pa_assert(PA_REFCNT_VALUE(a) > 0);
    pa_assert(a);
    pa_assert(a->current);

    if (a->current->semaphore) {
        a->current->ret = ret;
        pa_semaphore_post(a->current->semaphore);
    } else {

        if (a->current->free_cb)
            a->current->free_cb(a->current->userdata);

        if (a->current->object)
            pa_msgobject_unref(a->current->object);

        if (a->current->memchunk.memblock)
            pa_memblock_unref(a->current->memchunk.memblock);

        if (pa_flist_push(PA_STATIC_FLIST_GET(asyncmsgq), a->current) < 0)
            pa_xfree(a->current);
    }

    a->current = NULL;
}

int pa_asyncmsgq_wait_for(pa_asyncmsgq *a, int code) {
    int c;
    pa_assert(PA_REFCNT_VALUE(a) > 0);

    pa_asyncmsgq_ref(a);

    do {
        pa_msgobject *o;
        void *data;
        int64_t offset;
        pa_memchunk chunk;
        int ret;

        if (pa_asyncmsgq_get(a, &o, &c, &data, &offset, &chunk, TRUE) < 0)
            return -1;

        ret = pa_asyncmsgq_dispatch(o, c, data, offset, &chunk);
        pa_asyncmsgq_done(a, ret);

    } while (c != code);

    pa_asyncmsgq_unref(a);

    return 0;
}

int pa_asyncmsgq_process_one(pa_asyncmsgq *a) {
    pa_msgobject *object;
    int code;
    void *data;
    pa_memchunk chunk;
    int64_t offset;
    int ret;

    pa_assert(PA_REFCNT_VALUE(a) > 0);

    if (pa_asyncmsgq_get(a, &object, &code, &data, &offset, &chunk, FALSE) < 0)
        return 0;

    pa_asyncmsgq_ref(a);
    ret = pa_asyncmsgq_dispatch(object, code, data, offset, &chunk);
    pa_asyncmsgq_done(a, ret);
    pa_asyncmsgq_unref(a);

    return 1;
}

int pa_asyncmsgq_read_fd(pa_asyncmsgq *a) {
    pa_assert(PA_REFCNT_VALUE(a) > 0);

    return pa_asyncq_read_fd(a->asyncq);
}

int pa_asyncmsgq_read_before_poll(pa_asyncmsgq *a) {
    pa_assert(PA_REFCNT_VALUE(a) > 0);

    return pa_asyncq_read_before_poll(a->asyncq);
}

void pa_asyncmsgq_read_after_poll(pa_asyncmsgq *a) {
    pa_assert(PA_REFCNT_VALUE(a) > 0);

    pa_asyncq_read_after_poll(a->asyncq);
}

int pa_asyncmsgq_write_fd(pa_asyncmsgq *a) {
    pa_assert(PA_REFCNT_VALUE(a) > 0);

    return pa_asyncq_write_fd(a->asyncq);
}

void pa_asyncmsgq_write_before_poll(pa_asyncmsgq *a) {
    pa_assert(PA_REFCNT_VALUE(a) > 0);

    pa_asyncq_write_before_poll(a->asyncq);
}

void pa_asyncmsgq_write_after_poll(pa_asyncmsgq *a) {
    pa_assert(PA_REFCNT_VALUE(a) > 0);

    pa_asyncq_write_after_poll(a->asyncq);
}

int pa_asyncmsgq_dispatch(pa_msgobject *object, int code, void *userdata, int64_t offset, pa_memchunk *memchunk) {

    if (object)
        return object->process_msg(object, code, userdata, offset, pa_memchunk_isset(memchunk) ? memchunk : NULL);

    return 0;
}

void pa_asyncmsgq_flush(pa_asyncmsgq *a, pa_bool_t run) {
    pa_assert(PA_REFCNT_VALUE(a) > 0);

    for (;;) {
        pa_msgobject *object;
        int code;
        void *data;
        int64_t offset;
        pa_memchunk chunk;
        int ret;

        if (pa_asyncmsgq_get(a, &object, &code, &data, &offset, &chunk, FALSE) < 0)
            return;

        if (!run) {
            pa_asyncmsgq_done(a, -1);
            continue;
        }

        pa_asyncmsgq_ref(a);
        ret = pa_asyncmsgq_dispatch(object, code, data, offset, &chunk);
        pa_asyncmsgq_done(a, ret);
        pa_asyncmsgq_unref(a);
    }
}

pa_bool_t pa_asyncmsgq_dispatching(pa_asyncmsgq *a) {
    pa_assert(PA_REFCNT_VALUE(a) > 0);

    return !!a->current;
}