Blame libevent/evthread-internal.h

Packit e9ba0d
/*
Packit e9ba0d
 * Copyright (c) 2008-2012 Niels Provos, Nick Mathewson
Packit e9ba0d
 *
Packit e9ba0d
 * Redistribution and use in source and binary forms, with or without
Packit e9ba0d
 * modification, are permitted provided that the following conditions
Packit e9ba0d
 * are met:
Packit e9ba0d
 * 1. Redistributions of source code must retain the above copyright
Packit e9ba0d
 *    notice, this list of conditions and the following disclaimer.
Packit e9ba0d
 * 2. Redistributions in binary form must reproduce the above copyright
Packit e9ba0d
 *    notice, this list of conditions and the following disclaimer in the
Packit e9ba0d
 *    documentation and/or other materials provided with the distribution.
Packit e9ba0d
 * 3. The name of the author may not be used to endorse or promote products
Packit e9ba0d
 *    derived from this software without specific prior written permission.
Packit e9ba0d
 *
Packit e9ba0d
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
Packit e9ba0d
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
Packit e9ba0d
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
Packit e9ba0d
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
Packit e9ba0d
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
Packit e9ba0d
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit e9ba0d
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit e9ba0d
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit e9ba0d
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
Packit e9ba0d
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit e9ba0d
 */
Packit e9ba0d
#ifndef _EVTHREAD_INTERNAL_H_
Packit e9ba0d
#define _EVTHREAD_INTERNAL_H_
Packit e9ba0d
Packit e9ba0d
#ifdef __cplusplus
Packit e9ba0d
extern "C" {
Packit e9ba0d
#endif
Packit e9ba0d
Packit e9ba0d
#include "event2/thread.h"
Packit e9ba0d
#include "event2/event-config.h"
Packit e9ba0d
#include "util-internal.h"
Packit e9ba0d
Packit e9ba0d
struct event_base;
Packit e9ba0d
Packit e9ba0d
#ifndef WIN32
Packit e9ba0d
/* On Windows, the way we currently make DLLs, it's not allowed for us to
Packit e9ba0d
 * have shared global structures.  Thus, we only do the direct-call-to-function
Packit e9ba0d
 * code path if we know that the local shared library system supports it.
Packit e9ba0d
 */
Packit e9ba0d
#define EVTHREAD_EXPOSE_STRUCTS
Packit e9ba0d
#endif
Packit e9ba0d
Packit e9ba0d
#if ! defined(_EVENT_DISABLE_THREAD_SUPPORT) && defined(EVTHREAD_EXPOSE_STRUCTS)
Packit e9ba0d
/* Global function pointers to lock-related functions. NULL if locking isn't
Packit e9ba0d
   enabled. */
Packit e9ba0d
extern struct evthread_lock_callbacks _evthread_lock_fns;
Packit e9ba0d
extern struct evthread_condition_callbacks _evthread_cond_fns;
Packit e9ba0d
extern unsigned long (*_evthread_id_fn)(void);
Packit e9ba0d
extern int _evthread_lock_debugging_enabled;
Packit e9ba0d
Packit e9ba0d
/** Return the ID of the current thread, or 1 if threading isn't enabled. */
Packit e9ba0d
#define EVTHREAD_GET_ID() \
Packit e9ba0d
	(_evthread_id_fn ? _evthread_id_fn() : 1)
Packit e9ba0d
Packit e9ba0d
/** Return true iff we're in the thread that is currently (or most recently)
Packit e9ba0d
 * running a given event_base's loop. Requires lock. */
Packit e9ba0d
#define EVBASE_IN_THREAD(base)				 \
Packit e9ba0d
	(_evthread_id_fn == NULL ||			 \
Packit e9ba0d
	(base)->th_owner_id == _evthread_id_fn())
Packit e9ba0d
Packit e9ba0d
/** Return true iff we need to notify the base's main thread about changes to
Packit e9ba0d
 * its state, because it's currently running the main loop in another
Packit e9ba0d
 * thread. Requires lock. */
Packit e9ba0d
#define EVBASE_NEED_NOTIFY(base)			 \
Packit e9ba0d
	(_evthread_id_fn != NULL &&			 \
Packit e9ba0d
	    (base)->running_loop &&			 \
Packit e9ba0d
	    (base)->th_owner_id != _evthread_id_fn())
Packit e9ba0d
Packit e9ba0d
/** Allocate a new lock, and store it in lockvar, a void*.  Sets lockvar to
Packit e9ba0d
    NULL if locking is not enabled. */
Packit e9ba0d
#define EVTHREAD_ALLOC_LOCK(lockvar, locktype)		\
Packit e9ba0d
	((lockvar) = _evthread_lock_fns.alloc ?		\
Packit e9ba0d
	    _evthread_lock_fns.alloc(locktype) : NULL)
Packit e9ba0d
Packit e9ba0d
/** Free a given lock, if it is present and locking is enabled. */
Packit e9ba0d
#define EVTHREAD_FREE_LOCK(lockvar, locktype)				\
Packit e9ba0d
	do {								\
Packit e9ba0d
		void *_lock_tmp_ = (lockvar);				\
Packit e9ba0d
		if (_lock_tmp_ && _evthread_lock_fns.free)		\
Packit e9ba0d
			_evthread_lock_fns.free(_lock_tmp_, (locktype)); \
Packit e9ba0d
	} while (0)
Packit e9ba0d
Packit e9ba0d
/** Acquire a lock. */
Packit e9ba0d
#define EVLOCK_LOCK(lockvar,mode)					\
Packit e9ba0d
	do {								\
Packit e9ba0d
		if (lockvar)						\
Packit e9ba0d
			_evthread_lock_fns.lock(mode, lockvar);		\
Packit e9ba0d
	} while (0)
Packit e9ba0d
Packit e9ba0d
/** Release a lock */
Packit e9ba0d
#define EVLOCK_UNLOCK(lockvar,mode)					\
Packit e9ba0d
	do {								\
Packit e9ba0d
		if (lockvar)						\
Packit e9ba0d
			_evthread_lock_fns.unlock(mode, lockvar);	\
Packit e9ba0d
	} while (0)
Packit e9ba0d
Packit e9ba0d
/** Helper: put lockvar1 and lockvar2 into pointerwise ascending order. */
Packit e9ba0d
#define _EVLOCK_SORTLOCKS(lockvar1, lockvar2)				\
Packit e9ba0d
	do {								\
Packit e9ba0d
		if (lockvar1 && lockvar2 && lockvar1 > lockvar2) {	\
Packit e9ba0d
			void *tmp = lockvar1;				\
Packit e9ba0d
			lockvar1 = lockvar2;				\
Packit e9ba0d
			lockvar2 = tmp;					\
Packit e9ba0d
		}							\
Packit e9ba0d
	} while (0)
Packit e9ba0d
Packit e9ba0d
/** Lock an event_base, if it is set up for locking.  Acquires the lock
Packit e9ba0d
    in the base structure whose field is named 'lockvar'. */
Packit e9ba0d
#define EVBASE_ACQUIRE_LOCK(base, lockvar) do {				\
Packit e9ba0d
		EVLOCK_LOCK((base)->lockvar, 0);			\
Packit e9ba0d
	} while (0)
Packit e9ba0d
Packit e9ba0d
/** Unlock an event_base, if it is set up for locking. */
Packit e9ba0d
#define EVBASE_RELEASE_LOCK(base, lockvar) do {				\
Packit e9ba0d
		EVLOCK_UNLOCK((base)->lockvar, 0);			\
Packit e9ba0d
	} while (0)
Packit e9ba0d
Packit e9ba0d
/** If lock debugging is enabled, and lock is non-null, assert that 'lock' is
Packit e9ba0d
 * locked and held by us. */
Packit e9ba0d
#define EVLOCK_ASSERT_LOCKED(lock)					\
Packit e9ba0d
	do {								\
Packit e9ba0d
		if ((lock) && _evthread_lock_debugging_enabled) {	\
Packit e9ba0d
			EVUTIL_ASSERT(_evthread_is_debug_lock_held(lock)); \
Packit e9ba0d
		}							\
Packit e9ba0d
	} while (0)
Packit e9ba0d
Packit e9ba0d
/** Try to grab the lock for 'lockvar' without blocking, and return 1 if we
Packit e9ba0d
 * manage to get it. */
Packit e9ba0d
static inline int EVLOCK_TRY_LOCK(void *lock);
Packit e9ba0d
static inline int
Packit e9ba0d
EVLOCK_TRY_LOCK(void *lock)
Packit e9ba0d
{
Packit e9ba0d
	if (lock && _evthread_lock_fns.lock) {
Packit e9ba0d
		int r = _evthread_lock_fns.lock(EVTHREAD_TRY, lock);
Packit e9ba0d
		return !r;
Packit e9ba0d
	} else {
Packit e9ba0d
		/* Locking is disabled either globally or for this thing;
Packit e9ba0d
		 * of course we count as having the lock. */
Packit e9ba0d
		return 1;
Packit e9ba0d
	}
Packit e9ba0d
}
Packit e9ba0d
Packit e9ba0d
/** Allocate a new condition variable and store it in the void *, condvar */
Packit e9ba0d
#define EVTHREAD_ALLOC_COND(condvar)					\
Packit e9ba0d
	do {								\
Packit e9ba0d
		(condvar) = _evthread_cond_fns.alloc_condition ?	\
Packit e9ba0d
		    _evthread_cond_fns.alloc_condition(0) : NULL;	\
Packit e9ba0d
	} while (0)
Packit e9ba0d
/** Deallocate and free a condition variable in condvar */
Packit e9ba0d
#define EVTHREAD_FREE_COND(cond)					\
Packit e9ba0d
	do {								\
Packit e9ba0d
		if (cond)						\
Packit e9ba0d
			_evthread_cond_fns.free_condition((cond));	\
Packit e9ba0d
	} while (0)
Packit e9ba0d
/** Signal one thread waiting on cond */
Packit e9ba0d
#define EVTHREAD_COND_SIGNAL(cond)					\
Packit e9ba0d
	( (cond) ? _evthread_cond_fns.signal_condition((cond), 0) : 0 )
Packit e9ba0d
/** Signal all threads waiting on cond */
Packit e9ba0d
#define EVTHREAD_COND_BROADCAST(cond)					\
Packit e9ba0d
	( (cond) ? _evthread_cond_fns.signal_condition((cond), 1) : 0 )
Packit e9ba0d
/** Wait until the condition 'cond' is signalled.  Must be called while
Packit e9ba0d
 * holding 'lock'.  The lock will be released until the condition is
Packit e9ba0d
 * signalled, at which point it will be acquired again.  Returns 0 for
Packit e9ba0d
 * success, -1 for failure. */
Packit e9ba0d
#define EVTHREAD_COND_WAIT(cond, lock)					\
Packit e9ba0d
	( (cond) ? _evthread_cond_fns.wait_condition((cond), (lock), NULL) : 0 )
Packit e9ba0d
/** As EVTHREAD_COND_WAIT, but gives up after 'tv' has elapsed.  Returns 1
Packit e9ba0d
 * on timeout. */
Packit e9ba0d
#define EVTHREAD_COND_WAIT_TIMED(cond, lock, tv)			\
Packit e9ba0d
	( (cond) ? _evthread_cond_fns.wait_condition((cond), (lock), (tv)) : 0 )
Packit e9ba0d
Packit e9ba0d
/** True iff locking functions have been configured. */
Packit e9ba0d
#define EVTHREAD_LOCKING_ENABLED()		\
Packit e9ba0d
	(_evthread_lock_fns.lock != NULL)
Packit e9ba0d
Packit e9ba0d
#elif ! defined(_EVENT_DISABLE_THREAD_SUPPORT)
Packit e9ba0d
Packit e9ba0d
unsigned long _evthreadimpl_get_id(void);
Packit e9ba0d
int _evthreadimpl_is_lock_debugging_enabled(void);
Packit e9ba0d
void *_evthreadimpl_lock_alloc(unsigned locktype);
Packit e9ba0d
void _evthreadimpl_lock_free(void *lock, unsigned locktype);
Packit e9ba0d
int _evthreadimpl_lock_lock(unsigned mode, void *lock);
Packit e9ba0d
int _evthreadimpl_lock_unlock(unsigned mode, void *lock);
Packit e9ba0d
void *_evthreadimpl_cond_alloc(unsigned condtype);
Packit e9ba0d
void _evthreadimpl_cond_free(void *cond);
Packit e9ba0d
int _evthreadimpl_cond_signal(void *cond, int broadcast);
Packit e9ba0d
int _evthreadimpl_cond_wait(void *cond, void *lock, const struct timeval *tv);
Packit e9ba0d
int _evthreadimpl_locking_enabled(void);
Packit e9ba0d
Packit e9ba0d
#define EVTHREAD_GET_ID() _evthreadimpl_get_id()
Packit e9ba0d
#define EVBASE_IN_THREAD(base)				\
Packit e9ba0d
	((base)->th_owner_id == _evthreadimpl_get_id())
Packit e9ba0d
#define EVBASE_NEED_NOTIFY(base)			 \
Packit e9ba0d
	((base)->running_loop &&			 \
Packit e9ba0d
	    ((base)->th_owner_id != _evthreadimpl_get_id()))
Packit e9ba0d
Packit e9ba0d
#define EVTHREAD_ALLOC_LOCK(lockvar, locktype)		\
Packit e9ba0d
	((lockvar) = _evthreadimpl_lock_alloc(locktype))
Packit e9ba0d
Packit e9ba0d
#define EVTHREAD_FREE_LOCK(lockvar, locktype)				\
Packit e9ba0d
	do {								\
Packit e9ba0d
		void *_lock_tmp_ = (lockvar);				\
Packit e9ba0d
		if (_lock_tmp_)						\
Packit e9ba0d
			_evthreadimpl_lock_free(_lock_tmp_, (locktype)); \
Packit e9ba0d
	} while (0)
Packit e9ba0d
Packit e9ba0d
/** Acquire a lock. */
Packit e9ba0d
#define EVLOCK_LOCK(lockvar,mode)					\
Packit e9ba0d
	do {								\
Packit e9ba0d
		if (lockvar)						\
Packit e9ba0d
			_evthreadimpl_lock_lock(mode, lockvar);		\
Packit e9ba0d
	} while (0)
Packit e9ba0d
Packit e9ba0d
/** Release a lock */
Packit e9ba0d
#define EVLOCK_UNLOCK(lockvar,mode)					\
Packit e9ba0d
	do {								\
Packit e9ba0d
		if (lockvar)						\
Packit e9ba0d
			_evthreadimpl_lock_unlock(mode, lockvar);	\
Packit e9ba0d
	} while (0)
Packit e9ba0d
Packit e9ba0d
/** Lock an event_base, if it is set up for locking.  Acquires the lock
Packit e9ba0d
    in the base structure whose field is named 'lockvar'. */
Packit e9ba0d
#define EVBASE_ACQUIRE_LOCK(base, lockvar) do {				\
Packit e9ba0d
		EVLOCK_LOCK((base)->lockvar, 0);			\
Packit e9ba0d
	} while (0)
Packit e9ba0d
Packit e9ba0d
/** Unlock an event_base, if it is set up for locking. */
Packit e9ba0d
#define EVBASE_RELEASE_LOCK(base, lockvar) do {				\
Packit e9ba0d
		EVLOCK_UNLOCK((base)->lockvar, 0);			\
Packit e9ba0d
	} while (0)
Packit e9ba0d
Packit e9ba0d
/** If lock debugging is enabled, and lock is non-null, assert that 'lock' is
Packit e9ba0d
 * locked and held by us. */
Packit e9ba0d
#define EVLOCK_ASSERT_LOCKED(lock)					\
Packit e9ba0d
	do {								\
Packit e9ba0d
		if ((lock) && _evthreadimpl_is_lock_debugging_enabled()) { \
Packit e9ba0d
			EVUTIL_ASSERT(_evthread_is_debug_lock_held(lock)); \
Packit e9ba0d
		}							\
Packit e9ba0d
	} while (0)
Packit e9ba0d
Packit e9ba0d
/** Try to grab the lock for 'lockvar' without blocking, and return 1 if we
Packit e9ba0d
 * manage to get it. */
Packit e9ba0d
static inline int EVLOCK_TRY_LOCK(void *lock);
Packit e9ba0d
static inline int
Packit e9ba0d
EVLOCK_TRY_LOCK(void *lock)
Packit e9ba0d
{
Packit e9ba0d
	if (lock) {
Packit e9ba0d
		int r = _evthreadimpl_lock_lock(EVTHREAD_TRY, lock);
Packit e9ba0d
		return !r;
Packit e9ba0d
	} else {
Packit e9ba0d
		/* Locking is disabled either globally or for this thing;
Packit e9ba0d
		 * of course we count as having the lock. */
Packit e9ba0d
		return 1;
Packit e9ba0d
	}
Packit e9ba0d
}
Packit e9ba0d
Packit e9ba0d
/** Allocate a new condition variable and store it in the void *, condvar */
Packit e9ba0d
#define EVTHREAD_ALLOC_COND(condvar)					\
Packit e9ba0d
	do {								\
Packit e9ba0d
		(condvar) = _evthreadimpl_cond_alloc(0);		\
Packit e9ba0d
	} while (0)
Packit e9ba0d
/** Deallocate and free a condition variable in condvar */
Packit e9ba0d
#define EVTHREAD_FREE_COND(cond)					\
Packit e9ba0d
	do {								\
Packit e9ba0d
		if (cond)						\
Packit e9ba0d
			_evthreadimpl_cond_free((cond));		\
Packit e9ba0d
	} while (0)
Packit e9ba0d
/** Signal one thread waiting on cond */
Packit e9ba0d
#define EVTHREAD_COND_SIGNAL(cond)					\
Packit e9ba0d
	( (cond) ? _evthreadimpl_cond_signal((cond), 0) : 0 )
Packit e9ba0d
/** Signal all threads waiting on cond */
Packit e9ba0d
#define EVTHREAD_COND_BROADCAST(cond)					\
Packit e9ba0d
	( (cond) ? _evthreadimpl_cond_signal((cond), 1) : 0 )
Packit e9ba0d
/** Wait until the condition 'cond' is signalled.  Must be called while
Packit e9ba0d
 * holding 'lock'.  The lock will be released until the condition is
Packit e9ba0d
 * signalled, at which point it will be acquired again.  Returns 0 for
Packit e9ba0d
 * success, -1 for failure. */
Packit e9ba0d
#define EVTHREAD_COND_WAIT(cond, lock)					\
Packit e9ba0d
	( (cond) ? _evthreadimpl_cond_wait((cond), (lock), NULL) : 0 )
Packit e9ba0d
/** As EVTHREAD_COND_WAIT, but gives up after 'tv' has elapsed.  Returns 1
Packit e9ba0d
 * on timeout. */
Packit e9ba0d
#define EVTHREAD_COND_WAIT_TIMED(cond, lock, tv)			\
Packit e9ba0d
	( (cond) ? _evthreadimpl_cond_wait((cond), (lock), (tv)) : 0 )
Packit e9ba0d
Packit e9ba0d
#define EVTHREAD_LOCKING_ENABLED()		\
Packit e9ba0d
	(_evthreadimpl_locking_enabled())
Packit e9ba0d
Packit e9ba0d
#else /* _EVENT_DISABLE_THREAD_SUPPORT */
Packit e9ba0d
Packit e9ba0d
#define EVTHREAD_GET_ID()	1
Packit e9ba0d
#define EVTHREAD_ALLOC_LOCK(lockvar, locktype) _EVUTIL_NIL_STMT
Packit e9ba0d
#define EVTHREAD_FREE_LOCK(lockvar, locktype) _EVUTIL_NIL_STMT
Packit e9ba0d
Packit e9ba0d
#define EVLOCK_LOCK(lockvar, mode) _EVUTIL_NIL_STMT
Packit e9ba0d
#define EVLOCK_UNLOCK(lockvar, mode) _EVUTIL_NIL_STMT
Packit e9ba0d
#define EVLOCK_LOCK2(lock1,lock2,mode1,mode2) _EVUTIL_NIL_STMT
Packit e9ba0d
#define EVLOCK_UNLOCK2(lock1,lock2,mode1,mode2) _EVUTIL_NIL_STMT
Packit e9ba0d
Packit e9ba0d
#define EVBASE_IN_THREAD(base)	1
Packit e9ba0d
#define EVBASE_NEED_NOTIFY(base) 0
Packit e9ba0d
#define EVBASE_ACQUIRE_LOCK(base, lock) _EVUTIL_NIL_STMT
Packit e9ba0d
#define EVBASE_RELEASE_LOCK(base, lock) _EVUTIL_NIL_STMT
Packit e9ba0d
#define EVLOCK_ASSERT_LOCKED(lock) _EVUTIL_NIL_STMT
Packit e9ba0d
Packit e9ba0d
#define EVLOCK_TRY_LOCK(lock) 1
Packit e9ba0d
Packit e9ba0d
#define EVTHREAD_ALLOC_COND(condvar) _EVUTIL_NIL_STMT
Packit e9ba0d
#define EVTHREAD_FREE_COND(cond) _EVUTIL_NIL_STMT
Packit e9ba0d
#define EVTHREAD_COND_SIGNAL(cond) _EVUTIL_NIL_STMT
Packit e9ba0d
#define EVTHREAD_COND_BROADCAST(cond) _EVUTIL_NIL_STMT
Packit e9ba0d
#define EVTHREAD_COND_WAIT(cond, lock) _EVUTIL_NIL_STMT
Packit e9ba0d
#define EVTHREAD_COND_WAIT_TIMED(cond, lock, howlong) _EVUTIL_NIL_STMT
Packit e9ba0d
Packit e9ba0d
#define EVTHREAD_LOCKING_ENABLED() 0
Packit e9ba0d
Packit e9ba0d
#endif
Packit e9ba0d
Packit e9ba0d
/* This code is shared between both lock impls */
Packit e9ba0d
#if ! defined(_EVENT_DISABLE_THREAD_SUPPORT)
Packit e9ba0d
/** Helper: put lockvar1 and lockvar2 into pointerwise ascending order. */
Packit e9ba0d
#define _EVLOCK_SORTLOCKS(lockvar1, lockvar2)				\
Packit e9ba0d
	do {								\
Packit e9ba0d
		if (lockvar1 && lockvar2 && lockvar1 > lockvar2) {	\
Packit e9ba0d
			void *tmp = lockvar1;				\
Packit e9ba0d
			lockvar1 = lockvar2;				\
Packit e9ba0d
			lockvar2 = tmp;					\
Packit e9ba0d
		}							\
Packit e9ba0d
	} while (0)
Packit e9ba0d
Packit e9ba0d
/** Acquire both lock1 and lock2.  Always allocates locks in the same order,
Packit e9ba0d
 * so that two threads locking two locks with LOCK2 will not deadlock. */
Packit e9ba0d
#define EVLOCK_LOCK2(lock1,lock2,mode1,mode2)				\
Packit e9ba0d
	do {								\
Packit e9ba0d
		void *_lock1_tmplock = (lock1);				\
Packit e9ba0d
		void *_lock2_tmplock = (lock2);				\
Packit e9ba0d
		_EVLOCK_SORTLOCKS(_lock1_tmplock,_lock2_tmplock);	\
Packit e9ba0d
		EVLOCK_LOCK(_lock1_tmplock,mode1);			\
Packit e9ba0d
		if (_lock2_tmplock != _lock1_tmplock)			\
Packit e9ba0d
			EVLOCK_LOCK(_lock2_tmplock,mode2);		\
Packit e9ba0d
	} while (0)
Packit e9ba0d
/** Release both lock1 and lock2.  */
Packit e9ba0d
#define EVLOCK_UNLOCK2(lock1,lock2,mode1,mode2)				\
Packit e9ba0d
	do {								\
Packit e9ba0d
		void *_lock1_tmplock = (lock1);				\
Packit e9ba0d
		void *_lock2_tmplock = (lock2);				\
Packit e9ba0d
		_EVLOCK_SORTLOCKS(_lock1_tmplock,_lock2_tmplock);	\
Packit e9ba0d
		if (_lock2_tmplock != _lock1_tmplock)			\
Packit e9ba0d
			EVLOCK_UNLOCK(_lock2_tmplock,mode2);		\
Packit e9ba0d
		EVLOCK_UNLOCK(_lock1_tmplock,mode1);			\
Packit e9ba0d
	} while (0)
Packit e9ba0d
Packit e9ba0d
int _evthread_is_debug_lock_held(void *lock);
Packit e9ba0d
void *_evthread_debug_get_real_lock(void *lock);
Packit e9ba0d
Packit e9ba0d
void *evthread_setup_global_lock_(void *lock_, unsigned locktype,
Packit e9ba0d
    int enable_locks);
Packit e9ba0d
Packit e9ba0d
#define EVTHREAD_SETUP_GLOBAL_LOCK(lockvar, locktype)			\
Packit e9ba0d
	do {								\
Packit e9ba0d
		lockvar = evthread_setup_global_lock_(lockvar,		\
Packit e9ba0d
		    (locktype), enable_locks);				\
Packit e9ba0d
		if (!lockvar) {						\
Packit e9ba0d
			event_warn("Couldn't allocate %s", #lockvar);	\
Packit e9ba0d
			return -1;					\
Packit e9ba0d
		}							\
Packit e9ba0d
	} while (0);
Packit e9ba0d
Packit e9ba0d
int event_global_setup_locks_(const int enable_locks);
Packit e9ba0d
int evsig_global_setup_locks_(const int enable_locks);
Packit e9ba0d
int evutil_secure_rng_global_setup_locks_(const int enable_locks);
Packit e9ba0d
Packit e9ba0d
#endif
Packit e9ba0d
Packit e9ba0d
#ifdef __cplusplus
Packit e9ba0d
}
Packit e9ba0d
#endif
Packit e9ba0d
Packit e9ba0d
#endif /* _EVTHREAD_INTERNAL_H_ */