netstack3_base/sync.rs
1// Copyright 2026 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 primitive trait definitions for netstack3.
6
7use netstack3_sync::once;
8
9/// Synchronization primitive types provided by bindings.
10pub trait BindingsSyncTypes {
11 /// The raw mutex implementation used by [`netstack3_sync::Mutex`].
12 type Mutex: lock_api::RawMutex + Send + Sync + 'static;
13 /// The raw reader-writer lock implementation used by [`netstack3_sync::RwLock`].
14 type RwLock: lock_api::RawRwLock + Send + Sync + 'static;
15 /// The one-time initialization primitive used by [`netstack3_sync::OnceLock`].
16 type Once: once::Once + Send + Sync + 'static;
17}
18
19/// Concrete synchronization primitive implementations for tests.
20///
21/// This module provides default raw lock and `Once` types so fake bindings
22/// contexts and tests can implement [`BindingsSyncTypes`] without duplicating
23/// platform-specific lock selection.
24#[cfg(any(test, feature = "testutils"))]
25pub mod lock_impl {
26 extern crate std;
27
28 pub use fuchsia_sync::{RawMutex, RawRwLock};
29
30 /// Default [`netstack3_sync::once::Once`] implementation backed by
31 /// [`std::sync::Once`].
32 #[derive(Default)]
33 pub struct Once(std::sync::Once);
34
35 // SAFETY: `std::sync::Once` upholds all the guarantees required by
36 // `netstack3_sync::once::Once`.
37 unsafe impl super::once::Once for Once {
38 #[inline]
39 fn once(&self, f: impl FnOnce()) {
40 let Self(once) = self;
41 once.call_once(f);
42 }
43
44 #[inline]
45 fn is_completed(&self) -> bool {
46 let Self(once) = self;
47 once.is_completed()
48 }
49 }
50}