pal_async/unix/
mod.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! Unix-specific async infrastructure.
5
6// UNSAFETY: Calls to various libc functions to interact with os-level primitives
7// and handling their return values.
8#![expect(unsafe_code)]
9
10use cfg_if::cfg_if;
11
12pub mod local;
13pub mod pipe;
14pub mod wait;
15
16cfg_if! {
17    if #[cfg(target_os = "linux")] {
18        pub mod epoll;
19
20        pub use epoll::EpollDriver as DefaultDriver;
21        pub use epoll::EpollPool as DefaultPool;
22    } else if #[cfg(target_os = "macos")] {
23        pub mod kqueue;
24
25        pub use kqueue::KqueueDriver as DefaultDriver;
26        pub use kqueue::KqueuePool as DefaultPool;
27    }
28}
29
30pub(crate) fn monotonic_nanos_now() -> u64 {
31    let mut ts = libc::timespec {
32        tv_sec: 0,
33        tv_nsec: 0,
34    };
35
36    // SAFETY: calling C APIs as documented, with no special requirements, and validating its return value.
37    unsafe {
38        assert_eq!(libc::clock_gettime(libc::CLOCK_MONOTONIC, &mut ts), 0);
39    }
40
41    let sec: u64 = ts.tv_sec as u64;
42    sec.checked_mul(1000 * 1000 * 1000)
43        .and_then(|n| n.checked_add(ts.tv_nsec as u64))
44        .expect("time does not fit in u64")
45}