ksync/lock_token.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
5use core::marker::PhantomData;
6
7/// A token proving that a lock of lock class `Class` is currently held by the current thread.
8///
9/// This token acts as a static proof to permit type-safe borrow access to cell values guarded by
10/// this lock class (via `KCell`).
11pub struct LockToken<'a, Class> {
12 _marker: PhantomData<&'a Class>,
13 _phantom: PhantomData<*const ()>,
14}
15
16impl<'a, Class> LockToken<'a, Class> {
17 /// Creates a new proof token for the lock class `Class`.
18 ///
19 /// # Safety
20 ///
21 /// The caller must guarantee that the mutual exclusion lock associated with this lock class is
22 /// currently held by the current thread and that the lifetime `'a` is bounded to the duration
23 /// of the lock being held. Creating a token when the lock is not held, or letting the token
24 /// outlive the lock hold duration, can lead to concurrent mutation or data races, which is
25 /// undefined behavior.
26 #[inline]
27 pub unsafe fn new() -> Self {
28 Self { _marker: PhantomData, _phantom: PhantomData }
29 }
30}