forked from compio-rs/synchrony
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmutex_blocking.rs
More file actions
130 lines (109 loc) · 3.45 KB
/
mutex_blocking.rs
File metadata and controls
130 lines (109 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//! Blocking Mutex lock
/// Multithreaded blocking Mutex
pub mod sync {
use std::{
ops::{Deref, DerefMut},
sync::{Mutex as Inner, MutexGuard as InnerGuard},
};
/// A multithreaded Mutex based on [`std::sync::Mutex`].
pub struct Mutex<T: ?Sized>(Inner<T>);
impl<T> Mutex<T> {
/// Creates a new mutex in an unlocked state ready for use.
pub const fn new(val: T) -> Self {
Self(Inner::new(val))
}
/// Get the inner [`std::sync::Mutex`].
pub fn into_inner(self) -> Inner<T> {
self.0
}
}
impl<T: ?Sized> Mutex<T> {
/// Acquires a mutex, blocking the current thread until it is able to do
/// so.
///
/// See [`std::sync::Mutex::lock`] for detail.
///
/// # Panics
///
/// This function might panic when called if the lock is already held by
/// the current thread or is poisoned (some thread panicked while
/// holding the lock).
pub fn lock(&self) -> MutexGuard<'_, T> {
MutexGuard(self.0.lock().unwrap())
}
}
/// An RAII implementation of a "scoped lock" of a mutex. When this
/// structure is dropped (falls out of scope), the lock will be
/// unlocked.
pub struct MutexGuard<'a, T: ?Sized>(InnerGuard<'a, T>);
impl<'a, T: ?Sized> MutexGuard<'a, T> {
/// Get the inner [`std::sync::MutexGuard`].
pub fn into_inner(self) -> InnerGuard<'a, T> {
self.0
}
}
impl<'a, T> Deref for MutexGuard<'a, T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<'a, T> DerefMut for MutexGuard<'a, T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl<T: Send> crate::AssertMt for Mutex<T> {}
}
/// Singlethreaded blocking Mutex
pub mod unsync {
use std::{
cell::{RefCell as Inner, RefMut as InnerGuard},
ops::{Deref, DerefMut},
};
/// A singlethreaded Mutex based on [`std::cell::RefCell`].
pub struct Mutex<T: ?Sized>(Inner<T>);
impl<T> Mutex<T> {
/// Creates a new mutex in an unlocked state ready for use.
pub const fn new(val: T) -> Self {
Self(Inner::new(val))
}
/// Get the inner [`std::cell::RefCell`].
pub fn into_inner(self) -> Inner<T> {
self.0
}
}
impl<T: ?Sized> Mutex<T> {
/// Acquires a mutex.
///
/// See [`std::cell::RefCell::borrow_mut`] for detail.
///
/// # Panics
///
/// Panics if the value is currently borrowed.
pub fn lock(&self) -> MutexGuard<'_, T> {
MutexGuard(self.0.borrow_mut())
}
}
/// An RAII implementation of a "scoped lock" of a mutex. When this
/// structure is dropped (falls out of scope), the lock will be
/// unlocked.
pub struct MutexGuard<'a, T: ?Sized>(InnerGuard<'a, T>);
impl<'a, T: ?Sized> MutexGuard<'a, T> {
/// Get the inner [`std::cell::RefMut`].
pub fn into_inner(self) -> InnerGuard<'a, T> {
self.0
}
}
impl<'a, T> Deref for MutexGuard<'a, T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<'a, T> DerefMut for MutexGuard<'a, T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
}