[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[tor-commits] [tor/master] Use atomic ops to access lock_owner in WIN32 tor_mutex_t #17927
commit 328f38a59f5e1b049350d05dcb29e7c511cffd79
Author: Daniel Pinto <danielpinto52@xxxxxxxxx>
Date: Mon Nov 30 02:54:13 2020 +0000
Use atomic ops to access lock_owner in WIN32 tor_mutex_t #17927
---
src/lib/lock/compat_mutex.h | 2 +-
src/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
index 14bdf70ce5..518ba96b53 100644
--- 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
index 03cf5082e0..151a7b80f7 100644
--- 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);
}
_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits