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