blob: 3eac594d64dc8967f599f872568c1b425d9cde67 [file] [log] [blame]
Christopher Wileye8679812015-07-01 13:36:18 -07001/*
2 * Copyright (c) 2008-2012 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"
Narayan Kamathfc74cb42017-09-13 12:53:52 +010028#include "evconfig-private.h"
Christopher Wileye8679812015-07-01 13:36:18 -070029
Narayan Kamathfc74cb42017-09-13 12:53:52 +010030#ifndef EVENT__DISABLE_THREAD_SUPPORT
Christopher Wileye8679812015-07-01 13:36:18 -070031
32#include "event2/thread.h"
33
34#include <stdlib.h>
35#include <string.h>
36
37#include "log-internal.h"
38#include "mm-internal.h"
39#include "util-internal.h"
40#include "evthread-internal.h"
41
42#ifdef EVTHREAD_EXPOSE_STRUCTS
43#define GLOBAL
44#else
45#define GLOBAL static
46#endif
47
Narayan Kamathfc74cb42017-09-13 12:53:52 +010048#ifndef EVENT__DISABLE_DEBUG_MODE
Haibo Huangb2279672019-05-31 16:12:39 -070049extern int event_debug_created_threadable_ctx_;
Narayan Kamathfc74cb42017-09-13 12:53:52 +010050extern int event_debug_mode_on_;
51#endif
52
Christopher Wileye8679812015-07-01 13:36:18 -070053/* globals */
Narayan Kamathfc74cb42017-09-13 12:53:52 +010054GLOBAL int evthread_lock_debugging_enabled_ = 0;
55GLOBAL struct evthread_lock_callbacks evthread_lock_fns_ = {
Christopher Wileye8679812015-07-01 13:36:18 -070056 0, 0, NULL, NULL, NULL, NULL
57};
Narayan Kamathfc74cb42017-09-13 12:53:52 +010058GLOBAL unsigned long (*evthread_id_fn_)(void) = NULL;
59GLOBAL struct evthread_condition_callbacks evthread_cond_fns_ = {
Christopher Wileye8679812015-07-01 13:36:18 -070060 0, NULL, NULL, NULL, NULL
61};
62
63/* Used for debugging */
Narayan Kamathfc74cb42017-09-13 12:53:52 +010064static struct evthread_lock_callbacks original_lock_fns_ = {
Christopher Wileye8679812015-07-01 13:36:18 -070065 0, 0, NULL, NULL, NULL, NULL
66};
Narayan Kamathfc74cb42017-09-13 12:53:52 +010067static struct evthread_condition_callbacks original_cond_fns_ = {
Christopher Wileye8679812015-07-01 13:36:18 -070068 0, NULL, NULL, NULL, NULL
69};
70
71void
72evthread_set_id_callback(unsigned long (*id_fn)(void))
73{
Narayan Kamathfc74cb42017-09-13 12:53:52 +010074 evthread_id_fn_ = id_fn;
75}
76
77struct evthread_lock_callbacks *evthread_get_lock_callbacks()
78{
79 return evthread_lock_debugging_enabled_
80 ? &original_lock_fns_ : &evthread_lock_fns_;
81}
82struct evthread_condition_callbacks *evthread_get_condition_callbacks()
83{
84 return evthread_lock_debugging_enabled_
85 ? &original_cond_fns_ : &evthread_cond_fns_;
86}
87void evthreadimpl_disable_lock_debugging_(void)
88{
89 evthread_lock_debugging_enabled_ = 0;
Christopher Wileye8679812015-07-01 13:36:18 -070090}
91
92int
93evthread_set_lock_callbacks(const struct evthread_lock_callbacks *cbs)
94{
Narayan Kamathfc74cb42017-09-13 12:53:52 +010095 struct evthread_lock_callbacks *target = evthread_get_lock_callbacks();
96
97#ifndef EVENT__DISABLE_DEBUG_MODE
98 if (event_debug_mode_on_) {
99 if (event_debug_created_threadable_ctx_) {
100 event_errx(1, "evthread initialization must be called BEFORE anything else!");
101 }
102 }
103#endif
Christopher Wileye8679812015-07-01 13:36:18 -0700104
105 if (!cbs) {
106 if (target->alloc)
107 event_warnx("Trying to disable lock functions after "
108 "they have been set up will probaby not work.");
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100109 memset(target, 0, sizeof(evthread_lock_fns_));
Christopher Wileye8679812015-07-01 13:36:18 -0700110 return 0;
111 }
112 if (target->alloc) {
113 /* Uh oh; we already had locking callbacks set up.*/
114 if (target->lock_api_version == cbs->lock_api_version &&
115 target->supported_locktypes == cbs->supported_locktypes &&
116 target->alloc == cbs->alloc &&
117 target->free == cbs->free &&
118 target->lock == cbs->lock &&
119 target->unlock == cbs->unlock) {
120 /* no change -- allow this. */
121 return 0;
122 }
123 event_warnx("Can't change lock callbacks once they have been "
124 "initialized.");
125 return -1;
126 }
127 if (cbs->alloc && cbs->free && cbs->lock && cbs->unlock) {
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100128 memcpy(target, cbs, sizeof(evthread_lock_fns_));
Christopher Wileye8679812015-07-01 13:36:18 -0700129 return event_global_setup_locks_(1);
130 } else {
131 return -1;
132 }
133}
134
135int
136evthread_set_condition_callbacks(const struct evthread_condition_callbacks *cbs)
137{
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100138 struct evthread_condition_callbacks *target = evthread_get_condition_callbacks();
139
140#ifndef EVENT__DISABLE_DEBUG_MODE
141 if (event_debug_mode_on_) {
142 if (event_debug_created_threadable_ctx_) {
143 event_errx(1, "evthread initialization must be called BEFORE anything else!");
144 }
145 }
146#endif
Christopher Wileye8679812015-07-01 13:36:18 -0700147
148 if (!cbs) {
149 if (target->alloc_condition)
150 event_warnx("Trying to disable condition functions "
151 "after they have been set up will probaby not "
152 "work.");
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100153 memset(target, 0, sizeof(evthread_cond_fns_));
Christopher Wileye8679812015-07-01 13:36:18 -0700154 return 0;
155 }
156 if (target->alloc_condition) {
157 /* Uh oh; we already had condition callbacks set up.*/
158 if (target->condition_api_version == cbs->condition_api_version &&
159 target->alloc_condition == cbs->alloc_condition &&
160 target->free_condition == cbs->free_condition &&
161 target->signal_condition == cbs->signal_condition &&
162 target->wait_condition == cbs->wait_condition) {
163 /* no change -- allow this. */
164 return 0;
165 }
166 event_warnx("Can't change condition callbacks once they "
167 "have been initialized.");
168 return -1;
169 }
170 if (cbs->alloc_condition && cbs->free_condition &&
171 cbs->signal_condition && cbs->wait_condition) {
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100172 memcpy(target, cbs, sizeof(evthread_cond_fns_));
Christopher Wileye8679812015-07-01 13:36:18 -0700173 }
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100174 if (evthread_lock_debugging_enabled_) {
175 evthread_cond_fns_.alloc_condition = cbs->alloc_condition;
176 evthread_cond_fns_.free_condition = cbs->free_condition;
177 evthread_cond_fns_.signal_condition = cbs->signal_condition;
Christopher Wileye8679812015-07-01 13:36:18 -0700178 }
179 return 0;
180}
181
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100182#define DEBUG_LOCK_SIG 0xdeb0b10c
183
Christopher Wileye8679812015-07-01 13:36:18 -0700184struct debug_lock {
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100185 unsigned signature;
Christopher Wileye8679812015-07-01 13:36:18 -0700186 unsigned locktype;
187 unsigned long held_by;
188 /* XXXX if we ever use read-write locks, we will need a separate
189 * lock to protect count. */
190 int count;
191 void *lock;
192};
193
194static void *
195debug_lock_alloc(unsigned locktype)
196{
197 struct debug_lock *result = mm_malloc(sizeof(struct debug_lock));
198 if (!result)
199 return NULL;
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100200 if (original_lock_fns_.alloc) {
201 if (!(result->lock = original_lock_fns_.alloc(
Christopher Wileye8679812015-07-01 13:36:18 -0700202 locktype|EVTHREAD_LOCKTYPE_RECURSIVE))) {
203 mm_free(result);
204 return NULL;
205 }
206 } else {
207 result->lock = NULL;
208 }
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100209 result->signature = DEBUG_LOCK_SIG;
Christopher Wileye8679812015-07-01 13:36:18 -0700210 result->locktype = locktype;
211 result->count = 0;
212 result->held_by = 0;
213 return result;
214}
215
216static void
217debug_lock_free(void *lock_, unsigned locktype)
218{
219 struct debug_lock *lock = lock_;
220 EVUTIL_ASSERT(lock->count == 0);
221 EVUTIL_ASSERT(locktype == lock->locktype);
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100222 EVUTIL_ASSERT(DEBUG_LOCK_SIG == lock->signature);
223 if (original_lock_fns_.free) {
224 original_lock_fns_.free(lock->lock,
Christopher Wileye8679812015-07-01 13:36:18 -0700225 lock->locktype|EVTHREAD_LOCKTYPE_RECURSIVE);
226 }
227 lock->lock = NULL;
228 lock->count = -100;
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100229 lock->signature = 0x12300fda;
Christopher Wileye8679812015-07-01 13:36:18 -0700230 mm_free(lock);
231}
232
233static void
234evthread_debug_lock_mark_locked(unsigned mode, struct debug_lock *lock)
235{
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100236 EVUTIL_ASSERT(DEBUG_LOCK_SIG == lock->signature);
Christopher Wileye8679812015-07-01 13:36:18 -0700237 ++lock->count;
238 if (!(lock->locktype & EVTHREAD_LOCKTYPE_RECURSIVE))
239 EVUTIL_ASSERT(lock->count == 1);
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100240 if (evthread_id_fn_) {
Christopher Wileye8679812015-07-01 13:36:18 -0700241 unsigned long me;
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100242 me = evthread_id_fn_();
Christopher Wileye8679812015-07-01 13:36:18 -0700243 if (lock->count > 1)
244 EVUTIL_ASSERT(lock->held_by == me);
245 lock->held_by = me;
246 }
247}
248
249static int
250debug_lock_lock(unsigned mode, void *lock_)
251{
252 struct debug_lock *lock = lock_;
253 int res = 0;
254 if (lock->locktype & EVTHREAD_LOCKTYPE_READWRITE)
255 EVUTIL_ASSERT(mode & (EVTHREAD_READ|EVTHREAD_WRITE));
256 else
257 EVUTIL_ASSERT((mode & (EVTHREAD_READ|EVTHREAD_WRITE)) == 0);
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100258 if (original_lock_fns_.lock)
259 res = original_lock_fns_.lock(mode, lock->lock);
Christopher Wileye8679812015-07-01 13:36:18 -0700260 if (!res) {
261 evthread_debug_lock_mark_locked(mode, lock);
262 }
263 return res;
264}
265
266static void
267evthread_debug_lock_mark_unlocked(unsigned mode, struct debug_lock *lock)
268{
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100269 EVUTIL_ASSERT(DEBUG_LOCK_SIG == lock->signature);
Christopher Wileye8679812015-07-01 13:36:18 -0700270 if (lock->locktype & EVTHREAD_LOCKTYPE_READWRITE)
271 EVUTIL_ASSERT(mode & (EVTHREAD_READ|EVTHREAD_WRITE));
272 else
273 EVUTIL_ASSERT((mode & (EVTHREAD_READ|EVTHREAD_WRITE)) == 0);
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100274 if (evthread_id_fn_) {
275 unsigned long me;
276 me = evthread_id_fn_();
277 EVUTIL_ASSERT(lock->held_by == me);
Christopher Wileye8679812015-07-01 13:36:18 -0700278 if (lock->count == 1)
279 lock->held_by = 0;
280 }
281 --lock->count;
282 EVUTIL_ASSERT(lock->count >= 0);
283}
284
285static int
286debug_lock_unlock(unsigned mode, void *lock_)
287{
288 struct debug_lock *lock = lock_;
289 int res = 0;
290 evthread_debug_lock_mark_unlocked(mode, lock);
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100291 if (original_lock_fns_.unlock)
292 res = original_lock_fns_.unlock(mode, lock->lock);
Christopher Wileye8679812015-07-01 13:36:18 -0700293 return res;
294}
295
296static int
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100297debug_cond_wait(void *cond_, void *lock_, const struct timeval *tv)
Christopher Wileye8679812015-07-01 13:36:18 -0700298{
299 int r;
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100300 struct debug_lock *lock = lock_;
Christopher Wileye8679812015-07-01 13:36:18 -0700301 EVUTIL_ASSERT(lock);
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100302 EVUTIL_ASSERT(DEBUG_LOCK_SIG == lock->signature);
303 EVLOCK_ASSERT_LOCKED(lock_);
Christopher Wileye8679812015-07-01 13:36:18 -0700304 evthread_debug_lock_mark_unlocked(0, lock);
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100305 r = original_cond_fns_.wait_condition(cond_, lock->lock, tv);
Christopher Wileye8679812015-07-01 13:36:18 -0700306 evthread_debug_lock_mark_locked(0, lock);
307 return r;
308}
309
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100310/* misspelled version for backward compatibility */
Christopher Wileye8679812015-07-01 13:36:18 -0700311void
312evthread_enable_lock_debuging(void)
313{
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100314 evthread_enable_lock_debugging();
315}
316
317void
318evthread_enable_lock_debugging(void)
319{
Christopher Wileye8679812015-07-01 13:36:18 -0700320 struct evthread_lock_callbacks cbs = {
321 EVTHREAD_LOCK_API_VERSION,
322 EVTHREAD_LOCKTYPE_RECURSIVE,
323 debug_lock_alloc,
324 debug_lock_free,
325 debug_lock_lock,
326 debug_lock_unlock
327 };
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100328 if (evthread_lock_debugging_enabled_)
Christopher Wileye8679812015-07-01 13:36:18 -0700329 return;
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100330 memcpy(&original_lock_fns_, &evthread_lock_fns_,
Christopher Wileye8679812015-07-01 13:36:18 -0700331 sizeof(struct evthread_lock_callbacks));
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100332 memcpy(&evthread_lock_fns_, &cbs,
Christopher Wileye8679812015-07-01 13:36:18 -0700333 sizeof(struct evthread_lock_callbacks));
334
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100335 memcpy(&original_cond_fns_, &evthread_cond_fns_,
Christopher Wileye8679812015-07-01 13:36:18 -0700336 sizeof(struct evthread_condition_callbacks));
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100337 evthread_cond_fns_.wait_condition = debug_cond_wait;
338 evthread_lock_debugging_enabled_ = 1;
Christopher Wileye8679812015-07-01 13:36:18 -0700339
340 /* XXX return value should get checked. */
341 event_global_setup_locks_(0);
342}
343
344int
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100345evthread_is_debug_lock_held_(void *lock_)
Christopher Wileye8679812015-07-01 13:36:18 -0700346{
347 struct debug_lock *lock = lock_;
348 if (! lock->count)
349 return 0;
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100350 if (evthread_id_fn_) {
351 unsigned long me = evthread_id_fn_();
Christopher Wileye8679812015-07-01 13:36:18 -0700352 if (lock->held_by != me)
353 return 0;
354 }
355 return 1;
356}
357
358void *
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100359evthread_debug_get_real_lock_(void *lock_)
Christopher Wileye8679812015-07-01 13:36:18 -0700360{
361 struct debug_lock *lock = lock_;
362 return lock->lock;
363}
364
365void *
366evthread_setup_global_lock_(void *lock_, unsigned locktype, int enable_locks)
367{
368 /* there are four cases here:
369 1) we're turning on debugging; locking is not on.
370 2) we're turning on debugging; locking is on.
371 3) we're turning on locking; debugging is not on.
372 4) we're turning on locking; debugging is on. */
373
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100374 if (!enable_locks && original_lock_fns_.alloc == NULL) {
Christopher Wileye8679812015-07-01 13:36:18 -0700375 /* Case 1: allocate a debug lock. */
376 EVUTIL_ASSERT(lock_ == NULL);
377 return debug_lock_alloc(locktype);
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100378 } else if (!enable_locks && original_lock_fns_.alloc != NULL) {
Christopher Wileye8679812015-07-01 13:36:18 -0700379 /* Case 2: wrap the lock in a debug lock. */
380 struct debug_lock *lock;
381 EVUTIL_ASSERT(lock_ != NULL);
382
383 if (!(locktype & EVTHREAD_LOCKTYPE_RECURSIVE)) {
384 /* We can't wrap it: We need a recursive lock */
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100385 original_lock_fns_.free(lock_, locktype);
Christopher Wileye8679812015-07-01 13:36:18 -0700386 return debug_lock_alloc(locktype);
387 }
388 lock = mm_malloc(sizeof(struct debug_lock));
389 if (!lock) {
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100390 original_lock_fns_.free(lock_, locktype);
Christopher Wileye8679812015-07-01 13:36:18 -0700391 return NULL;
392 }
393 lock->lock = lock_;
394 lock->locktype = locktype;
395 lock->count = 0;
396 lock->held_by = 0;
397 return lock;
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100398 } else if (enable_locks && ! evthread_lock_debugging_enabled_) {
Christopher Wileye8679812015-07-01 13:36:18 -0700399 /* Case 3: allocate a regular lock */
400 EVUTIL_ASSERT(lock_ == NULL);
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100401 return evthread_lock_fns_.alloc(locktype);
Christopher Wileye8679812015-07-01 13:36:18 -0700402 } else {
403 /* Case 4: Fill in a debug lock with a real lock */
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100404 struct debug_lock *lock = lock_ ? lock_ : debug_lock_alloc(locktype);
Christopher Wileye8679812015-07-01 13:36:18 -0700405 EVUTIL_ASSERT(enable_locks &&
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100406 evthread_lock_debugging_enabled_);
Christopher Wileye8679812015-07-01 13:36:18 -0700407 EVUTIL_ASSERT(lock->locktype == locktype);
Christopher Wileye8679812015-07-01 13:36:18 -0700408 if (!lock->lock) {
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100409 lock->lock = original_lock_fns_.alloc(
410 locktype|EVTHREAD_LOCKTYPE_RECURSIVE);
411 if (!lock->lock) {
412 lock->count = -200;
413 mm_free(lock);
414 return NULL;
415 }
Christopher Wileye8679812015-07-01 13:36:18 -0700416 }
417 return lock;
418 }
419}
420
421
422#ifndef EVTHREAD_EXPOSE_STRUCTS
423unsigned long
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100424evthreadimpl_get_id_()
Christopher Wileye8679812015-07-01 13:36:18 -0700425{
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100426 return evthread_id_fn_ ? evthread_id_fn_() : 1;
Christopher Wileye8679812015-07-01 13:36:18 -0700427}
428void *
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100429evthreadimpl_lock_alloc_(unsigned locktype)
Christopher Wileye8679812015-07-01 13:36:18 -0700430{
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100431#ifndef EVENT__DISABLE_DEBUG_MODE
432 if (event_debug_mode_on_) {
433 event_debug_created_threadable_ctx_ = 1;
434 }
435#endif
436
437 return evthread_lock_fns_.alloc ?
438 evthread_lock_fns_.alloc(locktype) : NULL;
Christopher Wileye8679812015-07-01 13:36:18 -0700439}
440void
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100441evthreadimpl_lock_free_(void *lock, unsigned locktype)
Christopher Wileye8679812015-07-01 13:36:18 -0700442{
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100443 if (evthread_lock_fns_.free)
444 evthread_lock_fns_.free(lock, locktype);
Christopher Wileye8679812015-07-01 13:36:18 -0700445}
446int
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100447evthreadimpl_lock_lock_(unsigned mode, void *lock)
Christopher Wileye8679812015-07-01 13:36:18 -0700448{
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100449 if (evthread_lock_fns_.lock)
450 return evthread_lock_fns_.lock(mode, lock);
Christopher Wileye8679812015-07-01 13:36:18 -0700451 else
452 return 0;
453}
454int
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100455evthreadimpl_lock_unlock_(unsigned mode, void *lock)
Christopher Wileye8679812015-07-01 13:36:18 -0700456{
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100457 if (evthread_lock_fns_.unlock)
458 return evthread_lock_fns_.unlock(mode, lock);
Christopher Wileye8679812015-07-01 13:36:18 -0700459 else
460 return 0;
461}
462void *
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100463evthreadimpl_cond_alloc_(unsigned condtype)
Christopher Wileye8679812015-07-01 13:36:18 -0700464{
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100465#ifndef EVENT__DISABLE_DEBUG_MODE
466 if (event_debug_mode_on_) {
467 event_debug_created_threadable_ctx_ = 1;
468 }
469#endif
470
471 return evthread_cond_fns_.alloc_condition ?
472 evthread_cond_fns_.alloc_condition(condtype) : NULL;
Christopher Wileye8679812015-07-01 13:36:18 -0700473}
474void
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100475evthreadimpl_cond_free_(void *cond)
Christopher Wileye8679812015-07-01 13:36:18 -0700476{
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100477 if (evthread_cond_fns_.free_condition)
478 evthread_cond_fns_.free_condition(cond);
Christopher Wileye8679812015-07-01 13:36:18 -0700479}
480int
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100481evthreadimpl_cond_signal_(void *cond, int broadcast)
Christopher Wileye8679812015-07-01 13:36:18 -0700482{
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100483 if (evthread_cond_fns_.signal_condition)
484 return evthread_cond_fns_.signal_condition(cond, broadcast);
Christopher Wileye8679812015-07-01 13:36:18 -0700485 else
486 return 0;
487}
488int
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100489evthreadimpl_cond_wait_(void *cond, void *lock, const struct timeval *tv)
Christopher Wileye8679812015-07-01 13:36:18 -0700490{
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100491 if (evthread_cond_fns_.wait_condition)
492 return evthread_cond_fns_.wait_condition(cond, lock, tv);
Christopher Wileye8679812015-07-01 13:36:18 -0700493 else
494 return 0;
495}
496int
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100497evthreadimpl_is_lock_debugging_enabled_(void)
Christopher Wileye8679812015-07-01 13:36:18 -0700498{
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100499 return evthread_lock_debugging_enabled_;
Christopher Wileye8679812015-07-01 13:36:18 -0700500}
501
502int
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100503evthreadimpl_locking_enabled_(void)
Christopher Wileye8679812015-07-01 13:36:18 -0700504{
Narayan Kamathfc74cb42017-09-13 12:53:52 +0100505 return evthread_lock_fns_.lock != NULL;
Christopher Wileye8679812015-07-01 13:36:18 -0700506}
507#endif
508
509#endif