Skip to main content

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 process;
15pub mod wait;
16
17cfg_if! {
18    if #[cfg(target_os = "linux")] {
19        pub mod epoll;
20        mod epoll_uring;
21
22        pub use epoll::EpollDriver as DefaultDriver;
23        pub use epoll::EpollPool as DefaultPool;
24    } else if #[cfg(target_os = "macos")] {
25        pub mod kqueue;
26
27        pub use kqueue::KqueueDriver as DefaultDriver;
28        pub use kqueue::KqueuePool as DefaultPool;
29    }
30}
31
32pub(crate) fn monotonic_nanos_now() -> u64 {
33    let mut ts = libc::timespec {
34        tv_sec: 0,
35        tv_nsec: 0,
36    };
37
38    // SAFETY: calling C APIs as documented, with no special requirements, and validating its return value.
39    unsafe {
40        assert_eq!(libc::clock_gettime(libc::CLOCK_MONOTONIC, &mut ts), 0);
41    }
42
43    let sec: u64 = ts.tv_sec as u64;
44    sec.checked_mul(1000 * 1000 * 1000)
45        .and_then(|n| n.checked_add(ts.tv_nsec as u64))
46        .expect("time does not fit in u64")
47}