12b15cb3dSCy Schubert /*
22b15cb3dSCy Schubert * Copyright (c) 2008-2012 Niels Provos, Nick Mathewson
32b15cb3dSCy Schubert *
42b15cb3dSCy Schubert * Redistribution and use in source and binary forms, with or without
52b15cb3dSCy Schubert * modification, are permitted provided that the following conditions
62b15cb3dSCy Schubert * are met:
72b15cb3dSCy Schubert * 1. Redistributions of source code must retain the above copyright
82b15cb3dSCy Schubert * notice, this list of conditions and the following disclaimer.
92b15cb3dSCy Schubert * 2. Redistributions in binary form must reproduce the above copyright
102b15cb3dSCy Schubert * notice, this list of conditions and the following disclaimer in the
112b15cb3dSCy Schubert * documentation and/or other materials provided with the distribution.
122b15cb3dSCy Schubert * 3. The name of the author may not be used to endorse or promote products
132b15cb3dSCy Schubert * derived from this software without specific prior written permission.
142b15cb3dSCy Schubert *
152b15cb3dSCy Schubert * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
162b15cb3dSCy Schubert * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
172b15cb3dSCy Schubert * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
182b15cb3dSCy Schubert * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
192b15cb3dSCy Schubert * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
202b15cb3dSCy Schubert * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
212b15cb3dSCy Schubert * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
222b15cb3dSCy Schubert * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
232b15cb3dSCy Schubert * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
242b15cb3dSCy Schubert * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
252b15cb3dSCy Schubert */
262b15cb3dSCy Schubert #ifndef EVTHREAD_INTERNAL_H_INCLUDED_
272b15cb3dSCy Schubert #define EVTHREAD_INTERNAL_H_INCLUDED_
282b15cb3dSCy Schubert
292b15cb3dSCy Schubert #ifdef __cplusplus
302b15cb3dSCy Schubert extern "C" {
312b15cb3dSCy Schubert #endif
322b15cb3dSCy Schubert
332b15cb3dSCy Schubert #include "event2/event-config.h"
342b15cb3dSCy Schubert #include "evconfig-private.h"
352b15cb3dSCy Schubert
362b15cb3dSCy Schubert #include "event2/thread.h"
372b15cb3dSCy Schubert #include "util-internal.h"
382b15cb3dSCy Schubert
392b15cb3dSCy Schubert struct event_base;
402b15cb3dSCy Schubert
41*a466cc55SCy Schubert #if !defined(_WIN32) && !defined(__CYGWIN__)
422b15cb3dSCy Schubert /* On Windows, the way we currently make DLLs, it's not allowed for us to
432b15cb3dSCy Schubert * have shared global structures. Thus, we only do the direct-call-to-function
442b15cb3dSCy Schubert * code path if we know that the local shared library system supports it.
452b15cb3dSCy Schubert */
462b15cb3dSCy Schubert #define EVTHREAD_EXPOSE_STRUCTS
472b15cb3dSCy Schubert #endif
482b15cb3dSCy Schubert
492b15cb3dSCy Schubert #if ! defined(EVENT__DISABLE_THREAD_SUPPORT) && defined(EVTHREAD_EXPOSE_STRUCTS)
502b15cb3dSCy Schubert /* Global function pointers to lock-related functions. NULL if locking isn't
512b15cb3dSCy Schubert enabled. */
52*a466cc55SCy Schubert EVENT2_EXPORT_SYMBOL
532b15cb3dSCy Schubert extern struct evthread_lock_callbacks evthread_lock_fns_;
54*a466cc55SCy Schubert EVENT2_EXPORT_SYMBOL
552b15cb3dSCy Schubert extern struct evthread_condition_callbacks evthread_cond_fns_;
562b15cb3dSCy Schubert extern unsigned long (*evthread_id_fn_)(void);
57*a466cc55SCy Schubert EVENT2_EXPORT_SYMBOL
582b15cb3dSCy Schubert extern int evthread_lock_debugging_enabled_;
592b15cb3dSCy Schubert
602b15cb3dSCy Schubert /** Return the ID of the current thread, or 1 if threading isn't enabled. */
612b15cb3dSCy Schubert #define EVTHREAD_GET_ID() \
622b15cb3dSCy Schubert (evthread_id_fn_ ? evthread_id_fn_() : 1)
632b15cb3dSCy Schubert
642b15cb3dSCy Schubert /** Return true iff we're in the thread that is currently (or most recently)
652b15cb3dSCy Schubert * running a given event_base's loop. Requires lock. */
662b15cb3dSCy Schubert #define EVBASE_IN_THREAD(base) \
672b15cb3dSCy Schubert (evthread_id_fn_ == NULL || \
682b15cb3dSCy Schubert (base)->th_owner_id == evthread_id_fn_())
692b15cb3dSCy Schubert
702b15cb3dSCy Schubert /** Return true iff we need to notify the base's main thread about changes to
712b15cb3dSCy Schubert * its state, because it's currently running the main loop in another
722b15cb3dSCy Schubert * thread. Requires lock. */
732b15cb3dSCy Schubert #define EVBASE_NEED_NOTIFY(base) \
742b15cb3dSCy Schubert (evthread_id_fn_ != NULL && \
752b15cb3dSCy Schubert (base)->running_loop && \
762b15cb3dSCy Schubert (base)->th_owner_id != evthread_id_fn_())
772b15cb3dSCy Schubert
782b15cb3dSCy Schubert /** Allocate a new lock, and store it in lockvar, a void*. Sets lockvar to
792b15cb3dSCy Schubert NULL if locking is not enabled. */
802b15cb3dSCy Schubert #define EVTHREAD_ALLOC_LOCK(lockvar, locktype) \
812b15cb3dSCy Schubert ((lockvar) = evthread_lock_fns_.alloc ? \
822b15cb3dSCy Schubert evthread_lock_fns_.alloc(locktype) : NULL)
832b15cb3dSCy Schubert
842b15cb3dSCy Schubert /** Free a given lock, if it is present and locking is enabled. */
852b15cb3dSCy Schubert #define EVTHREAD_FREE_LOCK(lockvar, locktype) \
862b15cb3dSCy Schubert do { \
872b15cb3dSCy Schubert void *lock_tmp_ = (lockvar); \
882b15cb3dSCy Schubert if (lock_tmp_ && evthread_lock_fns_.free) \
892b15cb3dSCy Schubert evthread_lock_fns_.free(lock_tmp_, (locktype)); \
902b15cb3dSCy Schubert } while (0)
912b15cb3dSCy Schubert
922b15cb3dSCy Schubert /** Acquire a lock. */
932b15cb3dSCy Schubert #define EVLOCK_LOCK(lockvar,mode) \
942b15cb3dSCy Schubert do { \
952b15cb3dSCy Schubert if (lockvar) \
962b15cb3dSCy Schubert evthread_lock_fns_.lock(mode, lockvar); \
972b15cb3dSCy Schubert } while (0)
982b15cb3dSCy Schubert
992b15cb3dSCy Schubert /** Release a lock */
1002b15cb3dSCy Schubert #define EVLOCK_UNLOCK(lockvar,mode) \
1012b15cb3dSCy Schubert do { \
1022b15cb3dSCy Schubert if (lockvar) \
1032b15cb3dSCy Schubert evthread_lock_fns_.unlock(mode, lockvar); \
1042b15cb3dSCy Schubert } while (0)
1052b15cb3dSCy Schubert
1062b15cb3dSCy Schubert /** Helper: put lockvar1 and lockvar2 into pointerwise ascending order. */
1072b15cb3dSCy Schubert #define EVLOCK_SORTLOCKS_(lockvar1, lockvar2) \
1082b15cb3dSCy Schubert do { \
1092b15cb3dSCy Schubert if (lockvar1 && lockvar2 && lockvar1 > lockvar2) { \
1102b15cb3dSCy Schubert void *tmp = lockvar1; \
1112b15cb3dSCy Schubert lockvar1 = lockvar2; \
1122b15cb3dSCy Schubert lockvar2 = tmp; \
1132b15cb3dSCy Schubert } \
1142b15cb3dSCy Schubert } while (0)
1152b15cb3dSCy Schubert
1162b15cb3dSCy Schubert /** Lock an event_base, if it is set up for locking. Acquires the lock
1172b15cb3dSCy Schubert in the base structure whose field is named 'lockvar'. */
1182b15cb3dSCy Schubert #define EVBASE_ACQUIRE_LOCK(base, lockvar) do { \
1192b15cb3dSCy Schubert EVLOCK_LOCK((base)->lockvar, 0); \
1202b15cb3dSCy Schubert } while (0)
1212b15cb3dSCy Schubert
1222b15cb3dSCy Schubert /** Unlock an event_base, if it is set up for locking. */
1232b15cb3dSCy Schubert #define EVBASE_RELEASE_LOCK(base, lockvar) do { \
1242b15cb3dSCy Schubert EVLOCK_UNLOCK((base)->lockvar, 0); \
1252b15cb3dSCy Schubert } while (0)
1262b15cb3dSCy Schubert
1272b15cb3dSCy Schubert /** If lock debugging is enabled, and lock is non-null, assert that 'lock' is
1282b15cb3dSCy Schubert * locked and held by us. */
1292b15cb3dSCy Schubert #define EVLOCK_ASSERT_LOCKED(lock) \
1302b15cb3dSCy Schubert do { \
1312b15cb3dSCy Schubert if ((lock) && evthread_lock_debugging_enabled_) { \
1322b15cb3dSCy Schubert EVUTIL_ASSERT(evthread_is_debug_lock_held_(lock)); \
1332b15cb3dSCy Schubert } \
1342b15cb3dSCy Schubert } while (0)
1352b15cb3dSCy Schubert
1362b15cb3dSCy Schubert /** Try to grab the lock for 'lockvar' without blocking, and return 1 if we
1372b15cb3dSCy Schubert * manage to get it. */
1382b15cb3dSCy Schubert static inline int EVLOCK_TRY_LOCK_(void *lock);
1392b15cb3dSCy Schubert static inline int
EVLOCK_TRY_LOCK_(void * lock)1402b15cb3dSCy Schubert EVLOCK_TRY_LOCK_(void *lock)
1412b15cb3dSCy Schubert {
1422b15cb3dSCy Schubert if (lock && evthread_lock_fns_.lock) {
1432b15cb3dSCy Schubert int r = evthread_lock_fns_.lock(EVTHREAD_TRY, lock);
1442b15cb3dSCy Schubert return !r;
1452b15cb3dSCy Schubert } else {
1462b15cb3dSCy Schubert /* Locking is disabled either globally or for this thing;
1472b15cb3dSCy Schubert * of course we count as having the lock. */
1482b15cb3dSCy Schubert return 1;
1492b15cb3dSCy Schubert }
1502b15cb3dSCy Schubert }
1512b15cb3dSCy Schubert
1522b15cb3dSCy Schubert /** Allocate a new condition variable and store it in the void *, condvar */
1532b15cb3dSCy Schubert #define EVTHREAD_ALLOC_COND(condvar) \
1542b15cb3dSCy Schubert do { \
1552b15cb3dSCy Schubert (condvar) = evthread_cond_fns_.alloc_condition ? \
1562b15cb3dSCy Schubert evthread_cond_fns_.alloc_condition(0) : NULL; \
1572b15cb3dSCy Schubert } while (0)
1582b15cb3dSCy Schubert /** Deallocate and free a condition variable in condvar */
1592b15cb3dSCy Schubert #define EVTHREAD_FREE_COND(cond) \
1602b15cb3dSCy Schubert do { \
1612b15cb3dSCy Schubert if (cond) \
1622b15cb3dSCy Schubert evthread_cond_fns_.free_condition((cond)); \
1632b15cb3dSCy Schubert } while (0)
1642b15cb3dSCy Schubert /** Signal one thread waiting on cond */
1652b15cb3dSCy Schubert #define EVTHREAD_COND_SIGNAL(cond) \
1662b15cb3dSCy Schubert ( (cond) ? evthread_cond_fns_.signal_condition((cond), 0) : 0 )
1672b15cb3dSCy Schubert /** Signal all threads waiting on cond */
1682b15cb3dSCy Schubert #define EVTHREAD_COND_BROADCAST(cond) \
1692b15cb3dSCy Schubert ( (cond) ? evthread_cond_fns_.signal_condition((cond), 1) : 0 )
1702b15cb3dSCy Schubert /** Wait until the condition 'cond' is signalled. Must be called while
1712b15cb3dSCy Schubert * holding 'lock'. The lock will be released until the condition is
1722b15cb3dSCy Schubert * signalled, at which point it will be acquired again. Returns 0 for
1732b15cb3dSCy Schubert * success, -1 for failure. */
1742b15cb3dSCy Schubert #define EVTHREAD_COND_WAIT(cond, lock) \
1752b15cb3dSCy Schubert ( (cond) ? evthread_cond_fns_.wait_condition((cond), (lock), NULL) : 0 )
1762b15cb3dSCy Schubert /** As EVTHREAD_COND_WAIT, but gives up after 'tv' has elapsed. Returns 1
1772b15cb3dSCy Schubert * on timeout. */
1782b15cb3dSCy Schubert #define EVTHREAD_COND_WAIT_TIMED(cond, lock, tv) \
1792b15cb3dSCy Schubert ( (cond) ? evthread_cond_fns_.wait_condition((cond), (lock), (tv)) : 0 )
1802b15cb3dSCy Schubert
1812b15cb3dSCy Schubert /** True iff locking functions have been configured. */
1822b15cb3dSCy Schubert #define EVTHREAD_LOCKING_ENABLED() \
1832b15cb3dSCy Schubert (evthread_lock_fns_.lock != NULL)
1842b15cb3dSCy Schubert
1852b15cb3dSCy Schubert #elif ! defined(EVENT__DISABLE_THREAD_SUPPORT)
1862b15cb3dSCy Schubert
1872b15cb3dSCy Schubert unsigned long evthreadimpl_get_id_(void);
188*a466cc55SCy Schubert EVENT2_EXPORT_SYMBOL
1892b15cb3dSCy Schubert int evthreadimpl_is_lock_debugging_enabled_(void);
190*a466cc55SCy Schubert EVENT2_EXPORT_SYMBOL
1912b15cb3dSCy Schubert void *evthreadimpl_lock_alloc_(unsigned locktype);
192*a466cc55SCy Schubert EVENT2_EXPORT_SYMBOL
1932b15cb3dSCy Schubert void evthreadimpl_lock_free_(void *lock, unsigned locktype);
194*a466cc55SCy Schubert EVENT2_EXPORT_SYMBOL
1952b15cb3dSCy Schubert int evthreadimpl_lock_lock_(unsigned mode, void *lock);
196*a466cc55SCy Schubert EVENT2_EXPORT_SYMBOL
1972b15cb3dSCy Schubert int evthreadimpl_lock_unlock_(unsigned mode, void *lock);
198*a466cc55SCy Schubert EVENT2_EXPORT_SYMBOL
1992b15cb3dSCy Schubert void *evthreadimpl_cond_alloc_(unsigned condtype);
200*a466cc55SCy Schubert EVENT2_EXPORT_SYMBOL
2012b15cb3dSCy Schubert void evthreadimpl_cond_free_(void *cond);
202*a466cc55SCy Schubert EVENT2_EXPORT_SYMBOL
2032b15cb3dSCy Schubert int evthreadimpl_cond_signal_(void *cond, int broadcast);
204*a466cc55SCy Schubert EVENT2_EXPORT_SYMBOL
2052b15cb3dSCy Schubert int evthreadimpl_cond_wait_(void *cond, void *lock, const struct timeval *tv);
2062b15cb3dSCy Schubert int evthreadimpl_locking_enabled_(void);
2072b15cb3dSCy Schubert
2082b15cb3dSCy Schubert #define EVTHREAD_GET_ID() evthreadimpl_get_id_()
2092b15cb3dSCy Schubert #define EVBASE_IN_THREAD(base) \
2102b15cb3dSCy Schubert ((base)->th_owner_id == evthreadimpl_get_id_())
2112b15cb3dSCy Schubert #define EVBASE_NEED_NOTIFY(base) \
2122b15cb3dSCy Schubert ((base)->running_loop && \
2132b15cb3dSCy Schubert ((base)->th_owner_id != evthreadimpl_get_id_()))
2142b15cb3dSCy Schubert
2152b15cb3dSCy Schubert #define EVTHREAD_ALLOC_LOCK(lockvar, locktype) \
2162b15cb3dSCy Schubert ((lockvar) = evthreadimpl_lock_alloc_(locktype))
2172b15cb3dSCy Schubert
2182b15cb3dSCy Schubert #define EVTHREAD_FREE_LOCK(lockvar, locktype) \
2192b15cb3dSCy Schubert do { \
2202b15cb3dSCy Schubert void *lock_tmp_ = (lockvar); \
2212b15cb3dSCy Schubert if (lock_tmp_) \
2222b15cb3dSCy Schubert evthreadimpl_lock_free_(lock_tmp_, (locktype)); \
2232b15cb3dSCy Schubert } while (0)
2242b15cb3dSCy Schubert
2252b15cb3dSCy Schubert /** Acquire a lock. */
2262b15cb3dSCy Schubert #define EVLOCK_LOCK(lockvar,mode) \
2272b15cb3dSCy Schubert do { \
2282b15cb3dSCy Schubert if (lockvar) \
2292b15cb3dSCy Schubert evthreadimpl_lock_lock_(mode, lockvar); \
2302b15cb3dSCy Schubert } while (0)
2312b15cb3dSCy Schubert
2322b15cb3dSCy Schubert /** Release a lock */
2332b15cb3dSCy Schubert #define EVLOCK_UNLOCK(lockvar,mode) \
2342b15cb3dSCy Schubert do { \
2352b15cb3dSCy Schubert if (lockvar) \
2362b15cb3dSCy Schubert evthreadimpl_lock_unlock_(mode, lockvar); \
2372b15cb3dSCy Schubert } while (0)
2382b15cb3dSCy Schubert
2392b15cb3dSCy Schubert /** Lock an event_base, if it is set up for locking. Acquires the lock
2402b15cb3dSCy Schubert in the base structure whose field is named 'lockvar'. */
2412b15cb3dSCy Schubert #define EVBASE_ACQUIRE_LOCK(base, lockvar) do { \
2422b15cb3dSCy Schubert EVLOCK_LOCK((base)->lockvar, 0); \
2432b15cb3dSCy Schubert } while (0)
2442b15cb3dSCy Schubert
2452b15cb3dSCy Schubert /** Unlock an event_base, if it is set up for locking. */
2462b15cb3dSCy Schubert #define EVBASE_RELEASE_LOCK(base, lockvar) do { \
2472b15cb3dSCy Schubert EVLOCK_UNLOCK((base)->lockvar, 0); \
2482b15cb3dSCy Schubert } while (0)
2492b15cb3dSCy Schubert
2502b15cb3dSCy Schubert /** If lock debugging is enabled, and lock is non-null, assert that 'lock' is
2512b15cb3dSCy Schubert * locked and held by us. */
2522b15cb3dSCy Schubert #define EVLOCK_ASSERT_LOCKED(lock) \
2532b15cb3dSCy Schubert do { \
2542b15cb3dSCy Schubert if ((lock) && evthreadimpl_is_lock_debugging_enabled_()) { \
2552b15cb3dSCy Schubert EVUTIL_ASSERT(evthread_is_debug_lock_held_(lock)); \
2562b15cb3dSCy Schubert } \
2572b15cb3dSCy Schubert } while (0)
2582b15cb3dSCy Schubert
2592b15cb3dSCy Schubert /** Try to grab the lock for 'lockvar' without blocking, and return 1 if we
2602b15cb3dSCy Schubert * manage to get it. */
2612b15cb3dSCy Schubert static inline int EVLOCK_TRY_LOCK_(void *lock);
2622b15cb3dSCy Schubert static inline int
EVLOCK_TRY_LOCK_(void * lock)2632b15cb3dSCy Schubert EVLOCK_TRY_LOCK_(void *lock)
2642b15cb3dSCy Schubert {
2652b15cb3dSCy Schubert if (lock) {
2662b15cb3dSCy Schubert int r = evthreadimpl_lock_lock_(EVTHREAD_TRY, lock);
2672b15cb3dSCy Schubert return !r;
2682b15cb3dSCy Schubert } else {
2692b15cb3dSCy Schubert /* Locking is disabled either globally or for this thing;
2702b15cb3dSCy Schubert * of course we count as having the lock. */
2712b15cb3dSCy Schubert return 1;
2722b15cb3dSCy Schubert }
2732b15cb3dSCy Schubert }
2742b15cb3dSCy Schubert
2752b15cb3dSCy Schubert /** Allocate a new condition variable and store it in the void *, condvar */
2762b15cb3dSCy Schubert #define EVTHREAD_ALLOC_COND(condvar) \
2772b15cb3dSCy Schubert do { \
2782b15cb3dSCy Schubert (condvar) = evthreadimpl_cond_alloc_(0); \
2792b15cb3dSCy Schubert } while (0)
2802b15cb3dSCy Schubert /** Deallocate and free a condition variable in condvar */
2812b15cb3dSCy Schubert #define EVTHREAD_FREE_COND(cond) \
2822b15cb3dSCy Schubert do { \
2832b15cb3dSCy Schubert if (cond) \
2842b15cb3dSCy Schubert evthreadimpl_cond_free_((cond)); \
2852b15cb3dSCy Schubert } while (0)
2862b15cb3dSCy Schubert /** Signal one thread waiting on cond */
2872b15cb3dSCy Schubert #define EVTHREAD_COND_SIGNAL(cond) \
2882b15cb3dSCy Schubert ( (cond) ? evthreadimpl_cond_signal_((cond), 0) : 0 )
2892b15cb3dSCy Schubert /** Signal all threads waiting on cond */
2902b15cb3dSCy Schubert #define EVTHREAD_COND_BROADCAST(cond) \
2912b15cb3dSCy Schubert ( (cond) ? evthreadimpl_cond_signal_((cond), 1) : 0 )
2922b15cb3dSCy Schubert /** Wait until the condition 'cond' is signalled. Must be called while
2932b15cb3dSCy Schubert * holding 'lock'. The lock will be released until the condition is
2942b15cb3dSCy Schubert * signalled, at which point it will be acquired again. Returns 0 for
2952b15cb3dSCy Schubert * success, -1 for failure. */
2962b15cb3dSCy Schubert #define EVTHREAD_COND_WAIT(cond, lock) \
2972b15cb3dSCy Schubert ( (cond) ? evthreadimpl_cond_wait_((cond), (lock), NULL) : 0 )
2982b15cb3dSCy Schubert /** As EVTHREAD_COND_WAIT, but gives up after 'tv' has elapsed. Returns 1
2992b15cb3dSCy Schubert * on timeout. */
3002b15cb3dSCy Schubert #define EVTHREAD_COND_WAIT_TIMED(cond, lock, tv) \
3012b15cb3dSCy Schubert ( (cond) ? evthreadimpl_cond_wait_((cond), (lock), (tv)) : 0 )
3022b15cb3dSCy Schubert
3032b15cb3dSCy Schubert #define EVTHREAD_LOCKING_ENABLED() \
3042b15cb3dSCy Schubert (evthreadimpl_locking_enabled_())
3052b15cb3dSCy Schubert
3062b15cb3dSCy Schubert #else /* EVENT__DISABLE_THREAD_SUPPORT */
3072b15cb3dSCy Schubert
3082b15cb3dSCy Schubert #define EVTHREAD_GET_ID() 1
3092b15cb3dSCy Schubert #define EVTHREAD_ALLOC_LOCK(lockvar, locktype) EVUTIL_NIL_STMT_
3102b15cb3dSCy Schubert #define EVTHREAD_FREE_LOCK(lockvar, locktype) EVUTIL_NIL_STMT_
3112b15cb3dSCy Schubert
3122b15cb3dSCy Schubert #define EVLOCK_LOCK(lockvar, mode) EVUTIL_NIL_STMT_
3132b15cb3dSCy Schubert #define EVLOCK_UNLOCK(lockvar, mode) EVUTIL_NIL_STMT_
3142b15cb3dSCy Schubert #define EVLOCK_LOCK2(lock1,lock2,mode1,mode2) EVUTIL_NIL_STMT_
3152b15cb3dSCy Schubert #define EVLOCK_UNLOCK2(lock1,lock2,mode1,mode2) EVUTIL_NIL_STMT_
3162b15cb3dSCy Schubert
3172b15cb3dSCy Schubert #define EVBASE_IN_THREAD(base) 1
3182b15cb3dSCy Schubert #define EVBASE_NEED_NOTIFY(base) 0
3192b15cb3dSCy Schubert #define EVBASE_ACQUIRE_LOCK(base, lock) EVUTIL_NIL_STMT_
3202b15cb3dSCy Schubert #define EVBASE_RELEASE_LOCK(base, lock) EVUTIL_NIL_STMT_
3212b15cb3dSCy Schubert #define EVLOCK_ASSERT_LOCKED(lock) EVUTIL_NIL_STMT_
3222b15cb3dSCy Schubert
3232b15cb3dSCy Schubert #define EVLOCK_TRY_LOCK_(lock) 1
3242b15cb3dSCy Schubert
3252b15cb3dSCy Schubert #define EVTHREAD_ALLOC_COND(condvar) EVUTIL_NIL_STMT_
3262b15cb3dSCy Schubert #define EVTHREAD_FREE_COND(cond) EVUTIL_NIL_STMT_
3272b15cb3dSCy Schubert #define EVTHREAD_COND_SIGNAL(cond) EVUTIL_NIL_STMT_
3282b15cb3dSCy Schubert #define EVTHREAD_COND_BROADCAST(cond) EVUTIL_NIL_STMT_
3292b15cb3dSCy Schubert #define EVTHREAD_COND_WAIT(cond, lock) EVUTIL_NIL_STMT_
3302b15cb3dSCy Schubert #define EVTHREAD_COND_WAIT_TIMED(cond, lock, howlong) EVUTIL_NIL_STMT_
3312b15cb3dSCy Schubert
3322b15cb3dSCy Schubert #define EVTHREAD_LOCKING_ENABLED() 0
3332b15cb3dSCy Schubert
3342b15cb3dSCy Schubert #endif
3352b15cb3dSCy Schubert
3362b15cb3dSCy Schubert /* This code is shared between both lock impls */
3372b15cb3dSCy Schubert #if ! defined(EVENT__DISABLE_THREAD_SUPPORT)
3382b15cb3dSCy Schubert /** Helper: put lockvar1 and lockvar2 into pointerwise ascending order. */
3392b15cb3dSCy Schubert #define EVLOCK_SORTLOCKS_(lockvar1, lockvar2) \
3402b15cb3dSCy Schubert do { \
3412b15cb3dSCy Schubert if (lockvar1 && lockvar2 && lockvar1 > lockvar2) { \
3422b15cb3dSCy Schubert void *tmp = lockvar1; \
3432b15cb3dSCy Schubert lockvar1 = lockvar2; \
3442b15cb3dSCy Schubert lockvar2 = tmp; \
3452b15cb3dSCy Schubert } \
3462b15cb3dSCy Schubert } while (0)
3472b15cb3dSCy Schubert
3482b15cb3dSCy Schubert /** Acquire both lock1 and lock2. Always allocates locks in the same order,
3492b15cb3dSCy Schubert * so that two threads locking two locks with LOCK2 will not deadlock. */
3502b15cb3dSCy Schubert #define EVLOCK_LOCK2(lock1,lock2,mode1,mode2) \
3512b15cb3dSCy Schubert do { \
3522b15cb3dSCy Schubert void *lock1_tmplock_ = (lock1); \
3532b15cb3dSCy Schubert void *lock2_tmplock_ = (lock2); \
3542b15cb3dSCy Schubert EVLOCK_SORTLOCKS_(lock1_tmplock_,lock2_tmplock_); \
3552b15cb3dSCy Schubert EVLOCK_LOCK(lock1_tmplock_,mode1); \
3562b15cb3dSCy Schubert if (lock2_tmplock_ != lock1_tmplock_) \
3572b15cb3dSCy Schubert EVLOCK_LOCK(lock2_tmplock_,mode2); \
3582b15cb3dSCy Schubert } while (0)
3592b15cb3dSCy Schubert /** Release both lock1 and lock2. */
3602b15cb3dSCy Schubert #define EVLOCK_UNLOCK2(lock1,lock2,mode1,mode2) \
3612b15cb3dSCy Schubert do { \
3622b15cb3dSCy Schubert void *lock1_tmplock_ = (lock1); \
3632b15cb3dSCy Schubert void *lock2_tmplock_ = (lock2); \
3642b15cb3dSCy Schubert EVLOCK_SORTLOCKS_(lock1_tmplock_,lock2_tmplock_); \
3652b15cb3dSCy Schubert if (lock2_tmplock_ != lock1_tmplock_) \
3662b15cb3dSCy Schubert EVLOCK_UNLOCK(lock2_tmplock_,mode2); \
3672b15cb3dSCy Schubert EVLOCK_UNLOCK(lock1_tmplock_,mode1); \
3682b15cb3dSCy Schubert } while (0)
3692b15cb3dSCy Schubert
370*a466cc55SCy Schubert EVENT2_EXPORT_SYMBOL
3712b15cb3dSCy Schubert int evthread_is_debug_lock_held_(void *lock);
3722b15cb3dSCy Schubert void *evthread_debug_get_real_lock_(void *lock);
3732b15cb3dSCy Schubert
3742b15cb3dSCy Schubert void *evthread_setup_global_lock_(void *lock_, unsigned locktype,
3752b15cb3dSCy Schubert int enable_locks);
3762b15cb3dSCy Schubert
3772b15cb3dSCy Schubert #define EVTHREAD_SETUP_GLOBAL_LOCK(lockvar, locktype) \
3782b15cb3dSCy Schubert do { \
3792b15cb3dSCy Schubert lockvar = evthread_setup_global_lock_(lockvar, \
3802b15cb3dSCy Schubert (locktype), enable_locks); \
3812b15cb3dSCy Schubert if (!lockvar) { \
3822b15cb3dSCy Schubert event_warn("Couldn't allocate %s", #lockvar); \
3832b15cb3dSCy Schubert return -1; \
3842b15cb3dSCy Schubert } \
3852b15cb3dSCy Schubert } while (0);
3862b15cb3dSCy Schubert
3872b15cb3dSCy Schubert int event_global_setup_locks_(const int enable_locks);
3882b15cb3dSCy Schubert int evsig_global_setup_locks_(const int enable_locks);
3892b15cb3dSCy Schubert int evutil_global_setup_locks_(const int enable_locks);
3902b15cb3dSCy Schubert int evutil_secure_rng_global_setup_locks_(const int enable_locks);
3912b15cb3dSCy Schubert
392a25439b6SCy Schubert /** Return current evthread_lock_callbacks */
393*a466cc55SCy Schubert EVENT2_EXPORT_SYMBOL
394a25439b6SCy Schubert struct evthread_lock_callbacks *evthread_get_lock_callbacks(void);
395a25439b6SCy Schubert /** Return current evthread_condition_callbacks */
396a25439b6SCy Schubert struct evthread_condition_callbacks *evthread_get_condition_callbacks(void);
397a25439b6SCy Schubert /** Disable locking for internal usage (like global shutdown) */
398a25439b6SCy Schubert void evthreadimpl_disable_lock_debugging_(void);
399a25439b6SCy Schubert
4002b15cb3dSCy Schubert #endif
4012b15cb3dSCy Schubert
4022b15cb3dSCy Schubert #ifdef __cplusplus
4032b15cb3dSCy Schubert }
4042b15cb3dSCy Schubert #endif
4052b15cb3dSCy Schubert
4062b15cb3dSCy Schubert #endif /* EVTHREAD_INTERNAL_H_INCLUDED_ */
407