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