Skip to main content

std/sys/sync/once/
mod.rs

1// A "once" is a relatively simple primitive, and it's also typically provided
2// by the OS as well (see `pthread_once` or `InitOnceExecuteOnce`). The OS
3// primitives, however, tend to have surprising restrictions, such as the Unix
4// one doesn't allow an argument to be passed to the function.
5//
6// As a result, we end up implementing it ourselves in the standard library.
7// This also gives us the opportunity to optimize the implementation a bit which
8// should help the fast path on call sites.
9
10cfg_select! {
11    any(
12        all(target_os = "windows", not(target_vendor = "win7")),
13        target_os = "linux",
14        target_os = "android",
15        all(target_family = "wasm", target_feature = "atomics"),
16        target_os = "freebsd",
17        target_os = "motor",
18        target_os = "openbsd",
19        target_os = "dragonfly",
20        target_os = "fuchsia",
21        target_os = "hermit",
22        all(target_os = "wasi", target_env = "p3"),
23    ) => {
24        mod futex;
25        pub use futex::{Once, OnceState};
26    }
27    any(
28        windows,
29        target_family = "unix",
30        all(target_vendor = "fortanix", target_env = "sgx"),
31        target_os = "solid_asp3",
32        target_os = "xous",
33    ) => {
34        mod queue;
35        pub use queue::{Once, OnceState};
36    }
37    _ => {
38        mod no_threads;
39        pub use no_threads::{Once, OnceState};
40    }
41}