1-- Threads and waiting on an address: kernel32's calls, raw, and the
2-- synchronization set Windows 8 added, `WaitOnAddress` and its wakes,
3-- which MinGW-w64 and the Windows SDK both import through the
4-- `synchronization` library, the api-ms-win-core-synch-l1-2-0 set. A
5-- Windows thread runs on a stack the system reserves for it; a Landin
6-- thread switches from there to a stack of its own (hosted/thread).
7
8fixed if compiler.os == windows then
9 linker.library("synchronization")
10
11 --- The routine a thread runs, `LPTHREAD_START_ROUTINE`: called with the
12 --- argument `create_thread` was given, on the thread's own stack. Its
13 --- result's low 32 bits are the thread's exit code.
14 public thread_routine: type = extern(c) (argument: usize) -> (result: usize)
15
16 --- `STACK_SIZE_PARAM_IS_A_RESERVATION`: `create_thread`'s size is what
17 --- the stack reserves, of which a page is committed, rather than what
18 --- it commits.
19 public stack_size_is_a_reservation: u32 = 0x10000
20
21 --- Start a thread running `routine` with `argument`, `CreateThread`, with
22 --- no security attributes, `stack_size` the stack's size, zero for the
23 --- image's default, and `flags` zero or
24 --- `stack_size_is_a_reservation`: the thread's handle, with its
25 --- identifier in `id` when that is not zero, or `no_handle` with the
26 --- reason in `last_error`.
27 public extern(c) link(symbol: "CreateThread") create_thread:
28 (attributes: usize, stack_size: usize, routine: thread_routine,
29 argument: usize, flags: u32, id: ptr mut u32) -> (thread: handle)
30
31 --- `INFINITE`: wait for as long as it takes.
32 public infinite: u32 = 0xFFFFFFFF
33
34 --- Sleep while the `size` bytes at `address` equal those at `compare`,
35 --- `WaitOnAddress`, for at most `milliseconds`: nonzero when woken or
36 --- when they already differ, zero with `ERROR_TIMEOUT` in `last_error`.
37 --- The system compares and sleeps in one step, so a wake after a change
38 --- is never lost; it may also return with nobody having woken it.
39 --- `size` is 1, 2, 4 or 8.
40 public extern(c) link(symbol: "WaitOnAddress") wait_on_address:
41 (address: usize, compare: usize, size: usize, milliseconds: u32)
42 -> (woken: i32)
43
44 --- Wake one thread `wait_on_address` sleeps in on `address`, if any,
45 --- `WakeByAddressSingle`.
46 public extern(c) link(symbol: "WakeByAddressSingle") wake_by_address_single:
47 (address: usize) -> none
48
49end if