xref: /netbsd-src/external/bsd/ntp/dist/sntp/libevent/evthread-internal.h (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: evthread-internal.h,v 1.1.1.1 2013/12/27 23:31:24 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 2008-2012 Niels Provos, Nick Mathewson
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 #ifndef EVTHREAD_INTERNAL_H_INCLUDED_
29 #define EVTHREAD_INTERNAL_H_INCLUDED_
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
35 #include "event2/event-config.h"
36 #include "evconfig-private.h"
37 
38 #include "event2/thread.h"
39 #include "util-internal.h"
40 
41 struct event_base;
42 
43 #ifndef _WIN32
44 /* On Windows, the way we currently make DLLs, it's not allowed for us to
45  * have shared global structures.  Thus, we only do the direct-call-to-function
46  * code path if we know that the local shared library system supports it.
47  */
48 #define EVTHREAD_EXPOSE_STRUCTS
49 #endif
50 
51 #if ! defined(EVENT__DISABLE_THREAD_SUPPORT) && defined(EVTHREAD_EXPOSE_STRUCTS)
52 /* Global function pointers to lock-related functions. NULL if locking isn't
53    enabled. */
54 extern struct evthread_lock_callbacks evthread_lock_fns_;
55 extern struct evthread_condition_callbacks evthread_cond_fns_;
56 extern unsigned long (*evthread_id_fn_)(void);
57 extern int evthread_lock_debugging_enabled_;
58 
59 /** Return the ID of the current thread, or 1 if threading isn't enabled. */
60 #define EVTHREAD_GET_ID() \
61 	(evthread_id_fn_ ? evthread_id_fn_() : 1)
62 
63 /** Return true iff we're in the thread that is currently (or most recently)
64  * running a given event_base's loop. Requires lock. */
65 #define EVBASE_IN_THREAD(base)				 \
66 	(evthread_id_fn_ == NULL ||			 \
67 	(base)->th_owner_id == evthread_id_fn_())
68 
69 /** Return true iff we need to notify the base's main thread about changes to
70  * its state, because it's currently running the main loop in another
71  * thread. Requires lock. */
72 #define EVBASE_NEED_NOTIFY(base)			 \
73 	(evthread_id_fn_ != NULL &&			 \
74 	    (base)->running_loop &&			 \
75 	    (base)->th_owner_id != evthread_id_fn_())
76 
77 /** Allocate a new lock, and store it in lockvar, a void*.  Sets lockvar to
78     NULL if locking is not enabled. */
79 #define EVTHREAD_ALLOC_LOCK(lockvar, locktype)		\
80 	((lockvar) = evthread_lock_fns_.alloc ?		\
81 	    evthread_lock_fns_.alloc(locktype) : NULL)
82 
83 /** Free a given lock, if it is present and locking is enabled. */
84 #define EVTHREAD_FREE_LOCK(lockvar, locktype)				\
85 	do {								\
86 		void *lock_tmp_ = (lockvar);				\
87 		if (lock_tmp_ && evthread_lock_fns_.free)		\
88 			evthread_lock_fns_.free(lock_tmp_, (locktype)); \
89 	} while (0)
90 
91 /** Acquire a lock. */
92 #define EVLOCK_LOCK(lockvar,mode)					\
93 	do {								\
94 		if (lockvar)						\
95 			evthread_lock_fns_.lock(mode, lockvar);		\
96 	} while (0)
97 
98 /** Release a lock */
99 #define EVLOCK_UNLOCK(lockvar,mode)					\
100 	do {								\
101 		if (lockvar)						\
102 			evthread_lock_fns_.unlock(mode, lockvar);	\
103 	} while (0)
104 
105 /** Helper: put lockvar1 and lockvar2 into pointerwise ascending order. */
106 #define EVLOCK_SORTLOCKS_(lockvar1, lockvar2)				\
107 	do {								\
108 		if (lockvar1 && lockvar2 && lockvar1 > lockvar2) {	\
109 			void *tmp = lockvar1;				\
110 			lockvar1 = lockvar2;				\
111 			lockvar2 = tmp;					\
112 		}							\
113 	} while (0)
114 
115 /** Lock an event_base, if it is set up for locking.  Acquires the lock
116     in the base structure whose field is named 'lockvar'. */
117 #define EVBASE_ACQUIRE_LOCK(base, lockvar) do {				\
118 		EVLOCK_LOCK((base)->lockvar, 0);			\
119 	} while (0)
120 
121 /** Unlock an event_base, if it is set up for locking. */
122 #define EVBASE_RELEASE_LOCK(base, lockvar) do {				\
123 		EVLOCK_UNLOCK((base)->lockvar, 0);			\
124 	} while (0)
125 
126 /** If lock debugging is enabled, and lock is non-null, assert that 'lock' is
127  * locked and held by us. */
128 #define EVLOCK_ASSERT_LOCKED(lock)					\
129 	do {								\
130 		if ((lock) && evthread_lock_debugging_enabled_) {	\
131 			EVUTIL_ASSERT(evthread_is_debug_lock_held_(lock)); \
132 		}							\
133 	} while (0)
134 
135 /** Try to grab the lock for 'lockvar' without blocking, and return 1 if we
136  * manage to get it. */
137 static inline int EVLOCK_TRY_LOCK_(void *lock);
138 static inline int
139 EVLOCK_TRY_LOCK_(void *lock)
140 {
141 	if (lock && evthread_lock_fns_.lock) {
142 		int r = evthread_lock_fns_.lock(EVTHREAD_TRY, lock);
143 		return !r;
144 	} else {
145 		/* Locking is disabled either globally or for this thing;
146 		 * of course we count as having the lock. */
147 		return 1;
148 	}
149 }
150 
151 /** Allocate a new condition variable and store it in the void *, condvar */
152 #define EVTHREAD_ALLOC_COND(condvar)					\
153 	do {								\
154 		(condvar) = evthread_cond_fns_.alloc_condition ?	\
155 		    evthread_cond_fns_.alloc_condition(0) : NULL;	\
156 	} while (0)
157 /** Deallocate and free a condition variable in condvar */
158 #define EVTHREAD_FREE_COND(cond)					\
159 	do {								\
160 		if (cond)						\
161 			evthread_cond_fns_.free_condition((cond));	\
162 	} while (0)
163 /** Signal one thread waiting on cond */
164 #define EVTHREAD_COND_SIGNAL(cond)					\
165 	( (cond) ? evthread_cond_fns_.signal_condition((cond), 0) : 0 )
166 /** Signal all threads waiting on cond */
167 #define EVTHREAD_COND_BROADCAST(cond)					\
168 	( (cond) ? evthread_cond_fns_.signal_condition((cond), 1) : 0 )
169 /** Wait until the condition 'cond' is signalled.  Must be called while
170  * holding 'lock'.  The lock will be released until the condition is
171  * signalled, at which point it will be acquired again.  Returns 0 for
172  * success, -1 for failure. */
173 #define EVTHREAD_COND_WAIT(cond, lock)					\
174 	( (cond) ? evthread_cond_fns_.wait_condition((cond), (lock), NULL) : 0 )
175 /** As EVTHREAD_COND_WAIT, but gives up after 'tv' has elapsed.  Returns 1
176  * on timeout. */
177 #define EVTHREAD_COND_WAIT_TIMED(cond, lock, tv)			\
178 	( (cond) ? evthread_cond_fns_.wait_condition((cond), (lock), (tv)) : 0 )
179 
180 /** True iff locking functions have been configured. */
181 #define EVTHREAD_LOCKING_ENABLED()		\
182 	(evthread_lock_fns_.lock != NULL)
183 
184 #elif ! defined(EVENT__DISABLE_THREAD_SUPPORT)
185 
186 unsigned long evthreadimpl_get_id_(void);
187 int evthreadimpl_is_lock_debugging_enabled_(void);
188 void *evthreadimpl_lock_alloc_(unsigned locktype);
189 void evthreadimpl_lock_free_(void *lock, unsigned locktype);
190 int evthreadimpl_lock_lock_(unsigned mode, void *lock);
191 int evthreadimpl_lock_unlock_(unsigned mode, void *lock);
192 void *evthreadimpl_cond_alloc_(unsigned condtype);
193 void evthreadimpl_cond_free_(void *cond);
194 int evthreadimpl_cond_signal_(void *cond, int broadcast);
195 int evthreadimpl_cond_wait_(void *cond, void *lock, const struct timeval *tv);
196 int evthreadimpl_locking_enabled_(void);
197 
198 #define EVTHREAD_GET_ID() evthreadimpl_get_id_()
199 #define EVBASE_IN_THREAD(base)				\
200 	((base)->th_owner_id == evthreadimpl_get_id_())
201 #define EVBASE_NEED_NOTIFY(base)			 \
202 	((base)->running_loop &&			 \
203 	    ((base)->th_owner_id != evthreadimpl_get_id_()))
204 
205 #define EVTHREAD_ALLOC_LOCK(lockvar, locktype)		\
206 	((lockvar) = evthreadimpl_lock_alloc_(locktype))
207 
208 #define EVTHREAD_FREE_LOCK(lockvar, locktype)				\
209 	do {								\
210 		void *lock_tmp_ = (lockvar);				\
211 		if (lock_tmp_)						\
212 			evthreadimpl_lock_free_(lock_tmp_, (locktype)); \
213 	} while (0)
214 
215 /** Acquire a lock. */
216 #define EVLOCK_LOCK(lockvar,mode)					\
217 	do {								\
218 		if (lockvar)						\
219 			evthreadimpl_lock_lock_(mode, lockvar);		\
220 	} while (0)
221 
222 /** Release a lock */
223 #define EVLOCK_UNLOCK(lockvar,mode)					\
224 	do {								\
225 		if (lockvar)						\
226 			evthreadimpl_lock_unlock_(mode, lockvar);	\
227 	} while (0)
228 
229 /** Lock an event_base, if it is set up for locking.  Acquires the lock
230     in the base structure whose field is named 'lockvar'. */
231 #define EVBASE_ACQUIRE_LOCK(base, lockvar) do {				\
232 		EVLOCK_LOCK((base)->lockvar, 0);			\
233 	} while (0)
234 
235 /** Unlock an event_base, if it is set up for locking. */
236 #define EVBASE_RELEASE_LOCK(base, lockvar) do {				\
237 		EVLOCK_UNLOCK((base)->lockvar, 0);			\
238 	} while (0)
239 
240 /** If lock debugging is enabled, and lock is non-null, assert that 'lock' is
241  * locked and held by us. */
242 #define EVLOCK_ASSERT_LOCKED(lock)					\
243 	do {								\
244 		if ((lock) && evthreadimpl_is_lock_debugging_enabled_()) { \
245 			EVUTIL_ASSERT(evthread_is_debug_lock_held_(lock)); \
246 		}							\
247 	} while (0)
248 
249 /** Try to grab the lock for 'lockvar' without blocking, and return 1 if we
250  * manage to get it. */
251 static inline int EVLOCK_TRY_LOCK_(void *lock);
252 static inline int
253 EVLOCK_TRY_LOCK_(void *lock)
254 {
255 	if (lock) {
256 		int r = evthreadimpl_lock_lock_(EVTHREAD_TRY, lock);
257 		return !r;
258 	} else {
259 		/* Locking is disabled either globally or for this thing;
260 		 * of course we count as having the lock. */
261 		return 1;
262 	}
263 }
264 
265 /** Allocate a new condition variable and store it in the void *, condvar */
266 #define EVTHREAD_ALLOC_COND(condvar)					\
267 	do {								\
268 		(condvar) = evthreadimpl_cond_alloc_(0);		\
269 	} while (0)
270 /** Deallocate and free a condition variable in condvar */
271 #define EVTHREAD_FREE_COND(cond)					\
272 	do {								\
273 		if (cond)						\
274 			evthreadimpl_cond_free_((cond));		\
275 	} while (0)
276 /** Signal one thread waiting on cond */
277 #define EVTHREAD_COND_SIGNAL(cond)					\
278 	( (cond) ? evthreadimpl_cond_signal_((cond), 0) : 0 )
279 /** Signal all threads waiting on cond */
280 #define EVTHREAD_COND_BROADCAST(cond)					\
281 	( (cond) ? evthreadimpl_cond_signal_((cond), 1) : 0 )
282 /** Wait until the condition 'cond' is signalled.  Must be called while
283  * holding 'lock'.  The lock will be released until the condition is
284  * signalled, at which point it will be acquired again.  Returns 0 for
285  * success, -1 for failure. */
286 #define EVTHREAD_COND_WAIT(cond, lock)					\
287 	( (cond) ? evthreadimpl_cond_wait_((cond), (lock), NULL) : 0 )
288 /** As EVTHREAD_COND_WAIT, but gives up after 'tv' has elapsed.  Returns 1
289  * on timeout. */
290 #define EVTHREAD_COND_WAIT_TIMED(cond, lock, tv)			\
291 	( (cond) ? evthreadimpl_cond_wait_((cond), (lock), (tv)) : 0 )
292 
293 #define EVTHREAD_LOCKING_ENABLED()		\
294 	(evthreadimpl_locking_enabled_())
295 
296 #else /* EVENT__DISABLE_THREAD_SUPPORT */
297 
298 #define EVTHREAD_GET_ID()	1
299 #define EVTHREAD_ALLOC_LOCK(lockvar, locktype) EVUTIL_NIL_STMT_
300 #define EVTHREAD_FREE_LOCK(lockvar, locktype) EVUTIL_NIL_STMT_
301 
302 #define EVLOCK_LOCK(lockvar, mode) EVUTIL_NIL_STMT_
303 #define EVLOCK_UNLOCK(lockvar, mode) EVUTIL_NIL_STMT_
304 #define EVLOCK_LOCK2(lock1,lock2,mode1,mode2) EVUTIL_NIL_STMT_
305 #define EVLOCK_UNLOCK2(lock1,lock2,mode1,mode2) EVUTIL_NIL_STMT_
306 
307 #define EVBASE_IN_THREAD(base)	1
308 #define EVBASE_NEED_NOTIFY(base) 0
309 #define EVBASE_ACQUIRE_LOCK(base, lock) EVUTIL_NIL_STMT_
310 #define EVBASE_RELEASE_LOCK(base, lock) EVUTIL_NIL_STMT_
311 #define EVLOCK_ASSERT_LOCKED(lock) EVUTIL_NIL_STMT_
312 
313 #define EVLOCK_TRY_LOCK_(lock) 1
314 
315 #define EVTHREAD_ALLOC_COND(condvar) EVUTIL_NIL_STMT_
316 #define EVTHREAD_FREE_COND(cond) EVUTIL_NIL_STMT_
317 #define EVTHREAD_COND_SIGNAL(cond) EVUTIL_NIL_STMT_
318 #define EVTHREAD_COND_BROADCAST(cond) EVUTIL_NIL_STMT_
319 #define EVTHREAD_COND_WAIT(cond, lock) EVUTIL_NIL_STMT_
320 #define EVTHREAD_COND_WAIT_TIMED(cond, lock, howlong) EVUTIL_NIL_STMT_
321 
322 #define EVTHREAD_LOCKING_ENABLED() 0
323 
324 #endif
325 
326 /* This code is shared between both lock impls */
327 #if ! defined(EVENT__DISABLE_THREAD_SUPPORT)
328 /** Helper: put lockvar1 and lockvar2 into pointerwise ascending order. */
329 #define EVLOCK_SORTLOCKS_(lockvar1, lockvar2)				\
330 	do {								\
331 		if (lockvar1 && lockvar2 && lockvar1 > lockvar2) {	\
332 			void *tmp = lockvar1;				\
333 			lockvar1 = lockvar2;				\
334 			lockvar2 = tmp;					\
335 		}							\
336 	} while (0)
337 
338 /** Acquire both lock1 and lock2.  Always allocates locks in the same order,
339  * so that two threads locking two locks with LOCK2 will not deadlock. */
340 #define EVLOCK_LOCK2(lock1,lock2,mode1,mode2)				\
341 	do {								\
342 		void *lock1_tmplock_ = (lock1);				\
343 		void *lock2_tmplock_ = (lock2);				\
344 		EVLOCK_SORTLOCKS_(lock1_tmplock_,lock2_tmplock_);	\
345 		EVLOCK_LOCK(lock1_tmplock_,mode1);			\
346 		if (lock2_tmplock_ != lock1_tmplock_)			\
347 			EVLOCK_LOCK(lock2_tmplock_,mode2);		\
348 	} while (0)
349 /** Release both lock1 and lock2.  */
350 #define EVLOCK_UNLOCK2(lock1,lock2,mode1,mode2)				\
351 	do {								\
352 		void *lock1_tmplock_ = (lock1);				\
353 		void *lock2_tmplock_ = (lock2);				\
354 		EVLOCK_SORTLOCKS_(lock1_tmplock_,lock2_tmplock_);	\
355 		if (lock2_tmplock_ != lock1_tmplock_)			\
356 			EVLOCK_UNLOCK(lock2_tmplock_,mode2);		\
357 		EVLOCK_UNLOCK(lock1_tmplock_,mode1);			\
358 	} while (0)
359 
360 int evthread_is_debug_lock_held_(void *lock);
361 void *evthread_debug_get_real_lock_(void *lock);
362 
363 void *evthread_setup_global_lock_(void *lock_, unsigned locktype,
364     int enable_locks);
365 
366 #define EVTHREAD_SETUP_GLOBAL_LOCK(lockvar, locktype)			\
367 	do {								\
368 		lockvar = evthread_setup_global_lock_(lockvar,		\
369 		    (locktype), enable_locks);				\
370 		if (!lockvar) {						\
371 			event_warn("Couldn't allocate %s", #lockvar);	\
372 			return -1;					\
373 		}							\
374 	} while (0);
375 
376 int event_global_setup_locks_(const int enable_locks);
377 int evsig_global_setup_locks_(const int enable_locks);
378 int evutil_global_setup_locks_(const int enable_locks);
379 int evutil_secure_rng_global_setup_locks_(const int enable_locks);
380 
381 #endif
382 
383 #ifdef __cplusplus
384 }
385 #endif
386 
387 #endif /* EVTHREAD_INTERNAL_H_INCLUDED_ */
388