2 This file is part of PulseAudio.
4 Copyright 2006 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, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
29 #include <pulse/xmalloc.h>
31 #include <pulsecore/atomic.h>
32 #include <pulsecore/once.h>
33 #include <pulsecore/log.h>
34 #include <pulsecore/thread.h>
35 #include <pulsecore/semaphore.h>
36 #include <pulsecore/macro.h>
37 #include <pulsecore/core-util.h>
38 #include <pulsecore/flist.h>
40 #include "thread-mq.h"
42 PA_STATIC_TLS_DECLARE_NO_FREE(thread_mq
);
44 static void asyncmsgq_read_cb(pa_mainloop_api
*api
, pa_io_event
* e
, int fd
, pa_io_event_flags_t events
, void *userdata
) {
45 pa_thread_mq
*q
= userdata
;
48 pa_assert(pa_asyncmsgq_read_fd(q
->outq
) == fd
);
49 pa_assert(events
== PA_IO_EVENT_INPUT
);
51 pa_asyncmsgq_ref(aq
= q
->outq
);
52 pa_asyncmsgq_write_after_poll(aq
);
61 /* Check whether there is a message for us to process */
62 while (pa_asyncmsgq_get(aq
, &object
, &code
, &data
, &offset
, &chunk
, 0) >= 0) {
65 ret
= pa_asyncmsgq_dispatch(object
, code
, data
, offset
, &chunk
);
66 pa_asyncmsgq_done(aq
, ret
);
69 if (pa_asyncmsgq_read_before_poll(aq
) == 0)
73 pa_asyncmsgq_unref(aq
);
76 static void asyncmsgq_write_cb(pa_mainloop_api
*api
, pa_io_event
* e
, int fd
, pa_io_event_flags_t events
, void *userdata
) {
77 pa_thread_mq
*q
= userdata
;
79 pa_assert(pa_asyncmsgq_write_fd(q
->inq
) == fd
);
80 pa_assert(events
== PA_IO_EVENT_INPUT
);
82 pa_asyncmsgq_write_after_poll(q
->inq
);
83 pa_asyncmsgq_write_before_poll(q
->inq
);
86 void pa_thread_mq_init(pa_thread_mq
*q
, pa_mainloop_api
*mainloop
, pa_rtpoll
*rtpoll
) {
90 q
->mainloop
= mainloop
;
91 pa_assert_se(q
->inq
= pa_asyncmsgq_new(0));
92 pa_assert_se(q
->outq
= pa_asyncmsgq_new(0));
94 pa_assert_se(pa_asyncmsgq_read_before_poll(q
->outq
) == 0);
95 pa_assert_se(q
->read_event
= mainloop
->io_new(mainloop
, pa_asyncmsgq_read_fd(q
->outq
), PA_IO_EVENT_INPUT
, asyncmsgq_read_cb
, q
));
97 pa_asyncmsgq_write_before_poll(q
->inq
);
98 pa_assert_se(q
->write_event
= mainloop
->io_new(mainloop
, pa_asyncmsgq_write_fd(q
->inq
), PA_IO_EVENT_INPUT
, asyncmsgq_write_cb
, q
));
100 pa_rtpoll_item_new_asyncmsgq_read(rtpoll
, PA_RTPOLL_EARLY
, q
->inq
);
101 pa_rtpoll_item_new_asyncmsgq_write(rtpoll
, PA_RTPOLL_LATE
, q
->outq
);
104 void pa_thread_mq_done(pa_thread_mq
*q
) {
107 /* Since we are called from main context we can be sure that the
108 * inq is empty. However, the outq might still contain messages
109 * for the main loop, which we need to dispatch (e.g. release
110 * msgs, other stuff). Hence do so if we aren't currently
111 * dispatching anyway. */
113 if (!pa_asyncmsgq_dispatching(q
->outq
))
114 pa_asyncmsgq_flush(q
->outq
, TRUE
);
116 q
->mainloop
->io_free(q
->read_event
);
117 q
->mainloop
->io_free(q
->write_event
);
118 q
->read_event
= q
->write_event
= NULL
;
120 pa_asyncmsgq_unref(q
->inq
);
121 pa_asyncmsgq_unref(q
->outq
);
122 q
->inq
= q
->outq
= NULL
;
127 void pa_thread_mq_install(pa_thread_mq
*q
) {
130 pa_assert(!(PA_STATIC_TLS_GET(thread_mq
)));
131 PA_STATIC_TLS_SET(thread_mq
, q
);
134 pa_thread_mq
*pa_thread_mq_get(void) {
135 return PA_STATIC_TLS_GET(thread_mq
);