hle(host_posix): implement WaitOnAddress, WakeByAddressSingle, and WakeByAddressAll #555

Closed
opened 2026-11-28 03:48:34 +00:00 by Sheffy007 · 2 comments
Sheffy007 commented 2026-11-28 03:48:34 +00:00 (Migrated from github.com)
Contributor

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Sheffy007 on 2026-11-28T03:48:34Z.


Problem

docs/native-host.md lists WaitOnAddress and WakeByAddress* (api-ms-win-core-synch-l1-2-0.dll, kernelbase.dll, kernel32.dll) among the core Win32 synchronisation primitives for title and C++ runtime code, and victory_platform::sync already provides wait_on_address / wake_one / wake_all for internal locks.

However, crates/victory-hle/src/host_posix/sync.rs does not implement or export WaitOnAddress, WakeByAddressSingle, or WakeByAddressAll in sync::lookup. On macOS:

  • GetProcAddress for WaitOnAddress / WakeByAddressSingle / WakeByAddressAll fails with ERROR_PROC_NOT_FOUND.
  • Direct PE imports under --unimplemented zero return 0 immediately without waiting and without setting ERROR_TIMEOUT, causing any caller synchronizing via WaitOnAddress to either proceed prematurely or busy-spin.

Note that Win32 WaitOnAddress supports AddressSize values of 1, 2, 4, and 8 bytes (aligned or unaligned) while WakeByAddressSingle / WakeByAddressAll take only the target Address without a size argument (whereas Darwin's os_sync_wake_by_address_* requires size in {4, 8} matching the wait call and aligned to size).

Proposed Fix

  • Implement WaitOnAddress, WakeByAddressSingle, and WakeByAddressAll in crates/victory-hle/src/host_posix/sync.rs using striped wait queues keyed by exact Address on top of victory_platform::sync::{wait_on_address, wake_one}:
    • Support AddressSize in {1, 2, 4, 8} (using SeqCst atomic loads when naturally aligned and unaligned reads otherwise); reject null pointers or invalid AddressSize with ERROR_INVALID_PARAMETER.
    • Compare and enqueue atomically under the address stripe lock so wakes cannot be lost between comparison and parking, and never steal wakes across distinct addresses sharing a stripe.
    • Honor ms == 0, finite millisecond timeouts (ERROR_TIMEOUT), and INFINITE.
  • Register WaitOnAddress, WakeByAddressSingle, and WakeByAddressAll in sync::lookup.
  • Add unit tests covering 1/2/4/8-byte waits, immediate mismatch return, WakeByAddressSingle vs WakeByAddressAll isolation, timeouts, and invalid arguments.
<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:issue:6658795284 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/555). Originally posted by [Sheffy007](https://github.com/Sheffy007) on 2026-11-28T03:48:34Z. --- ## Problem `docs/native-host.md` lists `WaitOnAddress` and `WakeByAddress*` (`api-ms-win-core-synch-l1-2-0.dll`, `kernelbase.dll`, `kernel32.dll`) among the core Win32 synchronisation primitives for title and C++ runtime code, and `victory_platform::sync` already provides `wait_on_address` / `wake_one` / `wake_all` for internal locks. However, `crates/victory-hle/src/host_posix/sync.rs` does not implement or export `WaitOnAddress`, `WakeByAddressSingle`, or `WakeByAddressAll` in `sync::lookup`. On macOS: - `GetProcAddress` for `WaitOnAddress` / `WakeByAddressSingle` / `WakeByAddressAll` fails with `ERROR_PROC_NOT_FOUND`. - Direct PE imports under `--unimplemented zero` return `0` immediately without waiting and without setting `ERROR_TIMEOUT`, causing any caller synchronizing via `WaitOnAddress` to either proceed prematurely or busy-spin. Note that Win32 `WaitOnAddress` supports `AddressSize` values of `1`, `2`, `4`, and `8` bytes (aligned or unaligned) while `WakeByAddressSingle` / `WakeByAddressAll` take only the target `Address` without a size argument (whereas Darwin's `os_sync_wake_by_address_*` requires `size` in `{4, 8}` matching the wait call and aligned to `size`). ## Proposed Fix - Implement `WaitOnAddress`, `WakeByAddressSingle`, and `WakeByAddressAll` in `crates/victory-hle/src/host_posix/sync.rs` using striped wait queues keyed by exact `Address` on top of `victory_platform::sync::{wait_on_address, wake_one}`: - Support `AddressSize` in `{1, 2, 4, 8}` (using `SeqCst` atomic loads when naturally aligned and unaligned reads otherwise); reject null pointers or invalid `AddressSize` with `ERROR_INVALID_PARAMETER`. - Compare and enqueue atomically under the address stripe lock so wakes cannot be lost between comparison and parking, and never steal wakes across distinct addresses sharing a stripe. - Honor `ms == 0`, finite millisecond timeouts (`ERROR_TIMEOUT`), and `INFINITE`. - Register `WaitOnAddress`, `WakeByAddressSingle`, and `WakeByAddressAll` in `sync::lookup`. - Add unit tests covering 1/2/4/8-byte waits, immediate mismatch return, `WakeByAddressSingle` vs `WakeByAddressAll` isolation, timeouts, and invalid arguments.
Sheffy007 commented 2026-11-28 03:48:35 +00:00 (Migrated from github.com)
Author
Contributor

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Sheffy007 on 2026-11-28T03:48:35Z.


/claim

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7233404627 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/555#issuecomment-7233404627). Originally posted by [Sheffy007](https://github.com/Sheffy007) on 2026-11-28T03:48:35Z. --- /claim
github-actions[bot] commented 2026-11-28 03:48:46 +00:00 (Migrated from github.com)
Author
Contributor

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by github-actions[bot] on 2026-11-28T03:48:46Z.


Claimed by @Sheffy007. Open a draft pull request with Refs #555 in its description within 24 hours, or the claim expires.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7233407752 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/555#issuecomment-7233407752). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-11-28T03:48:46Z. --- Claimed by @Sheffy007. Open a draft pull request with `Refs #555` in its description within 24 hours, or the claim expires.
Sign in to join this conversation.
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
LakshanSivakumar/Native-Victory-macOS#555
No description provided.