OpenMPI 0.1.1

- Main Page
- Related Pages
- Modules
- Data Structures
- Files

- File List
- Globals

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