Skip to content
Newer
Older
100644 186 lines (158 sloc) 6.5 KB
558de9b @provos Provide OpenSSL style support for multiple threads accessing the same…
provos authored Mar 2, 2008
1 /*
eda27f9 Update copyright notices, add some missing license statements
Nick Mathewson authored Apr 19, 2009
2 * Copyright (c) 2008-2009 Niels Provos, Nick Mathewson
558de9b @provos Provide OpenSSL style support for multiple threads accessing the same…
provos authored Mar 2, 2008
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 Apr 17, 2009
30 extern "C" {
558de9b @provos Provide OpenSSL style support for multiple threads accessing the same…
provos authored Mar 2, 2008
31 #endif
32
689fc09 New EVTHREAD_TRY_LOCK function to try to grab a lock.
Nick Mathewson authored Nov 28, 2009
33 #include <event2/thread.h>
8889a77 Replace all use of config.h with event-config.h.
Nick Mathewson authored Jan 27, 2009
34 #include "event-config.h"
28255a2 Finally, get unit tests to pass with threading turned off again.
Nick Mathewson authored Apr 5, 2009
35 #include "util-internal.h"
558de9b @provos Provide OpenSSL style support for multiple threads accessing the same…
provos authored Mar 2, 2008
36
37 struct event_base;
ec35eb5 Make threading functions global, like the mm_ functions. Use the libe…
Nick Mathewson authored Feb 12, 2009
38
8889a77 Replace all use of config.h with event-config.h.
Nick Mathewson authored Jan 27, 2009
39 #ifndef _EVENT_DISABLE_THREAD_SUPPORT
838d0a8 Document many internal functions and pieces of code.
Nick Mathewson authored Apr 17, 2009
40 /* Global function pointers to lock-related functions. NULL if locking isn't
41 enabled. */
347952f Revise the locking API: deprecate the old locking callbacks and add t…
Nick Mathewson authored Nov 27, 2009
42 extern struct evthread_lock_callbacks _evthread_lock_fns;
ec35eb5 Make threading functions global, like the mm_ functions. Use the libe…
Nick Mathewson authored Feb 12, 2009
43 extern unsigned long (*_evthread_id_fn)(void);
0cd3bb9 Improved optional lock debugging.
Nick Mathewson authored Nov 27, 2009
44 extern int _evthread_lock_debugging_enabled;
ec35eb5 Make threading functions global, like the mm_ functions. Use the libe…
Nick Mathewson authored Feb 12, 2009
45
838d0a8 Document many internal functions and pieces of code.
Nick Mathewson authored Apr 17, 2009
46 /** 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 Feb 12, 2009
47 #define EVBASE_USING_LOCKS(base) \
48 (base != NULL && (base)->th_base_lock != NULL)
49
838d0a8 Document many internal functions and pieces of code.
Nick Mathewson authored Apr 17, 2009
50 /** 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 Feb 12, 2009
51 #define EVTHREAD_GET_ID() \
52 (_evthread_id_fn ? _evthread_id_fn() : 1)
558de9b @provos Provide OpenSSL style support for multiple threads accessing the same…
provos authored Mar 2, 2008
53
838d0a8 Document many internal functions and pieces of code.
Nick Mathewson authored Apr 17, 2009
54 /** Return true iff we're in the thread that is currently running a given
55 * event_base's loop. */
ec35eb5 Make threading functions global, like the mm_ functions. Use the libe…
Nick Mathewson authored Feb 12, 2009
56 #define EVBASE_IN_THREAD(base) \
57 (_evthread_id_fn == NULL || \
58 (base)->th_owner_id == _evthread_id_fn())
558de9b @provos Provide OpenSSL style support for multiple threads accessing the same…
provos authored Mar 2, 2008
59
838d0a8 Document many internal functions and pieces of code.
Nick Mathewson authored Apr 17, 2009
60 /** Allocate a new lock, and store it in lockvar, a void*. Sets lockvar to
61 NULL if locking is not enabled. */
347952f Revise the locking API: deprecate the old locking callbacks and add t…
Nick Mathewson authored Nov 27, 2009
62 #define EVTHREAD_ALLOC_LOCK(lockvar, locktype) \
63 ((lockvar) = _evthread_lock_fns.alloc ? \
64 _evthread_lock_fns.alloc(locktype) : NULL)
558de9b @provos Provide OpenSSL style support for multiple threads accessing the same…
provos authored Mar 2, 2008
65
838d0a8 Document many internal functions and pieces of code.
Nick Mathewson authored Apr 17, 2009
66 /** 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 Nov 27, 2009
67 #define EVTHREAD_FREE_LOCK(lockvar, locktype) \
68 do { \
69 void *_lock_tmp_ = (lockvar); \
70 if (_lock_tmp_ && _evthread_lock_fns.free) \
71 _evthread_lock_fns.free(_lock_tmp_, (locktype)); \
70ee390 Add some more utility macros to evthread-internal.h
Nick Mathewson authored Apr 5, 2009
72 } while (0)
73
838d0a8 Document many internal functions and pieces of code.
Nick Mathewson authored Apr 17, 2009
74 /** Acquire a lock. */
347952f Revise the locking API: deprecate the old locking callbacks and add t…
Nick Mathewson authored Nov 27, 2009
75 #define EVLOCK_LOCK(lockvar,mode) \
70ee390 Add some more utility macros to evthread-internal.h
Nick Mathewson authored Apr 5, 2009
76 do { \
347952f Revise the locking API: deprecate the old locking callbacks and add t…
Nick Mathewson authored Nov 27, 2009
77 if (lockvar) \
78 _evthread_lock_fns.lock(mode, lockvar); \
70ee390 Add some more utility macros to evthread-internal.h
Nick Mathewson authored Apr 5, 2009
79 } while (0)
838d0a8 Document many internal functions and pieces of code.
Nick Mathewson authored Apr 17, 2009
80
81 /** Release a lock */
347952f Revise the locking API: deprecate the old locking callbacks and add t…
Nick Mathewson authored Nov 27, 2009
82 #define EVLOCK_UNLOCK(lockvar,mode) \
70ee390 Add some more utility macros to evthread-internal.h
Nick Mathewson authored Apr 5, 2009
83 do { \
347952f Revise the locking API: deprecate the old locking callbacks and add t…
Nick Mathewson authored Nov 27, 2009
84 if (lockvar) \
85 _evthread_lock_fns.unlock(mode, lockvar); \
70ee390 Add some more utility macros to evthread-internal.h
Nick Mathewson authored Apr 5, 2009
86 } while (0)
838d0a8 Document many internal functions and pieces of code.
Nick Mathewson authored Apr 17, 2009
87
88 /** Helper: put lockvar1 and lockvar2 into pointerwise ascending order. */
70ee390 Add some more utility macros to evthread-internal.h
Nick Mathewson authored Apr 5, 2009
89 #define _EVLOCK_SORTLOCKS(lockvar1, lockvar2) \
90 do { \
91 if (lockvar1 && lockvar2 && lockvar1 > lockvar2) { \
92 void *tmp = lockvar1; \
93 lockvar1 = lockvar2; \
94 lockvar2 = tmp; \
95 } \
96 } while (0)
97
838d0a8 Document many internal functions and pieces of code.
Nick Mathewson authored Apr 17, 2009
98 /** Acquire both lock1 and lock2. Always allocates locks in the same order,
99 * 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 Apr 5, 2009
100 #define EVLOCK_LOCK2(lock1,lock2,mode1,mode2) \
101 do { \
102 void *_lock1_tmplock = (lock1); \
103 void *_lock2_tmplock = (lock2); \
104 _EVLOCK_SORTLOCKS(_lock1_tmplock,_lock2_tmplock); \
105 EVLOCK_LOCK(_lock1_tmplock,mode1); \
106 if (_lock2_tmplock != _lock1_tmplock) \
107 EVLOCK_LOCK(_lock2_tmplock,mode2); \
108 } while (0)
109
e3fd294 Spelling fixes in comments and strings.
Nick Mathewson authored Oct 16, 2009
110 /** Release both lock1 and lock2. */
70ee390 Add some more utility macros to evthread-internal.h
Nick Mathewson authored Apr 5, 2009
111 #define EVLOCK_UNLOCK2(lock1,lock2,mode1,mode2) \
112 do { \
113 void *_lock1_tmplock = (lock1); \
114 void *_lock2_tmplock = (lock2); \
115 _EVLOCK_SORTLOCKS(_lock1_tmplock,_lock2_tmplock); \
116 if (_lock2_tmplock != _lock1_tmplock) \
117 EVLOCK_UNLOCK(_lock2_tmplock,mode2); \
118 EVLOCK_UNLOCK(_lock1_tmplock,mode1); \
119 } while (0)
120
ec35eb5 Make threading functions global, like the mm_ functions. Use the libe…
Nick Mathewson authored Feb 12, 2009
121
838d0a8 Document many internal functions and pieces of code.
Nick Mathewson authored Apr 17, 2009
122 /** Lock an event_base, if it is set up for locking. Acquires the lock
76cd2b7 Stop passing EVTHREAD_READ and EVTHREAD_WRITE to non-rw locks.
Nick Mathewson authored Nov 27, 2009
123 in the base structure whose field is named 'lockvar'. */
124 #define EVBASE_ACQUIRE_LOCK(base, lockvar) do { \
ec35eb5 Make threading functions global, like the mm_ functions. Use the libe…
Nick Mathewson authored Feb 12, 2009
125 if (EVBASE_USING_LOCKS(base)) \
76cd2b7 Stop passing EVTHREAD_READ and EVTHREAD_WRITE to non-rw locks.
Nick Mathewson authored Nov 27, 2009
126 _evthread_lock_fns.lock(0, (base)->lockvar); \
558de9b @provos Provide OpenSSL style support for multiple threads accessing the same…
provos authored Mar 2, 2008
127 } while (0)
128
838d0a8 Document many internal functions and pieces of code.
Nick Mathewson authored Apr 17, 2009
129 /** Unlock an event_base, if it is set up for locking. */
76cd2b7 Stop passing EVTHREAD_READ and EVTHREAD_WRITE to non-rw locks.
Nick Mathewson authored Nov 27, 2009
130 #define EVBASE_RELEASE_LOCK(base, lockvar) do { \
ec35eb5 Make threading functions global, like the mm_ functions. Use the libe…
Nick Mathewson authored Feb 12, 2009
131 if (EVBASE_USING_LOCKS(base)) \
76cd2b7 Stop passing EVTHREAD_READ and EVTHREAD_WRITE to non-rw locks.
Nick Mathewson authored Nov 27, 2009
132 _evthread_lock_fns.unlock(0, (base)->lockvar); \
558de9b @provos Provide OpenSSL style support for multiple threads accessing the same…
provos authored Mar 2, 2008
133 } while (0)
0cd3bb9 Improved optional lock debugging.
Nick Mathewson authored Nov 27, 2009
134
135 /** If lock debugging is enabled, and lock is non-null, assert that 'lock' is
136 * locked and held by us. */
137 #define EVLOCK_ASSERT_LOCKED(lock) \
138 do { \
139 if ((lock) && _evthread_lock_debugging_enabled) { \
140 EVUTIL_ASSERT(_evthread_is_debug_lock_held(lock)); \
141 } \
142 } while (0)
143
144 int _evthread_is_debug_lock_held(void *lock);
145
689fc09 New EVTHREAD_TRY_LOCK function to try to grab a lock.
Nick Mathewson authored Nov 28, 2009
146 /** Try to grab the lock for 'lockvar' without blocking, and return 1 if we
147 * manage to get it. */
148 static inline int EVLOCK_TRY_LOCK(void *lock);
149 static inline int
150 EVLOCK_TRY_LOCK(void *lock)
151 {
152 if (lock && _evthread_lock_fns.lock) {
153 int r = _evthread_lock_fns.lock(EVTHREAD_TRY, lock);
154 return !r;
155 } else {
156 /* Locking is disabled either globally or for this thing;
157 * of course we count as having the lock. */
158 return 1;
159 }
160 }
161
8889a77 Replace all use of config.h with event-config.h.
Nick Mathewson authored Jan 27, 2009
162 #else /* _EVENT_DISABLE_THREAD_SUPPORT */
d13b59c Fix build with thread support disabled, and make no-op macros a littl…
Nick Mathewson authored Apr 5, 2009
163
ec35eb5 Make threading functions global, like the mm_ functions. Use the libe…
Nick Mathewson authored Feb 12, 2009
164 #define EVTHREAD_GET_ID() 1
347952f Revise the locking API: deprecate the old locking callbacks and add t…
Nick Mathewson authored Nov 27, 2009
165 #define EVTHREAD_ALLOC_LOCK(lockvar, locktype) _EVUTIL_NIL_STMT
166 #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 Apr 5, 2009
167
661b5ee Actually, move EVUTIL_NIL_STMT to util-internal.h
Nick Mathewson authored Apr 5, 2009
168 #define EVLOCK_LOCK(lockvar, mode) _EVUTIL_NIL_STMT
169 #define EVLOCK_UNLOCK(lockvar, mode) _EVUTIL_NIL_STMT
170 #define EVLOCK_LOCK2(lock1,lock2,mode1,mode2) _EVUTIL_NIL_STMT
171 #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 Apr 5, 2009
172
173 #define EVBASE_IN_THREAD(base) 1
76cd2b7 Stop passing EVTHREAD_READ and EVTHREAD_WRITE to non-rw locks.
Nick Mathewson authored Nov 27, 2009
174 #define EVBASE_ACQUIRE_LOCK(base, lock) _EVUTIL_NIL_STMT
175 #define EVBASE_RELEASE_LOCK(base, lock) _EVUTIL_NIL_STMT
0cd3bb9 Improved optional lock debugging.
Nick Mathewson authored Nov 27, 2009
176 #define EVLOCK_ASSERT_LOCKED(lock) _EVUTIL_NIL_STMT
689fc09 New EVTHREAD_TRY_LOCK function to try to grab a lock.
Nick Mathewson authored Nov 28, 2009
177
178 #define EVTHREAD_TRY_LOCK(lock) 1
558de9b @provos Provide OpenSSL style support for multiple threads accessing the same…
provos authored Mar 2, 2008
179 #endif
180
181 #ifdef __cplusplus
182 }
183 #endif
184
185 #endif /* _EVTHREAD_INTERNAL_H_ */
Something went wrong with that request. Please try again.