Skip to content

Commit

Permalink
sync: fix futexes on wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
laytan committed Dec 5, 2024
1 parent d452d37 commit ad438f4
Showing 1 changed file with 5 additions and 15 deletions.
20 changes: 5 additions & 15 deletions core/sync/futex_wasm.odin
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ _futex_wait :: proc "contextless" (f: ^Futex, expected: u32) -> bool {
when !intrinsics.has_target_feature("atomics") {
panic_contextless("usage of `core:sync` requires the `-target-feature:\"atomics\"` or a `-microarch` that supports it")
} else {
s := intrinsics.wasm_memory_atomic_wait32((^u32)(f), expected, -1)
return s != 0
intrinsics.wasm_memory_atomic_wait32((^u32)(f), expected, -1)
return true
}
}

Expand All @@ -22,33 +22,23 @@ _futex_wait_with_timeout :: proc "contextless" (f: ^Futex, expected: u32, durati
panic_contextless("usage of `core:sync` requires the `-target-feature:\"atomics\"` or a `-microarch` that supports it")
} else {
s := intrinsics.wasm_memory_atomic_wait32((^u32)(f), expected, i64(duration))
return s != 0
return s != 2
}
}

_futex_signal :: proc "contextless" (f: ^Futex) {
when !intrinsics.has_target_feature("atomics") {
panic_contextless("usage of `core:sync` requires the `-target-feature:\"atomics\"` or a `-microarch` that supports it")
} else {
loop: for {
s := intrinsics.wasm_memory_atomic_notify32((^u32)(f), 1)
if s >= 1 {
return
}
}
intrinsics.wasm_memory_atomic_notify32((^u32)(f), 1)
}
}

_futex_broadcast :: proc "contextless" (f: ^Futex) {
when !intrinsics.has_target_feature("atomics") {
panic_contextless("usage of `core:sync` requires the `-target-feature:\"atomics\"` or a `-microarch` that supports it")
} else {
loop: for {
s := intrinsics.wasm_memory_atomic_notify32((^u32)(f), ~u32(0))
if s >= 0 {
return
}
}
intrinsics.wasm_memory_atomic_notify32((^u32)(f), max(u32))
}
}

0 comments on commit ad438f4

Please sign in to comment.