]> arthur.barton.de Git - netatalk.git/blob - libevent/evthread.c
Update libevent to 2.0.12
[netatalk.git] / libevent / evthread.c
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
27 #include "event2/event-config.h"
28
29 #ifndef _EVENT_DISABLE_THREAD_SUPPORT
30
31 #include "event2/thread.h"
32
33 #include <stdlib.h>
34 #include <string.h>
35
36 #include "log-internal.h"
37 #include "mm-internal.h"
38 #include "util-internal.h"
39 #include "evthread-internal.h"
40
41 #ifdef EVTHREAD_EXPOSE_STRUCTS
42 #define GLOBAL
43 #else
44 #define GLOBAL static
45 #endif
46
47 /* globals */
48 GLOBAL int _evthread_lock_debugging_enabled = 0;
49 GLOBAL struct evthread_lock_callbacks _evthread_lock_fns = {
50         0, 0, NULL, NULL, NULL, NULL
51 };
52 GLOBAL unsigned long (*_evthread_id_fn)(void) = NULL;
53 GLOBAL struct evthread_condition_callbacks _evthread_cond_fns = {
54         0, NULL, NULL, NULL, NULL
55 };
56
57 /* Used for debugging */
58 static struct evthread_lock_callbacks _original_lock_fns = {
59         0, 0, NULL, NULL, NULL, NULL
60 };
61 static struct evthread_condition_callbacks _original_cond_fns = {
62         0, NULL, NULL, NULL, NULL
63 };
64
65 void
66 evthread_set_id_callback(unsigned long (*id_fn)(void))
67 {
68         _evthread_id_fn = id_fn;
69 }
70
71 int
72 evthread_set_lock_callbacks(const struct evthread_lock_callbacks *cbs)
73 {
74         struct evthread_lock_callbacks *target =
75             _evthread_lock_debugging_enabled
76             ? &_original_lock_fns : &_evthread_lock_fns;
77
78         if (!cbs) {
79                 memset(target, 0, sizeof(_evthread_lock_fns));
80                 return 0;
81         }
82         if (cbs->alloc && cbs->free && cbs->lock && cbs->unlock) {
83                 memcpy(target, cbs, sizeof(_evthread_lock_fns));
84                 return 0;
85         } else {
86                 return -1;
87         }
88 }
89
90 int
91 evthread_set_condition_callbacks(const struct evthread_condition_callbacks *cbs)
92 {
93         struct evthread_condition_callbacks *target =
94             _evthread_lock_debugging_enabled
95             ? &_original_cond_fns : &_evthread_cond_fns;
96
97         if (!cbs) {
98                 memset(target, 0, sizeof(_evthread_cond_fns));
99         } else if (cbs->alloc_condition && cbs->free_condition &&
100             cbs->signal_condition && cbs->wait_condition) {
101                 memcpy(target, cbs, sizeof(_evthread_cond_fns));
102         }
103         if (_evthread_lock_debugging_enabled) {
104                 _evthread_cond_fns.alloc_condition = cbs->alloc_condition;
105                 _evthread_cond_fns.free_condition = cbs->free_condition;
106                 _evthread_cond_fns.signal_condition = cbs->signal_condition;
107         }
108         return 0;
109 }
110
111 struct debug_lock {
112         unsigned locktype;
113         unsigned long held_by;
114         /* XXXX if we ever use read-write locks, we will need a separate
115          * lock to protect count. */
116         int count;
117         void *lock;
118 };
119
120 static void *
121 debug_lock_alloc(unsigned locktype)
122 {
123         struct debug_lock *result = mm_malloc(sizeof(struct debug_lock));
124         if (!result)
125                 return NULL;
126         if (_original_lock_fns.alloc) {
127                 if (!(result->lock = _original_lock_fns.alloc(
128                                 locktype|EVTHREAD_LOCKTYPE_RECURSIVE))) {
129                         mm_free(result);
130                         return NULL;
131                 }
132         } else {
133                 result->lock = NULL;
134         }
135         result->locktype = locktype;
136         result->count = 0;
137         result->held_by = 0;
138         return result;
139 }
140
141 static void
142 debug_lock_free(void *lock_, unsigned locktype)
143 {
144         struct debug_lock *lock = lock_;
145         EVUTIL_ASSERT(lock->count == 0);
146         EVUTIL_ASSERT(locktype == lock->locktype);
147         if (_original_lock_fns.free) {
148                 _original_lock_fns.free(lock->lock,
149                     lock->locktype|EVTHREAD_LOCKTYPE_RECURSIVE);
150         }
151         lock->lock = NULL;
152         lock->count = -100;
153         mm_free(lock);
154 }
155
156 static void
157 evthread_debug_lock_mark_locked(unsigned mode, struct debug_lock *lock)
158 {
159         ++lock->count;
160         if (!(lock->locktype & EVTHREAD_LOCKTYPE_RECURSIVE))
161                 EVUTIL_ASSERT(lock->count == 1);
162         if (_evthread_id_fn) {
163                 unsigned long me;
164                 me = _evthread_id_fn();
165                 if (lock->count > 1)
166                         EVUTIL_ASSERT(lock->held_by == me);
167                 lock->held_by = me;
168         }
169 }
170
171 static int
172 debug_lock_lock(unsigned mode, void *lock_)
173 {
174         struct debug_lock *lock = lock_;
175         int res = 0;
176         if (lock->locktype & EVTHREAD_LOCKTYPE_READWRITE)
177                 EVUTIL_ASSERT(mode & (EVTHREAD_READ|EVTHREAD_WRITE));
178         else
179                 EVUTIL_ASSERT((mode & (EVTHREAD_READ|EVTHREAD_WRITE)) == 0);
180         if (_original_lock_fns.lock)
181                 res = _original_lock_fns.lock(mode, lock->lock);
182         if (!res) {
183                 evthread_debug_lock_mark_locked(mode, lock);
184         }
185         return res;
186 }
187
188 static void
189 evthread_debug_lock_mark_unlocked(unsigned mode, struct debug_lock *lock)
190 {
191         if (lock->locktype & EVTHREAD_LOCKTYPE_READWRITE)
192                 EVUTIL_ASSERT(mode & (EVTHREAD_READ|EVTHREAD_WRITE));
193         else
194                 EVUTIL_ASSERT((mode & (EVTHREAD_READ|EVTHREAD_WRITE)) == 0);
195         if (_evthread_id_fn) {
196                 EVUTIL_ASSERT(lock->held_by == _evthread_id_fn());
197                 if (lock->count == 1)
198                         lock->held_by = 0;
199         }
200         --lock->count;
201         EVUTIL_ASSERT(lock->count >= 0);
202 }
203
204 static int
205 debug_lock_unlock(unsigned mode, void *lock_)
206 {
207         struct debug_lock *lock = lock_;
208         int res = 0;
209         evthread_debug_lock_mark_unlocked(mode, lock);
210         if (_original_lock_fns.unlock)
211                 res = _original_lock_fns.unlock(mode, lock->lock);
212         return res;
213 }
214
215 static int
216 debug_cond_wait(void *_cond, void *_lock, const struct timeval *tv)
217 {
218         int r;
219         struct debug_lock *lock = _lock;
220         EVUTIL_ASSERT(lock);
221         EVLOCK_ASSERT_LOCKED(_lock);
222         evthread_debug_lock_mark_unlocked(0, lock);
223         r = _original_cond_fns.wait_condition(_cond, lock->lock, tv);
224         evthread_debug_lock_mark_locked(0, lock);
225         return r;
226 }
227
228 void
229 evthread_enable_lock_debuging(void)
230 {
231         struct evthread_lock_callbacks cbs = {
232                 EVTHREAD_LOCK_API_VERSION,
233                 EVTHREAD_LOCKTYPE_RECURSIVE,
234                 debug_lock_alloc,
235                 debug_lock_free,
236                 debug_lock_lock,
237                 debug_lock_unlock
238         };
239         if (_evthread_lock_debugging_enabled)
240                 return;
241         memcpy(&_original_lock_fns, &_evthread_lock_fns,
242             sizeof(struct evthread_lock_callbacks));
243         memcpy(&_evthread_lock_fns, &cbs,
244             sizeof(struct evthread_lock_callbacks));
245
246         memcpy(&_original_cond_fns, &_evthread_cond_fns,
247             sizeof(struct evthread_condition_callbacks));
248         _evthread_cond_fns.wait_condition = debug_cond_wait;
249         _evthread_lock_debugging_enabled = 1;
250 }
251
252 int
253 _evthread_is_debug_lock_held(void *lock_)
254 {
255         struct debug_lock *lock = lock_;
256         if (! lock->count)
257                 return 0;
258         if (_evthread_id_fn) {
259                 unsigned long me = _evthread_id_fn();
260                 if (lock->held_by != me)
261                         return 0;
262         }
263         return 1;
264 }
265
266 void *
267 _evthread_debug_get_real_lock(void *lock_)
268 {
269         struct debug_lock *lock = lock_;
270         return lock->lock;
271 }
272
273 #ifndef EVTHREAD_EXPOSE_STRUCTS
274 unsigned long
275 _evthreadimpl_get_id()
276 {
277         return _evthread_id_fn ? _evthread_id_fn() : 1;
278 }
279 void *
280 _evthreadimpl_lock_alloc(unsigned locktype)
281 {
282         return _evthread_lock_fns.alloc ?
283             _evthread_lock_fns.alloc(locktype) : NULL;
284 }
285 void
286 _evthreadimpl_lock_free(void *lock, unsigned locktype)
287 {
288         if (_evthread_lock_fns.free)
289                 _evthread_lock_fns.free(lock, locktype);
290 }
291 int
292 _evthreadimpl_lock_lock(unsigned mode, void *lock)
293 {
294         if (_evthread_lock_fns.lock)
295                 return _evthread_lock_fns.lock(mode, lock);
296         else
297                 return 0;
298 }
299 int
300 _evthreadimpl_lock_unlock(unsigned mode, void *lock)
301 {
302         if (_evthread_lock_fns.unlock)
303                 return _evthread_lock_fns.unlock(mode, lock);
304         else
305                 return 0;
306 }
307 void *
308 _evthreadimpl_cond_alloc(unsigned condtype)
309 {
310         return _evthread_cond_fns.alloc_condition ?
311             _evthread_cond_fns.alloc_condition(condtype) : NULL;
312 }
313 void
314 _evthreadimpl_cond_free(void *cond)
315 {
316         if (_evthread_cond_fns.free_condition)
317                 _evthread_cond_fns.free_condition(cond);
318 }
319 int
320 _evthreadimpl_cond_signal(void *cond, int broadcast)
321 {
322         if (_evthread_cond_fns.signal_condition)
323                 return _evthread_cond_fns.signal_condition(cond, broadcast);
324         else
325                 return 0;
326 }
327 int
328 _evthreadimpl_cond_wait(void *cond, void *lock, const struct timeval *tv)
329 {
330         if (_evthread_cond_fns.wait_condition)
331                 return _evthread_cond_fns.wait_condition(cond, lock, tv);
332         else
333                 return 0;
334 }
335 int
336 _evthreadimpl_is_lock_debugging_enabled(void)
337 {
338         return _evthread_lock_debugging_enabled;
339 }
340 #endif
341
342 #endif