153a5a1b3Sopenharmony_ci#ifndef foomainloophfoo 253a5a1b3Sopenharmony_ci#define foomainloophfoo 353a5a1b3Sopenharmony_ci 453a5a1b3Sopenharmony_ci/*** 553a5a1b3Sopenharmony_ci This file is part of PulseAudio. 653a5a1b3Sopenharmony_ci 753a5a1b3Sopenharmony_ci Copyright 2004-2006 Lennart Poettering 853a5a1b3Sopenharmony_ci Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB 953a5a1b3Sopenharmony_ci 1053a5a1b3Sopenharmony_ci PulseAudio is free software; you can redistribute it and/or modify 1153a5a1b3Sopenharmony_ci it under the terms of the GNU Lesser General Public License as published 1253a5a1b3Sopenharmony_ci by the Free Software Foundation; either version 2.1 of the License, 1353a5a1b3Sopenharmony_ci or (at your option) any later version. 1453a5a1b3Sopenharmony_ci 1553a5a1b3Sopenharmony_ci PulseAudio is distributed in the hope that it will be useful, but 1653a5a1b3Sopenharmony_ci WITHOUT ANY WARRANTY; without even the implied warranty of 1753a5a1b3Sopenharmony_ci MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 1853a5a1b3Sopenharmony_ci General Public License for more details. 1953a5a1b3Sopenharmony_ci 2053a5a1b3Sopenharmony_ci You should have received a copy of the GNU Lesser General Public License 2153a5a1b3Sopenharmony_ci along with PulseAudio; if not, see <http://www.gnu.org/licenses/>. 2253a5a1b3Sopenharmony_ci***/ 2353a5a1b3Sopenharmony_ci 2453a5a1b3Sopenharmony_ci#include <pulse/mainloop-api.h> 2553a5a1b3Sopenharmony_ci#include <pulse/cdecl.h> 2653a5a1b3Sopenharmony_ci 2753a5a1b3Sopenharmony_ciPA_C_DECL_BEGIN 2853a5a1b3Sopenharmony_ci 2953a5a1b3Sopenharmony_cistruct pollfd; 3053a5a1b3Sopenharmony_ci 3153a5a1b3Sopenharmony_ci/** \page mainloop Main Loop 3253a5a1b3Sopenharmony_ci * 3353a5a1b3Sopenharmony_ci * \section overv_sec Overview 3453a5a1b3Sopenharmony_ci * 3553a5a1b3Sopenharmony_ci * The built-in main loop implementation is based on the poll() system call. 3653a5a1b3Sopenharmony_ci * It supports the functions defined in the main loop abstraction and very 3753a5a1b3Sopenharmony_ci * little else. 3853a5a1b3Sopenharmony_ci * 3953a5a1b3Sopenharmony_ci * The main loop is created using pa_mainloop_new() and destroyed using 4053a5a1b3Sopenharmony_ci * pa_mainloop_free(). To get access to the main loop abstraction, 4153a5a1b3Sopenharmony_ci * pa_mainloop_get_api() is used. 4253a5a1b3Sopenharmony_ci * 4353a5a1b3Sopenharmony_ci * \section iter_sec Iteration 4453a5a1b3Sopenharmony_ci * 4553a5a1b3Sopenharmony_ci * The main loop is designed around the concept of iterations. Each iteration 4653a5a1b3Sopenharmony_ci * consists of three steps that repeat during the application's entire 4753a5a1b3Sopenharmony_ci * lifetime: 4853a5a1b3Sopenharmony_ci * 4953a5a1b3Sopenharmony_ci * -# Prepare - Build a list of file descriptors 5053a5a1b3Sopenharmony_ci * that need to be monitored and calculate the next timeout. 5153a5a1b3Sopenharmony_ci * -# Poll - Execute the actual poll() system call. 5253a5a1b3Sopenharmony_ci * -# Dispatch - Dispatch any events that have fired. 5353a5a1b3Sopenharmony_ci * 5453a5a1b3Sopenharmony_ci * When using the main loop, the application can either execute each 5553a5a1b3Sopenharmony_ci * iteration, one at a time, using pa_mainloop_iterate(), or let the library 5653a5a1b3Sopenharmony_ci * iterate automatically using pa_mainloop_run(). 5753a5a1b3Sopenharmony_ci * 5853a5a1b3Sopenharmony_ci * \section thread_sec Threads 5953a5a1b3Sopenharmony_ci * 6053a5a1b3Sopenharmony_ci * The main loop functions are designed to be thread safe, but the objects 6153a5a1b3Sopenharmony_ci * are not. What this means is that multiple main loops can be used, but only 6253a5a1b3Sopenharmony_ci * one object per thread. 6353a5a1b3Sopenharmony_ci * 6453a5a1b3Sopenharmony_ci */ 6553a5a1b3Sopenharmony_ci 6653a5a1b3Sopenharmony_ci/** \file 6753a5a1b3Sopenharmony_ci * 6853a5a1b3Sopenharmony_ci * A minimal main loop implementation based on the C library's poll() 6953a5a1b3Sopenharmony_ci * function. Using the routines defined herein you may create a simple 7053a5a1b3Sopenharmony_ci * main loop supporting the generic main loop abstraction layer as 7153a5a1b3Sopenharmony_ci * defined in \ref mainloop-api.h. This implementation is thread safe 7253a5a1b3Sopenharmony_ci * as long as you access the main loop object from a single thread only. 7353a5a1b3Sopenharmony_ci * 7453a5a1b3Sopenharmony_ci * See also \subpage mainloop 7553a5a1b3Sopenharmony_ci */ 7653a5a1b3Sopenharmony_ci 7753a5a1b3Sopenharmony_ci/** An opaque main loop object */ 7853a5a1b3Sopenharmony_citypedef struct pa_mainloop pa_mainloop; 7953a5a1b3Sopenharmony_ci 8053a5a1b3Sopenharmony_ci/** Allocate a new main loop object. Free with pa_mainloop_free. */ 8153a5a1b3Sopenharmony_cipa_mainloop *pa_mainloop_new(void); 8253a5a1b3Sopenharmony_ci 8353a5a1b3Sopenharmony_ci/** Free a main loop object */ 8453a5a1b3Sopenharmony_civoid pa_mainloop_free(pa_mainloop* m); 8553a5a1b3Sopenharmony_ci 8653a5a1b3Sopenharmony_ci/** Prepare for a single iteration of the main loop. Returns a negative value 8753a5a1b3Sopenharmony_cion error or exit request. timeout specifies a maximum timeout for the subsequent 8853a5a1b3Sopenharmony_cipoll, or -1 for blocking behaviour. The timeout is specified in microseconds. */ 8953a5a1b3Sopenharmony_ciint pa_mainloop_prepare(pa_mainloop *m, int timeout); 9053a5a1b3Sopenharmony_ci 9153a5a1b3Sopenharmony_ci/** Execute the previously prepared poll. Returns a negative value on error.*/ 9253a5a1b3Sopenharmony_ciint pa_mainloop_poll(pa_mainloop *m); 9353a5a1b3Sopenharmony_ci 9453a5a1b3Sopenharmony_ci/** Dispatch timeout, io and deferred events from the previously executed poll. Returns 9553a5a1b3Sopenharmony_cia negative value on error. On success returns the number of source dispatched. */ 9653a5a1b3Sopenharmony_ciint pa_mainloop_dispatch(pa_mainloop *m); 9753a5a1b3Sopenharmony_ci 9853a5a1b3Sopenharmony_ci/** Return the return value as specified with the main loop's quit() routine. */ 9953a5a1b3Sopenharmony_ciint pa_mainloop_get_retval(const pa_mainloop *m); 10053a5a1b3Sopenharmony_ci 10153a5a1b3Sopenharmony_ci/** Run a single iteration of the main loop. This is a convenience function 10253a5a1b3Sopenharmony_cifor pa_mainloop_prepare(), pa_mainloop_poll() and pa_mainloop_dispatch(). 10353a5a1b3Sopenharmony_ciReturns a negative value on error or exit request. If block is nonzero, 10453a5a1b3Sopenharmony_ciblock for events if none are queued. Optionally return the return value as 10553a5a1b3Sopenharmony_cispecified with the main loop's quit() routine in the integer variable retval points 10653a5a1b3Sopenharmony_cito. On success returns the number of sources dispatched in this iteration. */ 10753a5a1b3Sopenharmony_ciint pa_mainloop_iterate(pa_mainloop *m, int block, int *retval); 10853a5a1b3Sopenharmony_ci 10953a5a1b3Sopenharmony_ci/** Run unlimited iterations of the main loop object until the main loop's 11053a5a1b3Sopenharmony_ciquit() routine is called. Returns a negative value on error. Optionally return 11153a5a1b3Sopenharmony_cithe return value as specified with the main loop's quit() routine in the integer 11253a5a1b3Sopenharmony_civariable retval points to. */ 11353a5a1b3Sopenharmony_ciint pa_mainloop_run(pa_mainloop *m, int *retval); 11453a5a1b3Sopenharmony_ci 11553a5a1b3Sopenharmony_ci/** Return the abstract main loop abstraction layer vtable for this 11653a5a1b3Sopenharmony_ci main loop. No need to free the API as it is owned by the loop 11753a5a1b3Sopenharmony_ci and is destroyed when the loop is freed. */ 11853a5a1b3Sopenharmony_cipa_mainloop_api* pa_mainloop_get_api(pa_mainloop*m); 11953a5a1b3Sopenharmony_ci 12053a5a1b3Sopenharmony_ci/** Shutdown the main loop with the specified return value */ 12153a5a1b3Sopenharmony_civoid pa_mainloop_quit(pa_mainloop *m, int retval); 12253a5a1b3Sopenharmony_ci 12353a5a1b3Sopenharmony_ci/** Interrupt a running poll (for threaded systems) */ 12453a5a1b3Sopenharmony_civoid pa_mainloop_wakeup(pa_mainloop *m); 12553a5a1b3Sopenharmony_ci 12653a5a1b3Sopenharmony_ci/** Generic prototype of a poll() like function */ 12753a5a1b3Sopenharmony_citypedef int (*pa_poll_func)(struct pollfd *ufds, unsigned long nfds, int timeout, void*userdata); 12853a5a1b3Sopenharmony_ci 12953a5a1b3Sopenharmony_ci/** Change the poll() implementation */ 13053a5a1b3Sopenharmony_civoid pa_mainloop_set_poll_func(pa_mainloop *m, pa_poll_func poll_func, void *userdata); 13153a5a1b3Sopenharmony_ci 13253a5a1b3Sopenharmony_ciPA_C_DECL_END 13353a5a1b3Sopenharmony_ci 13453a5a1b3Sopenharmony_ci#endif 135