futex_wasm.odin 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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("usage of `core:sync` requires the `-target-feature:\"atomics\"` or a `-microarch` that supports it")
  11. } else {
  12. s := intrinsics.wasm_memory_atomic_wait32((^u32)(f), expected, -1)
  13. return s != 0
  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("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 != 0
  22. }
  23. }
  24. _futex_signal :: proc "contextless" (f: ^Futex) {
  25. when !intrinsics.has_target_feature("atomics") {
  26. _panic("usage of `core:sync` requires the `-target-feature:\"atomics\"` or a `-microarch` that supports it")
  27. } else {
  28. loop: for {
  29. s := intrinsics.wasm_memory_atomic_notify32((^u32)(f), 1)
  30. if s >= 1 {
  31. return
  32. }
  33. }
  34. }
  35. }
  36. _futex_broadcast :: proc "contextless" (f: ^Futex) {
  37. when !intrinsics.has_target_feature("atomics") {
  38. _panic("usage of `core:sync` requires the `-target-feature:\"atomics\"` or a `-microarch` that supports it")
  39. } else {
  40. loop: for {
  41. s := intrinsics.wasm_memory_atomic_notify32((^u32)(f), ~u32(0))
  42. if s >= 0 {
  43. return
  44. }
  45. }
  46. }
  47. }