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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
//! An implementation of re-entrant mutexes.
//!
//! Re-entrant mutexes are like mutexes, but where it is expected
//! that a single thread may own a lock more than once.
//! It provides the same interface as https://github.com/rust-lang/rust/blob/master/src/libstd/sys/common/remutex.rs
//! so if those types are ever exported, we should be able to replace this implemtation.
use core::nonzero::NonZero;
use std::cell::{Cell, UnsafeCell};
use std::mem;
use std::ops::Deref;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{LockResult, Mutex, MutexGuard, PoisonError, TryLockError, TryLockResult};
/// A type for thread ids.
// TODO: can we use the thread-id crate for this?
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub struct ThreadId(NonZero<usize>);
lazy_static!{ static ref THREAD_COUNT: AtomicUsize = AtomicUsize::new(1); }
impl ThreadId {
#[allow(unsafe_code)]
fn new() -> ThreadId {
let number = THREAD_COUNT.fetch_add(1, Ordering::SeqCst);
ThreadId(unsafe { NonZero::new(number) })
}
pub fn current() -> ThreadId {
THREAD_ID.with(|tls| tls.clone())
}
}
thread_local!{ static THREAD_ID: ThreadId = ThreadId::new() }
/// A type for atomic storage of thread ids.
#[derive(Debug)]
pub struct AtomicOptThreadId(AtomicUsize);
impl AtomicOptThreadId {
pub fn new() -> AtomicOptThreadId {
AtomicOptThreadId(AtomicUsize::new(0))
}
pub fn store(&self, value: Option<ThreadId>, ordering: Ordering) {
let number = value.map(|id| *id.0).unwrap_or(0);
self.0.store(number, ordering);
}
#[allow(unsafe_code)]
pub fn load(&self, ordering: Ordering) -> Option<ThreadId> {
let number = self.0.load(ordering);
if number == 0 { None } else { Some(ThreadId(unsafe { NonZero::new(number) })) }
}
#[allow(unsafe_code)]
pub fn swap(&self, value: Option<ThreadId>, ordering: Ordering) -> Option<ThreadId> {
let number = value.map(|id| *id.0).unwrap_or(0);
let number = self.0.swap(number, ordering);
if number == 0 { None } else { Some(ThreadId(unsafe { NonZero::new(number) })) }
}
}
/// A type for hand-over-hand mutexes.
///
/// These support `lock` and `unlock` functions. `lock` blocks waiting to become the
/// mutex owner. `unlock` can only be called by the lock owner, and panics otherwise.
/// They have the same happens-before and poisoning semantics as `Mutex`.
// TODO: Can we use `raw_lock` and `raw_unlock` from `parking_lot`'s `Mutex` for this?
pub struct HandOverHandMutex {
mutex: Mutex<()>,
owner: AtomicOptThreadId,
guard: UnsafeCell<Option<MutexGuard<'static, ()>>>,
}
impl HandOverHandMutex {
pub fn new() -> HandOverHandMutex {
HandOverHandMutex {
mutex: Mutex::new(()),
owner: AtomicOptThreadId::new(),
guard: UnsafeCell::new(None),
}
}
#[allow(unsafe_code)]
pub fn lock(&self) -> LockResult<()> {
let (guard, result) = match self.mutex.lock() {
Ok(guard) => (guard, Ok(())),
Err(err) => (err.into_inner(), Err(PoisonError::new(()))),
};
unsafe { *self.guard.get().as_mut().unwrap() = mem::transmute(guard) };
self.owner.store(Some(ThreadId::current()), Ordering::Relaxed);
result
}
#[allow(unsafe_code)]
pub fn try_lock(&self) -> TryLockResult<()> {
let (guard, result) = match self.mutex.try_lock() {
Ok(guard) => (guard, Ok(())),
Err(TryLockError::WouldBlock) => return Err(TryLockError::WouldBlock),
Err(TryLockError::Poisoned(err)) => (err.into_inner(), Err(TryLockError::Poisoned(PoisonError::new(())))),
};
unsafe { *self.guard.get().as_mut().unwrap() = mem::transmute(guard) };
self.owner.store(Some(ThreadId::current()), Ordering::Relaxed);
result
}
#[allow(unsafe_code)]
pub fn unlock(&self) {
assert_eq!(Some(ThreadId::current()), self.owner.load(Ordering::Relaxed));
self.owner.store(None, Ordering::Relaxed);
unsafe { *self.guard.get().as_mut().unwrap() = None; }
}
pub fn owner(&self) -> Option<ThreadId> {
self.owner.load(Ordering::Relaxed)
}
}
#[allow(unsafe_code)]
unsafe impl Send for HandOverHandMutex {}
/// A type for re-entrant mutexes.
///
/// It provides the same interface as https://github.com/rust-lang/rust/blob/master/src/libstd/sys/common/remutex.rs
pub struct ReentrantMutex<T> {
mutex: HandOverHandMutex,
count: Cell<usize>,
data: T,
}
#[allow(unsafe_code)]
unsafe impl<T> Sync for ReentrantMutex<T> where T: Send {}
impl<T> ReentrantMutex<T> {
pub fn new(data: T) -> ReentrantMutex<T> {
debug!("{:?} Creating new lock.", ThreadId::current());
ReentrantMutex {
mutex: HandOverHandMutex::new(),
count: Cell::new(0),
data: data,
}
}
pub fn lock(&self) -> LockResult<ReentrantMutexGuard<T>> {
debug!("{:?} Locking.", ThreadId::current());
if self.mutex.owner() != Some(ThreadId::current()) {
debug!("{:?} Becoming owner.", ThreadId::current());
if let Err(_) = self.mutex.lock() {
debug!("{:?} Poison!", ThreadId::current());
return Err(PoisonError::new(self.mk_guard()));
}
debug!("{:?} Became owner.", ThreadId::current());
}
Ok(self.mk_guard())
}
pub fn try_lock(&self) -> TryLockResult<ReentrantMutexGuard<T>> {
debug!("{:?} Try locking.", ThreadId::current());
if self.mutex.owner() != Some(ThreadId::current()) {
debug!("{:?} Becoming owner?", ThreadId::current());
if let Err(err) = self.mutex.try_lock() {
match err {
TryLockError::WouldBlock => {
debug!("{:?} Would block.", ThreadId::current());
return Err(TryLockError::WouldBlock)
},
TryLockError::Poisoned(_) => {
debug!("{:?} Poison!", ThreadId::current());
return Err(TryLockError::Poisoned(PoisonError::new(self.mk_guard())));
},
}
}
debug!("{:?} Became owner.", ThreadId::current());
}
Ok(self.mk_guard())
}
fn unlock(&self) {
debug!("{:?} Unlocking.", ThreadId::current());
let count = self.count.get().checked_sub(1).expect("Underflowed lock count.");
debug!("{:?} Decrementing count to {}.", ThreadId::current(), count);
self.count.set(count);
if count == 0 {
debug!("{:?} Releasing mutex.", ThreadId::current());
self.mutex.unlock();
}
}
fn mk_guard(&self) -> ReentrantMutexGuard<T> {
let count = self.count.get().checked_add(1).expect("Overflowed lock count.");
debug!("{:?} Incrementing count to {}.", ThreadId::current(), count);
self.count.set(count);
ReentrantMutexGuard { mutex: self }
}
}
#[must_use]
pub struct ReentrantMutexGuard<'a, T> where T: 'static {
mutex: &'a ReentrantMutex<T>,
}
impl<'a, T> Drop for ReentrantMutexGuard<'a, T> {
#[allow(unsafe_code)]
fn drop(&mut self) {
self.mutex.unlock()
}
}
impl<'a, T> Deref for ReentrantMutexGuard<'a, T> {
type Target = T;
fn deref(&self) -> &T {
&self.mutex.data
}
}
|