mainloop.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #ifndef foomainloophfoo
  2. #define foomainloophfoo
  3. /***
  4. This file is part of PulseAudio.
  5. Copyright 2004-2006 Lennart Poettering
  6. Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
  7. PulseAudio is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU Lesser General Public License as published
  9. by the Free Software Foundation; either version 2.1 of the License,
  10. 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. General Public License for more details.
  15. You should have received a copy of the GNU Lesser General Public License
  16. along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
  17. ***/
  18. #include <pulse/mainloop-api.h>
  19. #include <pulse/cdecl.h>
  20. PA_C_DECL_BEGIN
  21. struct pollfd;
  22. /** \page mainloop Main Loop
  23. *
  24. * \section overv_sec Overview
  25. *
  26. * The built-in main loop implementation is based on the poll() system call.
  27. * It supports the functions defined in the main loop abstraction and very
  28. * little else.
  29. *
  30. * The main loop is created using pa_mainloop_new() and destroyed using
  31. * pa_mainloop_free(). To get access to the main loop abstraction,
  32. * pa_mainloop_get_api() is used.
  33. *
  34. * \section iter_sec Iteration
  35. *
  36. * The main loop is designed around the concept of iterations. Each iteration
  37. * consists of three steps that repeat during the application's entire
  38. * lifetime:
  39. *
  40. * -# Prepare - Build a list of file descriptors
  41. * that need to be monitored and calculate the next timeout.
  42. * -# Poll - Execute the actual poll() system call.
  43. * -# Dispatch - Dispatch any events that have fired.
  44. *
  45. * When using the main loop, the application can either execute each
  46. * iteration, one at a time, using pa_mainloop_iterate(), or let the library
  47. * iterate automatically using pa_mainloop_run().
  48. *
  49. * \section thread_sec Threads
  50. *
  51. * The main loop functions are designed to be thread safe, but the objects
  52. * are not. What this means is that multiple main loops can be used, but only
  53. * one object per thread.
  54. *
  55. */
  56. /** \file
  57. *
  58. * A minimal main loop implementation based on the C library's poll()
  59. * function. Using the routines defined herein you may create a simple
  60. * main loop supporting the generic main loop abstraction layer as
  61. * defined in \ref mainloop-api.h. This implementation is thread safe
  62. * as long as you access the main loop object from a single thread only.
  63. *
  64. * See also \subpage mainloop
  65. */
  66. /** An opaque main loop object */
  67. typedef struct pa_mainloop pa_mainloop;
  68. /** Allocate a new main loop object */
  69. pa_mainloop *pa_mainloop_new(void);
  70. /** Free a main loop object */
  71. void pa_mainloop_free(pa_mainloop* m);
  72. /** Prepare for a single iteration of the main loop. Returns a negative value
  73. on error or exit request. timeout specifies a maximum timeout for the subsequent
  74. poll, or -1 for blocking behaviour. .*/
  75. int pa_mainloop_prepare(pa_mainloop *m, int timeout);
  76. /** Execute the previously prepared poll. Returns a negative value on error.*/
  77. int pa_mainloop_poll(pa_mainloop *m);
  78. /** Dispatch timeout, io and deferred events from the previously executed poll. Returns
  79. a negative value on error. On success returns the number of source dispatched. */
  80. int pa_mainloop_dispatch(pa_mainloop *m);
  81. /** Return the return value as specified with the main loop's quit() routine. */
  82. int pa_mainloop_get_retval(pa_mainloop *m);
  83. /** Run a single iteration of the main loop. This is a convenience function
  84. for pa_mainloop_prepare(), pa_mainloop_poll() and pa_mainloop_dispatch().
  85. Returns a negative value on error or exit request. If block is nonzero,
  86. block for events if none are queued. Optionally return the return value as
  87. specified with the main loop's quit() routine in the integer variable retval points
  88. to. On success returns the number of sources dispatched in this iteration. */
  89. int pa_mainloop_iterate(pa_mainloop *m, int block, int *retval);
  90. /** Run unlimited iterations of the main loop object until the main loop's quit() routine is called. */
  91. int pa_mainloop_run(pa_mainloop *m, int *retval);
  92. /** Return the abstract main loop abstraction layer vtable for this
  93. main loop. No need to free the API as it is owned by the loop
  94. and is destroyed when the loop is freed. */
  95. pa_mainloop_api* pa_mainloop_get_api(pa_mainloop*m);
  96. /** Shutdown the main loop with the specified return value */
  97. void pa_mainloop_quit(pa_mainloop *m, int retval);
  98. /** Interrupt a running poll (for threaded systems) */
  99. void pa_mainloop_wakeup(pa_mainloop *m);
  100. /** Generic prototype of a poll() like function */
  101. typedef int (*pa_poll_func)(struct pollfd *ufds, unsigned long nfds, int timeout, void*userdata);
  102. /** Change the poll() implementation */
  103. void pa_mainloop_set_poll_func(pa_mainloop *m, pa_poll_func poll_func, void *userdata);
  104. PA_C_DECL_END
  105. #endif