Skip to main content

ksync/
lib.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#![no_std]
6
7#[cfg(test)]
8extern crate self as ksync;
9
10pub use kstring::declare_interned_string;
11pub use ksync_macro::guarded;
12pub use pin_init;
13
14/// Locks a mutex.
15///
16/// Usage:
17///   `ksync::lock!(let mut guard = self.lock_mu());`
18///   Locks the mutex and binds a mutable pin to `guard`. Useful when you need to mutate
19///   guarded fields via `guard.as_mut().fields_mut()`.
20///
21///   `ksync::lock!(let guard = self.lock_mu());`
22///   Locks the mutex and binds an immutable pin to `guard`. Useful for read-only access
23///   to guarded fields via `guard.fields()`.
24///
25///   `ksync::lock!(self.lock_mu());`
26///   Locks the mutex and keeps it locked until the end of the scope, without binding the guard.
27#[macro_export]
28macro_rules! lock {
29    (let mut $guard:ident = $lock_init:expr) => {
30        $crate::pin_init::stack_pin_init!(let $guard = $lock_init);
31        let mut $guard = $guard;
32    };
33    (let $guard:ident = $lock_init:expr) => {
34        $crate::pin_init::stack_pin_init!(let $guard = $lock_init);
35    };
36    ($lock_init:expr) => {
37        $crate::pin_init::stack_pin_init!(let _guard = $lock_init);
38    };
39}
40
41mod kcell;
42mod kmutex;
43mod lock_token;
44mod raw_lock;
45
46#[cfg(not(feature = "kernel"))]
47mod raw_userspace_mutex;
48
49#[cfg(feature = "kernel")]
50mod raw_kernel_mutex;
51
52pub use kcell::{KCell, KCellInit, kcell_init};
53pub use kmutex::{KMutex, KMutexGuard};
54pub use lock_token::LockToken;
55pub use lockdep::{LockClass, LockClassRegistration};
56pub use raw_lock::RawLock;
57
58#[cfg(not(feature = "kernel"))]
59pub use raw_userspace_mutex::RawMutex;
60
61#[cfg(feature = "kernel")]
62pub use raw_kernel_mutex::{RawCriticalMutex, RawMutex};