Begin work on Atomics for wasm32 (wait and notify intrinsics)

This commit is contained in:
gingerBill
2022-05-21 12:58:48 +01:00
parent 9eb4cbcbd2
commit e48f41165c
7 changed files with 224 additions and 9 deletions
+9
View File
@@ -190,6 +190,15 @@ constant_utf16_cstring :: proc($literal: string) -> [^]u16 ---
wasm_memory_grow :: proc(index, delta: uintptr) -> int ---
wasm_memory_size :: proc(index: uintptr) -> int ---
// `timeout_ns` is maximum number of nanoseconds the calling thread will be blocked for
// A negative value will be blocked forever
// Return value:
// 0 - indicates that the thread blocked and then was woken up
// 1 - the loaded value from `ptr` did not match `expected`, the thread did not block
// 2 - the thread blocked, but the timeout
wasm_memory_atomic_wait32 :: proc(ptr: ^u32, expected: u32, timeout_ns: i64) -> u32 ---
wasm_memory_atomic_notify32 :: proc(ptr: ^u32, waiters: u32) -> (waiters_woken_up: u32) ---
// Darwin targets only
objc_object :: struct{}
+36
View File
@@ -0,0 +1,36 @@
//+private
//+build wasm32
package sync
import "core:intrinsics"
import "core:time"
_futex_wait :: proc(f: ^Futex, expected: u32) -> bool {
s := intrinsics.wasm_memory_atomic_wait32((^u32)(f), expected, -1)
return s != 0
}
_futex_wait_with_timeout :: proc(f: ^Futex, expected: u32, duration: time.Duration) -> bool {
s := intrinsics.wasm_memory_atomic_wait32((^u32)(f), expected, i64(duration))
return s != 0
}
_futex_signal :: proc(f: ^Futex) {
loop: for {
s := intrinsics.wasm_memory_atomic_notify32((^u32)(f), 1)
if s >= 1 {
return
}
}
}
_futex_broadcast :: proc(f: ^Futex) {
loop: for {
s := intrinsics.wasm_memory_atomic_notify32((^u32)(f), ~u32(0))
if s >= 0 {
return
}
}
}
+8
View File
@@ -0,0 +1,8 @@
//+private
//+build wasm32
package sync
_current_thread_id :: proc "contextless" () -> int {
// TODO(bill): _current_thread_id for wasm32
return 0
}