Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/crates/bevy_platform/src/sync/rwlock.rs
6849 views
1
//! Provides `RwLock`, `RwLockReadGuard`, `RwLockWriteGuard`
2
3
pub use implementation::{RwLock, RwLockReadGuard, RwLockWriteGuard};
4
5
#[cfg(feature = "std")]
6
use std::sync as implementation;
7
8
#[cfg(not(feature = "std"))]
9
mod implementation {
10
use crate::sync::{LockResult, TryLockError, TryLockResult};
11
use core::fmt;
12
13
pub use spin::rwlock::{RwLockReadGuard, RwLockWriteGuard};
14
15
/// Fallback implementation of `RwLock` from the standard library.
16
pub struct RwLock<T: ?Sized> {
17
inner: spin::RwLock<T>,
18
}
19
20
impl<T> RwLock<T> {
21
/// Creates a new instance of an `RwLock<T>` which is unlocked.
22
///
23
/// See the standard library for further details.
24
pub const fn new(t: T) -> RwLock<T> {
25
Self {
26
inner: spin::RwLock::new(t),
27
}
28
}
29
}
30
31
impl<T: ?Sized> RwLock<T> {
32
/// Locks this `RwLock` with shared read access, blocking the current thread
33
/// until it can be acquired.
34
///
35
/// See the standard library for further details.
36
pub fn read(&self) -> LockResult<RwLockReadGuard<'_, T>> {
37
Ok(self.inner.read())
38
}
39
40
/// Attempts to acquire this `RwLock` with shared read access.
41
///
42
/// See the standard library for further details.
43
pub fn try_read(&self) -> TryLockResult<RwLockReadGuard<'_, T>> {
44
self.inner.try_read().ok_or(TryLockError::WouldBlock)
45
}
46
47
/// Locks this `RwLock` with exclusive write access, blocking the current
48
/// thread until it can be acquired.
49
///
50
/// See the standard library for further details.
51
pub fn write(&self) -> LockResult<RwLockWriteGuard<'_, T>> {
52
Ok(self.inner.write())
53
}
54
55
/// Attempts to lock this `RwLock` with exclusive write access.
56
///
57
/// See the standard library for further details.
58
pub fn try_write(&self) -> TryLockResult<RwLockWriteGuard<'_, T>> {
59
self.inner.try_write().ok_or(TryLockError::WouldBlock)
60
}
61
62
/// Determines whether the lock is poisoned.
63
///
64
/// See the standard library for further details.
65
pub fn is_poisoned(&self) -> bool {
66
false
67
}
68
69
/// Clear the poisoned state from a lock.
70
///
71
/// See the standard library for further details.
72
pub fn clear_poison(&self) {
73
// no-op
74
}
75
76
/// Consumes this `RwLock`, returning the underlying data.
77
///
78
/// See the standard library for further details.
79
pub fn into_inner(self) -> LockResult<T>
80
where
81
T: Sized,
82
{
83
Ok(self.inner.into_inner())
84
}
85
86
/// Returns a mutable reference to the underlying data.
87
///
88
/// See the standard library for further details.
89
pub fn get_mut(&mut self) -> LockResult<&mut T> {
90
Ok(self.inner.get_mut())
91
}
92
}
93
94
impl<T: ?Sized + fmt::Debug> fmt::Debug for RwLock<T> {
95
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
96
let mut d = f.debug_struct("RwLock");
97
match self.try_read() {
98
Ok(guard) => {
99
d.field("data", &&*guard);
100
}
101
Err(TryLockError::Poisoned(err)) => {
102
d.field("data", &&**err.get_ref());
103
}
104
Err(TryLockError::WouldBlock) => {
105
d.field("data", &format_args!("<locked>"));
106
}
107
}
108
d.field("poisoned", &false);
109
d.finish_non_exhaustive()
110
}
111
}
112
113
impl<T: Default> Default for RwLock<T> {
114
fn default() -> RwLock<T> {
115
RwLock::new(Default::default())
116
}
117
}
118
119
impl<T> From<T> for RwLock<T> {
120
fn from(t: T) -> Self {
121
RwLock::new(t)
122
}
123
}
124
}
125
126