Update core:sync/sync2 to have a generic Futex interface, and implement the calls appropriately for each platform

This commit is contained in:
gingerBill
2021-10-09 16:33:28 +01:00
parent d386563344
commit 2ef0e6b8f6
11 changed files with 486 additions and 402 deletions
-85
View File
@@ -54,55 +54,6 @@ _rw_mutex_try_shared_lock :: proc(rw: ^RW_Mutex) -> bool {
}
_Recursive_Mutex :: struct {
owner: u32,
claim_count: i32,
}
_recursive_mutex_lock :: proc(m: ^Recursive_Mutex) {
tid := win32.GetCurrentThreadId()
for {
prev_owner := atomic_compare_exchange_strong_acquire(&m.impl.owner, tid, 0)
switch prev_owner {
case 0, tid:
m.impl.claim_count += 1
// inside the lock
return
}
win32.WaitOnAddress(
&m.impl.owner,
&prev_owner,
size_of(prev_owner),
win32.INFINITE,
)
}
}
_recursive_mutex_unlock :: proc(m: ^Recursive_Mutex) {
m.impl.claim_count -= 1
if m.impl.claim_count != 0 {
return
}
atomic_exchange_release(&m.impl.owner, 0)
win32.WakeByAddressSingle(&m.impl.owner)
// outside the lock
}
_recursive_mutex_try_lock :: proc(m: ^Recursive_Mutex) -> bool {
tid := win32.GetCurrentThreadId()
prev_owner := atomic_compare_exchange_strong_acquire(&m.impl.owner, tid, 0)
switch prev_owner {
case 0, tid:
m.impl.claim_count += 1
// inside the lock
return true
}
return false
}
_Cond :: struct {
@@ -113,11 +64,6 @@ _cond_wait :: proc(c: ^Cond, m: ^Mutex) {
_ = win32.SleepConditionVariableSRW(&c.impl.cond, &m.impl.srwlock, win32.INFINITE, 0)
}
_cond_wait_with_timeout :: proc(c: ^Cond, m: ^Mutex, timeout: time.Duration) -> bool {
ms := win32.DWORD((max(time.duration_nanoseconds(timeout), 0) + 999999)/1000000)
return cast(bool)win32.SleepConditionVariableSRW(&c.impl.cond, &m.impl.srwlock, ms, 0)
}
_cond_signal :: proc(c: ^Cond) {
win32.WakeConditionVariable(&c.impl.cond)
}
@@ -126,34 +72,3 @@ _cond_broadcast :: proc(c: ^Cond) {
win32.WakeAllConditionVariable(&c.impl.cond)
}
_Sema :: struct {
count: i32,
}
_sema_wait :: proc(s: ^Sema) {
for {
original_count := s.impl.count
for original_count == 0 {
win32.WaitOnAddress(
&s.impl.count,
&original_count,
size_of(original_count),
win32.INFINITE,
)
original_count = s.impl.count
}
if original_count == atomic_compare_exchange_strong(&s.impl.count, original_count-1, original_count) {
return
}
}
}
_sema_post :: proc(s: ^Sema, count := 1) {
atomic_add(&s.impl.count, i32(count))
if count == 1 {
win32.WakeByAddressSingle(&s.impl.count)
} else {
win32.WakeByAddressAll(&s.impl.count)
}
}