thread.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * Copyright (c) 2008-2012 Niels Provos and Nick Mathewson
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * 3. The name of the author may not be used to endorse or promote products
  13. * derived from this software without specific prior written permission.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  16. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #ifndef _EVENT2_THREAD_H_
  27. #define _EVENT2_THREAD_H_
  28. /** @file event2/thread.h
  29. Functions for multi-threaded applications using Libevent.
  30. When using a multi-threaded application in which multiple threads
  31. add and delete events from a single event base, Libevent needs to
  32. lock its data structures.
  33. Like the memory-management function hooks, all of the threading functions
  34. _must_ be set up before an event_base is created if you want the base to
  35. use them.
  36. Most programs will either be using Windows threads or Posix threads. You
  37. can configure Libevent to use one of these event_use_windows_threads() or
  38. event_use_pthreads() respectively. If you're using another threading
  39. library, you'll need to configure threading functions manually using
  40. evthread_set_lock_callbacks() and evthread_set_condition_callbacks().
  41. */
  42. #ifdef __cplusplus
  43. extern "C" {
  44. #endif
  45. #include <event2/event-config.h>
  46. /**
  47. @name Flags passed to lock functions
  48. @{
  49. */
  50. /** A flag passed to a locking callback when the lock was allocated as a
  51. * read-write lock, and we want to acquire or release the lock for writing. */
  52. #define EVTHREAD_WRITE 0x04
  53. /** A flag passed to a locking callback when the lock was allocated as a
  54. * read-write lock, and we want to acquire or release the lock for reading. */
  55. #define EVTHREAD_READ 0x08
  56. /** A flag passed to a locking callback when we don't want to block waiting
  57. * for the lock; if we can't get the lock immediately, we will instead
  58. * return nonzero from the locking callback. */
  59. #define EVTHREAD_TRY 0x10
  60. /**@}*/
  61. #if !defined(_EVENT_DISABLE_THREAD_SUPPORT) || defined(_EVENT_IN_DOXYGEN)
  62. #define EVTHREAD_LOCK_API_VERSION 1
  63. /**
  64. @name Types of locks
  65. @{*/
  66. /** A recursive lock is one that can be acquired multiple times at once by the
  67. * same thread. No other process can allocate the lock until the thread that
  68. * has been holding it has unlocked it as many times as it locked it. */
  69. #define EVTHREAD_LOCKTYPE_RECURSIVE 1
  70. /* A read-write lock is one that allows multiple simultaneous readers, but
  71. * where any one writer excludes all other writers and readers. */
  72. #define EVTHREAD_LOCKTYPE_READWRITE 2
  73. /**@}*/
  74. /** This structure describes the interface a threading library uses for
  75. * locking. It's used to tell evthread_set_lock_callbacks() how to use
  76. * locking on this platform.
  77. */
  78. struct evthread_lock_callbacks {
  79. /** The current version of the locking API. Set this to
  80. * EVTHREAD_LOCK_API_VERSION */
  81. int lock_api_version;
  82. /** Which kinds of locks does this version of the locking API
  83. * support? A bitfield of EVTHREAD_LOCKTYPE_RECURSIVE and
  84. * EVTHREAD_LOCKTYPE_READWRITE.
  85. *
  86. * (Note that RECURSIVE locks are currently mandatory, and
  87. * READWRITE locks are not currently used.)
  88. **/
  89. unsigned supported_locktypes;
  90. /** Function to allocate and initialize new lock of type 'locktype'.
  91. * Returns NULL on failure. */
  92. void *(*alloc)(unsigned locktype);
  93. /** Funtion to release all storage held in 'lock', which was created
  94. * with type 'locktype'. */
  95. void (*free)(void *lock, unsigned locktype);
  96. /** Acquire an already-allocated lock at 'lock' with mode 'mode'.
  97. * Returns 0 on success, and nonzero on failure. */
  98. int (*lock)(unsigned mode, void *lock);
  99. /** Release a lock at 'lock' using mode 'mode'. Returns 0 on success,
  100. * and nonzero on failure. */
  101. int (*unlock)(unsigned mode, void *lock);
  102. };
  103. /** Sets a group of functions that Libevent should use for locking.
  104. * For full information on the required callback API, see the
  105. * documentation for the individual members of evthread_lock_callbacks.
  106. *
  107. * Note that if you're using Windows or the Pthreads threading library, you
  108. * probably shouldn't call this function; instead, use
  109. * evthread_use_windows_threads() or evthread_use_posix_threads() if you can.
  110. */
  111. int evthread_set_lock_callbacks(const struct evthread_lock_callbacks *);
  112. #define EVTHREAD_CONDITION_API_VERSION 1
  113. struct timeval;
  114. /** This structure describes the interface a threading library uses for
  115. * condition variables. It's used to tell evthread_set_condition_callbacks
  116. * how to use locking on this platform.
  117. */
  118. struct evthread_condition_callbacks {
  119. /** The current version of the conditions API. Set this to
  120. * EVTHREAD_CONDITION_API_VERSION */
  121. int condition_api_version;
  122. /** Function to allocate and initialize a new condition variable.
  123. * Returns the condition variable on success, and NULL on failure.
  124. * The 'condtype' argument will be 0 with this API version.
  125. */
  126. void *(*alloc_condition)(unsigned condtype);
  127. /** Function to free a condition variable. */
  128. void (*free_condition)(void *cond);
  129. /** Function to signal a condition variable. If 'broadcast' is 1, all
  130. * threads waiting on 'cond' should be woken; otherwise, only on one
  131. * thread is worken. Should return 0 on success, -1 on failure.
  132. * This function will only be called while holding the associated
  133. * lock for the condition.
  134. */
  135. int (*signal_condition)(void *cond, int broadcast);
  136. /** Function to wait for a condition variable. The lock 'lock'
  137. * will be held when this function is called; should be released
  138. * while waiting for the condition to be come signalled, and
  139. * should be held again when this function returns.
  140. * If timeout is provided, it is interval of seconds to wait for
  141. * the event to become signalled; if it is NULL, the function
  142. * should wait indefinitely.
  143. *
  144. * The function should return -1 on error; 0 if the condition
  145. * was signalled, or 1 on a timeout. */
  146. int (*wait_condition)(void *cond, void *lock,
  147. const struct timeval *timeout);
  148. };
  149. /** Sets a group of functions that Libevent should use for condition variables.
  150. * For full information on the required callback API, see the
  151. * documentation for the individual members of evthread_condition_callbacks.
  152. *
  153. * Note that if you're using Windows or the Pthreads threading library, you
  154. * probably shouldn't call this function; instead, use
  155. * evthread_use_windows_threads() or evthread_use_pthreads() if you can.
  156. */
  157. int evthread_set_condition_callbacks(
  158. const struct evthread_condition_callbacks *);
  159. /**
  160. Sets the function for determining the thread id.
  161. @param base the event base for which to set the id function
  162. @param id_fn the identify function Libevent should invoke to
  163. determine the identity of a thread.
  164. */
  165. void evthread_set_id_callback(
  166. unsigned long (*id_fn)(void));
  167. #if (defined(WIN32) && !defined(_EVENT_DISABLE_THREAD_SUPPORT)) || defined(_EVENT_IN_DOXYGEN)
  168. /** Sets up Libevent for use with Windows builtin locking and thread ID
  169. functions. Unavailable if Libevent is not built for Windows.
  170. @return 0 on success, -1 on failure. */
  171. int evthread_use_windows_threads(void);
  172. /**
  173. Defined if Libevent was built with support for evthread_use_windows_threads()
  174. */
  175. #define EVTHREAD_USE_WINDOWS_THREADS_IMPLEMENTED 1
  176. #endif
  177. #if defined(_EVENT_HAVE_PTHREADS) || defined(_EVENT_IN_DOXYGEN)
  178. /** Sets up Libevent for use with Pthreads locking and thread ID functions.
  179. Unavailable if Libevent is not build for use with pthreads. Requires
  180. libraries to link against Libevent_pthreads as well as Libevent.
  181. @return 0 on success, -1 on failure. */
  182. int evthread_use_pthreads(void);
  183. /** Defined if Libevent was built with support for evthread_use_pthreads() */
  184. #define EVTHREAD_USE_PTHREADS_IMPLEMENTED 1
  185. #endif
  186. /** Enable debugging wrappers around the current lock callbacks. If Libevent
  187. * makes one of several common locking errors, exit with an assertion failure.
  188. *
  189. * If you're going to call this function, you must do so before any locks are
  190. * allocated.
  191. **/
  192. void evthread_enable_lock_debuging(void);
  193. #endif /* _EVENT_DISABLE_THREAD_SUPPORT */
  194. struct event_base;
  195. /** Make sure it's safe to tell an event base to wake up from another thread
  196. or a signal handler.
  197. @return 0 on success, -1 on failure.
  198. */
  199. int evthread_make_base_notifiable(struct event_base *base);
  200. #ifdef __cplusplus
  201. }
  202. #endif
  203. #endif /* _EVENT2_THREAD_H_ */