From 689fc091a58dc90826ec90c1ca90a5d21b7184ad Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Sat, 28 Nov 2009 11:31:39 -0500 Subject: [PATCH] New EVTHREAD_TRY_LOCK function to try to grab a lock. --- evthread-internal.h | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/evthread-internal.h b/evthread-internal.h index 6335a361..e946947e 100644 --- a/evthread-internal.h +++ b/evthread-internal.h @@ -30,6 +30,7 @@ extern "C" { #endif +#include #include "event-config.h" #include "util-internal.h" @@ -142,6 +143,22 @@ extern int _evthread_lock_debugging_enabled; int _evthread_is_debug_lock_held(void *lock); +/** Try to grab the lock for 'lockvar' without blocking, and return 1 if we + * manage to get it. */ +static inline int EVLOCK_TRY_LOCK(void *lock); +static inline int +EVLOCK_TRY_LOCK(void *lock) +{ + if (lock && _evthread_lock_fns.lock) { + int r = _evthread_lock_fns.lock(EVTHREAD_TRY, lock); + return !r; + } else { + /* Locking is disabled either globally or for this thing; + * of course we count as having the lock. */ + return 1; + } +} + #else /* _EVENT_DISABLE_THREAD_SUPPORT */ #define EVTHREAD_GET_ID() 1 @@ -157,6 +174,8 @@ int _evthread_is_debug_lock_held(void *lock); #define EVBASE_ACQUIRE_LOCK(base, lock) _EVUTIL_NIL_STMT #define EVBASE_RELEASE_LOCK(base, lock) _EVUTIL_NIL_STMT #define EVLOCK_ASSERT_LOCKED(lock) _EVUTIL_NIL_STMT + +#define EVTHREAD_TRY_LOCK(lock) 1 #endif #ifdef __cplusplus