mirror of
https://gitlab.torproject.org/tpo/core/tor.git
synced 2024-12-06 19:41:15 +01:00
Use atomic ops to access lock_owner in WIN32 tor_mutex_t #17927
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user