1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
//! Waiting on a value in memory, and waking the waiters: the primitive
//! mutexes, condition variables and other locks are built from.
//!
//! A thread calls [`wait`] to sleep for as long as an [`AtomicU32`] holds a
//! value it does not like; a thread that changes the value calls
//! [`wake_one`] or [`wake_all`]. The system keeps the queue of waiters,
//! keyed on the address of the atomic: the atomic itself is ordinary memory
//! and takes no initialisation.
//!
//! | System | Calls |
//! |---|---|
//! | Linux | `futex` (`FUTEX_WAIT`, `FUTEX_WAKE`, private to the process) |
//! | macOS | `os_sync_wait_on_address`, `os_sync_wait_on_address_with_timeout`, `os_sync_wake_by_address_any`, `os_sync_wake_by_address_all`; before macOS 14.4, `__ulock_wait2`, `__ulock_wake` (`UL_COMPARE_AND_WAIT`) |
//! | Windows | `WaitOnAddress`, `WakeByAddressSingle`, `WakeByAddressAll` |
//!
//! Waiters are found by address within the process: an atomic in memory
//! shared with another process does not wake that process's waiters.
//!
//! On macOS the documented calls (`<os/os_sync_wait_on_address.h>`) need
//! macOS 14.4: they are looked up in the running system the first time
//! (`dlsym`), and used if all four are there. An older system is given the
//! calls they are made of, which the system's own libc++ and Rust's `std`
//! use, and which are not part of the documented SDK. Either way the
//! thread's `errno` is left as it was.
//!
//! WebAssembly is single-threaded here: see [`wait`] for what waiting means
//! when nobody can wake.
//!
//! ```
//! use core::sync::atomic::{AtomicU32, Ordering};
//! use oscall::sync;
//!
//! /// Blocks until `flag` is set.
//! fn wait_for(flag: &AtomicU32) -> oscall::Result<()> {
//! // The loop is not optional: `wait` can return with nothing changed.
//! while flag.load(Ordering::Acquire) == 0 {
//! sync::wait(flag, 0, None)?;
//! }
//! Ok(())
//! }
//!
//! fn set(flag: &AtomicU32) {
//! flag.store(1, Ordering::Release);
//! sync::wake_all(flag);
//! }
//!
//! let flag = AtomicU32::new(0);
//! set(&flag);
//! wait_for(&flag).unwrap();
//! ```
use AtomicU32;
use Duration;
use crateResult;
use cratesys;
/// Why [`wait`] returned.
/// Puts the calling thread to sleep if `atomic` holds `expected`, until it
/// is woken or `timeout` passes.
///
/// The comparison and the going to sleep are one atomic step as far as
/// [`wake_one`] and [`wake_all`] are concerned: a thread that stores a new
/// value and then wakes cannot slip between the two and leave the waiter
/// asleep. If the value is not `expected`, the call returns
/// [`Woken`](WaitOutcome::Woken) at once.
///
/// `timeout` is relative, and measured on the clock
/// [`Clock::Monotonic`](crate::time::Clock::Monotonic) reads; `None` waits
/// for as long as it takes. A zero timeout never sleeps: it only compares.
/// [`TimedOut`](WaitOutcome::TimedOut) is never returned before the timeout
/// has passed on that clock. Where the system would do so, the wait ends as
/// a spurious wake-up instead: on Windows, which counts in milliseconds
/// (rounded up here) and in timer ticks, and can give up a tick early; and
/// for a timeout longer than the system can count, which is shortened (49
/// days on Windows) or taken as no timeout at all (centuries elsewhere).
///
/// See [`WaitOutcome`] for what the result does and does not tell. Spurious
/// wake-ups are rare but real on every system; an interruption by a signal
/// is reported as one, not as an error.
///
/// # WebAssembly
///
/// oscall takes WebAssembly to be single-threaded, so nobody can wake a
/// waiter. If the value is not `expected` the call returns
/// [`Woken`](WaitOutcome::Woken), as everywhere. Otherwise, with a timeout,
/// the thread sleeps for its whole length and
/// [`TimedOut`](WaitOutcome::TimedOut) is returned (on WASI; without WASI
/// there is no way to sleep and the call fails with
/// [`Unsupported`](crate::ErrorKind::Unsupported), unless the timeout is
/// zero); with no timeout the wait could never end, and the call fails with
/// [`Deadlock`](crate::ErrorKind::Deadlock) instead of hanging.
/// Wakes one of the threads waiting on `atomic` in [`wait`], if there is
/// any. Which one is up to the system.
///
/// A thread that is about to wait, but has not compared the value yet, is
/// not a waiter: change the value before waking, so that it does not sleep.
///
/// Does nothing on WebAssembly, where there are no other threads.
/// Wakes all the threads waiting on `atomic` in [`wait`].
///
/// Does nothing on WebAssembly, where there are no other threads.