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