Implement sync2.Recursive_Mutex using WaitOnAddress and friends on Windows

This commit is contained in:
gingerBill
2021-04-27 17:19:12 +01:00
parent 7ac80544a1
commit 17390cd317
4 changed files with 151 additions and 37 deletions
+48
View File
@@ -5,6 +5,7 @@ package sync2
when #config(ODIN_SYNC_USE_PTHREADS, true) {
import "core:time"
import "core:runtime"
import "core:sys/unix"
_Mutex_State :: enum i32 {
@@ -120,6 +121,53 @@ _rw_mutex_try_shared_lock :: proc(rw: ^RW_Mutex) -> bool {
return false;
}
_Recursive_Mutex :: struct {
owner: int,
recursion: int,
mutex: Mutex,
}
_recursive_mutex_lock :: proc(m: ^Recursive_Mutex) {
tid := runtime.current_thread_id();
if tid != m.impl.owner {
mutex_lock(&m.impl.mutex);
}
// inside the lock
m.impl.owner = tid;
m.impl.recursion += 1;
}
_recursive_mutex_unlock :: proc(m: ^Recursive_Mutex) {
tid := runtime.current_thread_id();
assert(tid == m.impl.owner);
m.impl.recursion -= 1;
recursion := m.impl.recursion;
if recursion == 0 {
m.impl.owner = 0;
}
if recursion == 0 {
mutex_unlock(&m.impl.mutex);
}
// outside the lock
}
_recursive_mutex_try_lock :: proc(m: ^Recursive_Mutex) -> bool {
tid := runtime.current_thread_id();
if m.impl.owner == tid {
return mutex_try_lock(&m.impl.mutex);
}
if !mutex_try_lock(&m.impl.mutex) {
return false;
}
// inside the lock
m.impl.owner = tid;
m.impl.recursion += 1;
return true;
}
_Cond :: struct {
pthread_cond: unix.pthread_cond_t,
}