diff options
author | Nick Mathewson <nickm@torproject.org> | 2009-11-27 15:20:43 -0500 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2009-11-27 17:36:24 -0500 |
commit | 347952ffe0039369ff4e7c94c4e27e7333ba8ecb (patch) | |
tree | 51a3147fc57fdfa8290ba880f01068a26f626394 /evthread_win32.c | |
parent | e1ffbb82e34d786d07a5acdb16077f6526f610c3 (diff) | |
download | libevent-347952ffe0039369ff4e7c94c4e27e7333ba8ecb.tar.gz |
Revise the locking API: deprecate the old locking callbacks and add trylock.
Previously, there was no good way to request different kinds of lock
(say, read/write vs writeonly or recursive vs nonrecursive), or for a
lock function to signal failure (which would be important for a
trylock mode).
This patch revises the lock API to be a bit more useful. The older
lock calls are still supported for now.
We also add a debugging mode to catch common errors in using the
locking APIs.
Diffstat (limited to 'evthread_win32.c')
-rw-r--r-- | evthread_win32.c | 35 |
1 files changed, 25 insertions, 10 deletions
diff --git a/evthread_win32.c b/evthread_win32.c index 5e341f88..05ed9217 100644 --- a/evthread_win32.c +++ b/evthread_win32.c @@ -39,7 +39,7 @@ struct event_base; #include "mm-internal.h" static void * -evthread_win32_lock_create(void) +evthread_win32_lock_create(unsigned locktype) { CRITICAL_SECTION *lock = mm_malloc(sizeof(CRITICAL_SECTION)); if (!lock) @@ -49,20 +49,29 @@ evthread_win32_lock_create(void) } static void -evthread_win32_lock_free(void *_lock) +evthread_win32_lock_free(void *_lock, unsigned locktype) { CRITICAL_SECTION *lock = _lock; DeleteCriticalSection(lock); } static void -evthread_win32_lock(int mode, void *_lock) +evthread_win32_lock(unsigned mode, void *_lock) { CRITICAL_SECTION *lock = _lock; - if (0 != (mode & EVTHREAD_LOCK)) + if ((mode & EVTHREAD_TRY)) { + return TryEnterCriticalSection(lock) != 0; + } else { EnterCriticalSection(lock); - else - LeaveCriticalSection(lock); + return 0; + } +} + +static void +evthread_win32_unlock(unsigned mode, void *_lock) +{ + CRITICAL_SECTION *lock = _lock; + LeaveCriticalSection(lock) } static unsigned long @@ -74,10 +83,16 @@ evthread_win32_get_id(void) int evthread_use_windows_threads(void) { - evthread_set_lock_create_callbacks( - evthread_win32_lock_create, - evthread_win32_lock_free); - evthread_set_locking_callback(evthread_win32_lock); + struct evthread_lock_callbacks cbs = { + EVTHREAD_LOCK_API_VERSION, + EVTHREAD_LOCKTYPE_RECURSIVE, + evthread_win32_lock_alloc, + evthread_win32_lock_free, + evthread_win32_lock, + evthread_win32_unlock + }; + + evthread_set_lock_callbacks(&cbs); evthread_set_id_callback(evthread_win32_get_id); return 0; } |