static_init/
phase_locker.rs

1#[cfg(not(feature = "spin_loop"))]
2mod futex;
3
4mod spin_wait;
5
6#[cfg(not(feature = "spin_loop"))]
7mod sync;
8
9#[cfg(feature = "spin_loop")]
10#[path = "phase_locker/sync_spinning.rs"]
11mod sync;
12
13pub(crate) use sync::Mutex;
14pub(crate) use sync::{SyncPhaseGuard, SyncPhaseLocker, SyncReadPhaseGuard};
15
16mod unsync;
17pub(crate) use unsync::UnSyncPhaseLocker;
18pub(crate) use unsync::{UnSyncPhaseGuard, UnSyncReadPhaseGuard};
19
20use super::{Phase, Phased};
21
22/// A phase guard ensure that the target object will
23/// performed atomic phase transition
24///
25/// # Safety
26///
27/// The trait is unsafe because the implementation must fullfill the
28/// following requirement described in the documentation of the functions
29pub(crate) unsafe trait PhaseGuard<'a, T: ?Sized + 'a> {
30    /// Set the phase at which will be the traget object
31    /// when the phase guard will be dropped
32    ///
33    /// # Safety
34    ///
35    /// This function is unsafe because not providing a correct phase
36    /// may lead to miss interpretation of the state of the target object
37    /// which may result in unsound lazy
38    fn set_phase(&mut self, p: Phase);
39    /// Set the phase of the target object with release semantic if the
40    /// PhaseGuard is Sync
41    fn commit_phase(&mut self);
42    /// Return the phase at which will be the object
43    fn phase(&self) -> Phase;
44    /// Execute the function f then:
45    ///   - if execution of f does not panic change, call Self::set_phase(on_success)
46    ///   - if execution of f panics: set the phase of the target object to on_panic and
47    ///   release the lock.
48    fn transition<R>(
49        &mut self,
50        f: impl FnOnce(&'a T) -> R,
51        on_success: Phase,
52        on_panic: Phase,
53    ) -> R;
54}
55
56pub(crate) trait Mappable<T, V, U> {
57    fn map<F: FnOnce(&T) -> &V>(self, f: F) -> U;
58}
59
60pub(crate) unsafe trait PhaseLocker<'a, T: 'a> {
61    type ReadGuard: Phased;
62    type WriteGuard: Phased + PhaseGuard<'a, T>;
63
64    fn lock<FL: Fn(Phase) -> LockNature, FW: Fn(Phase) -> LockNature>(
65        &'a self,
66        value: &'a T,
67        lock_nature: FL,
68        on_wake_nature: FW,
69        hint: Phase,
70    ) -> LockResult<Self::ReadGuard, Self::WriteGuard>;
71    fn lock_mut(&'a mut self, value: &'a T) -> Self::WriteGuard;
72    fn try_lock<F: Fn(Phase) -> LockNature>(
73        &'a self,
74        value: &'a T,
75        lock_nature: F,
76        hint: Phase,
77    ) -> Option<LockResult<Self::ReadGuard, Self::WriteGuard>>;
78    fn phase(&self) -> Phase;
79}
80
81pub(crate) unsafe trait MutPhaseLocker {
82    fn get_phase_unique(&mut self) -> Phase;
83    fn set_phase(&mut self, p: Phase);
84
85    fn transition<R>(&mut self, f: impl FnOnce() -> R, on_success: Phase, on_panic: Phase) -> R;
86}
87
88/// Nature of the lock requested
89#[derive(Copy, Clone, Debug, Eq, PartialEq)]
90pub enum LockNature {
91    Read,
92    Write,
93    None,
94}
95/// Result of a Phased locking
96pub enum LockResult<R, W> {
97    Read(R),
98    Write(W),
99    None(Phase),
100}
101
102#[cfg(all(feature = "lock_statistics", not(feature = "spin_loop")))]
103pub use sync::LockStatistics;