futex_wasm.odin 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #+private
  2. #+build wasm32, wasm64p32
  3. package sync
  4. import "base:intrinsics"
  5. import "core:time"
  6. // NOTE: because `core:sync` is in the dependency chain of a lot of the core packages (mostly through `core:mem`)
  7. // without actually calling into it much, I opted for a runtime panic instead of a compile error here.
  8. _futex_wait :: proc "contextless" (f: ^Futex, expected: u32) -> bool {
  9. when !intrinsics.has_target_feature("atomics") {
  10. panic_contextless("usage of `core:sync` requires the `-target-feature:\"atomics\"` or a `-microarch` that supports it")
  11. } else {
  12. _ = intrinsics.wasm_memory_atomic_wait32((^u32)(f), expected, -1)
  13. return true
  14. }
  15. }
  16. _futex_wait_with_timeout :: proc "contextless" (f: ^Futex, expected: u32, duration: time.Duration) -> bool {
  17. when !intrinsics.has_target_feature("atomics") {
  18. panic_contextless("usage of `core:sync` requires the `-target-feature:\"atomics\"` or a `-microarch` that supports it")
  19. } else {
  20. s := intrinsics.wasm_memory_atomic_wait32((^u32)(f), expected, i64(duration))
  21. return s != 2
  22. }
  23. }
  24. _futex_signal :: proc "contextless" (f: ^Futex) {
  25. when !intrinsics.has_target_feature("atomics") {
  26. panic_contextless("usage of `core:sync` requires the `-target-feature:\"atomics\"` or a `-microarch` that supports it")
  27. } else {
  28. _ = intrinsics.wasm_memory_atomic_notify32((^u32)(f), 1)
  29. }
  30. }
  31. _futex_broadcast :: proc "contextless" (f: ^Futex) {
  32. when !intrinsics.has_target_feature("atomics") {
  33. panic_contextless("usage of `core:sync` requires the `-target-feature:\"atomics\"` or a `-microarch` that supports it")
  34. } else {
  35. _ = intrinsics.wasm_memory_atomic_notify32((^u32)(f), max(u32))
  36. }
  37. }