static_init/
phase_locker.rs1#[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
22pub(crate) unsafe trait PhaseGuard<'a, T: ?Sized + 'a> {
30 fn set_phase(&mut self, p: Phase);
39 fn commit_phase(&mut self);
42 fn phase(&self) -> Phase;
44 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#[derive(Copy, Clone, Debug, Eq, PartialEq)]
90pub enum LockNature {
91 Read,
92 Write,
93 None,
94}
95pub 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;