tor

The Tor anonymity network
git clone https://git.dasho.dev/tor.git
Log | Files | Refs | README | LICENSE

commit 328f38a59f5e1b049350d05dcb29e7c511cffd79
parent 877dbfc0561dfc5297cf446fe653cc3095d20b46
Author: Daniel Pinto <danielpinto52@gmail.com>
Date:   Mon, 30 Nov 2020 02:54:13 +0000

Use atomic ops to access lock_owner in WIN32 tor_mutex_t #17927

Diffstat:
Msrc/lib/lock/compat_mutex.h | 2+-
Msrc/lib/lock/compat_mutex_winthreads.c | 10++++++----
2 files changed, 7 insertions(+), 5 deletions(-)

diff --git a/src/lib/lock/compat_mutex.h b/src/lib/lock/compat_mutex.h @@ -46,7 +46,7 @@ typedef struct tor_mutex_t { NON_RECURSIVE = 0, RECURSIVE } type; - DWORD lock_owner; // id of the thread that owns the lock + LONG lock_owner; // id of the thread that owns the lock int lock_count; // number of times the lock is held recursively #elif defined(USE_PTHREADS) /** Pthreads-only: with pthreads, we implement locks with diff --git a/src/lib/lock/compat_mutex_winthreads.c b/src/lib/lock/compat_mutex_winthreads.c @@ -58,13 +58,15 @@ tor_mutex_uninit(tor_mutex_t *m) static void tor_mutex_acquire_recursive(tor_mutex_t *m) { - DWORD thread_id = GetCurrentThreadId(); - if (thread_id == m->lock_owner) { + LONG thread_id = GetCurrentThreadId(); + // use InterlockedCompareExchange to perform an atomic read + LONG lock_owner = InterlockedCompareExchange(&m->lock_owner, 0, 0); + if (thread_id == lock_owner) { ++m->lock_count; return; } AcquireSRWLockExclusive(&m->mutex); - m->lock_owner = thread_id; + InterlockedExchange(&m->lock_owner, thread_id); m->lock_count = 1; } @@ -91,7 +93,7 @@ tor_mutex_release_recursive(tor_mutex_t *m) if (--m->lock_count) { return; } - m->lock_owner = 0; + InterlockedExchange(&m->lock_owner, 0); ReleaseSRWLockExclusive(&m->mutex); }