Use atomic ops to access lock_owner in WIN32 tor_mutex_t #17927

This commit is contained in:
Daniel Pinto
2020-11-30 02:54:13 +00:00
parent 877dbfc056
commit 328f38a59f
2 changed files with 7 additions and 5 deletions
+1 -1
View File
@@ -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
+6 -4
View File
@@ -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);
}