evthread-internal.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*
  2. * Copyright (c) 2008-2012 Niels Provos, 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 EVTHREAD_INTERNAL_H_INCLUDED_
  27. #define EVTHREAD_INTERNAL_H_INCLUDED_
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. #include "event2/event-config.h"
  32. #include "evconfig-private.h"
  33. #include "event2/thread.h"
  34. #include "util-internal.h"
  35. struct event_base;
  36. #if !defined(_WIN32) && !defined(__CYGWIN__)
  37. /* On Windows, the way we currently make DLLs, it's not allowed for us to
  38. * have shared global structures. Thus, we only do the direct-call-to-function
  39. * code path if we know that the local shared library system supports it.
  40. */
  41. #define EVTHREAD_EXPOSE_STRUCTS
  42. #endif
  43. #if ! defined(EVENT__DISABLE_THREAD_SUPPORT) && defined(EVTHREAD_EXPOSE_STRUCTS)
  44. /* Global function pointers to lock-related functions. NULL if locking isn't
  45. enabled. */
  46. EVENT2_EXPORT_SYMBOL
  47. extern struct evthread_lock_callbacks evthread_lock_fns_;
  48. EVENT2_EXPORT_SYMBOL
  49. extern struct evthread_condition_callbacks evthread_cond_fns_;
  50. extern unsigned long (*evthread_id_fn_)(void);
  51. EVENT2_EXPORT_SYMBOL
  52. extern int evthread_lock_debugging_enabled_;
  53. /** Return the ID of the current thread, or 1 if threading isn't enabled. */
  54. #define EVTHREAD_GET_ID() \
  55. (evthread_id_fn_ ? evthread_id_fn_() : 1)
  56. /** Return true iff we're in the thread that is currently (or most recently)
  57. * running a given event_base's loop. Requires lock. */
  58. #define EVBASE_IN_THREAD(base) \
  59. (evthread_id_fn_ == NULL || \
  60. (base)->th_owner_id == evthread_id_fn_())
  61. /** Return true iff we need to notify the base's main thread about changes to
  62. * its state, because it's currently running the main loop in another
  63. * thread. Requires lock. */
  64. #define EVBASE_NEED_NOTIFY(base) \
  65. (evthread_id_fn_ != NULL && \
  66. (base)->running_loop && \
  67. (base)->th_owner_id != evthread_id_fn_())
  68. /** Allocate a new lock, and store it in lockvar, a void*. Sets lockvar to
  69. NULL if locking is not enabled. */
  70. #define EVTHREAD_ALLOC_LOCK(lockvar, locktype) \
  71. ((lockvar) = evthread_lock_fns_.alloc ? \
  72. evthread_lock_fns_.alloc(locktype) : NULL)
  73. /** Free a given lock, if it is present and locking is enabled. */
  74. #define EVTHREAD_FREE_LOCK(lockvar, locktype) \
  75. do { \
  76. void *lock_tmp_ = (lockvar); \
  77. if (lock_tmp_ && evthread_lock_fns_.free) \
  78. evthread_lock_fns_.free(lock_tmp_, (locktype)); \
  79. } while (0)
  80. /** Acquire a lock. */
  81. #define EVLOCK_LOCK(lockvar,mode) \
  82. do { \
  83. if (lockvar) \
  84. evthread_lock_fns_.lock(mode, lockvar); \
  85. } while (0)
  86. /** Release a lock */
  87. #define EVLOCK_UNLOCK(lockvar,mode) \
  88. do { \
  89. if (lockvar) \
  90. evthread_lock_fns_.unlock(mode, lockvar); \
  91. } while (0)
  92. /** Helper: put lockvar1 and lockvar2 into pointerwise ascending order. */
  93. #define EVLOCK_SORTLOCKS_(lockvar1, lockvar2) \
  94. do { \
  95. if (lockvar1 && lockvar2 && lockvar1 > lockvar2) { \
  96. void *tmp = lockvar1; \
  97. lockvar1 = lockvar2; \
  98. lockvar2 = tmp; \
  99. } \
  100. } while (0)
  101. /** Lock an event_base, if it is set up for locking. Acquires the lock
  102. in the base structure whose field is named 'lockvar'. */
  103. #define EVBASE_ACQUIRE_LOCK(base, lockvar) do { \
  104. EVLOCK_LOCK((base)->lockvar, 0); \
  105. } while (0)
  106. /** Unlock an event_base, if it is set up for locking. */
  107. #define EVBASE_RELEASE_LOCK(base, lockvar) do { \
  108. EVLOCK_UNLOCK((base)->lockvar, 0); \
  109. } while (0)
  110. /** If lock debugging is enabled, and lock is non-null, assert that 'lock' is
  111. * locked and held by us. */
  112. #define EVLOCK_ASSERT_LOCKED(lock) \
  113. do { \
  114. if ((lock) && evthread_lock_debugging_enabled_) { \
  115. EVUTIL_ASSERT(evthread_is_debug_lock_held_(lock)); \
  116. } \
  117. } while (0)
  118. /** Try to grab the lock for 'lockvar' without blocking, and return 1 if we
  119. * manage to get it. */
  120. static inline int EVLOCK_TRY_LOCK_(void *lock);
  121. static inline int
  122. EVLOCK_TRY_LOCK_(void *lock)
  123. {
  124. if (lock && evthread_lock_fns_.lock) {
  125. int r = evthread_lock_fns_.lock(EVTHREAD_TRY, lock);
  126. return !r;
  127. } else {
  128. /* Locking is disabled either globally or for this thing;
  129. * of course we count as having the lock. */
  130. return 1;
  131. }
  132. }
  133. /** Allocate a new condition variable and store it in the void *, condvar */
  134. #define EVTHREAD_ALLOC_COND(condvar) \
  135. do { \
  136. (condvar) = evthread_cond_fns_.alloc_condition ? \
  137. evthread_cond_fns_.alloc_condition(0) : NULL; \
  138. } while (0)
  139. /** Deallocate and free a condition variable in condvar */
  140. #define EVTHREAD_FREE_COND(cond) \
  141. do { \
  142. if (cond) \
  143. evthread_cond_fns_.free_condition((cond)); \
  144. } while (0)
  145. /** Signal one thread waiting on cond */
  146. #define EVTHREAD_COND_SIGNAL(cond) \
  147. ( (cond) ? evthread_cond_fns_.signal_condition((cond), 0) : 0 )
  148. /** Signal all threads waiting on cond */
  149. #define EVTHREAD_COND_BROADCAST(cond) \
  150. ( (cond) ? evthread_cond_fns_.signal_condition((cond), 1) : 0 )
  151. /** Wait until the condition 'cond' is signalled. Must be called while
  152. * holding 'lock'. The lock will be released until the condition is
  153. * signalled, at which point it will be acquired again. Returns 0 for
  154. * success, -1 for failure. */
  155. #define EVTHREAD_COND_WAIT(cond, lock) \
  156. ( (cond) ? evthread_cond_fns_.wait_condition((cond), (lock), NULL) : 0 )
  157. /** As EVTHREAD_COND_WAIT, but gives up after 'tv' has elapsed. Returns 1
  158. * on timeout. */
  159. #define EVTHREAD_COND_WAIT_TIMED(cond, lock, tv) \
  160. ( (cond) ? evthread_cond_fns_.wait_condition((cond), (lock), (tv)) : 0 )
  161. /** True iff locking functions have been configured. */
  162. #define EVTHREAD_LOCKING_ENABLED() \
  163. (evthread_lock_fns_.lock != NULL)
  164. #elif ! defined(EVENT__DISABLE_THREAD_SUPPORT)
  165. unsigned long evthreadimpl_get_id_(void);
  166. EVENT2_EXPORT_SYMBOL
  167. int evthreadimpl_is_lock_debugging_enabled_(void);
  168. EVENT2_EXPORT_SYMBOL
  169. void *evthreadimpl_lock_alloc_(unsigned locktype);
  170. EVENT2_EXPORT_SYMBOL
  171. void evthreadimpl_lock_free_(void *lock, unsigned locktype);
  172. EVENT2_EXPORT_SYMBOL
  173. int evthreadimpl_lock_lock_(unsigned mode, void *lock);
  174. EVENT2_EXPORT_SYMBOL
  175. int evthreadimpl_lock_unlock_(unsigned mode, void *lock);
  176. EVENT2_EXPORT_SYMBOL
  177. void *evthreadimpl_cond_alloc_(unsigned condtype);
  178. EVENT2_EXPORT_SYMBOL
  179. void evthreadimpl_cond_free_(void *cond);
  180. EVENT2_EXPORT_SYMBOL
  181. int evthreadimpl_cond_signal_(void *cond, int broadcast);
  182. EVENT2_EXPORT_SYMBOL
  183. int evthreadimpl_cond_wait_(void *cond, void *lock, const struct timeval *tv);
  184. int evthreadimpl_locking_enabled_(void);
  185. #define EVTHREAD_GET_ID() evthreadimpl_get_id_()
  186. #define EVBASE_IN_THREAD(base) \
  187. ((base)->th_owner_id == evthreadimpl_get_id_())
  188. #define EVBASE_NEED_NOTIFY(base) \
  189. ((base)->running_loop && \
  190. ((base)->th_owner_id != evthreadimpl_get_id_()))
  191. #define EVTHREAD_ALLOC_LOCK(lockvar, locktype) \
  192. ((lockvar) = evthreadimpl_lock_alloc_(locktype))
  193. #define EVTHREAD_FREE_LOCK(lockvar, locktype) \
  194. do { \
  195. void *lock_tmp_ = (lockvar); \
  196. if (lock_tmp_) \
  197. evthreadimpl_lock_free_(lock_tmp_, (locktype)); \
  198. } while (0)
  199. /** Acquire a lock. */
  200. #define EVLOCK_LOCK(lockvar,mode) \
  201. do { \
  202. if (lockvar) \
  203. evthreadimpl_lock_lock_(mode, lockvar); \
  204. } while (0)
  205. /** Release a lock */
  206. #define EVLOCK_UNLOCK(lockvar,mode) \
  207. do { \
  208. if (lockvar) \
  209. evthreadimpl_lock_unlock_(mode, lockvar); \
  210. } while (0)
  211. /** Lock an event_base, if it is set up for locking. Acquires the lock
  212. in the base structure whose field is named 'lockvar'. */
  213. #define EVBASE_ACQUIRE_LOCK(base, lockvar) do { \
  214. EVLOCK_LOCK((base)->lockvar, 0); \
  215. } while (0)
  216. /** Unlock an event_base, if it is set up for locking. */
  217. #define EVBASE_RELEASE_LOCK(base, lockvar) do { \
  218. EVLOCK_UNLOCK((base)->lockvar, 0); \
  219. } while (0)
  220. /** If lock debugging is enabled, and lock is non-null, assert that 'lock' is
  221. * locked and held by us. */
  222. #define EVLOCK_ASSERT_LOCKED(lock) \
  223. do { \
  224. if ((lock) && evthreadimpl_is_lock_debugging_enabled_()) { \
  225. EVUTIL_ASSERT(evthread_is_debug_lock_held_(lock)); \
  226. } \
  227. } while (0)
  228. /** Try to grab the lock for 'lockvar' without blocking, and return 1 if we
  229. * manage to get it. */
  230. static inline int EVLOCK_TRY_LOCK_(void *lock);
  231. static inline int
  232. EVLOCK_TRY_LOCK_(void *lock)
  233. {
  234. if (lock) {
  235. int r = evthreadimpl_lock_lock_(EVTHREAD_TRY, lock);
  236. return !r;
  237. } else {
  238. /* Locking is disabled either globally or for this thing;
  239. * of course we count as having the lock. */
  240. return 1;
  241. }
  242. }
  243. /** Allocate a new condition variable and store it in the void *, condvar */
  244. #define EVTHREAD_ALLOC_COND(condvar) \
  245. do { \
  246. (condvar) = evthreadimpl_cond_alloc_(0); \
  247. } while (0)
  248. /** Deallocate and free a condition variable in condvar */
  249. #define EVTHREAD_FREE_COND(cond) \
  250. do { \
  251. if (cond) \
  252. evthreadimpl_cond_free_((cond)); \
  253. } while (0)
  254. /** Signal one thread waiting on cond */
  255. #define EVTHREAD_COND_SIGNAL(cond) \
  256. ( (cond) ? evthreadimpl_cond_signal_((cond), 0) : 0 )
  257. /** Signal all threads waiting on cond */
  258. #define EVTHREAD_COND_BROADCAST(cond) \
  259. ( (cond) ? evthreadimpl_cond_signal_((cond), 1) : 0 )
  260. /** Wait until the condition 'cond' is signalled. Must be called while
  261. * holding 'lock'. The lock will be released until the condition is
  262. * signalled, at which point it will be acquired again. Returns 0 for
  263. * success, -1 for failure. */
  264. #define EVTHREAD_COND_WAIT(cond, lock) \
  265. ( (cond) ? evthreadimpl_cond_wait_((cond), (lock), NULL) : 0 )
  266. /** As EVTHREAD_COND_WAIT, but gives up after 'tv' has elapsed. Returns 1
  267. * on timeout. */
  268. #define EVTHREAD_COND_WAIT_TIMED(cond, lock, tv) \
  269. ( (cond) ? evthreadimpl_cond_wait_((cond), (lock), (tv)) : 0 )
  270. #define EVTHREAD_LOCKING_ENABLED() \
  271. (evthreadimpl_locking_enabled_())
  272. #else /* EVENT__DISABLE_THREAD_SUPPORT */
  273. #define EVTHREAD_GET_ID() 1
  274. #define EVTHREAD_ALLOC_LOCK(lockvar, locktype) EVUTIL_NIL_STMT_
  275. #define EVTHREAD_FREE_LOCK(lockvar, locktype) EVUTIL_NIL_STMT_
  276. #define EVLOCK_LOCK(lockvar, mode) EVUTIL_NIL_STMT_
  277. #define EVLOCK_UNLOCK(lockvar, mode) EVUTIL_NIL_STMT_
  278. #define EVLOCK_LOCK2(lock1,lock2,mode1,mode2) EVUTIL_NIL_STMT_
  279. #define EVLOCK_UNLOCK2(lock1,lock2,mode1,mode2) EVUTIL_NIL_STMT_
  280. #define EVBASE_IN_THREAD(base) 1
  281. #define EVBASE_NEED_NOTIFY(base) 0
  282. #define EVBASE_ACQUIRE_LOCK(base, lock) EVUTIL_NIL_STMT_
  283. #define EVBASE_RELEASE_LOCK(base, lock) EVUTIL_NIL_STMT_
  284. #define EVLOCK_ASSERT_LOCKED(lock) EVUTIL_NIL_STMT_
  285. #define EVLOCK_TRY_LOCK_(lock) 1
  286. #define EVTHREAD_ALLOC_COND(condvar) EVUTIL_NIL_STMT_
  287. #define EVTHREAD_FREE_COND(cond) EVUTIL_NIL_STMT_
  288. #define EVTHREAD_COND_SIGNAL(cond) EVUTIL_NIL_STMT_
  289. #define EVTHREAD_COND_BROADCAST(cond) EVUTIL_NIL_STMT_
  290. #define EVTHREAD_COND_WAIT(cond, lock) EVUTIL_NIL_STMT_
  291. #define EVTHREAD_COND_WAIT_TIMED(cond, lock, howlong) EVUTIL_NIL_STMT_
  292. #define EVTHREAD_LOCKING_ENABLED() 0
  293. #endif
  294. /* This code is shared between both lock impls */
  295. #if ! defined(EVENT__DISABLE_THREAD_SUPPORT)
  296. /** Helper: put lockvar1 and lockvar2 into pointerwise ascending order. */
  297. #define EVLOCK_SORTLOCKS_(lockvar1, lockvar2) \
  298. do { \
  299. if (lockvar1 && lockvar2 && lockvar1 > lockvar2) { \
  300. void *tmp = lockvar1; \
  301. lockvar1 = lockvar2; \
  302. lockvar2 = tmp; \
  303. } \
  304. } while (0)
  305. /** Acquire both lock1 and lock2. Always allocates locks in the same order,
  306. * so that two threads locking two locks with LOCK2 will not deadlock. */
  307. #define EVLOCK_LOCK2(lock1,lock2,mode1,mode2) \
  308. do { \
  309. void *lock1_tmplock_ = (lock1); \
  310. void *lock2_tmplock_ = (lock2); \
  311. EVLOCK_SORTLOCKS_(lock1_tmplock_,lock2_tmplock_); \
  312. EVLOCK_LOCK(lock1_tmplock_,mode1); \
  313. if (lock2_tmplock_ != lock1_tmplock_) \
  314. EVLOCK_LOCK(lock2_tmplock_,mode2); \
  315. } while (0)
  316. /** Release both lock1 and lock2. */
  317. #define EVLOCK_UNLOCK2(lock1,lock2,mode1,mode2) \
  318. do { \
  319. void *lock1_tmplock_ = (lock1); \
  320. void *lock2_tmplock_ = (lock2); \
  321. EVLOCK_SORTLOCKS_(lock1_tmplock_,lock2_tmplock_); \
  322. if (lock2_tmplock_ != lock1_tmplock_) \
  323. EVLOCK_UNLOCK(lock2_tmplock_,mode2); \
  324. EVLOCK_UNLOCK(lock1_tmplock_,mode1); \
  325. } while (0)
  326. EVENT2_EXPORT_SYMBOL
  327. int evthread_is_debug_lock_held_(void *lock);
  328. void *evthread_debug_get_real_lock_(void *lock);
  329. void *evthread_setup_global_lock_(void *lock_, unsigned locktype,
  330. int enable_locks);
  331. #define EVTHREAD_SETUP_GLOBAL_LOCK(lockvar, locktype) \
  332. do { \
  333. lockvar = evthread_setup_global_lock_(lockvar, \
  334. (locktype), enable_locks); \
  335. if (!lockvar) { \
  336. event_warn("Couldn't allocate %s", #lockvar); \
  337. return -1; \
  338. } \
  339. } while (0);
  340. int event_global_setup_locks_(const int enable_locks);
  341. int evsig_global_setup_locks_(const int enable_locks);
  342. int evutil_global_setup_locks_(const int enable_locks);
  343. int evutil_secure_rng_global_setup_locks_(const int enable_locks);
  344. /** Return current evthread_lock_callbacks */
  345. EVENT2_EXPORT_SYMBOL
  346. struct evthread_lock_callbacks *evthread_get_lock_callbacks(void);
  347. /** Return current evthread_condition_callbacks */
  348. struct evthread_condition_callbacks *evthread_get_condition_callbacks(void);
  349. /** Disable locking for internal usage (like global shutdown) */
  350. void evthreadimpl_disable_lock_debugging_(void);
  351. #endif
  352. #ifdef __cplusplus
  353. }
  354. #endif
  355. #endif /* EVTHREAD_INTERNAL_H_INCLUDED_ */