xref: /libevent-2.1.12/evthread-internal.h (revision 8a9b5655)
1558de9b3SNiels Provos /*
2e49e2891SNick Mathewson  * Copyright (c) 2008-2012 Niels Provos, Nick Mathewson
3558de9b3SNiels Provos  *
4558de9b3SNiels Provos  * Redistribution and use in source and binary forms, with or without
5558de9b3SNiels Provos  * modification, are permitted provided that the following conditions
6558de9b3SNiels Provos  * are met:
7558de9b3SNiels Provos  * 1. Redistributions of source code must retain the above copyright
8558de9b3SNiels Provos  *    notice, this list of conditions and the following disclaimer.
9558de9b3SNiels Provos  * 2. Redistributions in binary form must reproduce the above copyright
10558de9b3SNiels Provos  *    notice, this list of conditions and the following disclaimer in the
11558de9b3SNiels Provos  *    documentation and/or other materials provided with the distribution.
12558de9b3SNiels Provos  * 3. The name of the author may not be used to endorse or promote products
13558de9b3SNiels Provos  *    derived from this software without specific prior written permission.
14558de9b3SNiels Provos  *
15558de9b3SNiels Provos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16558de9b3SNiels Provos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17558de9b3SNiels Provos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18558de9b3SNiels Provos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19558de9b3SNiels Provos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20558de9b3SNiels Provos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21558de9b3SNiels Provos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22558de9b3SNiels Provos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23558de9b3SNiels Provos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24558de9b3SNiels Provos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25558de9b3SNiels Provos  */
263f8c7cd0SNick Mathewson #ifndef EVTHREAD_INTERNAL_H_INCLUDED_
273f8c7cd0SNick Mathewson #define EVTHREAD_INTERNAL_H_INCLUDED_
28558de9b3SNiels Provos 
29558de9b3SNiels Provos #ifdef __cplusplus
30838d0a81SNick Mathewson extern "C" {
31558de9b3SNiels Provos #endif
32558de9b3SNiels Provos 
33ec347b92SNick Mathewson #include "event2/event-config.h"
340915ca0aSKevin Bowling #include "evconfig-private.h"
350915ca0aSKevin Bowling 
360915ca0aSKevin Bowling #include "event2/thread.h"
3728255a26SNick Mathewson #include "util-internal.h"
38558de9b3SNiels Provos 
39558de9b3SNiels Provos struct event_base;
40ec35eb55SNick Mathewson 
41*8a9b5655SAzat Khuzhin #if !defined(_WIN32) && !defined(__CYGWIN__)
425de2bcb7SNick Mathewson /* On Windows, the way we currently make DLLs, it's not allowed for us to
435de2bcb7SNick Mathewson  * have shared global structures.  Thus, we only do the direct-call-to-function
445de2bcb7SNick Mathewson  * code path if we know that the local shared library system supports it.
455de2bcb7SNick Mathewson  */
465de2bcb7SNick Mathewson #define EVTHREAD_EXPOSE_STRUCTS
475de2bcb7SNick Mathewson #endif
485de2bcb7SNick Mathewson 
4968120d9bSNick Mathewson #if ! defined(EVENT__DISABLE_THREAD_SUPPORT) && defined(EVTHREAD_EXPOSE_STRUCTS)
50838d0a81SNick Mathewson /* Global function pointers to lock-related functions. NULL if locking isn't
51838d0a81SNick Mathewson    enabled. */
529806b126SAzat Khuzhin EVENT2_EXPORT_SYMBOL
53cb9da0bfSNick Mathewson extern struct evthread_lock_callbacks evthread_lock_fns_;
549806b126SAzat Khuzhin EVENT2_EXPORT_SYMBOL
55cb9da0bfSNick Mathewson extern struct evthread_condition_callbacks evthread_cond_fns_;
56cb9da0bfSNick Mathewson extern unsigned long (*evthread_id_fn_)(void);
579806b126SAzat Khuzhin EVENT2_EXPORT_SYMBOL
58cb9da0bfSNick Mathewson extern int evthread_lock_debugging_enabled_;
59558de9b3SNiels Provos 
60838d0a81SNick Mathewson /** Return the ID of the current thread, or 1 if threading isn't enabled. */
61ec35eb55SNick Mathewson #define EVTHREAD_GET_ID() \
62cb9da0bfSNick Mathewson 	(evthread_id_fn_ ? evthread_id_fn_() : 1)
63558de9b3SNiels Provos 
64c7a06bfaSNick Mathewson /** Return true iff we're in the thread that is currently (or most recently)
65c7a06bfaSNick Mathewson  * running a given event_base's loop. Requires lock. */
66ec35eb55SNick Mathewson #define EVBASE_IN_THREAD(base)				 \
67cb9da0bfSNick Mathewson 	(evthread_id_fn_ == NULL ||			 \
68cb9da0bfSNick Mathewson 	(base)->th_owner_id == evthread_id_fn_())
69ec35eb55SNick Mathewson 
70c7a06bfaSNick Mathewson /** Return true iff we need to notify the base's main thread about changes to
71c7a06bfaSNick Mathewson  * its state, because it's currently running the main loop in another
72c7a06bfaSNick Mathewson  * thread. Requires lock. */
73c7a06bfaSNick Mathewson #define EVBASE_NEED_NOTIFY(base)			 \
74cb9da0bfSNick Mathewson 	(evthread_id_fn_ != NULL &&			 \
75c7a06bfaSNick Mathewson 	    (base)->running_loop &&			 \
76cb9da0bfSNick Mathewson 	    (base)->th_owner_id != evthread_id_fn_())
77c7a06bfaSNick Mathewson 
78838d0a81SNick Mathewson /** Allocate a new lock, and store it in lockvar, a void*.  Sets lockvar to
79838d0a81SNick Mathewson     NULL if locking is not enabled. */
80347952ffSNick Mathewson #define EVTHREAD_ALLOC_LOCK(lockvar, locktype)		\
81cb9da0bfSNick Mathewson 	((lockvar) = evthread_lock_fns_.alloc ?		\
82cb9da0bfSNick Mathewson 	    evthread_lock_fns_.alloc(locktype) : NULL)
83ec35eb55SNick Mathewson 
84838d0a81SNick Mathewson /** Free a given lock, if it is present and locking is enabled. */
85347952ffSNick Mathewson #define EVTHREAD_FREE_LOCK(lockvar, locktype)				\
86ec35eb55SNick Mathewson 	do {								\
87946b5841SNick Mathewson 		void *lock_tmp_ = (lockvar);				\
88946b5841SNick Mathewson 		if (lock_tmp_ && evthread_lock_fns_.free)		\
89946b5841SNick Mathewson 			evthread_lock_fns_.free(lock_tmp_, (locktype)); \
9070ee390fSNick Mathewson 	} while (0)
9170ee390fSNick Mathewson 
92838d0a81SNick Mathewson /** Acquire a lock. */
93347952ffSNick Mathewson #define EVLOCK_LOCK(lockvar,mode)					\
9470ee390fSNick Mathewson 	do {								\
95347952ffSNick Mathewson 		if (lockvar)						\
96cb9da0bfSNick Mathewson 			evthread_lock_fns_.lock(mode, lockvar);		\
9770ee390fSNick Mathewson 	} while (0)
98838d0a81SNick Mathewson 
99838d0a81SNick Mathewson /** Release a lock */
100347952ffSNick Mathewson #define EVLOCK_UNLOCK(lockvar,mode)					\
10170ee390fSNick Mathewson 	do {								\
102347952ffSNick Mathewson 		if (lockvar)						\
103cb9da0bfSNick Mathewson 			evthread_lock_fns_.unlock(mode, lockvar);	\
10470ee390fSNick Mathewson 	} while (0)
105838d0a81SNick Mathewson 
106838d0a81SNick Mathewson /** Helper: put lockvar1 and lockvar2 into pointerwise ascending order. */
107cb9da0bfSNick Mathewson #define EVLOCK_SORTLOCKS_(lockvar1, lockvar2)				\
10870ee390fSNick Mathewson 	do {								\
10970ee390fSNick Mathewson 		if (lockvar1 && lockvar2 && lockvar1 > lockvar2) {	\
11070ee390fSNick Mathewson 			void *tmp = lockvar1;				\
11170ee390fSNick Mathewson 			lockvar1 = lockvar2;				\
11270ee390fSNick Mathewson 			lockvar2 = tmp;					\
11370ee390fSNick Mathewson 		}							\
11470ee390fSNick Mathewson 	} while (0)
11570ee390fSNick Mathewson 
116838d0a81SNick Mathewson /** Lock an event_base, if it is set up for locking.  Acquires the lock
11776cd2b70SNick Mathewson     in the base structure whose field is named 'lockvar'. */
11876cd2b70SNick Mathewson #define EVBASE_ACQUIRE_LOCK(base, lockvar) do {				\
119218a3c37SNick Mathewson 		EVLOCK_LOCK((base)->lockvar, 0);			\
120558de9b3SNiels Provos 	} while (0)
121558de9b3SNiels Provos 
122838d0a81SNick Mathewson /** Unlock an event_base, if it is set up for locking. */
12376cd2b70SNick Mathewson #define EVBASE_RELEASE_LOCK(base, lockvar) do {				\
124218a3c37SNick Mathewson 		EVLOCK_UNLOCK((base)->lockvar, 0);			\
125558de9b3SNiels Provos 	} while (0)
1260cd3bb9fSNick Mathewson 
1270cd3bb9fSNick Mathewson /** If lock debugging is enabled, and lock is non-null, assert that 'lock' is
1280cd3bb9fSNick Mathewson  * locked and held by us. */
1290cd3bb9fSNick Mathewson #define EVLOCK_ASSERT_LOCKED(lock)					\
1300cd3bb9fSNick Mathewson 	do {								\
131cb9da0bfSNick Mathewson 		if ((lock) && evthread_lock_debugging_enabled_) {	\
132cb9da0bfSNick Mathewson 			EVUTIL_ASSERT(evthread_is_debug_lock_held_(lock)); \
1330cd3bb9fSNick Mathewson 		}							\
1340cd3bb9fSNick Mathewson 	} while (0)
1350cd3bb9fSNick Mathewson 
136689fc091SNick Mathewson /** Try to grab the lock for 'lockvar' without blocking, and return 1 if we
137689fc091SNick Mathewson  * manage to get it. */
1388ac3c4c2SNick Mathewson static inline int EVLOCK_TRY_LOCK_(void *lock);
139689fc091SNick Mathewson static inline int
EVLOCK_TRY_LOCK_(void * lock)1408ac3c4c2SNick Mathewson EVLOCK_TRY_LOCK_(void *lock)
141689fc091SNick Mathewson {
142cb9da0bfSNick Mathewson 	if (lock && evthread_lock_fns_.lock) {
143cb9da0bfSNick Mathewson 		int r = evthread_lock_fns_.lock(EVTHREAD_TRY, lock);
144689fc091SNick Mathewson 		return !r;
145689fc091SNick Mathewson 	} else {
146689fc091SNick Mathewson 		/* Locking is disabled either globally or for this thing;
147689fc091SNick Mathewson 		 * of course we count as having the lock. */
148689fc091SNick Mathewson 		return 1;
149689fc091SNick Mathewson 	}
150689fc091SNick Mathewson }
151689fc091SNick Mathewson 
152d4977b52SNick Mathewson /** Allocate a new condition variable and store it in the void *, condvar */
153d4977b52SNick Mathewson #define EVTHREAD_ALLOC_COND(condvar)					\
154d4977b52SNick Mathewson 	do {								\
155cb9da0bfSNick Mathewson 		(condvar) = evthread_cond_fns_.alloc_condition ?	\
156cb9da0bfSNick Mathewson 		    evthread_cond_fns_.alloc_condition(0) : NULL;	\
157d4977b52SNick Mathewson 	} while (0)
158d4977b52SNick Mathewson /** Deallocate and free a condition variable in condvar */
159d4977b52SNick Mathewson #define EVTHREAD_FREE_COND(cond)					\
160d4977b52SNick Mathewson 	do {								\
161d4977b52SNick Mathewson 		if (cond)						\
162cb9da0bfSNick Mathewson 			evthread_cond_fns_.free_condition((cond));	\
163d4977b52SNick Mathewson 	} while (0)
164d4977b52SNick Mathewson /** Signal one thread waiting on cond */
165d4977b52SNick Mathewson #define EVTHREAD_COND_SIGNAL(cond)					\
166cb9da0bfSNick Mathewson 	( (cond) ? evthread_cond_fns_.signal_condition((cond), 0) : 0 )
167d4977b52SNick Mathewson /** Signal all threads waiting on cond */
168d4977b52SNick Mathewson #define EVTHREAD_COND_BROADCAST(cond)					\
169cb9da0bfSNick Mathewson 	( (cond) ? evthread_cond_fns_.signal_condition((cond), 1) : 0 )
170d4977b52SNick Mathewson /** Wait until the condition 'cond' is signalled.  Must be called while
171d4977b52SNick Mathewson  * holding 'lock'.  The lock will be released until the condition is
172d4977b52SNick Mathewson  * signalled, at which point it will be acquired again.  Returns 0 for
173d4977b52SNick Mathewson  * success, -1 for failure. */
174d4977b52SNick Mathewson #define EVTHREAD_COND_WAIT(cond, lock)					\
175cb9da0bfSNick Mathewson 	( (cond) ? evthread_cond_fns_.wait_condition((cond), (lock), NULL) : 0 )
176d4977b52SNick Mathewson /** As EVTHREAD_COND_WAIT, but gives up after 'tv' has elapsed.  Returns 1
177d4977b52SNick Mathewson  * on timeout. */
178d4977b52SNick Mathewson #define EVTHREAD_COND_WAIT_TIMED(cond, lock, tv)			\
179cb9da0bfSNick Mathewson 	( (cond) ? evthread_cond_fns_.wait_condition((cond), (lock), (tv)) : 0 )
180d4977b52SNick Mathewson 
181e7874133SNick Mathewson /** True iff locking functions have been configured. */
182e7874133SNick Mathewson #define EVTHREAD_LOCKING_ENABLED()		\
183cb9da0bfSNick Mathewson 	(evthread_lock_fns_.lock != NULL)
184e7874133SNick Mathewson 
18568120d9bSNick Mathewson #elif ! defined(EVENT__DISABLE_THREAD_SUPPORT)
1865de2bcb7SNick Mathewson 
187cb9da0bfSNick Mathewson unsigned long evthreadimpl_get_id_(void);
188ebd12e6dSAzat Khuzhin EVENT2_EXPORT_SYMBOL
189cb9da0bfSNick Mathewson int evthreadimpl_is_lock_debugging_enabled_(void);
190ebd12e6dSAzat Khuzhin EVENT2_EXPORT_SYMBOL
191cb9da0bfSNick Mathewson void *evthreadimpl_lock_alloc_(unsigned locktype);
192ebd12e6dSAzat Khuzhin EVENT2_EXPORT_SYMBOL
193cb9da0bfSNick Mathewson void evthreadimpl_lock_free_(void *lock, unsigned locktype);
194ebd12e6dSAzat Khuzhin EVENT2_EXPORT_SYMBOL
195cb9da0bfSNick Mathewson int evthreadimpl_lock_lock_(unsigned mode, void *lock);
196ebd12e6dSAzat Khuzhin EVENT2_EXPORT_SYMBOL
197cb9da0bfSNick Mathewson int evthreadimpl_lock_unlock_(unsigned mode, void *lock);
198ebd12e6dSAzat Khuzhin EVENT2_EXPORT_SYMBOL
199cb9da0bfSNick Mathewson void *evthreadimpl_cond_alloc_(unsigned condtype);
200ebd12e6dSAzat Khuzhin EVENT2_EXPORT_SYMBOL
201cb9da0bfSNick Mathewson void evthreadimpl_cond_free_(void *cond);
202ebd12e6dSAzat Khuzhin EVENT2_EXPORT_SYMBOL
203cb9da0bfSNick Mathewson int evthreadimpl_cond_signal_(void *cond, int broadcast);
204ebd12e6dSAzat Khuzhin EVENT2_EXPORT_SYMBOL
205cb9da0bfSNick Mathewson int evthreadimpl_cond_wait_(void *cond, void *lock, const struct timeval *tv);
206cb9da0bfSNick Mathewson int evthreadimpl_locking_enabled_(void);
2075de2bcb7SNick Mathewson 
208cb9da0bfSNick Mathewson #define EVTHREAD_GET_ID() evthreadimpl_get_id_()
2095de2bcb7SNick Mathewson #define EVBASE_IN_THREAD(base)				\
210cb9da0bfSNick Mathewson 	((base)->th_owner_id == evthreadimpl_get_id_())
211e7dc501eSNick Mathewson #define EVBASE_NEED_NOTIFY(base)			 \
212e7dc501eSNick Mathewson 	((base)->running_loop &&			 \
213cb9da0bfSNick Mathewson 	    ((base)->th_owner_id != evthreadimpl_get_id_()))
214e7dc501eSNick Mathewson 
2155de2bcb7SNick Mathewson #define EVTHREAD_ALLOC_LOCK(lockvar, locktype)		\
216cb9da0bfSNick Mathewson 	((lockvar) = evthreadimpl_lock_alloc_(locktype))
2175de2bcb7SNick Mathewson 
2185de2bcb7SNick Mathewson #define EVTHREAD_FREE_LOCK(lockvar, locktype)				\
2195de2bcb7SNick Mathewson 	do {								\
220946b5841SNick Mathewson 		void *lock_tmp_ = (lockvar);				\
221946b5841SNick Mathewson 		if (lock_tmp_)						\
222946b5841SNick Mathewson 			evthreadimpl_lock_free_(lock_tmp_, (locktype)); \
2235de2bcb7SNick Mathewson 	} while (0)
2245de2bcb7SNick Mathewson 
2255de2bcb7SNick Mathewson /** Acquire a lock. */
2265de2bcb7SNick Mathewson #define EVLOCK_LOCK(lockvar,mode)					\
2275de2bcb7SNick Mathewson 	do {								\
2285de2bcb7SNick Mathewson 		if (lockvar)						\
229cb9da0bfSNick Mathewson 			evthreadimpl_lock_lock_(mode, lockvar);		\
2305de2bcb7SNick Mathewson 	} while (0)
2315de2bcb7SNick Mathewson 
2325de2bcb7SNick Mathewson /** Release a lock */
2335de2bcb7SNick Mathewson #define EVLOCK_UNLOCK(lockvar,mode)					\
2345de2bcb7SNick Mathewson 	do {								\
2355de2bcb7SNick Mathewson 		if (lockvar)						\
236cb9da0bfSNick Mathewson 			evthreadimpl_lock_unlock_(mode, lockvar);	\
2375de2bcb7SNick Mathewson 	} while (0)
2385de2bcb7SNick Mathewson 
2395de2bcb7SNick Mathewson /** Lock an event_base, if it is set up for locking.  Acquires the lock
2405de2bcb7SNick Mathewson     in the base structure whose field is named 'lockvar'. */
2415de2bcb7SNick Mathewson #define EVBASE_ACQUIRE_LOCK(base, lockvar) do {				\
2425de2bcb7SNick Mathewson 		EVLOCK_LOCK((base)->lockvar, 0);			\
2435de2bcb7SNick Mathewson 	} while (0)
2445de2bcb7SNick Mathewson 
2455de2bcb7SNick Mathewson /** Unlock an event_base, if it is set up for locking. */
2465de2bcb7SNick Mathewson #define EVBASE_RELEASE_LOCK(base, lockvar) do {				\
2475de2bcb7SNick Mathewson 		EVLOCK_UNLOCK((base)->lockvar, 0);			\
2485de2bcb7SNick Mathewson 	} while (0)
2495de2bcb7SNick Mathewson 
2505de2bcb7SNick Mathewson /** If lock debugging is enabled, and lock is non-null, assert that 'lock' is
2515de2bcb7SNick Mathewson  * locked and held by us. */
2525de2bcb7SNick Mathewson #define EVLOCK_ASSERT_LOCKED(lock)					\
2535de2bcb7SNick Mathewson 	do {								\
254cb9da0bfSNick Mathewson 		if ((lock) && evthreadimpl_is_lock_debugging_enabled_()) { \
255cb9da0bfSNick Mathewson 			EVUTIL_ASSERT(evthread_is_debug_lock_held_(lock)); \
2565de2bcb7SNick Mathewson 		}							\
2575de2bcb7SNick Mathewson 	} while (0)
2585de2bcb7SNick Mathewson 
2595de2bcb7SNick Mathewson /** Try to grab the lock for 'lockvar' without blocking, and return 1 if we
2605de2bcb7SNick Mathewson  * manage to get it. */
2618ac3c4c2SNick Mathewson static inline int EVLOCK_TRY_LOCK_(void *lock);
2625de2bcb7SNick Mathewson static inline int
EVLOCK_TRY_LOCK_(void * lock)2638ac3c4c2SNick Mathewson EVLOCK_TRY_LOCK_(void *lock)
2645de2bcb7SNick Mathewson {
2655de2bcb7SNick Mathewson 	if (lock) {
266cb9da0bfSNick Mathewson 		int r = evthreadimpl_lock_lock_(EVTHREAD_TRY, lock);
2675de2bcb7SNick Mathewson 		return !r;
2685de2bcb7SNick Mathewson 	} else {
2695de2bcb7SNick Mathewson 		/* Locking is disabled either globally or for this thing;
2705de2bcb7SNick Mathewson 		 * of course we count as having the lock. */
2715de2bcb7SNick Mathewson 		return 1;
2725de2bcb7SNick Mathewson 	}
2735de2bcb7SNick Mathewson }
2745de2bcb7SNick Mathewson 
2755de2bcb7SNick Mathewson /** Allocate a new condition variable and store it in the void *, condvar */
2765de2bcb7SNick Mathewson #define EVTHREAD_ALLOC_COND(condvar)					\
2775de2bcb7SNick Mathewson 	do {								\
278cb9da0bfSNick Mathewson 		(condvar) = evthreadimpl_cond_alloc_(0);		\
2795de2bcb7SNick Mathewson 	} while (0)
2805de2bcb7SNick Mathewson /** Deallocate and free a condition variable in condvar */
2815de2bcb7SNick Mathewson #define EVTHREAD_FREE_COND(cond)					\
2825de2bcb7SNick Mathewson 	do {								\
2835de2bcb7SNick Mathewson 		if (cond)						\
284cb9da0bfSNick Mathewson 			evthreadimpl_cond_free_((cond));		\
2855de2bcb7SNick Mathewson 	} while (0)
2865de2bcb7SNick Mathewson /** Signal one thread waiting on cond */
2875de2bcb7SNick Mathewson #define EVTHREAD_COND_SIGNAL(cond)					\
288cb9da0bfSNick Mathewson 	( (cond) ? evthreadimpl_cond_signal_((cond), 0) : 0 )
2895de2bcb7SNick Mathewson /** Signal all threads waiting on cond */
2905de2bcb7SNick Mathewson #define EVTHREAD_COND_BROADCAST(cond)					\
291cb9da0bfSNick Mathewson 	( (cond) ? evthreadimpl_cond_signal_((cond), 1) : 0 )
2925de2bcb7SNick Mathewson /** Wait until the condition 'cond' is signalled.  Must be called while
2935de2bcb7SNick Mathewson  * holding 'lock'.  The lock will be released until the condition is
2945de2bcb7SNick Mathewson  * signalled, at which point it will be acquired again.  Returns 0 for
2955de2bcb7SNick Mathewson  * success, -1 for failure. */
2965de2bcb7SNick Mathewson #define EVTHREAD_COND_WAIT(cond, lock)					\
297cb9da0bfSNick Mathewson 	( (cond) ? evthreadimpl_cond_wait_((cond), (lock), NULL) : 0 )
2985de2bcb7SNick Mathewson /** As EVTHREAD_COND_WAIT, but gives up after 'tv' has elapsed.  Returns 1
2995de2bcb7SNick Mathewson  * on timeout. */
3005de2bcb7SNick Mathewson #define EVTHREAD_COND_WAIT_TIMED(cond, lock, tv)			\
301cb9da0bfSNick Mathewson 	( (cond) ? evthreadimpl_cond_wait_((cond), (lock), (tv)) : 0 )
3025de2bcb7SNick Mathewson 
303e7874133SNick Mathewson #define EVTHREAD_LOCKING_ENABLED()		\
304cb9da0bfSNick Mathewson 	(evthreadimpl_locking_enabled_())
305e7874133SNick Mathewson 
30668120d9bSNick Mathewson #else /* EVENT__DISABLE_THREAD_SUPPORT */
307ec35eb55SNick Mathewson 
308d13b59ceSNick Mathewson #define EVTHREAD_GET_ID()	1
309cb9da0bfSNick Mathewson #define EVTHREAD_ALLOC_LOCK(lockvar, locktype) EVUTIL_NIL_STMT_
310cb9da0bfSNick Mathewson #define EVTHREAD_FREE_LOCK(lockvar, locktype) EVUTIL_NIL_STMT_
311d13b59ceSNick Mathewson 
312cb9da0bfSNick Mathewson #define EVLOCK_LOCK(lockvar, mode) EVUTIL_NIL_STMT_
313cb9da0bfSNick Mathewson #define EVLOCK_UNLOCK(lockvar, mode) EVUTIL_NIL_STMT_
314cb9da0bfSNick Mathewson #define EVLOCK_LOCK2(lock1,lock2,mode1,mode2) EVUTIL_NIL_STMT_
315cb9da0bfSNick Mathewson #define EVLOCK_UNLOCK2(lock1,lock2,mode1,mode2) EVUTIL_NIL_STMT_
316d13b59ceSNick Mathewson 
317d13b59ceSNick Mathewson #define EVBASE_IN_THREAD(base)	1
318c7a06bfaSNick Mathewson #define EVBASE_NEED_NOTIFY(base) 0
319cb9da0bfSNick Mathewson #define EVBASE_ACQUIRE_LOCK(base, lock) EVUTIL_NIL_STMT_
320cb9da0bfSNick Mathewson #define EVBASE_RELEASE_LOCK(base, lock) EVUTIL_NIL_STMT_
321cb9da0bfSNick Mathewson #define EVLOCK_ASSERT_LOCKED(lock) EVUTIL_NIL_STMT_
322689fc091SNick Mathewson 
3238ac3c4c2SNick Mathewson #define EVLOCK_TRY_LOCK_(lock) 1
324d4977b52SNick Mathewson 
325cb9da0bfSNick Mathewson #define EVTHREAD_ALLOC_COND(condvar) EVUTIL_NIL_STMT_
326cb9da0bfSNick Mathewson #define EVTHREAD_FREE_COND(cond) EVUTIL_NIL_STMT_
327cb9da0bfSNick Mathewson #define EVTHREAD_COND_SIGNAL(cond) EVUTIL_NIL_STMT_
328cb9da0bfSNick Mathewson #define EVTHREAD_COND_BROADCAST(cond) EVUTIL_NIL_STMT_
329cb9da0bfSNick Mathewson #define EVTHREAD_COND_WAIT(cond, lock) EVUTIL_NIL_STMT_
330cb9da0bfSNick Mathewson #define EVTHREAD_COND_WAIT_TIMED(cond, lock, howlong) EVUTIL_NIL_STMT_
331d4977b52SNick Mathewson 
332e7874133SNick Mathewson #define EVTHREAD_LOCKING_ENABLED() 0
333e7874133SNick Mathewson 
334558de9b3SNiels Provos #endif
335558de9b3SNiels Provos 
3365de2bcb7SNick Mathewson /* This code is shared between both lock impls */
33768120d9bSNick Mathewson #if ! defined(EVENT__DISABLE_THREAD_SUPPORT)
3385de2bcb7SNick Mathewson /** Helper: put lockvar1 and lockvar2 into pointerwise ascending order. */
339cb9da0bfSNick Mathewson #define EVLOCK_SORTLOCKS_(lockvar1, lockvar2)				\
3405de2bcb7SNick Mathewson 	do {								\
3415de2bcb7SNick Mathewson 		if (lockvar1 && lockvar2 && lockvar1 > lockvar2) {	\
3425de2bcb7SNick Mathewson 			void *tmp = lockvar1;				\
3435de2bcb7SNick Mathewson 			lockvar1 = lockvar2;				\
3445de2bcb7SNick Mathewson 			lockvar2 = tmp;					\
3455de2bcb7SNick Mathewson 		}							\
3465de2bcb7SNick Mathewson 	} while (0)
3475de2bcb7SNick Mathewson 
3485de2bcb7SNick Mathewson /** Acquire both lock1 and lock2.  Always allocates locks in the same order,
3495de2bcb7SNick Mathewson  * so that two threads locking two locks with LOCK2 will not deadlock. */
3505de2bcb7SNick Mathewson #define EVLOCK_LOCK2(lock1,lock2,mode1,mode2)				\
3515de2bcb7SNick Mathewson 	do {								\
352946b5841SNick Mathewson 		void *lock1_tmplock_ = (lock1);				\
353946b5841SNick Mathewson 		void *lock2_tmplock_ = (lock2);				\
354946b5841SNick Mathewson 		EVLOCK_SORTLOCKS_(lock1_tmplock_,lock2_tmplock_);	\
355946b5841SNick Mathewson 		EVLOCK_LOCK(lock1_tmplock_,mode1);			\
356946b5841SNick Mathewson 		if (lock2_tmplock_ != lock1_tmplock_)			\
357946b5841SNick Mathewson 			EVLOCK_LOCK(lock2_tmplock_,mode2);		\
3585de2bcb7SNick Mathewson 	} while (0)
3595de2bcb7SNick Mathewson /** Release both lock1 and lock2.  */
3605de2bcb7SNick Mathewson #define EVLOCK_UNLOCK2(lock1,lock2,mode1,mode2)				\
3615de2bcb7SNick Mathewson 	do {								\
362946b5841SNick Mathewson 		void *lock1_tmplock_ = (lock1);				\
363946b5841SNick Mathewson 		void *lock2_tmplock_ = (lock2);				\
364946b5841SNick Mathewson 		EVLOCK_SORTLOCKS_(lock1_tmplock_,lock2_tmplock_);	\
365946b5841SNick Mathewson 		if (lock2_tmplock_ != lock1_tmplock_)			\
366946b5841SNick Mathewson 			EVLOCK_UNLOCK(lock2_tmplock_,mode2);		\
367946b5841SNick Mathewson 		EVLOCK_UNLOCK(lock1_tmplock_,mode1);			\
3685de2bcb7SNick Mathewson 	} while (0)
3695de2bcb7SNick Mathewson 
3709806b126SAzat Khuzhin EVENT2_EXPORT_SYMBOL
371cb9da0bfSNick Mathewson int evthread_is_debug_lock_held_(void *lock);
372cb9da0bfSNick Mathewson void *evthread_debug_get_real_lock_(void *lock);
373b683cae3SNick Mathewson 
374b683cae3SNick Mathewson void *evthread_setup_global_lock_(void *lock_, unsigned locktype,
375b683cae3SNick Mathewson     int enable_locks);
376b683cae3SNick Mathewson 
377b683cae3SNick Mathewson #define EVTHREAD_SETUP_GLOBAL_LOCK(lockvar, locktype)			\
378b683cae3SNick Mathewson 	do {								\
379b683cae3SNick Mathewson 		lockvar = evthread_setup_global_lock_(lockvar,		\
380b683cae3SNick Mathewson 		    (locktype), enable_locks);				\
381b683cae3SNick Mathewson 		if (!lockvar) {						\
382b683cae3SNick Mathewson 			event_warn("Couldn't allocate %s", #lockvar);	\
383b683cae3SNick Mathewson 			return -1;					\
384b683cae3SNick Mathewson 		}							\
385b683cae3SNick Mathewson 	} while (0);
386b683cae3SNick Mathewson 
387b683cae3SNick Mathewson int event_global_setup_locks_(const int enable_locks);
388b683cae3SNick Mathewson int evsig_global_setup_locks_(const int enable_locks);
3890c6ec5d8SPatrick Pelletier int evutil_global_setup_locks_(const int enable_locks);
390b683cae3SNick Mathewson int evutil_secure_rng_global_setup_locks_(const int enable_locks);
391b683cae3SNick Mathewson 
392c0b34f6fSAzat Khuzhin /** Return current evthread_lock_callbacks */
3939806b126SAzat Khuzhin EVENT2_EXPORT_SYMBOL
394746d2c50SThomas Bernard struct evthread_lock_callbacks *evthread_get_lock_callbacks(void);
395c0b34f6fSAzat Khuzhin /** Return current evthread_condition_callbacks */
396746d2c50SThomas Bernard struct evthread_condition_callbacks *evthread_get_condition_callbacks(void);
397ccc55937SAzat Khuzhin /** Disable locking for internal usage (like global shutdown) */
398ccc55937SAzat Khuzhin void evthreadimpl_disable_lock_debugging_(void);
399c0b34f6fSAzat Khuzhin 
4005de2bcb7SNick Mathewson #endif
4015de2bcb7SNick Mathewson 
402558de9b3SNiels Provos #ifdef __cplusplus
403558de9b3SNiels Provos }
404558de9b3SNiels Provos #endif
405558de9b3SNiels Provos 
4063f8c7cd0SNick Mathewson #endif /* EVTHREAD_INTERNAL_H_INCLUDED_ */
407