1 /* $NetBSD: evthread-internal.h,v 1.6 2021/04/10 19:18:45 rillig 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 #if !defined(_WIN32) && !defined(__CYGWIN__)
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 EVENT2_EXPORT_SYMBOL
55 extern struct evthread_lock_callbacks evthread_lock_fns_;
56 EVENT2_EXPORT_SYMBOL
57 extern struct evthread_condition_callbacks evthread_cond_fns_;
58 extern unsigned long (*evthread_id_fn_)(void);
59 EVENT2_EXPORT_SYMBOL
60 extern int evthread_lock_debugging_enabled_;
61
62 /** Return the ID of the current thread, or 1 if threading isn't enabled. */
63 #define EVTHREAD_GET_ID() \
64 (evthread_id_fn_ ? evthread_id_fn_() : 1)
65
66 /** Return true iff we're in the thread that is currently (or most recently)
67 * running a given event_base's loop. Requires lock. */
68 #define EVBASE_IN_THREAD(base) \
69 (evthread_id_fn_ == NULL || \
70 (base)->th_owner_id == evthread_id_fn_())
71
72 /** Return true iff we need to notify the base's main thread about changes to
73 * its state, because it's currently running the main loop in another
74 * thread. Requires lock. */
75 #define EVBASE_NEED_NOTIFY(base) \
76 (evthread_id_fn_ != NULL && \
77 (base)->running_loop && \
78 (base)->th_owner_id != evthread_id_fn_())
79
80 /** Allocate a new lock, and store it in lockvar, a void*. Sets lockvar to
81 NULL if locking is not enabled. */
82 #define EVTHREAD_ALLOC_LOCK(lockvar, locktype) \
83 ((lockvar) = evthread_lock_fns_.alloc ? \
84 evthread_lock_fns_.alloc(locktype) : NULL)
85
86 /** Free a given lock, if it is present and locking is enabled. */
87 #define EVTHREAD_FREE_LOCK(lockvar, locktype) \
88 do { \
89 void *lock_tmp_ = (lockvar); \
90 if (lock_tmp_ && evthread_lock_fns_.free) \
91 evthread_lock_fns_.free(lock_tmp_, (locktype)); \
92 } while (0)
93
94 /** Acquire a lock. */
95 #define EVLOCK_LOCK(lockvar,mode) \
96 do { \
97 if (lockvar) \
98 evthread_lock_fns_.lock(mode, lockvar); \
99 } while (0)
100
101 /** Release a lock */
102 #define EVLOCK_UNLOCK(lockvar,mode) \
103 do { \
104 if (lockvar) \
105 evthread_lock_fns_.unlock(mode, lockvar); \
106 } while (0)
107
108 /** Helper: put lockvar1 and lockvar2 into pointerwise ascending order. */
109 #define EVLOCK_SORTLOCKS_(lockvar1, lockvar2) \
110 do { \
111 if (lockvar1 && lockvar2 && lockvar1 > lockvar2) { \
112 void *tmp = lockvar1; \
113 lockvar1 = lockvar2; \
114 lockvar2 = tmp; \
115 } \
116 } while (0)
117
118 /** Lock an event_base, if it is set up for locking. Acquires the lock
119 in the base structure whose field is named 'lockvar'. */
120 #define EVBASE_ACQUIRE_LOCK(base, lockvar) do { \
121 EVLOCK_LOCK((base)->lockvar, 0); \
122 } while (0)
123
124 /** Unlock an event_base, if it is set up for locking. */
125 #define EVBASE_RELEASE_LOCK(base, lockvar) do { \
126 EVLOCK_UNLOCK((base)->lockvar, 0); \
127 } while (0)
128
129 /** If lock debugging is enabled, and lock is non-null, assert that 'lock' is
130 * locked and held by us. */
131 #define EVLOCK_ASSERT_LOCKED(lock) \
132 do { \
133 if ((lock) && evthread_lock_debugging_enabled_) { \
134 EVUTIL_ASSERT(evthread_is_debug_lock_held_(lock)); \
135 } \
136 } while (0)
137
138 /** Try to grab the lock for 'lockvar' without blocking, and return 1 if we
139 * manage to get it. */
140 static inline int EVLOCK_TRY_LOCK_(void *lock);
141 static inline int
EVLOCK_TRY_LOCK_(void * lock)142 EVLOCK_TRY_LOCK_(void *lock)
143 {
144 if (lock && evthread_lock_fns_.lock) {
145 int r = evthread_lock_fns_.lock(EVTHREAD_TRY, lock);
146 return !r;
147 } else {
148 /* Locking is disabled either globally or for this thing;
149 * of course we count as having the lock. */
150 return 1;
151 }
152 }
153
154 /** Allocate a new condition variable and store it in the void *, condvar */
155 #define EVTHREAD_ALLOC_COND(condvar) \
156 do { \
157 (condvar) = evthread_cond_fns_.alloc_condition ? \
158 evthread_cond_fns_.alloc_condition(0) : NULL; \
159 } while (0)
160 /** Deallocate and free a condition variable in condvar */
161 #define EVTHREAD_FREE_COND(cond) \
162 do { \
163 if (cond) \
164 evthread_cond_fns_.free_condition((cond)); \
165 } while (0)
166 /** Signal one thread waiting on cond */
167 #define EVTHREAD_COND_SIGNAL(cond) \
168 ( (cond) ? evthread_cond_fns_.signal_condition((cond), 0) : 0 )
169 /** Signal all threads waiting on cond */
170 #define EVTHREAD_COND_BROADCAST(cond) \
171 ( (cond) ? evthread_cond_fns_.signal_condition((cond), 1) : 0 )
172 /** Wait until the condition 'cond' is signalled. Must be called while
173 * holding 'lock'. The lock will be released until the condition is
174 * signalled, at which point it will be acquired again. Returns 0 for
175 * success, -1 for failure. */
176 #define EVTHREAD_COND_WAIT(cond, lock) \
177 ( (cond) ? evthread_cond_fns_.wait_condition((cond), (lock), NULL) : 0 )
178 /** As EVTHREAD_COND_WAIT, but gives up after 'tv' has elapsed. Returns 1
179 * on timeout. */
180 #define EVTHREAD_COND_WAIT_TIMED(cond, lock, tv) \
181 ( (cond) ? evthread_cond_fns_.wait_condition((cond), (lock), (tv)) : 0 )
182
183 /** True iff locking functions have been configured. */
184 #define EVTHREAD_LOCKING_ENABLED() \
185 (evthread_lock_fns_.lock != NULL)
186
187 #elif ! defined(EVENT__DISABLE_THREAD_SUPPORT)
188
189 unsigned long evthreadimpl_get_id_(void);
190 EVENT2_EXPORT_SYMBOL
191 int evthreadimpl_is_lock_debugging_enabled_(void);
192 EVENT2_EXPORT_SYMBOL
193 void *evthreadimpl_lock_alloc_(unsigned locktype);
194 EVENT2_EXPORT_SYMBOL
195 void evthreadimpl_lock_free_(void *lock, unsigned locktype);
196 EVENT2_EXPORT_SYMBOL
197 int evthreadimpl_lock_lock_(unsigned mode, void *lock);
198 EVENT2_EXPORT_SYMBOL
199 int evthreadimpl_lock_unlock_(unsigned mode, void *lock);
200 EVENT2_EXPORT_SYMBOL
201 void *evthreadimpl_cond_alloc_(unsigned condtype);
202 EVENT2_EXPORT_SYMBOL
203 void evthreadimpl_cond_free_(void *cond);
204 EVENT2_EXPORT_SYMBOL
205 int evthreadimpl_cond_signal_(void *cond, int broadcast);
206 EVENT2_EXPORT_SYMBOL
207 int evthreadimpl_cond_wait_(void *cond, void *lock, const struct timeval *tv);
208 int evthreadimpl_locking_enabled_(void);
209
210 #define EVTHREAD_GET_ID() evthreadimpl_get_id_()
211 #define EVBASE_IN_THREAD(base) \
212 ((base)->th_owner_id == evthreadimpl_get_id_())
213 #define EVBASE_NEED_NOTIFY(base) \
214 ((base)->running_loop && \
215 ((base)->th_owner_id != evthreadimpl_get_id_()))
216
217 #define EVTHREAD_ALLOC_LOCK(lockvar, locktype) \
218 ((lockvar) = evthreadimpl_lock_alloc_(locktype))
219
220 #define EVTHREAD_FREE_LOCK(lockvar, locktype) \
221 do { \
222 void *lock_tmp_ = (lockvar); \
223 if (lock_tmp_) \
224 evthreadimpl_lock_free_(lock_tmp_, (locktype)); \
225 } while (0)
226
227 /** Acquire a lock. */
228 #define EVLOCK_LOCK(lockvar,mode) \
229 do { \
230 if (lockvar) \
231 evthreadimpl_lock_lock_(mode, lockvar); \
232 } while (0)
233
234 /** Release a lock */
235 #define EVLOCK_UNLOCK(lockvar,mode) \
236 do { \
237 if (lockvar) \
238 evthreadimpl_lock_unlock_(mode, lockvar); \
239 } while (0)
240
241 /** Lock an event_base, if it is set up for locking. Acquires the lock
242 in the base structure whose field is named 'lockvar'. */
243 #define EVBASE_ACQUIRE_LOCK(base, lockvar) do { \
244 EVLOCK_LOCK((base)->lockvar, 0); \
245 } while (0)
246
247 /** Unlock an event_base, if it is set up for locking. */
248 #define EVBASE_RELEASE_LOCK(base, lockvar) do { \
249 EVLOCK_UNLOCK((base)->lockvar, 0); \
250 } while (0)
251
252 /** If lock debugging is enabled, and lock is non-null, assert that 'lock' is
253 * locked and held by us. */
254 #define EVLOCK_ASSERT_LOCKED(lock) \
255 do { \
256 if ((lock) && evthreadimpl_is_lock_debugging_enabled_()) { \
257 EVUTIL_ASSERT(evthread_is_debug_lock_held_(lock)); \
258 } \
259 } while (0)
260
261 /** Try to grab the lock for 'lockvar' without blocking, and return 1 if we
262 * manage to get it. */
263 static inline int EVLOCK_TRY_LOCK_(void *lock);
264 static inline int
EVLOCK_TRY_LOCK_(void * lock)265 EVLOCK_TRY_LOCK_(void *lock)
266 {
267 if (lock) {
268 int r = evthreadimpl_lock_lock_(EVTHREAD_TRY, lock);
269 return !r;
270 } else {
271 /* Locking is disabled either globally or for this thing;
272 * of course we count as having the lock. */
273 return 1;
274 }
275 }
276
277 /** Allocate a new condition variable and store it in the void *, condvar */
278 #define EVTHREAD_ALLOC_COND(condvar) \
279 do { \
280 (condvar) = evthreadimpl_cond_alloc_(0); \
281 } while (0)
282 /** Deallocate and free a condition variable in condvar */
283 #define EVTHREAD_FREE_COND(cond) \
284 do { \
285 if (cond) \
286 evthreadimpl_cond_free_((cond)); \
287 } while (0)
288 /** Signal one thread waiting on cond */
289 #define EVTHREAD_COND_SIGNAL(cond) \
290 ( (cond) ? evthreadimpl_cond_signal_((cond), 0) : 0 )
291 /** Signal all threads waiting on cond */
292 #define EVTHREAD_COND_BROADCAST(cond) \
293 ( (cond) ? evthreadimpl_cond_signal_((cond), 1) : 0 )
294 /** Wait until the condition 'cond' is signalled. Must be called while
295 * holding 'lock'. The lock will be released until the condition is
296 * signalled, at which point it will be acquired again. Returns 0 for
297 * success, -1 for failure. */
298 #define EVTHREAD_COND_WAIT(cond, lock) \
299 ( (cond) ? evthreadimpl_cond_wait_((cond), (lock), NULL) : 0 )
300 /** As EVTHREAD_COND_WAIT, but gives up after 'tv' has elapsed. Returns 1
301 * on timeout. */
302 #define EVTHREAD_COND_WAIT_TIMED(cond, lock, tv) \
303 ( (cond) ? evthreadimpl_cond_wait_((cond), (lock), (tv)) : 0 )
304
305 #define EVTHREAD_LOCKING_ENABLED() \
306 (evthreadimpl_locking_enabled_())
307
308 #else /* EVENT__DISABLE_THREAD_SUPPORT */
309
310 #define EVTHREAD_GET_ID() 1
311 #define EVTHREAD_ALLOC_LOCK(lockvar, locktype) EVUTIL_NIL_STMT_
312 #define EVTHREAD_FREE_LOCK(lockvar, locktype) EVUTIL_NIL_STMT_
313
314 #define EVLOCK_LOCK(lockvar, mode) EVUTIL_NIL_STMT_
315 #define EVLOCK_UNLOCK(lockvar, mode) EVUTIL_NIL_STMT_
316 #define EVLOCK_LOCK2(lock1,lock2,mode1,mode2) EVUTIL_NIL_STMT_
317 #define EVLOCK_UNLOCK2(lock1,lock2,mode1,mode2) EVUTIL_NIL_STMT_
318
319 #define EVBASE_IN_THREAD(base) 1
320 #define EVBASE_NEED_NOTIFY(base) 0
321 #define EVBASE_ACQUIRE_LOCK(base, lock) EVUTIL_NIL_STMT_
322 #define EVBASE_RELEASE_LOCK(base, lock) EVUTIL_NIL_STMT_
323 #define EVLOCK_ASSERT_LOCKED(lock) EVUTIL_NIL_STMT_
324
325 #define EVLOCK_TRY_LOCK_(lock) 1
326
327 #define EVTHREAD_ALLOC_COND(condvar) EVUTIL_NIL_STMT_
328 #define EVTHREAD_FREE_COND(cond) EVUTIL_NIL_STMT_
329 #define EVTHREAD_COND_SIGNAL(cond) EVUTIL_NIL_STMT_
330 #define EVTHREAD_COND_BROADCAST(cond) EVUTIL_NIL_STMT_
331 #define EVTHREAD_COND_WAIT(cond, lock) EVUTIL_NIL_STMT_
332 #define EVTHREAD_COND_WAIT_TIMED(cond, lock, howlong) EVUTIL_NIL_STMT_
333
334 #define EVTHREAD_LOCKING_ENABLED() 0
335
336 #endif
337
338 /* This code is shared between both lock impls */
339 #if ! defined(EVENT__DISABLE_THREAD_SUPPORT)
340 /** Helper: put lockvar1 and lockvar2 into pointerwise ascending order. */
341 #define EVLOCK_SORTLOCKS_(lockvar1, lockvar2) \
342 do { \
343 if (lockvar1 && lockvar2 && lockvar1 > lockvar2) { \
344 void *tmp = lockvar1; \
345 lockvar1 = lockvar2; \
346 lockvar2 = tmp; \
347 } \
348 } while (0)
349
350 /** Acquire both lock1 and lock2. Always allocates locks in the same order,
351 * so that two threads locking two locks with LOCK2 will not deadlock. */
352 #define EVLOCK_LOCK2(lock1,lock2,mode1,mode2) \
353 do { \
354 void *lock1_tmplock_ = (lock1); \
355 void *lock2_tmplock_ = (lock2); \
356 EVLOCK_SORTLOCKS_(lock1_tmplock_,lock2_tmplock_); \
357 EVLOCK_LOCK(lock1_tmplock_,mode1); \
358 if (lock2_tmplock_ != lock1_tmplock_) \
359 EVLOCK_LOCK(lock2_tmplock_,mode2); \
360 } while (0)
361 /** Release both lock1 and lock2. */
362 #define EVLOCK_UNLOCK2(lock1,lock2,mode1,mode2) \
363 do { \
364 void *lock1_tmplock_ = (lock1); \
365 void *lock2_tmplock_ = (lock2); \
366 EVLOCK_SORTLOCKS_(lock1_tmplock_,lock2_tmplock_); \
367 if (lock2_tmplock_ != lock1_tmplock_) \
368 EVLOCK_UNLOCK(lock2_tmplock_,mode2); \
369 EVLOCK_UNLOCK(lock1_tmplock_,mode1); \
370 } while (0)
371
372 EVENT2_EXPORT_SYMBOL
373 int evthread_is_debug_lock_held_(void *lock);
374 void *evthread_debug_get_real_lock_(void *lock);
375
376 void *evthread_setup_global_lock_(void *lock_, unsigned locktype,
377 int enable_locks);
378
379 #define EVTHREAD_SETUP_GLOBAL_LOCK(lockvar, locktype) \
380 do { \
381 lockvar = evthread_setup_global_lock_(lockvar, \
382 (locktype), enable_locks); \
383 if (!lockvar) { \
384 event_warn("Couldn't allocate %s", #lockvar); \
385 return -1; \
386 } \
387 } while (0);
388
389 int event_global_setup_locks_(const int enable_locks);
390 int evsig_global_setup_locks_(const int enable_locks);
391 int evutil_global_setup_locks_(const int enable_locks);
392 int evutil_secure_rng_global_setup_locks_(const int enable_locks);
393
394 /** Return current evthread_lock_callbacks */
395 EVENT2_EXPORT_SYMBOL
396 struct evthread_lock_callbacks *evthread_get_lock_callbacks(void);
397 /** Return current evthread_condition_callbacks */
398 struct evthread_condition_callbacks *evthread_get_condition_callbacks(void);
399 /** Disable locking for internal usage (like global shutdown) */
400 void evthreadimpl_disable_lock_debugging_(void);
401
402 #endif
403
404 #ifdef __cplusplus
405 }
406 #endif
407
408 #endif /* EVTHREAD_INTERNAL_H_INCLUDED_ */
409