aboutsummaryrefslogtreecommitdiff
path: root/evthread.c
diff options
context:
space:
mode:
Diffstat (limited to 'evthread.c')
-rw-r--r--evthread.c248
1 files changed, 92 insertions, 156 deletions
diff --git a/evthread.c b/evthread.c
index f3f1edd..90e195d 100644
--- a/evthread.c
+++ b/evthread.c
@@ -25,9 +25,8 @@
*/
#include "event2/event-config.h"
-#include "evconfig-private.h"
-#ifndef EVENT__DISABLE_THREAD_SUPPORT
+#ifndef _EVENT_DISABLE_THREAD_SUPPORT
#include "event2/thread.h"
@@ -45,68 +44,42 @@
#define GLOBAL static
#endif
-#ifndef EVENT__DISABLE_DEBUG_MODE
-extern int event_debug_created_threadable_ctx_;
-extern int event_debug_mode_on_;
-#endif
-
/* globals */
-GLOBAL int evthread_lock_debugging_enabled_ = 0;
-GLOBAL struct evthread_lock_callbacks evthread_lock_fns_ = {
+GLOBAL int _evthread_lock_debugging_enabled = 0;
+GLOBAL struct evthread_lock_callbacks _evthread_lock_fns = {
0, 0, NULL, NULL, NULL, NULL
};
-GLOBAL unsigned long (*evthread_id_fn_)(void) = NULL;
-GLOBAL struct evthread_condition_callbacks evthread_cond_fns_ = {
+GLOBAL unsigned long (*_evthread_id_fn)(void) = NULL;
+GLOBAL struct evthread_condition_callbacks _evthread_cond_fns = {
0, NULL, NULL, NULL, NULL
};
/* Used for debugging */
-static struct evthread_lock_callbacks original_lock_fns_ = {
+static struct evthread_lock_callbacks _original_lock_fns = {
0, 0, NULL, NULL, NULL, NULL
};
-static struct evthread_condition_callbacks original_cond_fns_ = {
+static struct evthread_condition_callbacks _original_cond_fns = {
0, NULL, NULL, NULL, NULL
};
void
evthread_set_id_callback(unsigned long (*id_fn)(void))
{
- evthread_id_fn_ = id_fn;
-}
-
-struct evthread_lock_callbacks *evthread_get_lock_callbacks()
-{
- return evthread_lock_debugging_enabled_
- ? &original_lock_fns_ : &evthread_lock_fns_;
-}
-struct evthread_condition_callbacks *evthread_get_condition_callbacks()
-{
- return evthread_lock_debugging_enabled_
- ? &original_cond_fns_ : &evthread_cond_fns_;
-}
-void evthreadimpl_disable_lock_debugging_(void)
-{
- evthread_lock_debugging_enabled_ = 0;
+ _evthread_id_fn = id_fn;
}
int
evthread_set_lock_callbacks(const struct evthread_lock_callbacks *cbs)
{
- struct evthread_lock_callbacks *target = evthread_get_lock_callbacks();
-
-#ifndef EVENT__DISABLE_DEBUG_MODE
- if (event_debug_mode_on_) {
- if (event_debug_created_threadable_ctx_) {
- event_errx(1, "evthread initialization must be called BEFORE anything else!");
- }
- }
-#endif
+ struct evthread_lock_callbacks *target =
+ _evthread_lock_debugging_enabled
+ ? &_original_lock_fns : &_evthread_lock_fns;
if (!cbs) {
if (target->alloc)
event_warnx("Trying to disable lock functions after "
"they have been set up will probaby not work.");
- memset(target, 0, sizeof(evthread_lock_fns_));
+ memset(target, 0, sizeof(_evthread_lock_fns));
return 0;
}
if (target->alloc) {
@@ -125,7 +98,7 @@ evthread_set_lock_callbacks(const struct evthread_lock_callbacks *cbs)
return -1;
}
if (cbs->alloc && cbs->free && cbs->lock && cbs->unlock) {
- memcpy(target, cbs, sizeof(evthread_lock_fns_));
+ memcpy(target, cbs, sizeof(_evthread_lock_fns));
return event_global_setup_locks_(1);
} else {
return -1;
@@ -135,22 +108,16 @@ evthread_set_lock_callbacks(const struct evthread_lock_callbacks *cbs)
int
evthread_set_condition_callbacks(const struct evthread_condition_callbacks *cbs)
{
- struct evthread_condition_callbacks *target = evthread_get_condition_callbacks();
-
-#ifndef EVENT__DISABLE_DEBUG_MODE
- if (event_debug_mode_on_) {
- if (event_debug_created_threadable_ctx_) {
- event_errx(1, "evthread initialization must be called BEFORE anything else!");
- }
- }
-#endif
+ struct evthread_condition_callbacks *target =
+ _evthread_lock_debugging_enabled
+ ? &_original_cond_fns : &_evthread_cond_fns;
if (!cbs) {
if (target->alloc_condition)
event_warnx("Trying to disable condition functions "
"after they have been set up will probaby not "
"work.");
- memset(target, 0, sizeof(evthread_cond_fns_));
+ memset(target, 0, sizeof(_evthread_cond_fns));
return 0;
}
if (target->alloc_condition) {
@@ -169,20 +136,17 @@ evthread_set_condition_callbacks(const struct evthread_condition_callbacks *cbs)
}
if (cbs->alloc_condition && cbs->free_condition &&
cbs->signal_condition && cbs->wait_condition) {
- memcpy(target, cbs, sizeof(evthread_cond_fns_));
+ memcpy(target, cbs, sizeof(_evthread_cond_fns));
}
- if (evthread_lock_debugging_enabled_) {
- evthread_cond_fns_.alloc_condition = cbs->alloc_condition;
- evthread_cond_fns_.free_condition = cbs->free_condition;
- evthread_cond_fns_.signal_condition = cbs->signal_condition;
+ if (_evthread_lock_debugging_enabled) {
+ _evthread_cond_fns.alloc_condition = cbs->alloc_condition;
+ _evthread_cond_fns.free_condition = cbs->free_condition;
+ _evthread_cond_fns.signal_condition = cbs->signal_condition;
}
return 0;
}
-#define DEBUG_LOCK_SIG 0xdeb0b10c
-
struct debug_lock {
- unsigned signature;
unsigned locktype;
unsigned long held_by;
/* XXXX if we ever use read-write locks, we will need a separate
@@ -197,8 +161,8 @@ debug_lock_alloc(unsigned locktype)
struct debug_lock *result = mm_malloc(sizeof(struct debug_lock));
if (!result)
return NULL;
- if (original_lock_fns_.alloc) {
- if (!(result->lock = original_lock_fns_.alloc(
+ if (_original_lock_fns.alloc) {
+ if (!(result->lock = _original_lock_fns.alloc(
locktype|EVTHREAD_LOCKTYPE_RECURSIVE))) {
mm_free(result);
return NULL;
@@ -206,7 +170,6 @@ debug_lock_alloc(unsigned locktype)
} else {
result->lock = NULL;
}
- result->signature = DEBUG_LOCK_SIG;
result->locktype = locktype;
result->count = 0;
result->held_by = 0;
@@ -219,27 +182,24 @@ debug_lock_free(void *lock_, unsigned locktype)
struct debug_lock *lock = lock_;
EVUTIL_ASSERT(lock->count == 0);
EVUTIL_ASSERT(locktype == lock->locktype);
- EVUTIL_ASSERT(DEBUG_LOCK_SIG == lock->signature);
- if (original_lock_fns_.free) {
- original_lock_fns_.free(lock->lock,
+ if (_original_lock_fns.free) {
+ _original_lock_fns.free(lock->lock,
lock->locktype|EVTHREAD_LOCKTYPE_RECURSIVE);
}
lock->lock = NULL;
lock->count = -100;
- lock->signature = 0x12300fda;
mm_free(lock);
}
static void
evthread_debug_lock_mark_locked(unsigned mode, struct debug_lock *lock)
{
- EVUTIL_ASSERT(DEBUG_LOCK_SIG == lock->signature);
++lock->count;
if (!(lock->locktype & EVTHREAD_LOCKTYPE_RECURSIVE))
EVUTIL_ASSERT(lock->count == 1);
- if (evthread_id_fn_) {
+ if (_evthread_id_fn) {
unsigned long me;
- me = evthread_id_fn_();
+ me = _evthread_id_fn();
if (lock->count > 1)
EVUTIL_ASSERT(lock->held_by == me);
lock->held_by = me;
@@ -255,8 +215,8 @@ debug_lock_lock(unsigned mode, void *lock_)
EVUTIL_ASSERT(mode & (EVTHREAD_READ|EVTHREAD_WRITE));
else
EVUTIL_ASSERT((mode & (EVTHREAD_READ|EVTHREAD_WRITE)) == 0);
- if (original_lock_fns_.lock)
- res = original_lock_fns_.lock(mode, lock->lock);
+ if (_original_lock_fns.lock)
+ res = _original_lock_fns.lock(mode, lock->lock);
if (!res) {
evthread_debug_lock_mark_locked(mode, lock);
}
@@ -266,15 +226,12 @@ debug_lock_lock(unsigned mode, void *lock_)
static void
evthread_debug_lock_mark_unlocked(unsigned mode, struct debug_lock *lock)
{
- EVUTIL_ASSERT(DEBUG_LOCK_SIG == lock->signature);
if (lock->locktype & EVTHREAD_LOCKTYPE_READWRITE)
EVUTIL_ASSERT(mode & (EVTHREAD_READ|EVTHREAD_WRITE));
else
EVUTIL_ASSERT((mode & (EVTHREAD_READ|EVTHREAD_WRITE)) == 0);
- if (evthread_id_fn_) {
- unsigned long me;
- me = evthread_id_fn_();
- EVUTIL_ASSERT(lock->held_by == me);
+ if (_evthread_id_fn) {
+ EVUTIL_ASSERT(lock->held_by == _evthread_id_fn());
if (lock->count == 1)
lock->held_by = 0;
}
@@ -288,35 +245,27 @@ debug_lock_unlock(unsigned mode, void *lock_)
struct debug_lock *lock = lock_;
int res = 0;
evthread_debug_lock_mark_unlocked(mode, lock);
- if (original_lock_fns_.unlock)
- res = original_lock_fns_.unlock(mode, lock->lock);
+ if (_original_lock_fns.unlock)
+ res = _original_lock_fns.unlock(mode, lock->lock);
return res;
}
static int
-debug_cond_wait(void *cond_, void *lock_, const struct timeval *tv)
+debug_cond_wait(void *_cond, void *_lock, const struct timeval *tv)
{
int r;
- struct debug_lock *lock = lock_;
+ struct debug_lock *lock = _lock;
EVUTIL_ASSERT(lock);
- EVUTIL_ASSERT(DEBUG_LOCK_SIG == lock->signature);
- EVLOCK_ASSERT_LOCKED(lock_);
+ EVLOCK_ASSERT_LOCKED(_lock);
evthread_debug_lock_mark_unlocked(0, lock);
- r = original_cond_fns_.wait_condition(cond_, lock->lock, tv);
+ r = _original_cond_fns.wait_condition(_cond, lock->lock, tv);
evthread_debug_lock_mark_locked(0, lock);
return r;
}
-/* misspelled version for backward compatibility */
void
evthread_enable_lock_debuging(void)
{
- evthread_enable_lock_debugging();
-}
-
-void
-evthread_enable_lock_debugging(void)
-{
struct evthread_lock_callbacks cbs = {
EVTHREAD_LOCK_API_VERSION,
EVTHREAD_LOCKTYPE_RECURSIVE,
@@ -325,30 +274,30 @@ evthread_enable_lock_debugging(void)
debug_lock_lock,
debug_lock_unlock
};
- if (evthread_lock_debugging_enabled_)
+ if (_evthread_lock_debugging_enabled)
return;
- memcpy(&original_lock_fns_, &evthread_lock_fns_,
+ memcpy(&_original_lock_fns, &_evthread_lock_fns,
sizeof(struct evthread_lock_callbacks));
- memcpy(&evthread_lock_fns_, &cbs,
+ memcpy(&_evthread_lock_fns, &cbs,
sizeof(struct evthread_lock_callbacks));
- memcpy(&original_cond_fns_, &evthread_cond_fns_,
+ memcpy(&_original_cond_fns, &_evthread_cond_fns,
sizeof(struct evthread_condition_callbacks));
- evthread_cond_fns_.wait_condition = debug_cond_wait;
- evthread_lock_debugging_enabled_ = 1;
+ _evthread_cond_fns.wait_condition = debug_cond_wait;
+ _evthread_lock_debugging_enabled = 1;
/* XXX return value should get checked. */
event_global_setup_locks_(0);
}
int
-evthread_is_debug_lock_held_(void *lock_)
+_evthread_is_debug_lock_held(void *lock_)
{
struct debug_lock *lock = lock_;
if (! lock->count)
return 0;
- if (evthread_id_fn_) {
- unsigned long me = evthread_id_fn_();
+ if (_evthread_id_fn) {
+ unsigned long me = _evthread_id_fn();
if (lock->held_by != me)
return 0;
}
@@ -356,7 +305,7 @@ evthread_is_debug_lock_held_(void *lock_)
}
void *
-evthread_debug_get_real_lock_(void *lock_)
+_evthread_debug_get_real_lock(void *lock_)
{
struct debug_lock *lock = lock_;
return lock->lock;
@@ -371,23 +320,23 @@ evthread_setup_global_lock_(void *lock_, unsigned locktype, int enable_locks)
3) we're turning on locking; debugging is not on.
4) we're turning on locking; debugging is on. */
- if (!enable_locks && original_lock_fns_.alloc == NULL) {
+ if (!enable_locks && _original_lock_fns.alloc == NULL) {
/* Case 1: allocate a debug lock. */
EVUTIL_ASSERT(lock_ == NULL);
return debug_lock_alloc(locktype);
- } else if (!enable_locks && original_lock_fns_.alloc != NULL) {
+ } else if (!enable_locks && _original_lock_fns.alloc != NULL) {
/* Case 2: wrap the lock in a debug lock. */
struct debug_lock *lock;
EVUTIL_ASSERT(lock_ != NULL);
if (!(locktype & EVTHREAD_LOCKTYPE_RECURSIVE)) {
/* We can't wrap it: We need a recursive lock */
- original_lock_fns_.free(lock_, locktype);
+ _original_lock_fns.free(lock_, locktype);
return debug_lock_alloc(locktype);
}
lock = mm_malloc(sizeof(struct debug_lock));
if (!lock) {
- original_lock_fns_.free(lock_, locktype);
+ _original_lock_fns.free(lock_, locktype);
return NULL;
}
lock->lock = lock_;
@@ -395,24 +344,23 @@ evthread_setup_global_lock_(void *lock_, unsigned locktype, int enable_locks)
lock->count = 0;
lock->held_by = 0;
return lock;
- } else if (enable_locks && ! evthread_lock_debugging_enabled_) {
+ } else if (enable_locks && ! _evthread_lock_debugging_enabled) {
/* Case 3: allocate a regular lock */
EVUTIL_ASSERT(lock_ == NULL);
- return evthread_lock_fns_.alloc(locktype);
+ return _evthread_lock_fns.alloc(locktype);
} else {
/* Case 4: Fill in a debug lock with a real lock */
- struct debug_lock *lock = lock_ ? lock_ : debug_lock_alloc(locktype);
+ struct debug_lock *lock = lock_;
EVUTIL_ASSERT(enable_locks &&
- evthread_lock_debugging_enabled_);
+ _evthread_lock_debugging_enabled);
EVUTIL_ASSERT(lock->locktype == locktype);
+ EVUTIL_ASSERT(lock->lock == NULL);
+ lock->lock = _original_lock_fns.alloc(
+ locktype|EVTHREAD_LOCKTYPE_RECURSIVE);
if (!lock->lock) {
- lock->lock = original_lock_fns_.alloc(
- locktype|EVTHREAD_LOCKTYPE_RECURSIVE);
- if (!lock->lock) {
- lock->count = -200;
- mm_free(lock);
- return NULL;
- }
+ lock->count = -200;
+ mm_free(lock);
+ return NULL;
}
return lock;
}
@@ -421,88 +369,76 @@ evthread_setup_global_lock_(void *lock_, unsigned locktype, int enable_locks)
#ifndef EVTHREAD_EXPOSE_STRUCTS
unsigned long
-evthreadimpl_get_id_()
+_evthreadimpl_get_id()
{
- return evthread_id_fn_ ? evthread_id_fn_() : 1;
+ return _evthread_id_fn ? _evthread_id_fn() : 1;
}
void *
-evthreadimpl_lock_alloc_(unsigned locktype)
+_evthreadimpl_lock_alloc(unsigned locktype)
{
-#ifndef EVENT__DISABLE_DEBUG_MODE
- if (event_debug_mode_on_) {
- event_debug_created_threadable_ctx_ = 1;
- }
-#endif
-
- return evthread_lock_fns_.alloc ?
- evthread_lock_fns_.alloc(locktype) : NULL;
+ return _evthread_lock_fns.alloc ?
+ _evthread_lock_fns.alloc(locktype) : NULL;
}
void
-evthreadimpl_lock_free_(void *lock, unsigned locktype)
+_evthreadimpl_lock_free(void *lock, unsigned locktype)
{
- if (evthread_lock_fns_.free)
- evthread_lock_fns_.free(lock, locktype);
+ if (_evthread_lock_fns.free)
+ _evthread_lock_fns.free(lock, locktype);
}
int
-evthreadimpl_lock_lock_(unsigned mode, void *lock)
+_evthreadimpl_lock_lock(unsigned mode, void *lock)
{
- if (evthread_lock_fns_.lock)
- return evthread_lock_fns_.lock(mode, lock);
+ if (_evthread_lock_fns.lock)
+ return _evthread_lock_fns.lock(mode, lock);
else
return 0;
}
int
-evthreadimpl_lock_unlock_(unsigned mode, void *lock)
+_evthreadimpl_lock_unlock(unsigned mode, void *lock)
{
- if (evthread_lock_fns_.unlock)
- return evthread_lock_fns_.unlock(mode, lock);
+ if (_evthread_lock_fns.unlock)
+ return _evthread_lock_fns.unlock(mode, lock);
else
return 0;
}
void *
-evthreadimpl_cond_alloc_(unsigned condtype)
+_evthreadimpl_cond_alloc(unsigned condtype)
{
-#ifndef EVENT__DISABLE_DEBUG_MODE
- if (event_debug_mode_on_) {
- event_debug_created_threadable_ctx_ = 1;
- }
-#endif
-
- return evthread_cond_fns_.alloc_condition ?
- evthread_cond_fns_.alloc_condition(condtype) : NULL;
+ return _evthread_cond_fns.alloc_condition ?
+ _evthread_cond_fns.alloc_condition(condtype) : NULL;
}
void
-evthreadimpl_cond_free_(void *cond)
+_evthreadimpl_cond_free(void *cond)
{
- if (evthread_cond_fns_.free_condition)
- evthread_cond_fns_.free_condition(cond);
+ if (_evthread_cond_fns.free_condition)
+ _evthread_cond_fns.free_condition(cond);
}
int
-evthreadimpl_cond_signal_(void *cond, int broadcast)
+_evthreadimpl_cond_signal(void *cond, int broadcast)
{
- if (evthread_cond_fns_.signal_condition)
- return evthread_cond_fns_.signal_condition(cond, broadcast);
+ if (_evthread_cond_fns.signal_condition)
+ return _evthread_cond_fns.signal_condition(cond, broadcast);
else
return 0;
}
int
-evthreadimpl_cond_wait_(void *cond, void *lock, const struct timeval *tv)
+_evthreadimpl_cond_wait(void *cond, void *lock, const struct timeval *tv)
{
- if (evthread_cond_fns_.wait_condition)
- return evthread_cond_fns_.wait_condition(cond, lock, tv);
+ if (_evthread_cond_fns.wait_condition)
+ return _evthread_cond_fns.wait_condition(cond, lock, tv);
else
return 0;
}
int
-evthreadimpl_is_lock_debugging_enabled_(void)
+_evthreadimpl_is_lock_debugging_enabled(void)
{
- return evthread_lock_debugging_enabled_;
+ return _evthread_lock_debugging_enabled;
}
int
-evthreadimpl_locking_enabled_(void)
+_evthreadimpl_locking_enabled(void)
{
- return evthread_lock_fns_.lock != NULL;
+ return _evthread_lock_fns.lock != NULL;
}
#endif