xref: /netbsd-src/external/bsd/libevent/dist/evthread-internal.h (revision 81d2345cc4f87f54efa9edec2199119599391d4b)
1*81d2345cSrillig /*	$NetBSD: evthread-internal.h,v 1.6 2021/04/10 19:18:45 rillig Exp $	*/
27e68cdd7Schristos 
36ecf6635Schristos /*
46ecf6635Schristos  * Copyright (c) 2008-2012 Niels Provos, Nick Mathewson
56ecf6635Schristos  *
66ecf6635Schristos  * Redistribution and use in source and binary forms, with or without
76ecf6635Schristos  * modification, are permitted provided that the following conditions
86ecf6635Schristos  * are met:
96ecf6635Schristos  * 1. Redistributions of source code must retain the above copyright
106ecf6635Schristos  *    notice, this list of conditions and the following disclaimer.
116ecf6635Schristos  * 2. Redistributions in binary form must reproduce the above copyright
126ecf6635Schristos  *    notice, this list of conditions and the following disclaimer in the
136ecf6635Schristos  *    documentation and/or other materials provided with the distribution.
146ecf6635Schristos  * 3. The name of the author may not be used to endorse or promote products
156ecf6635Schristos  *    derived from this software without specific prior written permission.
166ecf6635Schristos  *
176ecf6635Schristos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
186ecf6635Schristos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
196ecf6635Schristos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
206ecf6635Schristos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
216ecf6635Schristos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
226ecf6635Schristos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
236ecf6635Schristos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
246ecf6635Schristos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
256ecf6635Schristos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
266ecf6635Schristos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
276ecf6635Schristos  */
280d738af4Schristos #ifndef EVTHREAD_INTERNAL_H_INCLUDED_
290d738af4Schristos #define EVTHREAD_INTERNAL_H_INCLUDED_
306ecf6635Schristos 
316ecf6635Schristos #ifdef __cplusplus
326ecf6635Schristos extern "C" {
336ecf6635Schristos #endif
346ecf6635Schristos 
356ecf6635Schristos #include "event2/event-config.h"
360d738af4Schristos #include "evconfig-private.h"
370d738af4Schristos 
380d738af4Schristos #include "event2/thread.h"
396ecf6635Schristos #include "util-internal.h"
406ecf6635Schristos 
416ecf6635Schristos struct event_base;
426ecf6635Schristos 
437e68cdd7Schristos #if !defined(_WIN32) && !defined(__CYGWIN__)
446ecf6635Schristos /* On Windows, the way we currently make DLLs, it's not allowed for us to
456ecf6635Schristos  * have shared global structures.  Thus, we only do the direct-call-to-function
466ecf6635Schristos  * code path if we know that the local shared library system supports it.
476ecf6635Schristos  */
486ecf6635Schristos #define EVTHREAD_EXPOSE_STRUCTS
496ecf6635Schristos #endif
506ecf6635Schristos 
510d738af4Schristos #if ! defined(EVENT__DISABLE_THREAD_SUPPORT) && defined(EVTHREAD_EXPOSE_STRUCTS)
526ecf6635Schristos /* Global function pointers to lock-related functions. NULL if locking isn't
536ecf6635Schristos    enabled. */
547e68cdd7Schristos EVENT2_EXPORT_SYMBOL
550d738af4Schristos extern struct evthread_lock_callbacks evthread_lock_fns_;
567e68cdd7Schristos EVENT2_EXPORT_SYMBOL
570d738af4Schristos extern struct evthread_condition_callbacks evthread_cond_fns_;
580d738af4Schristos extern unsigned long (*evthread_id_fn_)(void);
597e68cdd7Schristos EVENT2_EXPORT_SYMBOL
600d738af4Schristos extern int evthread_lock_debugging_enabled_;
616ecf6635Schristos 
626ecf6635Schristos /** Return the ID of the current thread, or 1 if threading isn't enabled. */
636ecf6635Schristos #define EVTHREAD_GET_ID() \
640d738af4Schristos 	(evthread_id_fn_ ? evthread_id_fn_() : 1)
656ecf6635Schristos 
666ecf6635Schristos /** Return true iff we're in the thread that is currently (or most recently)
676ecf6635Schristos  * running a given event_base's loop. Requires lock. */
686ecf6635Schristos #define EVBASE_IN_THREAD(base)				 \
690d738af4Schristos 	(evthread_id_fn_ == NULL ||			 \
700d738af4Schristos 	(base)->th_owner_id == evthread_id_fn_())
716ecf6635Schristos 
726ecf6635Schristos /** Return true iff we need to notify the base's main thread about changes to
736ecf6635Schristos  * its state, because it's currently running the main loop in another
746ecf6635Schristos  * thread. Requires lock. */
756ecf6635Schristos #define EVBASE_NEED_NOTIFY(base)			 \
760d738af4Schristos 	(evthread_id_fn_ != NULL &&			 \
776ecf6635Schristos 	    (base)->running_loop &&			 \
780d738af4Schristos 	    (base)->th_owner_id != evthread_id_fn_())
796ecf6635Schristos 
806ecf6635Schristos /** Allocate a new lock, and store it in lockvar, a void*.  Sets lockvar to
816ecf6635Schristos     NULL if locking is not enabled. */
826ecf6635Schristos #define EVTHREAD_ALLOC_LOCK(lockvar, locktype)		\
830d738af4Schristos 	((lockvar) = evthread_lock_fns_.alloc ?		\
840d738af4Schristos 	    evthread_lock_fns_.alloc(locktype) : NULL)
856ecf6635Schristos 
866ecf6635Schristos /** Free a given lock, if it is present and locking is enabled. */
876ecf6635Schristos #define EVTHREAD_FREE_LOCK(lockvar, locktype)				\
886ecf6635Schristos 	do {								\
890d738af4Schristos 		void *lock_tmp_ = (lockvar);				\
900d738af4Schristos 		if (lock_tmp_ && evthread_lock_fns_.free)		\
910d738af4Schristos 			evthread_lock_fns_.free(lock_tmp_, (locktype)); \
92a848e48cSrillig 	} while (0)
936ecf6635Schristos 
946ecf6635Schristos /** Acquire a lock. */
956ecf6635Schristos #define EVLOCK_LOCK(lockvar,mode)					\
966ecf6635Schristos 	do {								\
976ecf6635Schristos 		if (lockvar)						\
980d738af4Schristos 			evthread_lock_fns_.lock(mode, lockvar);		\
99a848e48cSrillig 	} while (0)
1006ecf6635Schristos 
1016ecf6635Schristos /** Release a lock */
1026ecf6635Schristos #define EVLOCK_UNLOCK(lockvar,mode)					\
1036ecf6635Schristos 	do {								\
1046ecf6635Schristos 		if (lockvar)						\
1050d738af4Schristos 			evthread_lock_fns_.unlock(mode, lockvar);	\
106a848e48cSrillig 	} while (0)
1076ecf6635Schristos 
1086ecf6635Schristos /** Helper: put lockvar1 and lockvar2 into pointerwise ascending order. */
1090d738af4Schristos #define EVLOCK_SORTLOCKS_(lockvar1, lockvar2)				\
1106ecf6635Schristos 	do {								\
1116ecf6635Schristos 		if (lockvar1 && lockvar2 && lockvar1 > lockvar2) {	\
1126ecf6635Schristos 			void *tmp = lockvar1;				\
1136ecf6635Schristos 			lockvar1 = lockvar2;				\
1146ecf6635Schristos 			lockvar2 = tmp;					\
1156ecf6635Schristos 		}							\
116a848e48cSrillig 	} while (0)
1176ecf6635Schristos 
1186ecf6635Schristos /** Lock an event_base, if it is set up for locking.  Acquires the lock
1196ecf6635Schristos     in the base structure whose field is named 'lockvar'. */
1206ecf6635Schristos #define EVBASE_ACQUIRE_LOCK(base, lockvar) do {				\
1216ecf6635Schristos 		EVLOCK_LOCK((base)->lockvar, 0);			\
122a848e48cSrillig 	} while (0)
1236ecf6635Schristos 
1246ecf6635Schristos /** Unlock an event_base, if it is set up for locking. */
1256ecf6635Schristos #define EVBASE_RELEASE_LOCK(base, lockvar) do {				\
1266ecf6635Schristos 		EVLOCK_UNLOCK((base)->lockvar, 0);			\
127a848e48cSrillig 	} while (0)
1286ecf6635Schristos 
1296ecf6635Schristos /** If lock debugging is enabled, and lock is non-null, assert that 'lock' is
1306ecf6635Schristos  * locked and held by us. */
1316ecf6635Schristos #define EVLOCK_ASSERT_LOCKED(lock)					\
1326ecf6635Schristos 	do {								\
1330d738af4Schristos 		if ((lock) && evthread_lock_debugging_enabled_) {	\
1340d738af4Schristos 			EVUTIL_ASSERT(evthread_is_debug_lock_held_(lock)); \
1356ecf6635Schristos 		}							\
136a848e48cSrillig 	} while (0)
1376ecf6635Schristos 
1386ecf6635Schristos /** Try to grab the lock for 'lockvar' without blocking, and return 1 if we
1396ecf6635Schristos  * manage to get it. */
1400d738af4Schristos static inline int EVLOCK_TRY_LOCK_(void *lock);
1416ecf6635Schristos static inline int
EVLOCK_TRY_LOCK_(void * lock)1420d738af4Schristos EVLOCK_TRY_LOCK_(void *lock)
1436ecf6635Schristos {
1440d738af4Schristos 	if (lock && evthread_lock_fns_.lock) {
1450d738af4Schristos 		int r = evthread_lock_fns_.lock(EVTHREAD_TRY, lock);
1466ecf6635Schristos 		return !r;
1476ecf6635Schristos 	} else {
1486ecf6635Schristos 		/* Locking is disabled either globally or for this thing;
1496ecf6635Schristos 		 * of course we count as having the lock. */
1506ecf6635Schristos 		return 1;
1516ecf6635Schristos 	}
1526ecf6635Schristos }
1536ecf6635Schristos 
1546ecf6635Schristos /** Allocate a new condition variable and store it in the void *, condvar */
1556ecf6635Schristos #define EVTHREAD_ALLOC_COND(condvar)					\
1566ecf6635Schristos 	do {								\
1570d738af4Schristos 		(condvar) = evthread_cond_fns_.alloc_condition ?	\
1580d738af4Schristos 		    evthread_cond_fns_.alloc_condition(0) : NULL;	\
159a848e48cSrillig 	} while (0)
1606ecf6635Schristos /** Deallocate and free a condition variable in condvar */
1616ecf6635Schristos #define EVTHREAD_FREE_COND(cond)					\
1626ecf6635Schristos 	do {								\
1636ecf6635Schristos 		if (cond)						\
1640d738af4Schristos 			evthread_cond_fns_.free_condition((cond));	\
165a848e48cSrillig 	} while (0)
1666ecf6635Schristos /** Signal one thread waiting on cond */
1676ecf6635Schristos #define EVTHREAD_COND_SIGNAL(cond)					\
1680d738af4Schristos 	( (cond) ? evthread_cond_fns_.signal_condition((cond), 0) : 0 )
1696ecf6635Schristos /** Signal all threads waiting on cond */
1706ecf6635Schristos #define EVTHREAD_COND_BROADCAST(cond)					\
1710d738af4Schristos 	( (cond) ? evthread_cond_fns_.signal_condition((cond), 1) : 0 )
1726ecf6635Schristos /** Wait until the condition 'cond' is signalled.  Must be called while
1736ecf6635Schristos  * holding 'lock'.  The lock will be released until the condition is
1746ecf6635Schristos  * signalled, at which point it will be acquired again.  Returns 0 for
1756ecf6635Schristos  * success, -1 for failure. */
1766ecf6635Schristos #define EVTHREAD_COND_WAIT(cond, lock)					\
1770d738af4Schristos 	( (cond) ? evthread_cond_fns_.wait_condition((cond), (lock), NULL) : 0 )
1786ecf6635Schristos /** As EVTHREAD_COND_WAIT, but gives up after 'tv' has elapsed.  Returns 1
1796ecf6635Schristos  * on timeout. */
1806ecf6635Schristos #define EVTHREAD_COND_WAIT_TIMED(cond, lock, tv)			\
1810d738af4Schristos 	( (cond) ? evthread_cond_fns_.wait_condition((cond), (lock), (tv)) : 0 )
1826ecf6635Schristos 
1836ecf6635Schristos /** True iff locking functions have been configured. */
1846ecf6635Schristos #define EVTHREAD_LOCKING_ENABLED()		\
1850d738af4Schristos 	(evthread_lock_fns_.lock != NULL)
1866ecf6635Schristos 
1870d738af4Schristos #elif ! defined(EVENT__DISABLE_THREAD_SUPPORT)
1886ecf6635Schristos 
1890d738af4Schristos unsigned long evthreadimpl_get_id_(void);
1907e68cdd7Schristos EVENT2_EXPORT_SYMBOL
1910d738af4Schristos int evthreadimpl_is_lock_debugging_enabled_(void);
1927e68cdd7Schristos EVENT2_EXPORT_SYMBOL
1930d738af4Schristos void *evthreadimpl_lock_alloc_(unsigned locktype);
1947e68cdd7Schristos EVENT2_EXPORT_SYMBOL
1950d738af4Schristos void evthreadimpl_lock_free_(void *lock, unsigned locktype);
1967e68cdd7Schristos EVENT2_EXPORT_SYMBOL
1970d738af4Schristos int evthreadimpl_lock_lock_(unsigned mode, void *lock);
1987e68cdd7Schristos EVENT2_EXPORT_SYMBOL
1990d738af4Schristos int evthreadimpl_lock_unlock_(unsigned mode, void *lock);
2007e68cdd7Schristos EVENT2_EXPORT_SYMBOL
2010d738af4Schristos void *evthreadimpl_cond_alloc_(unsigned condtype);
2027e68cdd7Schristos EVENT2_EXPORT_SYMBOL
2030d738af4Schristos void evthreadimpl_cond_free_(void *cond);
2047e68cdd7Schristos EVENT2_EXPORT_SYMBOL
2050d738af4Schristos int evthreadimpl_cond_signal_(void *cond, int broadcast);
2067e68cdd7Schristos EVENT2_EXPORT_SYMBOL
2070d738af4Schristos int evthreadimpl_cond_wait_(void *cond, void *lock, const struct timeval *tv);
2080d738af4Schristos int evthreadimpl_locking_enabled_(void);
2096ecf6635Schristos 
2100d738af4Schristos #define EVTHREAD_GET_ID() evthreadimpl_get_id_()
2116ecf6635Schristos #define EVBASE_IN_THREAD(base)				\
2120d738af4Schristos 	((base)->th_owner_id == evthreadimpl_get_id_())
2136ecf6635Schristos #define EVBASE_NEED_NOTIFY(base)			 \
2146ecf6635Schristos 	((base)->running_loop &&			 \
2150d738af4Schristos 	    ((base)->th_owner_id != evthreadimpl_get_id_()))
2166ecf6635Schristos 
2176ecf6635Schristos #define EVTHREAD_ALLOC_LOCK(lockvar, locktype)		\
2180d738af4Schristos 	((lockvar) = evthreadimpl_lock_alloc_(locktype))
2196ecf6635Schristos 
2206ecf6635Schristos #define EVTHREAD_FREE_LOCK(lockvar, locktype)				\
2216ecf6635Schristos 	do {								\
2220d738af4Schristos 		void *lock_tmp_ = (lockvar);				\
2230d738af4Schristos 		if (lock_tmp_)						\
2240d738af4Schristos 			evthreadimpl_lock_free_(lock_tmp_, (locktype)); \
225a848e48cSrillig 	} while (0)
2266ecf6635Schristos 
2276ecf6635Schristos /** Acquire a lock. */
2286ecf6635Schristos #define EVLOCK_LOCK(lockvar,mode)					\
2296ecf6635Schristos 	do {								\
2306ecf6635Schristos 		if (lockvar)						\
2310d738af4Schristos 			evthreadimpl_lock_lock_(mode, lockvar);		\
232a848e48cSrillig 	} while (0)
2336ecf6635Schristos 
2346ecf6635Schristos /** Release a lock */
2356ecf6635Schristos #define EVLOCK_UNLOCK(lockvar,mode)					\
2366ecf6635Schristos 	do {								\
2376ecf6635Schristos 		if (lockvar)						\
2380d738af4Schristos 			evthreadimpl_lock_unlock_(mode, lockvar);	\
239a848e48cSrillig 	} while (0)
2406ecf6635Schristos 
2416ecf6635Schristos /** Lock an event_base, if it is set up for locking.  Acquires the lock
2426ecf6635Schristos     in the base structure whose field is named 'lockvar'. */
2436ecf6635Schristos #define EVBASE_ACQUIRE_LOCK(base, lockvar) do {				\
2446ecf6635Schristos 		EVLOCK_LOCK((base)->lockvar, 0);			\
245a848e48cSrillig 	} while (0)
2466ecf6635Schristos 
2476ecf6635Schristos /** Unlock an event_base, if it is set up for locking. */
2486ecf6635Schristos #define EVBASE_RELEASE_LOCK(base, lockvar) do {				\
2496ecf6635Schristos 		EVLOCK_UNLOCK((base)->lockvar, 0);			\
250a848e48cSrillig 	} while (0)
2516ecf6635Schristos 
2526ecf6635Schristos /** If lock debugging is enabled, and lock is non-null, assert that 'lock' is
2536ecf6635Schristos  * locked and held by us. */
2546ecf6635Schristos #define EVLOCK_ASSERT_LOCKED(lock)					\
2556ecf6635Schristos 	do {								\
2560d738af4Schristos 		if ((lock) && evthreadimpl_is_lock_debugging_enabled_()) { \
2570d738af4Schristos 			EVUTIL_ASSERT(evthread_is_debug_lock_held_(lock)); \
2586ecf6635Schristos 		}							\
259a848e48cSrillig 	} while (0)
2606ecf6635Schristos 
2616ecf6635Schristos /** Try to grab the lock for 'lockvar' without blocking, and return 1 if we
2626ecf6635Schristos  * manage to get it. */
2630d738af4Schristos static inline int EVLOCK_TRY_LOCK_(void *lock);
2646ecf6635Schristos static inline int
EVLOCK_TRY_LOCK_(void * lock)2650d738af4Schristos EVLOCK_TRY_LOCK_(void *lock)
2666ecf6635Schristos {
2676ecf6635Schristos 	if (lock) {
2680d738af4Schristos 		int r = evthreadimpl_lock_lock_(EVTHREAD_TRY, lock);
2696ecf6635Schristos 		return !r;
2706ecf6635Schristos 	} else {
2716ecf6635Schristos 		/* Locking is disabled either globally or for this thing;
2726ecf6635Schristos 		 * of course we count as having the lock. */
2736ecf6635Schristos 		return 1;
2746ecf6635Schristos 	}
2756ecf6635Schristos }
2766ecf6635Schristos 
2776ecf6635Schristos /** Allocate a new condition variable and store it in the void *, condvar */
2786ecf6635Schristos #define EVTHREAD_ALLOC_COND(condvar)					\
2796ecf6635Schristos 	do {								\
2800d738af4Schristos 		(condvar) = evthreadimpl_cond_alloc_(0);		\
281a848e48cSrillig 	} while (0)
2826ecf6635Schristos /** Deallocate and free a condition variable in condvar */
2836ecf6635Schristos #define EVTHREAD_FREE_COND(cond)					\
2846ecf6635Schristos 	do {								\
2856ecf6635Schristos 		if (cond)						\
2860d738af4Schristos 			evthreadimpl_cond_free_((cond));		\
287a848e48cSrillig 	} while (0)
2886ecf6635Schristos /** Signal one thread waiting on cond */
2896ecf6635Schristos #define EVTHREAD_COND_SIGNAL(cond)					\
2900d738af4Schristos 	( (cond) ? evthreadimpl_cond_signal_((cond), 0) : 0 )
2916ecf6635Schristos /** Signal all threads waiting on cond */
2926ecf6635Schristos #define EVTHREAD_COND_BROADCAST(cond)					\
2930d738af4Schristos 	( (cond) ? evthreadimpl_cond_signal_((cond), 1) : 0 )
2946ecf6635Schristos /** Wait until the condition 'cond' is signalled.  Must be called while
2956ecf6635Schristos  * holding 'lock'.  The lock will be released until the condition is
2966ecf6635Schristos  * signalled, at which point it will be acquired again.  Returns 0 for
2976ecf6635Schristos  * success, -1 for failure. */
2986ecf6635Schristos #define EVTHREAD_COND_WAIT(cond, lock)					\
2990d738af4Schristos 	( (cond) ? evthreadimpl_cond_wait_((cond), (lock), NULL) : 0 )
3006ecf6635Schristos /** As EVTHREAD_COND_WAIT, but gives up after 'tv' has elapsed.  Returns 1
3016ecf6635Schristos  * on timeout. */
3026ecf6635Schristos #define EVTHREAD_COND_WAIT_TIMED(cond, lock, tv)			\
3030d738af4Schristos 	( (cond) ? evthreadimpl_cond_wait_((cond), (lock), (tv)) : 0 )
3046ecf6635Schristos 
3056ecf6635Schristos #define EVTHREAD_LOCKING_ENABLED()		\
3060d738af4Schristos 	(evthreadimpl_locking_enabled_())
3076ecf6635Schristos 
3080d738af4Schristos #else /* EVENT__DISABLE_THREAD_SUPPORT */
3096ecf6635Schristos 
3106ecf6635Schristos #define EVTHREAD_GET_ID()	1
3110d738af4Schristos #define EVTHREAD_ALLOC_LOCK(lockvar, locktype) EVUTIL_NIL_STMT_
3120d738af4Schristos #define EVTHREAD_FREE_LOCK(lockvar, locktype) EVUTIL_NIL_STMT_
3136ecf6635Schristos 
3140d738af4Schristos #define EVLOCK_LOCK(lockvar, mode) EVUTIL_NIL_STMT_
3150d738af4Schristos #define EVLOCK_UNLOCK(lockvar, mode) EVUTIL_NIL_STMT_
3160d738af4Schristos #define EVLOCK_LOCK2(lock1,lock2,mode1,mode2) EVUTIL_NIL_STMT_
3170d738af4Schristos #define EVLOCK_UNLOCK2(lock1,lock2,mode1,mode2) EVUTIL_NIL_STMT_
3186ecf6635Schristos 
3196ecf6635Schristos #define EVBASE_IN_THREAD(base)	1
3206ecf6635Schristos #define EVBASE_NEED_NOTIFY(base) 0
3210d738af4Schristos #define EVBASE_ACQUIRE_LOCK(base, lock) EVUTIL_NIL_STMT_
3220d738af4Schristos #define EVBASE_RELEASE_LOCK(base, lock) EVUTIL_NIL_STMT_
3230d738af4Schristos #define EVLOCK_ASSERT_LOCKED(lock) EVUTIL_NIL_STMT_
3246ecf6635Schristos 
3250d738af4Schristos #define EVLOCK_TRY_LOCK_(lock) 1
3266ecf6635Schristos 
3270d738af4Schristos #define EVTHREAD_ALLOC_COND(condvar) EVUTIL_NIL_STMT_
3280d738af4Schristos #define EVTHREAD_FREE_COND(cond) EVUTIL_NIL_STMT_
3290d738af4Schristos #define EVTHREAD_COND_SIGNAL(cond) EVUTIL_NIL_STMT_
3300d738af4Schristos #define EVTHREAD_COND_BROADCAST(cond) EVUTIL_NIL_STMT_
3310d738af4Schristos #define EVTHREAD_COND_WAIT(cond, lock) EVUTIL_NIL_STMT_
3320d738af4Schristos #define EVTHREAD_COND_WAIT_TIMED(cond, lock, howlong) EVUTIL_NIL_STMT_
3336ecf6635Schristos 
3346ecf6635Schristos #define EVTHREAD_LOCKING_ENABLED() 0
3356ecf6635Schristos 
3366ecf6635Schristos #endif
3376ecf6635Schristos 
3386ecf6635Schristos /* This code is shared between both lock impls */
3390d738af4Schristos #if ! defined(EVENT__DISABLE_THREAD_SUPPORT)
3406ecf6635Schristos /** Helper: put lockvar1 and lockvar2 into pointerwise ascending order. */
3410d738af4Schristos #define EVLOCK_SORTLOCKS_(lockvar1, lockvar2)				\
3426ecf6635Schristos 	do {								\
3436ecf6635Schristos 		if (lockvar1 && lockvar2 && lockvar1 > lockvar2) {	\
3446ecf6635Schristos 			void *tmp = lockvar1;				\
3456ecf6635Schristos 			lockvar1 = lockvar2;				\
3466ecf6635Schristos 			lockvar2 = tmp;					\
3476ecf6635Schristos 		}							\
348a848e48cSrillig 	} while (0)
3496ecf6635Schristos 
3506ecf6635Schristos /** Acquire both lock1 and lock2.  Always allocates locks in the same order,
3516ecf6635Schristos  * so that two threads locking two locks with LOCK2 will not deadlock. */
3526ecf6635Schristos #define EVLOCK_LOCK2(lock1,lock2,mode1,mode2)				\
3536ecf6635Schristos 	do {								\
3540d738af4Schristos 		void *lock1_tmplock_ = (lock1);				\
3550d738af4Schristos 		void *lock2_tmplock_ = (lock2);				\
3560d738af4Schristos 		EVLOCK_SORTLOCKS_(lock1_tmplock_,lock2_tmplock_);	\
3570d738af4Schristos 		EVLOCK_LOCK(lock1_tmplock_,mode1);			\
3580d738af4Schristos 		if (lock2_tmplock_ != lock1_tmplock_)			\
3590d738af4Schristos 			EVLOCK_LOCK(lock2_tmplock_,mode2);		\
360a848e48cSrillig 	} while (0)
3616ecf6635Schristos /** Release both lock1 and lock2.  */
3626ecf6635Schristos #define EVLOCK_UNLOCK2(lock1,lock2,mode1,mode2)				\
3636ecf6635Schristos 	do {								\
3640d738af4Schristos 		void *lock1_tmplock_ = (lock1);				\
3650d738af4Schristos 		void *lock2_tmplock_ = (lock2);				\
3660d738af4Schristos 		EVLOCK_SORTLOCKS_(lock1_tmplock_,lock2_tmplock_);	\
3670d738af4Schristos 		if (lock2_tmplock_ != lock1_tmplock_)			\
3680d738af4Schristos 			EVLOCK_UNLOCK(lock2_tmplock_,mode2);		\
3690d738af4Schristos 		EVLOCK_UNLOCK(lock1_tmplock_,mode1);			\
370a848e48cSrillig 	} while (0)
3716ecf6635Schristos 
3727e68cdd7Schristos EVENT2_EXPORT_SYMBOL
3730d738af4Schristos int evthread_is_debug_lock_held_(void *lock);
3740d738af4Schristos void *evthread_debug_get_real_lock_(void *lock);
3756ecf6635Schristos 
3766ecf6635Schristos void *evthread_setup_global_lock_(void *lock_, unsigned locktype,
3776ecf6635Schristos     int enable_locks);
3786ecf6635Schristos 
3796ecf6635Schristos #define EVTHREAD_SETUP_GLOBAL_LOCK(lockvar, locktype)			\
3806ecf6635Schristos 	do {								\
3816ecf6635Schristos 		lockvar = evthread_setup_global_lock_(lockvar,		\
3826ecf6635Schristos 		    (locktype), enable_locks);				\
3836ecf6635Schristos 		if (!lockvar) {						\
3846ecf6635Schristos 			event_warn("Couldn't allocate %s", #lockvar);	\
3856ecf6635Schristos 			return -1;					\
3866ecf6635Schristos 		}							\
387*81d2345cSrillig 	} while (0);
3886ecf6635Schristos 
3896ecf6635Schristos int event_global_setup_locks_(const int enable_locks);
3906ecf6635Schristos int evsig_global_setup_locks_(const int enable_locks);
3910d738af4Schristos int evutil_global_setup_locks_(const int enable_locks);
3926ecf6635Schristos int evutil_secure_rng_global_setup_locks_(const int enable_locks);
3936ecf6635Schristos 
3940d738af4Schristos /** Return current evthread_lock_callbacks */
3957e68cdd7Schristos EVENT2_EXPORT_SYMBOL
3960d738af4Schristos struct evthread_lock_callbacks *evthread_get_lock_callbacks(void);
3970d738af4Schristos /** Return current evthread_condition_callbacks */
3980d738af4Schristos struct evthread_condition_callbacks *evthread_get_condition_callbacks(void);
3990d738af4Schristos /** Disable locking for internal usage (like global shutdown) */
4000d738af4Schristos void evthreadimpl_disable_lock_debugging_(void);
4010d738af4Schristos 
4026ecf6635Schristos #endif
4036ecf6635Schristos 
4046ecf6635Schristos #ifdef __cplusplus
4056ecf6635Schristos }
4066ecf6635Schristos #endif
4076ecf6635Schristos 
4080d738af4Schristos #endif /* EVTHREAD_INTERNAL_H_INCLUDED_ */
409