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 object_waiter;
15mod port_event;
16mod rcu_lock;
17mod rw_seq_lock;
18mod thread_affinity;
19
20pub use atomic_time::*;
21pub use condvar::*;
22pub use interruptible_event::*;
23pub use lock_dep_mutex::*;
24pub use lock_ordering::*;
25pub use lock_ordering_macro::*;
26pub use lock_traits::*;
27pub use locks::*;
28pub use object_waiter::*;
29pub use port_event::*;
30pub use rcu_lock::*;
31pub use rw_seq_lock::*;
32pub use thread_affinity::*;
33
34// This allows lock_ordering! macro to use paths within this crate
35// by referring to them by the external crate name.
36extern crate self as starnix_sync;
37
38/// The duration a blocking wait must sleep before `clean_stack()` is called to decommit unused
39/// stack pages.
40pub(crate) const STACK_CLEAN_DELAY: zx::MonotonicDuration = zx::MonotonicDuration::from_seconds(5);
41
42/// Computes the initial deadline for a blocking wait so that waits longer than `clean_delay` wake
43/// once after `clean_delay` to call `starnix_stack::clean_stack()` before sleeping until
44/// `deadline`.
45#[inline]
46pub(crate) fn initial_clean_deadline(
47    deadline: zx::MonotonicInstant,
48    clean_delay: zx::MonotonicDuration,
49) -> zx::MonotonicInstant {
50    // Avoid querying the monotonic clock when the deadline is already in the past (e.g.
51    // `INFINITE_PAST` or `ZERO` on non-blocking polls).
52    if deadline > zx::MonotonicInstant::ZERO {
53        std::cmp::min(deadline, zx::MonotonicInstant::after(clean_delay))
54    } else {
55        deadline
56    }
57}