xref: /libevent-2.1.12/evthread.c (revision 9806b126)
1347952ffSNick Mathewson /*
2e49e2891SNick Mathewson  * Copyright (c) 2008-2012 Niels Provos, Nick Mathewson
3347952ffSNick Mathewson  *
4347952ffSNick Mathewson  * Redistribution and use in source and binary forms, with or without
5347952ffSNick Mathewson  * modification, are permitted provided that the following conditions
6347952ffSNick Mathewson  * are met:
7347952ffSNick Mathewson  * 1. Redistributions of source code must retain the above copyright
8347952ffSNick Mathewson  *    notice, this list of conditions and the following disclaimer.
9347952ffSNick Mathewson  * 2. Redistributions in binary form must reproduce the above copyright
10347952ffSNick Mathewson  *    notice, this list of conditions and the following disclaimer in the
11347952ffSNick Mathewson  *    documentation and/or other materials provided with the distribution.
12347952ffSNick Mathewson  * 3. The name of the author may not be used to endorse or promote products
13347952ffSNick Mathewson  *    derived from this software without specific prior written permission.
14347952ffSNick Mathewson  *
15347952ffSNick Mathewson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16347952ffSNick Mathewson  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17347952ffSNick Mathewson  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18347952ffSNick Mathewson  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19347952ffSNick Mathewson  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20347952ffSNick Mathewson  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21347952ffSNick Mathewson  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22347952ffSNick Mathewson  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23347952ffSNick Mathewson  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24347952ffSNick Mathewson  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25347952ffSNick Mathewson  */
26347952ffSNick Mathewson 
27ec347b92SNick Mathewson #include "event2/event-config.h"
280915ca0aSKevin Bowling #include "evconfig-private.h"
29347952ffSNick Mathewson 
3068120d9bSNick Mathewson #ifndef EVENT__DISABLE_THREAD_SUPPORT
31347952ffSNick Mathewson 
32fbe64f21SEvan Jones #include "event2/thread.h"
33347952ffSNick Mathewson 
34347952ffSNick Mathewson #include <stdlib.h>
35347952ffSNick Mathewson #include <string.h>
36347952ffSNick Mathewson 
37347952ffSNick Mathewson #include "log-internal.h"
38347952ffSNick Mathewson #include "mm-internal.h"
39347952ffSNick Mathewson #include "util-internal.h"
400cd3bb9fSNick Mathewson #include "evthread-internal.h"
41347952ffSNick Mathewson 
425de2bcb7SNick Mathewson #ifdef EVTHREAD_EXPOSE_STRUCTS
435de2bcb7SNick Mathewson #define GLOBAL
445de2bcb7SNick Mathewson #else
455de2bcb7SNick Mathewson #define GLOBAL static
465de2bcb7SNick Mathewson #endif
475de2bcb7SNick Mathewson 
48dcfb19a2SMark Ellzey #ifndef EVENT__DISABLE_DEBUG_MODE
49dcfb19a2SMark Ellzey extern int event_debug_created_threadable_ctx_;
50dcfb19a2SMark Ellzey extern int event_debug_mode_on_;
51dcfb19a2SMark Ellzey #endif
52dcfb19a2SMark Ellzey 
53347952ffSNick Mathewson /* globals */
54cb9da0bfSNick Mathewson GLOBAL int evthread_lock_debugging_enabled_ = 0;
55cb9da0bfSNick Mathewson GLOBAL struct evthread_lock_callbacks evthread_lock_fns_ = {
56347952ffSNick Mathewson 	0, 0, NULL, NULL, NULL, NULL
57347952ffSNick Mathewson };
58cb9da0bfSNick Mathewson GLOBAL unsigned long (*evthread_id_fn_)(void) = NULL;
59cb9da0bfSNick Mathewson GLOBAL struct evthread_condition_callbacks evthread_cond_fns_ = {
60d4977b52SNick Mathewson 	0, NULL, NULL, NULL, NULL
61d4977b52SNick Mathewson };
62d4977b52SNick Mathewson 
63347952ffSNick Mathewson /* Used for debugging */
64cb9da0bfSNick Mathewson static struct evthread_lock_callbacks original_lock_fns_ = {
65347952ffSNick Mathewson 	0, 0, NULL, NULL, NULL, NULL
66347952ffSNick Mathewson };
67cb9da0bfSNick Mathewson static struct evthread_condition_callbacks original_cond_fns_ = {
68d4977b52SNick Mathewson 	0, NULL, NULL, NULL, NULL
69d4977b52SNick Mathewson };
70347952ffSNick Mathewson 
71347952ffSNick Mathewson void
evthread_set_id_callback(unsigned long (* id_fn)(void))72347952ffSNick Mathewson evthread_set_id_callback(unsigned long (*id_fn)(void))
73347952ffSNick Mathewson {
74cb9da0bfSNick Mathewson 	evthread_id_fn_ = id_fn;
75347952ffSNick Mathewson }
76347952ffSNick Mathewson 
evthread_get_lock_callbacks()77c0b34f6fSAzat Khuzhin struct evthread_lock_callbacks *evthread_get_lock_callbacks()
78c0b34f6fSAzat Khuzhin {
79c0b34f6fSAzat Khuzhin 	return evthread_lock_debugging_enabled_
80c0b34f6fSAzat Khuzhin 	    ? &original_lock_fns_ : &evthread_lock_fns_;
81c0b34f6fSAzat Khuzhin }
evthread_get_condition_callbacks()82c0b34f6fSAzat Khuzhin struct evthread_condition_callbacks *evthread_get_condition_callbacks()
83c0b34f6fSAzat Khuzhin {
84c0b34f6fSAzat Khuzhin 	return evthread_lock_debugging_enabled_
85c0b34f6fSAzat Khuzhin 	    ? &original_cond_fns_ : &evthread_cond_fns_;
86c0b34f6fSAzat Khuzhin }
evthreadimpl_disable_lock_debugging_(void)87ccc55937SAzat Khuzhin void evthreadimpl_disable_lock_debugging_(void)
88ccc55937SAzat Khuzhin {
89ccc55937SAzat Khuzhin 	evthread_lock_debugging_enabled_ = 0;
90ccc55937SAzat Khuzhin }
91c0b34f6fSAzat Khuzhin 
92347952ffSNick Mathewson int
evthread_set_lock_callbacks(const struct evthread_lock_callbacks * cbs)93347952ffSNick Mathewson evthread_set_lock_callbacks(const struct evthread_lock_callbacks *cbs)
94347952ffSNick Mathewson {
95c0b34f6fSAzat Khuzhin 	struct evthread_lock_callbacks *target = evthread_get_lock_callbacks();
96347952ffSNick Mathewson 
97dcfb19a2SMark Ellzey #ifndef EVENT__DISABLE_DEBUG_MODE
98dcfb19a2SMark Ellzey 	if (event_debug_mode_on_) {
99dcfb19a2SMark Ellzey 		if (event_debug_created_threadable_ctx_) {
100dcfb19a2SMark Ellzey 		    event_errx(1, "evthread initialization must be called BEFORE anything else!");
101dcfb19a2SMark Ellzey 		}
102dcfb19a2SMark Ellzey 	}
103dcfb19a2SMark Ellzey #endif
104dcfb19a2SMark Ellzey 
105347952ffSNick Mathewson 	if (!cbs) {
106cb6ecee7SNick Mathewson 		if (target->alloc)
107cb6ecee7SNick Mathewson 			event_warnx("Trying to disable lock functions after "
108cb6ecee7SNick Mathewson 			    "they have been set up will probaby not work.");
109cb9da0bfSNick Mathewson 		memset(target, 0, sizeof(evthread_lock_fns_));
110347952ffSNick Mathewson 		return 0;
111347952ffSNick Mathewson 	}
112cb6ecee7SNick Mathewson 	if (target->alloc) {
113cb6ecee7SNick Mathewson 		/* Uh oh; we already had locking callbacks set up.*/
114c94a5f2aSNate R 		if (target->lock_api_version == cbs->lock_api_version &&
115c94a5f2aSNate R 			target->supported_locktypes == cbs->supported_locktypes &&
116c94a5f2aSNate R 			target->alloc == cbs->alloc &&
117c94a5f2aSNate R 			target->free == cbs->free &&
118c94a5f2aSNate R 			target->lock == cbs->lock &&
119c94a5f2aSNate R 			target->unlock == cbs->unlock) {
120cb6ecee7SNick Mathewson 			/* no change -- allow this. */
121cb6ecee7SNick Mathewson 			return 0;
122cb6ecee7SNick Mathewson 		}
123cb6ecee7SNick Mathewson 		event_warnx("Can't change lock callbacks once they have been "
124cb6ecee7SNick Mathewson 		    "initialized.");
125cb6ecee7SNick Mathewson 		return -1;
126cb6ecee7SNick Mathewson 	}
127347952ffSNick Mathewson 	if (cbs->alloc && cbs->free && cbs->lock && cbs->unlock) {
128cb9da0bfSNick Mathewson 		memcpy(target, cbs, sizeof(evthread_lock_fns_));
129b683cae3SNick Mathewson 		return event_global_setup_locks_(1);
130347952ffSNick Mathewson 	} else {
131347952ffSNick Mathewson 		return -1;
132347952ffSNick Mathewson 	}
133347952ffSNick Mathewson }
134347952ffSNick Mathewson 
135d4977b52SNick Mathewson int
evthread_set_condition_callbacks(const struct evthread_condition_callbacks * cbs)136d4977b52SNick Mathewson evthread_set_condition_callbacks(const struct evthread_condition_callbacks *cbs)
137d4977b52SNick Mathewson {
138c0b34f6fSAzat Khuzhin 	struct evthread_condition_callbacks *target = evthread_get_condition_callbacks();
139d4977b52SNick Mathewson 
140dcfb19a2SMark Ellzey #ifndef EVENT__DISABLE_DEBUG_MODE
141dcfb19a2SMark Ellzey 	if (event_debug_mode_on_) {
142dcfb19a2SMark Ellzey 		if (event_debug_created_threadable_ctx_) {
143dcfb19a2SMark Ellzey 		    event_errx(1, "evthread initialization must be called BEFORE anything else!");
144dcfb19a2SMark Ellzey 		}
145dcfb19a2SMark Ellzey 	}
146dcfb19a2SMark Ellzey #endif
147dcfb19a2SMark Ellzey 
148d4977b52SNick Mathewson 	if (!cbs) {
149cb6ecee7SNick Mathewson 		if (target->alloc_condition)
150cb6ecee7SNick Mathewson 			event_warnx("Trying to disable condition functions "
151cb6ecee7SNick Mathewson 			    "after they have been set up will probaby not "
152cb6ecee7SNick Mathewson 			    "work.");
153cb9da0bfSNick Mathewson 		memset(target, 0, sizeof(evthread_cond_fns_));
154cb6ecee7SNick Mathewson 		return 0;
155cb6ecee7SNick Mathewson 	}
156cb6ecee7SNick Mathewson 	if (target->alloc_condition) {
157cb6ecee7SNick Mathewson 		/* Uh oh; we already had condition callbacks set up.*/
158c94a5f2aSNate R 		if (target->condition_api_version == cbs->condition_api_version &&
159c94a5f2aSNate R 			target->alloc_condition == cbs->alloc_condition &&
160c94a5f2aSNate R 			target->free_condition == cbs->free_condition &&
161c94a5f2aSNate R 			target->signal_condition == cbs->signal_condition &&
162c94a5f2aSNate R 			target->wait_condition == cbs->wait_condition) {
163cb6ecee7SNick Mathewson 			/* no change -- allow this. */
164cb6ecee7SNick Mathewson 			return 0;
165cb6ecee7SNick Mathewson 		}
166cb6ecee7SNick Mathewson 		event_warnx("Can't change condition callbacks once they "
167cb6ecee7SNick Mathewson 		    "have been initialized.");
168cb6ecee7SNick Mathewson 		return -1;
169cb6ecee7SNick Mathewson 	}
170cb6ecee7SNick Mathewson 	if (cbs->alloc_condition && cbs->free_condition &&
171d4977b52SNick Mathewson 	    cbs->signal_condition && cbs->wait_condition) {
172cb9da0bfSNick Mathewson 		memcpy(target, cbs, sizeof(evthread_cond_fns_));
173d4977b52SNick Mathewson 	}
174cb9da0bfSNick Mathewson 	if (evthread_lock_debugging_enabled_) {
175cb9da0bfSNick Mathewson 		evthread_cond_fns_.alloc_condition = cbs->alloc_condition;
176cb9da0bfSNick Mathewson 		evthread_cond_fns_.free_condition = cbs->free_condition;
177cb9da0bfSNick Mathewson 		evthread_cond_fns_.signal_condition = cbs->signal_condition;
178d4977b52SNick Mathewson 	}
179d4977b52SNick Mathewson 	return 0;
180d4977b52SNick Mathewson }
181d4977b52SNick Mathewson 
182b4a29c0aSDave Hart #define DEBUG_LOCK_SIG	0xdeb0b10c
183b4a29c0aSDave Hart 
184347952ffSNick Mathewson struct debug_lock {
185b4a29c0aSDave Hart 	unsigned signature;
186347952ffSNick Mathewson 	unsigned locktype;
1870cd3bb9fSNick Mathewson 	unsigned long held_by;
18876cd2b70SNick Mathewson 	/* XXXX if we ever use read-write locks, we will need a separate
18976cd2b70SNick Mathewson 	 * lock to protect count. */
190347952ffSNick Mathewson 	int count;
191347952ffSNick Mathewson 	void *lock;
192347952ffSNick Mathewson };
193347952ffSNick Mathewson 
194347952ffSNick Mathewson static void *
debug_lock_alloc(unsigned locktype)195347952ffSNick Mathewson debug_lock_alloc(unsigned locktype)
196347952ffSNick Mathewson {
197347952ffSNick Mathewson 	struct debug_lock *result = mm_malloc(sizeof(struct debug_lock));
198347952ffSNick Mathewson 	if (!result)
199347952ffSNick Mathewson 		return NULL;
200cb9da0bfSNick Mathewson 	if (original_lock_fns_.alloc) {
201cb9da0bfSNick Mathewson 		if (!(result->lock = original_lock_fns_.alloc(
202347952ffSNick Mathewson 				locktype|EVTHREAD_LOCKTYPE_RECURSIVE))) {
203347952ffSNick Mathewson 			mm_free(result);
204347952ffSNick Mathewson 			return NULL;
205347952ffSNick Mathewson 		}
206c51bb3c3Sunknown 	} else {
207c51bb3c3Sunknown 		result->lock = NULL;
208347952ffSNick Mathewson 	}
209b4a29c0aSDave Hart 	result->signature = DEBUG_LOCK_SIG;
210347952ffSNick Mathewson 	result->locktype = locktype;
211347952ffSNick Mathewson 	result->count = 0;
2120cd3bb9fSNick Mathewson 	result->held_by = 0;
213347952ffSNick Mathewson 	return result;
214347952ffSNick Mathewson }
215347952ffSNick Mathewson 
216347952ffSNick Mathewson static void
debug_lock_free(void * lock_,unsigned locktype)217347952ffSNick Mathewson debug_lock_free(void *lock_, unsigned locktype)
218347952ffSNick Mathewson {
219347952ffSNick Mathewson 	struct debug_lock *lock = lock_;
220347952ffSNick Mathewson 	EVUTIL_ASSERT(lock->count == 0);
22176cd2b70SNick Mathewson 	EVUTIL_ASSERT(locktype == lock->locktype);
222b4a29c0aSDave Hart 	EVUTIL_ASSERT(DEBUG_LOCK_SIG == lock->signature);
223cb9da0bfSNick Mathewson 	if (original_lock_fns_.free) {
224cb9da0bfSNick Mathewson 		original_lock_fns_.free(lock->lock,
225347952ffSNick Mathewson 		    lock->locktype|EVTHREAD_LOCKTYPE_RECURSIVE);
226347952ffSNick Mathewson 	}
227347952ffSNick Mathewson 	lock->lock = NULL;
228347952ffSNick Mathewson 	lock->count = -100;
229f28084ddSNick Mathewson 	lock->signature = 0x12300fda;
230347952ffSNick Mathewson 	mm_free(lock);
231347952ffSNick Mathewson }
232347952ffSNick Mathewson 
233d4977b52SNick Mathewson static void
evthread_debug_lock_mark_locked(unsigned mode,struct debug_lock * lock)234d4977b52SNick Mathewson evthread_debug_lock_mark_locked(unsigned mode, struct debug_lock *lock)
235d4977b52SNick Mathewson {
236b4a29c0aSDave Hart 	EVUTIL_ASSERT(DEBUG_LOCK_SIG == lock->signature);
237d4977b52SNick Mathewson 	++lock->count;
238d4977b52SNick Mathewson 	if (!(lock->locktype & EVTHREAD_LOCKTYPE_RECURSIVE))
239d4977b52SNick Mathewson 		EVUTIL_ASSERT(lock->count == 1);
240cb9da0bfSNick Mathewson 	if (evthread_id_fn_) {
241d4977b52SNick Mathewson 		unsigned long me;
242cb9da0bfSNick Mathewson 		me = evthread_id_fn_();
243d4977b52SNick Mathewson 		if (lock->count > 1)
244d4977b52SNick Mathewson 			EVUTIL_ASSERT(lock->held_by == me);
245d4977b52SNick Mathewson 		lock->held_by = me;
246d4977b52SNick Mathewson 	}
247d4977b52SNick Mathewson }
248d4977b52SNick Mathewson 
249347952ffSNick Mathewson static int
debug_lock_lock(unsigned mode,void * lock_)250347952ffSNick Mathewson debug_lock_lock(unsigned mode, void *lock_)
251347952ffSNick Mathewson {
252347952ffSNick Mathewson 	struct debug_lock *lock = lock_;
253347952ffSNick Mathewson 	int res = 0;
25476cd2b70SNick Mathewson 	if (lock->locktype & EVTHREAD_LOCKTYPE_READWRITE)
25576cd2b70SNick Mathewson 		EVUTIL_ASSERT(mode & (EVTHREAD_READ|EVTHREAD_WRITE));
25676cd2b70SNick Mathewson 	else
25776cd2b70SNick Mathewson 		EVUTIL_ASSERT((mode & (EVTHREAD_READ|EVTHREAD_WRITE)) == 0);
258cb9da0bfSNick Mathewson 	if (original_lock_fns_.lock)
259cb9da0bfSNick Mathewson 		res = original_lock_fns_.lock(mode, lock->lock);
260347952ffSNick Mathewson 	if (!res) {
261d4977b52SNick Mathewson 		evthread_debug_lock_mark_locked(mode, lock);
262347952ffSNick Mathewson 	}
263347952ffSNick Mathewson 	return res;
264347952ffSNick Mathewson }
265347952ffSNick Mathewson 
266d4977b52SNick Mathewson static void
evthread_debug_lock_mark_unlocked(unsigned mode,struct debug_lock * lock)267d4977b52SNick Mathewson evthread_debug_lock_mark_unlocked(unsigned mode, struct debug_lock *lock)
268347952ffSNick Mathewson {
269b4a29c0aSDave Hart 	EVUTIL_ASSERT(DEBUG_LOCK_SIG == lock->signature);
27076cd2b70SNick Mathewson 	if (lock->locktype & EVTHREAD_LOCKTYPE_READWRITE)
27176cd2b70SNick Mathewson 		EVUTIL_ASSERT(mode & (EVTHREAD_READ|EVTHREAD_WRITE));
27276cd2b70SNick Mathewson 	else
27376cd2b70SNick Mathewson 		EVUTIL_ASSERT((mode & (EVTHREAD_READ|EVTHREAD_WRITE)) == 0);
274cb9da0bfSNick Mathewson 	if (evthread_id_fn_) {
275b4a29c0aSDave Hart 		unsigned long me;
276cb9da0bfSNick Mathewson 		me = evthread_id_fn_();
277b4a29c0aSDave Hart 		EVUTIL_ASSERT(lock->held_by == me);
2780cd3bb9fSNick Mathewson 		if (lock->count == 1)
2790cd3bb9fSNick Mathewson 			lock->held_by = 0;
2800cd3bb9fSNick Mathewson 	}
281347952ffSNick Mathewson 	--lock->count;
282347952ffSNick Mathewson 	EVUTIL_ASSERT(lock->count >= 0);
283d4977b52SNick Mathewson }
284d4977b52SNick Mathewson 
285d4977b52SNick Mathewson static int
debug_lock_unlock(unsigned mode,void * lock_)286d4977b52SNick Mathewson debug_lock_unlock(unsigned mode, void *lock_)
287d4977b52SNick Mathewson {
288d4977b52SNick Mathewson 	struct debug_lock *lock = lock_;
289d4977b52SNick Mathewson 	int res = 0;
290d4977b52SNick Mathewson 	evthread_debug_lock_mark_unlocked(mode, lock);
291cb9da0bfSNick Mathewson 	if (original_lock_fns_.unlock)
292cb9da0bfSNick Mathewson 		res = original_lock_fns_.unlock(mode, lock->lock);
293347952ffSNick Mathewson 	return res;
294347952ffSNick Mathewson }
295347952ffSNick Mathewson 
296d4977b52SNick Mathewson static int
debug_cond_wait(void * cond_,void * lock_,const struct timeval * tv)297946b5841SNick Mathewson debug_cond_wait(void *cond_, void *lock_, const struct timeval *tv)
298d4977b52SNick Mathewson {
299d4977b52SNick Mathewson 	int r;
300946b5841SNick Mathewson 	struct debug_lock *lock = lock_;
301b0ff7eb5SSebastian Hahn 	EVUTIL_ASSERT(lock);
302b4a29c0aSDave Hart 	EVUTIL_ASSERT(DEBUG_LOCK_SIG == lock->signature);
303946b5841SNick Mathewson 	EVLOCK_ASSERT_LOCKED(lock_);
304d4977b52SNick Mathewson 	evthread_debug_lock_mark_unlocked(0, lock);
305946b5841SNick Mathewson 	r = original_cond_fns_.wait_condition(cond_, lock->lock, tv);
306d4977b52SNick Mathewson 	evthread_debug_lock_mark_locked(0, lock);
307d4977b52SNick Mathewson 	return r;
308d4977b52SNick Mathewson }
309d4977b52SNick Mathewson 
31007e132e3SNick Mathewson /* misspelled version for backward compatibility */
311347952ffSNick Mathewson void
evthread_enable_lock_debuging(void)312347952ffSNick Mathewson evthread_enable_lock_debuging(void)
313347952ffSNick Mathewson {
31407e132e3SNick Mathewson 	evthread_enable_lock_debugging();
31507e132e3SNick Mathewson }
31607e132e3SNick Mathewson 
31707e132e3SNick Mathewson void
evthread_enable_lock_debugging(void)31807e132e3SNick Mathewson evthread_enable_lock_debugging(void)
31907e132e3SNick Mathewson {
320347952ffSNick Mathewson 	struct evthread_lock_callbacks cbs = {
321347952ffSNick Mathewson 		EVTHREAD_LOCK_API_VERSION,
322347952ffSNick Mathewson 		EVTHREAD_LOCKTYPE_RECURSIVE,
323347952ffSNick Mathewson 		debug_lock_alloc,
324347952ffSNick Mathewson 		debug_lock_free,
325347952ffSNick Mathewson 		debug_lock_lock,
326347952ffSNick Mathewson 		debug_lock_unlock
327347952ffSNick Mathewson 	};
328cb9da0bfSNick Mathewson 	if (evthread_lock_debugging_enabled_)
32976cd2b70SNick Mathewson 		return;
330cb9da0bfSNick Mathewson 	memcpy(&original_lock_fns_, &evthread_lock_fns_,
331347952ffSNick Mathewson 	    sizeof(struct evthread_lock_callbacks));
332cb9da0bfSNick Mathewson 	memcpy(&evthread_lock_fns_, &cbs,
333347952ffSNick Mathewson 	    sizeof(struct evthread_lock_callbacks));
334d4977b52SNick Mathewson 
335cb9da0bfSNick Mathewson 	memcpy(&original_cond_fns_, &evthread_cond_fns_,
336d4977b52SNick Mathewson 	    sizeof(struct evthread_condition_callbacks));
337cb9da0bfSNick Mathewson 	evthread_cond_fns_.wait_condition = debug_cond_wait;
338cb9da0bfSNick Mathewson 	evthread_lock_debugging_enabled_ = 1;
339b683cae3SNick Mathewson 
340b683cae3SNick Mathewson 	/* XXX return value should get checked. */
341b683cae3SNick Mathewson 	event_global_setup_locks_(0);
342347952ffSNick Mathewson }
343347952ffSNick Mathewson 
3440cd3bb9fSNick Mathewson int
evthread_is_debug_lock_held_(void * lock_)345cb9da0bfSNick Mathewson evthread_is_debug_lock_held_(void *lock_)
3460cd3bb9fSNick Mathewson {
3470cd3bb9fSNick Mathewson 	struct debug_lock *lock = lock_;
3480cd3bb9fSNick Mathewson 	if (! lock->count)
3490cd3bb9fSNick Mathewson 		return 0;
350cb9da0bfSNick Mathewson 	if (evthread_id_fn_) {
351cb9da0bfSNick Mathewson 		unsigned long me = evthread_id_fn_();
3520cd3bb9fSNick Mathewson 		if (lock->held_by != me)
3530cd3bb9fSNick Mathewson 			return 0;
3540cd3bb9fSNick Mathewson 	}
3550cd3bb9fSNick Mathewson 	return 1;
3560cd3bb9fSNick Mathewson }
3570cd3bb9fSNick Mathewson 
358d4977b52SNick Mathewson void *
evthread_debug_get_real_lock_(void * lock_)359cb9da0bfSNick Mathewson evthread_debug_get_real_lock_(void *lock_)
360d4977b52SNick Mathewson {
361d4977b52SNick Mathewson 	struct debug_lock *lock = lock_;
362d4977b52SNick Mathewson 	return lock->lock;
363d4977b52SNick Mathewson }
364d4977b52SNick Mathewson 
365b683cae3SNick Mathewson void *
evthread_setup_global_lock_(void * lock_,unsigned locktype,int enable_locks)366b683cae3SNick Mathewson evthread_setup_global_lock_(void *lock_, unsigned locktype, int enable_locks)
367b683cae3SNick Mathewson {
368b683cae3SNick Mathewson 	/* there are four cases here:
369b683cae3SNick Mathewson 	   1) we're turning on debugging; locking is not on.
370b683cae3SNick Mathewson 	   2) we're turning on debugging; locking is on.
371b683cae3SNick Mathewson 	   3) we're turning on locking; debugging is not on.
372b683cae3SNick Mathewson 	   4) we're turning on locking; debugging is on. */
373b683cae3SNick Mathewson 
374cb9da0bfSNick Mathewson 	if (!enable_locks && original_lock_fns_.alloc == NULL) {
375b683cae3SNick Mathewson 		/* Case 1: allocate a debug lock. */
376b683cae3SNick Mathewson 		EVUTIL_ASSERT(lock_ == NULL);
377b683cae3SNick Mathewson 		return debug_lock_alloc(locktype);
378cb9da0bfSNick Mathewson 	} else if (!enable_locks && original_lock_fns_.alloc != NULL) {
379b683cae3SNick Mathewson 		/* Case 2: wrap the lock in a debug lock. */
380b683cae3SNick Mathewson 		struct debug_lock *lock;
381b683cae3SNick Mathewson 		EVUTIL_ASSERT(lock_ != NULL);
382b683cae3SNick Mathewson 
383b683cae3SNick Mathewson 		if (!(locktype & EVTHREAD_LOCKTYPE_RECURSIVE)) {
384b683cae3SNick Mathewson 			/* We can't wrap it: We need a recursive lock */
385cb9da0bfSNick Mathewson 			original_lock_fns_.free(lock_, locktype);
386b683cae3SNick Mathewson 			return debug_lock_alloc(locktype);
387b683cae3SNick Mathewson 		}
388b683cae3SNick Mathewson 		lock = mm_malloc(sizeof(struct debug_lock));
389b683cae3SNick Mathewson 		if (!lock) {
390cb9da0bfSNick Mathewson 			original_lock_fns_.free(lock_, locktype);
391b683cae3SNick Mathewson 			return NULL;
392b683cae3SNick Mathewson 		}
393b683cae3SNick Mathewson 		lock->lock = lock_;
394b683cae3SNick Mathewson 		lock->locktype = locktype;
395b683cae3SNick Mathewson 		lock->count = 0;
396b683cae3SNick Mathewson 		lock->held_by = 0;
397b683cae3SNick Mathewson 		return lock;
398cb9da0bfSNick Mathewson 	} else if (enable_locks && ! evthread_lock_debugging_enabled_) {
399b683cae3SNick Mathewson 		/* Case 3: allocate a regular lock */
400b683cae3SNick Mathewson 		EVUTIL_ASSERT(lock_ == NULL);
401cb9da0bfSNick Mathewson 		return evthread_lock_fns_.alloc(locktype);
402b683cae3SNick Mathewson 	} else {
403b683cae3SNick Mathewson 		/* Case 4: Fill in a debug lock with a real lock */
404*e4556fcdSAzat Khuzhin 		struct debug_lock *lock = lock_ ? lock_ : debug_lock_alloc(locktype);
405b683cae3SNick Mathewson 		EVUTIL_ASSERT(enable_locks &&
406cb9da0bfSNick Mathewson 		              evthread_lock_debugging_enabled_);
407b683cae3SNick Mathewson 		EVUTIL_ASSERT(lock->locktype == locktype);
408*e4556fcdSAzat Khuzhin 		if (!lock->lock) {
409cb9da0bfSNick Mathewson 			lock->lock = original_lock_fns_.alloc(
410b683cae3SNick Mathewson 				locktype|EVTHREAD_LOCKTYPE_RECURSIVE);
411b683cae3SNick Mathewson 			if (!lock->lock) {
412b683cae3SNick Mathewson 				lock->count = -200;
413b683cae3SNick Mathewson 				mm_free(lock);
414b683cae3SNick Mathewson 				return NULL;
415b683cae3SNick Mathewson 			}
416*e4556fcdSAzat Khuzhin 		}
417b683cae3SNick Mathewson 		return lock;
418b683cae3SNick Mathewson 	}
419b683cae3SNick Mathewson }
420b683cae3SNick Mathewson 
421b683cae3SNick Mathewson 
4225de2bcb7SNick Mathewson #ifndef EVTHREAD_EXPOSE_STRUCTS
4235de2bcb7SNick Mathewson unsigned long
evthreadimpl_get_id_()424cb9da0bfSNick Mathewson evthreadimpl_get_id_()
4255de2bcb7SNick Mathewson {
426cb9da0bfSNick Mathewson 	return evthread_id_fn_ ? evthread_id_fn_() : 1;
4275de2bcb7SNick Mathewson }
4285de2bcb7SNick Mathewson void *
evthreadimpl_lock_alloc_(unsigned locktype)429cb9da0bfSNick Mathewson evthreadimpl_lock_alloc_(unsigned locktype)
4305de2bcb7SNick Mathewson {
431dcfb19a2SMark Ellzey #ifndef EVENT__DISABLE_DEBUG_MODE
432dcfb19a2SMark Ellzey 	if (event_debug_mode_on_) {
433dcfb19a2SMark Ellzey 		event_debug_created_threadable_ctx_ = 1;
434dcfb19a2SMark Ellzey 	}
435dcfb19a2SMark Ellzey #endif
436dcfb19a2SMark Ellzey 
437cb9da0bfSNick Mathewson 	return evthread_lock_fns_.alloc ?
438cb9da0bfSNick Mathewson 	    evthread_lock_fns_.alloc(locktype) : NULL;
4395de2bcb7SNick Mathewson }
4405de2bcb7SNick Mathewson void
evthreadimpl_lock_free_(void * lock,unsigned locktype)441cb9da0bfSNick Mathewson evthreadimpl_lock_free_(void *lock, unsigned locktype)
4425de2bcb7SNick Mathewson {
443cb9da0bfSNick Mathewson 	if (evthread_lock_fns_.free)
444cb9da0bfSNick Mathewson 		evthread_lock_fns_.free(lock, locktype);
4455de2bcb7SNick Mathewson }
4465de2bcb7SNick Mathewson int
evthreadimpl_lock_lock_(unsigned mode,void * lock)447cb9da0bfSNick Mathewson evthreadimpl_lock_lock_(unsigned mode, void *lock)
4485de2bcb7SNick Mathewson {
449cb9da0bfSNick Mathewson 	if (evthread_lock_fns_.lock)
450cb9da0bfSNick Mathewson 		return evthread_lock_fns_.lock(mode, lock);
4515de2bcb7SNick Mathewson 	else
4525de2bcb7SNick Mathewson 		return 0;
4535de2bcb7SNick Mathewson }
4545de2bcb7SNick Mathewson int
evthreadimpl_lock_unlock_(unsigned mode,void * lock)455cb9da0bfSNick Mathewson evthreadimpl_lock_unlock_(unsigned mode, void *lock)
4565de2bcb7SNick Mathewson {
457cb9da0bfSNick Mathewson 	if (evthread_lock_fns_.unlock)
458cb9da0bfSNick Mathewson 		return evthread_lock_fns_.unlock(mode, lock);
4595de2bcb7SNick Mathewson 	else
4605de2bcb7SNick Mathewson 		return 0;
4615de2bcb7SNick Mathewson }
4625de2bcb7SNick Mathewson void *
evthreadimpl_cond_alloc_(unsigned condtype)463cb9da0bfSNick Mathewson evthreadimpl_cond_alloc_(unsigned condtype)
4645de2bcb7SNick Mathewson {
465dcfb19a2SMark Ellzey #ifndef EVENT__DISABLE_DEBUG_MODE
466dcfb19a2SMark Ellzey 	if (event_debug_mode_on_) {
467dcfb19a2SMark Ellzey 		event_debug_created_threadable_ctx_ = 1;
468dcfb19a2SMark Ellzey 	}
469dcfb19a2SMark Ellzey #endif
470dcfb19a2SMark Ellzey 
471cb9da0bfSNick Mathewson 	return evthread_cond_fns_.alloc_condition ?
472cb9da0bfSNick Mathewson 	    evthread_cond_fns_.alloc_condition(condtype) : NULL;
4735de2bcb7SNick Mathewson }
4745de2bcb7SNick Mathewson void
evthreadimpl_cond_free_(void * cond)475cb9da0bfSNick Mathewson evthreadimpl_cond_free_(void *cond)
4765de2bcb7SNick Mathewson {
477cb9da0bfSNick Mathewson 	if (evthread_cond_fns_.free_condition)
478cb9da0bfSNick Mathewson 		evthread_cond_fns_.free_condition(cond);
4795de2bcb7SNick Mathewson }
4805de2bcb7SNick Mathewson int
evthreadimpl_cond_signal_(void * cond,int broadcast)481cb9da0bfSNick Mathewson evthreadimpl_cond_signal_(void *cond, int broadcast)
4825de2bcb7SNick Mathewson {
483cb9da0bfSNick Mathewson 	if (evthread_cond_fns_.signal_condition)
484cb9da0bfSNick Mathewson 		return evthread_cond_fns_.signal_condition(cond, broadcast);
4855de2bcb7SNick Mathewson 	else
4865de2bcb7SNick Mathewson 		return 0;
4875de2bcb7SNick Mathewson }
4885de2bcb7SNick Mathewson int
evthreadimpl_cond_wait_(void * cond,void * lock,const struct timeval * tv)489cb9da0bfSNick Mathewson evthreadimpl_cond_wait_(void *cond, void *lock, const struct timeval *tv)
4905de2bcb7SNick Mathewson {
491cb9da0bfSNick Mathewson 	if (evthread_cond_fns_.wait_condition)
492cb9da0bfSNick Mathewson 		return evthread_cond_fns_.wait_condition(cond, lock, tv);
4935de2bcb7SNick Mathewson 	else
4945de2bcb7SNick Mathewson 		return 0;
4955de2bcb7SNick Mathewson }
4965de2bcb7SNick Mathewson int
evthreadimpl_is_lock_debugging_enabled_(void)497cb9da0bfSNick Mathewson evthreadimpl_is_lock_debugging_enabled_(void)
4985de2bcb7SNick Mathewson {
499cb9da0bfSNick Mathewson 	return evthread_lock_debugging_enabled_;
5005de2bcb7SNick Mathewson }
501e7874133SNick Mathewson 
502e7874133SNick Mathewson int
evthreadimpl_locking_enabled_(void)503cb9da0bfSNick Mathewson evthreadimpl_locking_enabled_(void)
504e7874133SNick Mathewson {
505cb9da0bfSNick Mathewson 	return evthread_lock_fns_.lock != NULL;
506e7874133SNick Mathewson }
5075de2bcb7SNick Mathewson #endif
5085de2bcb7SNick Mathewson 
509347952ffSNick Mathewson #endif
510