Skip to content
Snippets Groups Projects
Commit 37469449 authored by Deucе's avatar Deucе :ok_hand_tone4:
Browse files

Fix rwlocks for Win32

Various bits of broken no longer broken.
parent 4c803cd4
No related branches found
No related tags found
No related merge requests found
Pipeline #5307 passed
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#if defined(_WIN32) #if defined(_WIN32)
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h>
static struct rwlock_reader_thread * static struct rwlock_reader_thread *
find_self(rwlock_t *lock, struct rwlock_reader_thread ***prev) find_self(rwlock_t *lock, struct rwlock_reader_thread ***prev)
...@@ -12,7 +13,7 @@ find_self(rwlock_t *lock, struct rwlock_reader_thread ***prev) ...@@ -12,7 +13,7 @@ find_self(rwlock_t *lock, struct rwlock_reader_thread ***prev)
if (prev) if (prev)
*prev = &lock->rthreads; *prev = &lock->rthreads;
for (ret = NULL; ret; ret = ret->next) { for (ret = lock->rthreads; ret; ret = ret->next) {
if (ret->id == self) if (ret->id == self)
return ret; return ret;
if (prev) { if (prev) {
...@@ -94,7 +95,7 @@ rwlock_tryrdlock(rwlock_t *lock) ...@@ -94,7 +95,7 @@ rwlock_tryrdlock(rwlock_t *lock)
LeaveCriticalSection(&lock->lk); LeaveCriticalSection(&lock->lk);
return FALSE; return FALSE;
} }
if (lock->writers == 0 && lock->writers_waiting == 0) { if (rc->count || (lock->writers == 0 && lock->writers_waiting == 0)) {
rc->count++; rc->count++;
lock->readers++; lock->readers++;
ret = TRUE; ret = TRUE;
...@@ -106,23 +107,33 @@ rwlock_tryrdlock(rwlock_t *lock) ...@@ -106,23 +107,33 @@ rwlock_tryrdlock(rwlock_t *lock)
BOOL BOOL
rwlock_wrlock(rwlock_t *lock) rwlock_wrlock(rwlock_t *lock)
{ {
BOOL ret = FALSE;
EnterCriticalSection(&lock->lk); EnterCriticalSection(&lock->lk);
lock->writers_waiting++; lock->writers_waiting++;
LeaveCriticalSection(&lock->lk); LeaveCriticalSection(&lock->lk);
EnterCriticalSection(&lock->wlk); EnterCriticalSection(&lock->wlk);
EnterCriticalSection(&lock->lk); EnterCriticalSection(&lock->lk);
// No recursion // No recursion
if (lock->writers == 0) { while (lock->readers) {
LeaveCriticalSection(&lock->lk);
LeaveCriticalSection(&lock->wlk);
Sleep(1);
EnterCriticalSection(&lock->wlk);
EnterCriticalSection(&lock->lk);
}
if (lock->writers) {
lock->writers_waiting--;
ret = FALSE;
}
else {
lock->writers_waiting--; lock->writers_waiting--;
lock->writers++; lock->writers++;
lock->writer = GetCurrentThreadId(); lock->writer = GetCurrentThreadId();
LeaveCriticalSection(&lock->lk); ret = TRUE;
// Keep holding wlk
return TRUE;
} }
LeaveCriticalSection(&lock->lk); LeaveCriticalSection(&lock->lk);
LeaveCriticalSection(&lock->wlk); LeaveCriticalSection(&lock->wlk);
return FALSE; return ret;
} }
BOOL BOOL
...@@ -131,7 +142,7 @@ rwlock_trywrlock(rwlock_t *lock) ...@@ -131,7 +142,7 @@ rwlock_trywrlock(rwlock_t *lock)
if (TryEnterCriticalSection(&lock->wlk)) { if (TryEnterCriticalSection(&lock->wlk)) {
EnterCriticalSection(&lock->lk); EnterCriticalSection(&lock->lk);
// Prevent recursing on writer locks // Prevent recursing on writer locks
if (lock->writers == 0) { if (lock->readers == 0 && lock->writers == 0) {
lock->writers++; lock->writers++;
lock->writer = GetCurrentThreadId(); lock->writer = GetCurrentThreadId();
LeaveCriticalSection(&lock->lk); LeaveCriticalSection(&lock->lk);
...@@ -163,7 +174,7 @@ rwlock_unlock(rwlock_t *lock) ...@@ -163,7 +174,7 @@ rwlock_unlock(rwlock_t *lock)
return FALSE; return FALSE;
} }
if (lock->readers) { if (lock->readers) {
rc = find_self(lock, NULL); rc = find_self(lock, &prev);
if (rc && rc->count) { if (rc && rc->count) {
rc->count--; rc->count--;
lock->readers--; lock->readers--;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment