Skip to content
Newer
Older
100644 154 lines (130 sloc) 5.6 KB
558de9b @provos Provide OpenSSL style support for multiple threads accessing the same…
provos authored
1 /*
eda27f9 Update copyright notices, add some missing license statements
Nick Mathewson authored
2 * Copyright (c) 2008-2009 Niels Provos, Nick Mathewson
558de9b @provos Provide OpenSSL style support for multiple threads accessing the same…
provos authored
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
838d0a8 Document many internal functions and pieces of code.
Nick Mathewson authored
30 extern "C" {
558de9b @provos Provide OpenSSL style support for multiple threads accessing the same…
provos authored
31 #endif
32
8889a77 Replace all use of config.h with event-config.h.
Nick Mathewson authored
33 #include "event-config.h"
28255a2 Finally, get unit tests to pass with threading turned off again.
Nick Mathewson authored
34 #include "util-internal.h"
558de9b @provos Provide OpenSSL style support for multiple threads accessing the same…
provos authored
35
36 struct event_base;
ec35eb5 Make threading functions global, like the mm_ functions. Use the libe…
Nick Mathewson authored
37
8889a77 Replace all use of config.h with event-config.h.
Nick Mathewson authored
38 #ifndef _EVENT_DISABLE_THREAD_SUPPORT
838d0a8 Document many internal functions and pieces of code.
Nick Mathewson authored
39 /* Global function pointers to lock-related functions. NULL if locking isn't
40 enabled. */
347952f Revise the locking API: deprecate the old locking callbacks and add t…
Nick Mathewson authored
41 extern struct evthread_lock_callbacks _evthread_lock_fns;
ec35eb5 Make threading functions global, like the mm_ functions. Use the libe…
Nick Mathewson authored
42 extern unsigned long (*_evthread_id_fn)(void);
43
838d0a8 Document many internal functions and pieces of code.
Nick Mathewson authored
44 /** True iff the given event_base is set up to use locking */
ec35eb5 Make threading functions global, like the mm_ functions. Use the libe…
Nick Mathewson authored
45 #define EVBASE_USING_LOCKS(base) \
46 (base != NULL && (base)->th_base_lock != NULL)
47
838d0a8 Document many internal functions and pieces of code.
Nick Mathewson authored
48 /** Return the ID of the current thread, or 1 if threading isn't enabled. */
ec35eb5 Make threading functions global, like the mm_ functions. Use the libe…
Nick Mathewson authored
49 #define EVTHREAD_GET_ID() \
50 (_evthread_id_fn ? _evthread_id_fn() : 1)
558de9b @provos Provide OpenSSL style support for multiple threads accessing the same…
provos authored
51
838d0a8 Document many internal functions and pieces of code.
Nick Mathewson authored
52 /** Return true iff we're in the thread that is currently running a given
53 * event_base's loop. */
ec35eb5 Make threading functions global, like the mm_ functions. Use the libe…
Nick Mathewson authored
54 #define EVBASE_IN_THREAD(base) \
55 (_evthread_id_fn == NULL || \
56 (base)->th_owner_id == _evthread_id_fn())
558de9b @provos Provide OpenSSL style support for multiple threads accessing the same…
provos authored
57
838d0a8 Document many internal functions and pieces of code.
Nick Mathewson authored
58 /** Allocate a new lock, and store it in lockvar, a void*. Sets lockvar to
59 NULL if locking is not enabled. */
347952f Revise the locking API: deprecate the old locking callbacks and add t…
Nick Mathewson authored
60 #define EVTHREAD_ALLOC_LOCK(lockvar, locktype) \
61 ((lockvar) = _evthread_lock_fns.alloc ? \
62 _evthread_lock_fns.alloc(locktype) : NULL)
558de9b @provos Provide OpenSSL style support for multiple threads accessing the same…
provos authored
63
838d0a8 Document many internal functions and pieces of code.
Nick Mathewson authored
64 /** Free a given lock, if it is present and locking is enabled. */
347952f Revise the locking API: deprecate the old locking callbacks and add t…
Nick Mathewson authored
65 #define EVTHREAD_FREE_LOCK(lockvar, locktype) \
66 do { \
67 void *_lock_tmp_ = (lockvar); \
68 if (_lock_tmp_ && _evthread_lock_fns.free) \
69 _evthread_lock_fns.free(_lock_tmp_, (locktype)); \
70ee390 Add some more utility macros to evthread-internal.h
Nick Mathewson authored
70 } while (0)
71
838d0a8 Document many internal functions and pieces of code.
Nick Mathewson authored
72 /** Acquire a lock. */
347952f Revise the locking API: deprecate the old locking callbacks and add t…
Nick Mathewson authored
73 #define EVLOCK_LOCK(lockvar,mode) \
70ee390 Add some more utility macros to evthread-internal.h
Nick Mathewson authored
74 do { \
347952f Revise the locking API: deprecate the old locking callbacks and add t…
Nick Mathewson authored
75 if (lockvar) \
76 _evthread_lock_fns.lock(mode, lockvar); \
70ee390 Add some more utility macros to evthread-internal.h
Nick Mathewson authored
77 } while (0)
838d0a8 Document many internal functions and pieces of code.
Nick Mathewson authored
78
79 /** Release a lock */
347952f Revise the locking API: deprecate the old locking callbacks and add t…
Nick Mathewson authored
80 #define EVLOCK_UNLOCK(lockvar,mode) \
70ee390 Add some more utility macros to evthread-internal.h
Nick Mathewson authored
81 do { \
347952f Revise the locking API: deprecate the old locking callbacks and add t…
Nick Mathewson authored
82 if (lockvar) \
83 _evthread_lock_fns.unlock(mode, lockvar); \
70ee390 Add some more utility macros to evthread-internal.h
Nick Mathewson authored
84 } while (0)
838d0a8 Document many internal functions and pieces of code.
Nick Mathewson authored
85
86 /** Helper: put lockvar1 and lockvar2 into pointerwise ascending order. */
70ee390 Add some more utility macros to evthread-internal.h
Nick Mathewson authored
87 #define _EVLOCK_SORTLOCKS(lockvar1, lockvar2) \
88 do { \
89 if (lockvar1 && lockvar2 && lockvar1 > lockvar2) { \
90 void *tmp = lockvar1; \
91 lockvar1 = lockvar2; \
92 lockvar2 = tmp; \
93 } \
94 } while (0)
95
838d0a8 Document many internal functions and pieces of code.
Nick Mathewson authored
96 /** Acquire both lock1 and lock2. Always allocates locks in the same order,
97 * so that two threads locking two locks with LOCK2 will not deadlock. */
70ee390 Add some more utility macros to evthread-internal.h
Nick Mathewson authored
98 #define EVLOCK_LOCK2(lock1,lock2,mode1,mode2) \
99 do { \
100 void *_lock1_tmplock = (lock1); \
101 void *_lock2_tmplock = (lock2); \
102 _EVLOCK_SORTLOCKS(_lock1_tmplock,_lock2_tmplock); \
103 EVLOCK_LOCK(_lock1_tmplock,mode1); \
104 if (_lock2_tmplock != _lock1_tmplock) \
105 EVLOCK_LOCK(_lock2_tmplock,mode2); \
106 } while (0)
107
e3fd294 Spelling fixes in comments and strings.
Nick Mathewson authored
108 /** Release both lock1 and lock2. */
70ee390 Add some more utility macros to evthread-internal.h
Nick Mathewson authored
109 #define EVLOCK_UNLOCK2(lock1,lock2,mode1,mode2) \
110 do { \
111 void *_lock1_tmplock = (lock1); \
112 void *_lock2_tmplock = (lock2); \
113 _EVLOCK_SORTLOCKS(_lock1_tmplock,_lock2_tmplock); \
114 if (_lock2_tmplock != _lock1_tmplock) \
115 EVLOCK_UNLOCK(_lock2_tmplock,mode2); \
116 EVLOCK_UNLOCK(_lock1_tmplock,mode1); \
117 } while (0)
118
ec35eb5 Make threading functions global, like the mm_ functions. Use the libe…
Nick Mathewson authored
119
838d0a8 Document many internal functions and pieces of code.
Nick Mathewson authored
120 /** Lock an event_base, if it is set up for locking. Acquires the lock
347952f Revise the locking API: deprecate the old locking callbacks and add t…
Nick Mathewson authored
121 in the base structure whose field is named 'lck'. */
122 #define EVBASE_ACQUIRE_LOCK(base, mode, lockvar) do { \
ec35eb5 Make threading functions global, like the mm_ functions. Use the libe…
Nick Mathewson authored
123 if (EVBASE_USING_LOCKS(base)) \
347952f Revise the locking API: deprecate the old locking callbacks and add t…
Nick Mathewson authored
124 _evthread_lock_fns.lock(mode, (base)->lockvar); \
558de9b @provos Provide OpenSSL style support for multiple threads accessing the same…
provos authored
125 } while (0)
126
838d0a8 Document many internal functions and pieces of code.
Nick Mathewson authored
127 /** Unlock an event_base, if it is set up for locking. */
347952f Revise the locking API: deprecate the old locking callbacks and add t…
Nick Mathewson authored
128 #define EVBASE_RELEASE_LOCK(base, mode, lockvar) do { \
ec35eb5 Make threading functions global, like the mm_ functions. Use the libe…
Nick Mathewson authored
129 if (EVBASE_USING_LOCKS(base)) \
347952f Revise the locking API: deprecate the old locking callbacks and add t…
Nick Mathewson authored
130 _evthread_lock_fns.unlock(mode, (base)->lockvar); \
558de9b @provos Provide OpenSSL style support for multiple threads accessing the same…
provos authored
131 } while (0)
8889a77 Replace all use of config.h with event-config.h.
Nick Mathewson authored
132 #else /* _EVENT_DISABLE_THREAD_SUPPORT */
d13b59c Fix build with thread support disabled, and make no-op macros a littl…
Nick Mathewson authored
133
ec35eb5 Make threading functions global, like the mm_ functions. Use the libe…
Nick Mathewson authored
134 #define EVTHREAD_GET_ID() 1
347952f Revise the locking API: deprecate the old locking callbacks and add t…
Nick Mathewson authored
135 #define EVTHREAD_ALLOC_LOCK(lockvar, locktype) _EVUTIL_NIL_STMT
136 #define EVTHREAD_FREE_LOCK(lockvar, locktype) _EVUTIL_NIL_STMT
d13b59c Fix build with thread support disabled, and make no-op macros a littl…
Nick Mathewson authored
137
661b5ee Actually, move EVUTIL_NIL_STMT to util-internal.h
Nick Mathewson authored
138 #define EVLOCK_LOCK(lockvar, mode) _EVUTIL_NIL_STMT
139 #define EVLOCK_UNLOCK(lockvar, mode) _EVUTIL_NIL_STMT
140 #define EVLOCK_LOCK2(lock1,lock2,mode1,mode2) _EVUTIL_NIL_STMT
141 #define EVLOCK_UNLOCK2(lock1,lock2,mode1,mode2) _EVUTIL_NIL_STMT
d13b59c Fix build with thread support disabled, and make no-op macros a littl…
Nick Mathewson authored
142
143 #define EVBASE_IN_THREAD(base) 1
661b5ee Actually, move EVUTIL_NIL_STMT to util-internal.h
Nick Mathewson authored
144 #define EVBASE_ACQUIRE_LOCK(base, mode, lock) _EVUTIL_NIL_STMT
145 #define EVBASE_RELEASE_LOCK(base, mode, lock) _EVUTIL_NIL_STMT
ec35eb5 Make threading functions global, like the mm_ functions. Use the libe…
Nick Mathewson authored
146
558de9b @provos Provide OpenSSL style support for multiple threads accessing the same…
provos authored
147 #endif
148
149 #ifdef __cplusplus
150 }
151 #endif
152
153 #endif /* _EVTHREAD_INTERNAL_H_ */
Something went wrong with that request. Please try again.