thread.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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_INCLUDED_
  27. #define EVENT2_THREAD_H_INCLUDED_
  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. #include <event2/visibility.h>
  43. #ifdef __cplusplus
  44. extern "C" {
  45. #endif
  46. #include <event2/event-config.h>
  47. /**
  48. @name Flags passed to lock functions
  49. @{
  50. */
  51. /** A flag passed to a locking callback when the lock was allocated as a
  52. * read-write lock, and we want to acquire or release the lock for writing. */
  53. #define EVTHREAD_WRITE 0x04
  54. /** A flag passed to a locking callback when the lock was allocated as a
  55. * read-write lock, and we want to acquire or release the lock for reading. */
  56. #define EVTHREAD_READ 0x08
  57. /** A flag passed to a locking callback when we don't want to block waiting
  58. * for the lock; if we can't get the lock immediately, we will instead
  59. * return nonzero from the locking callback. */
  60. #define EVTHREAD_TRY 0x10
  61. /**@}*/
  62. #if !defined(EVENT__DISABLE_THREAD_SUPPORT) || defined(EVENT_IN_DOXYGEN_)
  63. #define EVTHREAD_LOCK_API_VERSION 1
  64. /**
  65. @name Types of locks
  66. @{*/
  67. /** A recursive lock is one that can be acquired multiple times at once by the
  68. * same thread. No other process can allocate the lock until the thread that
  69. * has been holding it has unlocked it as many times as it locked it. */
  70. #define EVTHREAD_LOCKTYPE_RECURSIVE 1
  71. /* A read-write lock is one that allows multiple simultaneous readers, but
  72. * where any one writer excludes all other writers and readers. */
  73. #define EVTHREAD_LOCKTYPE_READWRITE 2
  74. /**@}*/
  75. /** This structure describes the interface a threading library uses for
  76. * locking. It's used to tell evthread_set_lock_callbacks() how to use
  77. * locking on this platform.
  78. */
  79. struct evthread_lock_callbacks {
  80. /** The current version of the locking API. Set this to
  81. * EVTHREAD_LOCK_API_VERSION */
  82. int lock_api_version;
  83. /** Which kinds of locks does this version of the locking API
  84. * support? A bitfield of EVTHREAD_LOCKTYPE_RECURSIVE and
  85. * EVTHREAD_LOCKTYPE_READWRITE.
  86. *
  87. * (Note that RECURSIVE locks are currently mandatory, and
  88. * READWRITE locks are not currently used.)
  89. **/
  90. unsigned supported_locktypes;
  91. /** Function to allocate and initialize new lock of type 'locktype'.
  92. * Returns NULL on failure. */
  93. void *(*alloc)(unsigned locktype);
  94. /** Funtion to release all storage held in 'lock', which was created
  95. * with type 'locktype'. */
  96. void (*free)(void *lock, unsigned locktype);
  97. /** Acquire an already-allocated lock at 'lock' with mode 'mode'.
  98. * Returns 0 on success, and nonzero on failure. */
  99. int (*lock)(unsigned mode, void *lock);
  100. /** Release a lock at 'lock' using mode 'mode'. Returns 0 on success,
  101. * and nonzero on failure. */
  102. int (*unlock)(unsigned mode, void *lock);
  103. };
  104. /** Sets a group of functions that Libevent should use for locking.
  105. * For full information on the required callback API, see the
  106. * documentation for the individual members of evthread_lock_callbacks.
  107. *
  108. * Note that if you're using Windows or the Pthreads threading library, you
  109. * probably shouldn't call this function; instead, use
  110. * evthread_use_windows_threads() or evthread_use_posix_threads() if you can.
  111. */
  112. EVENT2_EXPORT_SYMBOL
  113. int evthread_set_lock_callbacks(const struct evthread_lock_callbacks *);
  114. #define EVTHREAD_CONDITION_API_VERSION 1
  115. struct timeval;
  116. /** This structure describes the interface a threading library uses for
  117. * condition variables. It's used to tell evthread_set_condition_callbacks
  118. * how to use locking on this platform.
  119. */
  120. struct evthread_condition_callbacks {
  121. /** The current version of the conditions API. Set this to
  122. * EVTHREAD_CONDITION_API_VERSION */
  123. int condition_api_version;
  124. /** Function to allocate and initialize a new condition variable.
  125. * Returns the condition variable on success, and NULL on failure.
  126. * The 'condtype' argument will be 0 with this API version.
  127. */
  128. void *(*alloc_condition)(unsigned condtype);
  129. /** Function to free a condition variable. */
  130. void (*free_condition)(void *cond);
  131. /** Function to signal a condition variable. If 'broadcast' is 1, all
  132. * threads waiting on 'cond' should be woken; otherwise, only on one
  133. * thread is worken. Should return 0 on success, -1 on failure.
  134. * This function will only be called while holding the associated
  135. * lock for the condition.
  136. */
  137. int (*signal_condition)(void *cond, int broadcast);
  138. /** Function to wait for a condition variable. The lock 'lock'
  139. * will be held when this function is called; should be released
  140. * while waiting for the condition to be come signalled, and
  141. * should be held again when this function returns.
  142. * If timeout is provided, it is interval of seconds to wait for
  143. * the event to become signalled; if it is NULL, the function
  144. * should wait indefinitely.
  145. *
  146. * The function should return -1 on error; 0 if the condition
  147. * was signalled, or 1 on a timeout. */
  148. int (*wait_condition)(void *cond, void *lock,
  149. const struct timeval *timeout);
  150. };
  151. /** Sets a group of functions that Libevent should use for condition variables.
  152. * For full information on the required callback API, see the
  153. * documentation for the individual members of evthread_condition_callbacks.
  154. *
  155. * Note that if you're using Windows or the Pthreads threading library, you
  156. * probably shouldn't call this function; instead, use
  157. * evthread_use_windows_threads() or evthread_use_pthreads() if you can.
  158. */
  159. EVENT2_EXPORT_SYMBOL
  160. int evthread_set_condition_callbacks(
  161. const struct evthread_condition_callbacks *);
  162. /**
  163. Sets the function for determining the thread id.
  164. @param base the event base for which to set the id function
  165. @param id_fn the identify function Libevent should invoke to
  166. determine the identity of a thread.
  167. */
  168. EVENT2_EXPORT_SYMBOL
  169. void evthread_set_id_callback(
  170. unsigned long (*id_fn)(void));
  171. #if (defined(_WIN32) && !defined(EVENT__DISABLE_THREAD_SUPPORT)) || defined(EVENT_IN_DOXYGEN_)
  172. /** Sets up Libevent for use with Windows builtin locking and thread ID
  173. functions. Unavailable if Libevent is not built for Windows.
  174. @return 0 on success, -1 on failure. */
  175. EVENT2_EXPORT_SYMBOL
  176. int evthread_use_windows_threads(void);
  177. /**
  178. Defined if Libevent was built with support for evthread_use_windows_threads()
  179. */
  180. #define EVTHREAD_USE_WINDOWS_THREADS_IMPLEMENTED 1
  181. #endif
  182. #if defined(EVENT__HAVE_PTHREADS) || defined(EVENT_IN_DOXYGEN_)
  183. /** Sets up Libevent for use with Pthreads locking and thread ID functions.
  184. Unavailable if Libevent is not build for use with pthreads. Requires
  185. libraries to link against Libevent_pthreads as well as Libevent.
  186. @return 0 on success, -1 on failure. */
  187. EVENT2_EXPORT_SYMBOL
  188. int evthread_use_pthreads(void);
  189. /** Defined if Libevent was built with support for evthread_use_pthreads() */
  190. #define EVTHREAD_USE_PTHREADS_IMPLEMENTED 1
  191. #endif
  192. /** Enable debugging wrappers around the current lock callbacks. If Libevent
  193. * makes one of several common locking errors, exit with an assertion failure.
  194. *
  195. * If you're going to call this function, you must do so before any locks are
  196. * allocated.
  197. **/
  198. EVENT2_EXPORT_SYMBOL
  199. void evthread_enable_lock_debugging(void);
  200. /* Old (misspelled) version: This is deprecated; use
  201. * evthread_enable_log_debugging instead. */
  202. EVENT2_EXPORT_SYMBOL
  203. void evthread_enable_lock_debuging(void);
  204. #endif /* EVENT__DISABLE_THREAD_SUPPORT */
  205. struct event_base;
  206. /** Make sure it's safe to tell an event base to wake up from another thread
  207. or a signal handler.
  208. You shouldn't need to call this by hand; configuring the base with thread
  209. support should be necessary and sufficient.
  210. @return 0 on success, -1 on failure.
  211. */
  212. EVENT2_EXPORT_SYMBOL
  213. int evthread_make_base_notifiable(struct event_base *base);
  214. #ifdef __cplusplus
  215. }
  216. #endif
  217. #endif /* EVENT2_THREAD_H_INCLUDED_ */