summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorDaniel Pinto <danielpinto52@gmail.com>2020-11-30 02:54:13 +0000
committerDaniel Pinto <danielpinto52@gmail.com>2020-11-30 02:54:13 +0000
commit328f38a59f5e1b049350d05dcb29e7c511cffd79 (patch)
tree9265a3e1908731600a0e04c5dc6c60c10194207a /src/lib
parent877dbfc0561dfc5297cf446fe653cc3095d20b46 (diff)
downloadtor-328f38a59f5e1b049350d05dcb29e7c511cffd79.tar.gz
tor-328f38a59f5e1b049350d05dcb29e7c511cffd79.zip
Use atomic ops to access lock_owner in WIN32 tor_mutex_t #17927
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/lock/compat_mutex.h2
-rw-r--r--src/lib/lock/compat_mutex_winthreads.c10
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);
}