Skip to main content

starnix_sync/
lib.rs

1// Copyright 2023 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5//! Synchronization objects used by Starnix
6
7mod atomic_time;
8mod condvar;
9mod interruptible_event;
10mod lock_dep_mutex;
11mod lock_ordering;
12mod lock_traits;
13mod locks;
14mod port_event;
15mod rw_seq_lock;
16mod thread_affinity;
17
18pub use atomic_time::*;
19pub use condvar::*;
20pub use interruptible_event::*;
21pub use lock_dep_mutex::*;
22pub use lock_ordering::*;
23pub use lock_ordering_macro::*;
24pub use lock_traits::*;
25pub use locks::*;
26pub use port_event::*;
27pub use rw_seq_lock::*;
28pub use thread_affinity::*;
29
30// This allows lock_ordering! macro to use paths within this crate
31// by referring to them by the external crate name.
32extern crate self as starnix_sync;
33
34/// The duration a blocking wait must sleep before `clean_stack()` is called to decommit unused
35/// stack pages.
36pub(crate) const STACK_CLEAN_DELAY: zx::MonotonicDuration = zx::MonotonicDuration::from_seconds(5);
37
38/// Computes the initial deadline for a blocking wait so that waits longer than `clean_delay` wake
39/// once after `clean_delay` to call `starnix_stack::clean_stack()` before sleeping until
40/// `deadline`.
41#[inline]
42pub(crate) fn initial_clean_deadline(
43    deadline: zx::MonotonicInstant,
44    clean_delay: zx::MonotonicDuration,
45) -> zx::MonotonicInstant {
46    // Avoid querying the monotonic clock when the deadline is already in the past (e.g.
47    // `INFINITE_PAST` or `ZERO` on non-blocking polls).
48    if deadline > zx::MonotonicInstant::ZERO {
49        std::cmp::min(deadline, zx::MonotonicInstant::after(clean_delay))
50    } else {
51        deadline
52    }
53}