Path: blob/master/thirdparty/linuxbsd_headers/pulse/thread-mainloop.h
9905 views
#ifndef foothreadmainloophfoo1#define foothreadmainloophfoo23/***4This file is part of PulseAudio.56Copyright 2006 Lennart Poettering7Copyright 2006 Pierre Ossman <[email protected]> for Cendio AB89PulseAudio is free software; you can redistribute it and/or modify10it under the terms of the GNU Lesser General Public License as published11by the Free Software Foundation; either version 2.1 of the License,12or (at your option) any later version.1314PulseAudio is distributed in the hope that it will be useful, but15WITHOUT ANY WARRANTY; without even the implied warranty of16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU17General Public License for more details.1819You should have received a copy of the GNU Lesser General Public License20along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.21***/2223#include <pulse/mainloop-api.h>24#include <pulse/cdecl.h>25#include <pulse/version.h>2627PA_C_DECL_BEGIN2829/** \page threaded_mainloop Threaded Main Loop30*31* \section overv_sec Overview32*33* The threaded main loop implementation is a special version of the primary34* main loop implementation (see \ref mainloop). For the basic design, see35* its documentation.36*37* The added feature in the threaded main loop is that it spawns a new thread38* that runs the real main loop. This allows a synchronous application to use39* the asynchronous API without risking to stall the PulseAudio library.40*41* \section creat_sec Creation42*43* A pa_threaded_mainloop object is created using pa_threaded_mainloop_new().44* This will only allocate the required structures though, so to use it the45* thread must also be started. This is done through46* pa_threaded_mainloop_start(), after which you can start using the main loop.47*48* \section destr_sec Destruction49*50* When the PulseAudio connection has been terminated, the thread must be51* stopped and the resources freed. Stopping the thread is done using52* pa_threaded_mainloop_stop(), which must be called without the lock (see53* below) held. When that function returns, the thread is stopped and the54* pa_threaded_mainloop object can be freed using pa_threaded_mainloop_free().55*56* \section lock_sec Locking57*58* Since the PulseAudio API doesn't allow concurrent accesses to objects,59* a locking scheme must be used to guarantee safe usage. The threaded main60* loop API provides such a scheme through the functions61* pa_threaded_mainloop_lock() and pa_threaded_mainloop_unlock().62*63* The lock is recursive, so it's safe to use it multiple times from the same64* thread. Just make sure you call pa_threaded_mainloop_unlock() the same65* number of times you called pa_threaded_mainloop_lock().66*67* The lock needs to be held whenever you call any PulseAudio function that68* uses an object associated with this main loop. Make sure you do not hold69* on to the lock more than necessary though, as the threaded main loop stops70* while the lock is held.71*72* Example:73*74* \code75* void my_check_stream_func(pa_threaded_mainloop *m, pa_stream *s) {76* pa_stream_state_t state;77*78* pa_threaded_mainloop_lock(m);79*80* state = pa_stream_get_state(s);81*82* pa_threaded_mainloop_unlock(m);83*84* if (state == PA_STREAM_READY)85* printf("Stream is ready!");86* else87* printf("Stream is not ready!");88* }89* \endcode90*91* \section cb_sec Callbacks92*93* Callbacks in PulseAudio are asynchronous, so they require extra care when94* using them together with a threaded main loop.95*96* The easiest way to turn the callback based operations into synchronous97* ones, is to simply wait for the callback to be called and continue from98* there. This is the approach chosen in PulseAudio's threaded API.99*100* \subsection basic_subsec Basic callbacks101*102* For the basic case, where all that is required is to wait for the callback103* to be invoked, the code should look something like this:104*105* Example:106*107* \code108* static void my_drain_callback(pa_stream *s, int success, void *userdata) {109* pa_threaded_mainloop *m;110*111* m = userdata;112* assert(m);113*114* pa_threaded_mainloop_signal(m, 0);115* }116*117* void my_drain_stream_func(pa_threaded_mainloop *m, pa_stream *s) {118* pa_operation *o;119*120* pa_threaded_mainloop_lock(m);121*122* o = pa_stream_drain(s, my_drain_callback, m);123* assert(o);124*125* while (pa_operation_get_state(o) == PA_OPERATION_RUNNING)126* pa_threaded_mainloop_wait(m);127*128* pa_operation_unref(o);129*130* pa_threaded_mainloop_unlock(m);131* }132* \endcode133*134* The main function, my_drain_stream_func(), will wait for the callback to135* be called using pa_threaded_mainloop_wait().136*137* If your application is multi-threaded, then this waiting must be138* done inside a while loop. The reason for this is that multiple139* threads might be using pa_threaded_mainloop_wait() at the same140* time. Each thread must therefore verify that it was its callback141* that was invoked. Also the underlying OS synchronization primitives142* are usually not free of spurious wake-ups, so a143* pa_threaded_mainloop_wait() must be called within a loop even if144* you have only one thread waiting.145*146* The callback, my_drain_callback(), indicates to the main function that it147* has been called using pa_threaded_mainloop_signal().148*149* As you can see, pa_threaded_mainloop_wait() may only be called with150* the lock held. The same thing is true for pa_threaded_mainloop_signal(),151* but as the lock is held before the callback is invoked, you do not have to152* deal with that.153*154* The functions will not dead lock because the wait function will release155* the lock before waiting and then regrab it once it has been signalled.156* For those of you familiar with threads, the behaviour is that of a157* condition variable.158*159* \subsection data_subsec Data callbacks160*161* For many callbacks, simply knowing that they have been called is162* insufficient. The callback also receives some data that is desired. To163* access this data safely, we must extend our example a bit:164*165* \code166* static int * volatile drain_result = NULL;167*168* static void my_drain_callback(pa_stream*s, int success, void *userdata) {169* pa_threaded_mainloop *m;170*171* m = userdata;172* assert(m);173*174* drain_result = &success;175*176* pa_threaded_mainloop_signal(m, 1);177* }178*179* void my_drain_stream_func(pa_threaded_mainloop *m, pa_stream *s) {180* pa_operation *o;181*182* pa_threaded_mainloop_lock(m);183*184* o = pa_stream_drain(s, my_drain_callback, m);185* assert(o);186*187* while (drain_result == NULL)188* pa_threaded_mainloop_wait(m);189*190* pa_operation_unref(o);191*192* if (*drain_result)193* printf("Success!");194* else195* printf("Bitter defeat...");196*197* pa_threaded_mainloop_accept(m);198*199* pa_threaded_mainloop_unlock(m);200* }201* \endcode202*203* The example is a bit silly as it would probably have been easier to just204* copy the contents of success, but for larger data structures this can be205* wasteful.206*207* The difference here compared to the basic callback is the value 1 passed208* to pa_threaded_mainloop_signal() and the call to209* pa_threaded_mainloop_accept(). What will happen is that210* pa_threaded_mainloop_signal() will signal the main function and then wait.211* The main function is then free to use the data in the callback until212* pa_threaded_mainloop_accept() is called, which will allow the callback213* to continue.214*215* Note that pa_threaded_mainloop_accept() must be called some time between216* exiting the while loop and unlocking the main loop! Failure to do so will217* result in a race condition. I.e. it is not ok to release the lock and218* regrab it before calling pa_threaded_mainloop_accept().219*220* \subsection async_subsec Asynchronous callbacks221*222* PulseAudio also has callbacks that are completely asynchronous, meaning223* that they can be called at any time. The threaded main loop API provides224* the locking mechanism to handle concurrent accesses, but nothing else.225* Applications will have to handle communication from the callback to the226* main program through their own mechanisms.227*228* The callbacks that are completely asynchronous are:229*230* \li State callbacks for contexts, streams, etc.231* \li Subscription notifications232*/233234/** \file235*236* A thread based event loop implementation based on pa_mainloop. The237* event loop is run in a helper thread in the background. A few238* synchronization primitives are available to access the objects239* attached to the event loop safely.240*241* See also \subpage threaded_mainloop242*/243244/** An opaque threaded main loop object */245typedef struct pa_threaded_mainloop pa_threaded_mainloop;246247/** Allocate a new threaded main loop object. You have to call248* pa_threaded_mainloop_start() before the event loop thread starts249* running. */250pa_threaded_mainloop *pa_threaded_mainloop_new(void);251252/** Free a threaded main loop object. If the event loop thread is253* still running, terminate it with pa_threaded_mainloop_stop()254* first. */255void pa_threaded_mainloop_free(pa_threaded_mainloop* m);256257/** Start the event loop thread. */258int pa_threaded_mainloop_start(pa_threaded_mainloop *m);259260/** Terminate the event loop thread cleanly. Make sure to unlock the261* mainloop object before calling this function. */262void pa_threaded_mainloop_stop(pa_threaded_mainloop *m);263264/** Lock the event loop object, effectively blocking the event loop265* thread from processing events. You can use this to enforce266* exclusive access to all objects attached to the event loop. This267* lock is recursive. This function may not be called inside the event268* loop thread. Events that are dispatched from the event loop thread269* are executed with this lock held. */270void pa_threaded_mainloop_lock(pa_threaded_mainloop *m);271272/** Unlock the event loop object, inverse of pa_threaded_mainloop_lock(). */273void pa_threaded_mainloop_unlock(pa_threaded_mainloop *m);274275/** Wait for an event to be signalled by the event loop thread. You276* can use this to pass data from the event loop thread to the main277* thread in a synchronized fashion. This function may not be called278* inside the event loop thread. Prior to this call the event loop279* object needs to be locked using pa_threaded_mainloop_lock(). While280* waiting the lock will be released. Immediately before returning it281* will be acquired again. This function may spuriously wake up even282* without pa_threaded_mainloop_signal() being called. You need to283* make sure to handle that! */284void pa_threaded_mainloop_wait(pa_threaded_mainloop *m);285286/** Signal all threads waiting for a signalling event in287* pa_threaded_mainloop_wait(). If wait_for_accept is non-zero, do288* not return before the signal was accepted by a289* pa_threaded_mainloop_accept() call. While waiting for that condition290* the event loop object is unlocked. */291void pa_threaded_mainloop_signal(pa_threaded_mainloop *m, int wait_for_accept);292293/** Accept a signal from the event thread issued with294* pa_threaded_mainloop_signal(). This call should only be used in295* conjunction with pa_threaded_mainloop_signal() with a non-zero296* wait_for_accept value. */297void pa_threaded_mainloop_accept(pa_threaded_mainloop *m);298299/** Return the return value as specified with the main loop's300* pa_mainloop_quit() routine. */301int pa_threaded_mainloop_get_retval(pa_threaded_mainloop *m);302303/** Return the main loop abstraction layer vtable for this main loop.304* There is no need to free this object as it is owned by the loop305* and is destroyed when the loop is freed. */306pa_mainloop_api* pa_threaded_mainloop_get_api(pa_threaded_mainloop*m);307308/** Returns non-zero when called from within the event loop thread. \since 0.9.7 */309int pa_threaded_mainloop_in_thread(pa_threaded_mainloop *m);310311/** Sets the name of the thread. \since 5.0 */312void pa_threaded_mainloop_set_name(pa_threaded_mainloop *m, const char *name);313314PA_C_DECL_END315316#endif317318319