2020-08-27 13:45:01 +00:00
|
|
|
|
#[cfg(all(test, not(target_os = "emscripten")))]
|
|
|
|
|
mod tests;
|
|
|
|
|
|
2019-02-10 19:23:21 +00:00
|
|
|
|
use crate::fmt;
|
|
|
|
|
use crate::marker;
|
|
|
|
|
use crate::ops::Deref;
|
2019-12-22 22:42:04 +00:00
|
|
|
|
use crate::panic::{RefUnwindSafe, UnwindSafe};
|
2019-02-10 19:23:21 +00:00
|
|
|
|
use crate::sys::mutex as sys;
|
2015-04-03 21:46:54 +00:00
|
|
|
|
|
|
|
|
|
/// A re-entrant mutual exclusion
|
|
|
|
|
///
|
2015-05-05 23:44:28 +00:00
|
|
|
|
/// This mutex will block *other* threads waiting for the lock to become
|
|
|
|
|
/// available. The thread which has already locked the mutex can lock it
|
|
|
|
|
/// multiple times without blocking, preventing a common source of deadlocks.
|
2015-04-03 21:46:54 +00:00
|
|
|
|
pub struct ReentrantMutex<T> {
|
2020-03-12 18:39:30 +00:00
|
|
|
|
inner: sys::ReentrantMutex,
|
2015-04-03 21:46:54 +00:00
|
|
|
|
data: T,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsafe impl<T: Send> Send for ReentrantMutex<T> {}
|
|
|
|
|
unsafe impl<T: Send> Sync for ReentrantMutex<T> {}
|
|
|
|
|
|
2018-07-01 21:30:16 +00:00
|
|
|
|
impl<T> UnwindSafe for ReentrantMutex<T> {}
|
|
|
|
|
impl<T> RefUnwindSafe for ReentrantMutex<T> {}
|
|
|
|
|
|
2015-04-03 21:46:54 +00:00
|
|
|
|
/// An RAII implementation of a "scoped lock" of a mutex. When this structure is
|
|
|
|
|
/// dropped (falls out of scope), the lock will be unlocked.
|
|
|
|
|
///
|
|
|
|
|
/// The data protected by the mutex can be accessed through this guard via its
|
2015-08-12 12:39:49 +00:00
|
|
|
|
/// Deref implementation.
|
|
|
|
|
///
|
|
|
|
|
/// # Mutability
|
|
|
|
|
///
|
|
|
|
|
/// Unlike `MutexGuard`, `ReentrantMutexGuard` does not implement `DerefMut`,
|
|
|
|
|
/// because implementation of the trait would violate Rust’s reference aliasing
|
|
|
|
|
/// rules. Use interior mutability (usually `RefCell`) in order to mutate the
|
|
|
|
|
/// guarded data.
|
2018-05-07 16:27:50 +00:00
|
|
|
|
#[must_use = "if unused the ReentrantMutex will immediately unlock"]
|
2015-04-03 21:46:54 +00:00
|
|
|
|
pub struct ReentrantMutexGuard<'a, T: 'a> {
|
2015-08-12 12:39:49 +00:00
|
|
|
|
// funny underscores due to how Deref currently works (it disregards field
|
|
|
|
|
// privacy).
|
2015-04-03 21:46:54 +00:00
|
|
|
|
__lock: &'a ReentrantMutex<T>,
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-18 03:42:36 +00:00
|
|
|
|
impl<T> !marker::Send for ReentrantMutexGuard<'_, T> {}
|
2015-04-03 21:46:54 +00:00
|
|
|
|
|
|
|
|
|
impl<T> ReentrantMutex<T> {
|
|
|
|
|
/// Creates a new reentrant mutex in an unlocked state.
|
2020-03-12 18:39:30 +00:00
|
|
|
|
///
|
|
|
|
|
/// # Unsafety
|
|
|
|
|
///
|
|
|
|
|
/// This function is unsafe because it is required that `init` is called
|
|
|
|
|
/// once this mutex is in its final resting place, and only then are the
|
|
|
|
|
/// lock/unlock methods safe.
|
|
|
|
|
pub const unsafe fn new(t: T) -> ReentrantMutex<T> {
|
|
|
|
|
ReentrantMutex { inner: sys::ReentrantMutex::uninitialized(), data: t }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Initializes this mutex so it's ready for use.
|
|
|
|
|
///
|
|
|
|
|
/// # Unsafety
|
|
|
|
|
///
|
|
|
|
|
/// Unsafe to call more than once, and must be called after this will no
|
|
|
|
|
/// longer move in memory.
|
|
|
|
|
pub unsafe fn init(&self) {
|
|
|
|
|
self.inner.init();
|
2015-04-03 21:46:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Acquires a mutex, blocking the current thread until it is able to do so.
|
|
|
|
|
///
|
|
|
|
|
/// This function will block the caller until it is available to acquire the mutex.
|
|
|
|
|
/// Upon returning, the thread is the only thread with the mutex held. When the thread
|
|
|
|
|
/// calling this method already holds the lock, the call shall succeed without
|
|
|
|
|
/// blocking.
|
|
|
|
|
///
|
2016-02-02 02:41:29 +00:00
|
|
|
|
/// # Errors
|
2015-04-03 21:46:54 +00:00
|
|
|
|
///
|
|
|
|
|
/// If another user of this mutex panicked while holding the mutex, then
|
|
|
|
|
/// this call will return failure if the mutex would otherwise be
|
|
|
|
|
/// acquired.
|
2020-03-12 18:39:30 +00:00
|
|
|
|
pub fn lock(&self) -> ReentrantMutexGuard<'_, T> {
|
2015-04-03 21:46:54 +00:00
|
|
|
|
unsafe { self.inner.lock() }
|
|
|
|
|
ReentrantMutexGuard::new(&self)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Attempts to acquire this lock.
|
|
|
|
|
///
|
|
|
|
|
/// If the lock could not be acquired at this time, then `Err` is returned.
|
|
|
|
|
/// Otherwise, an RAII guard is returned.
|
|
|
|
|
///
|
|
|
|
|
/// This function does not block.
|
|
|
|
|
///
|
2016-02-02 02:41:29 +00:00
|
|
|
|
/// # Errors
|
2015-04-03 21:46:54 +00:00
|
|
|
|
///
|
|
|
|
|
/// If another user of this mutex panicked while holding the mutex, then
|
|
|
|
|
/// this call will return failure if the mutex would otherwise be
|
|
|
|
|
/// acquired.
|
2020-03-12 18:39:30 +00:00
|
|
|
|
pub fn try_lock(&self) -> Option<ReentrantMutexGuard<'_, T>> {
|
|
|
|
|
if unsafe { self.inner.try_lock() } { Some(ReentrantMutexGuard::new(&self)) } else { None }
|
2015-04-03 21:46:54 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T> Drop for ReentrantMutex<T> {
|
|
|
|
|
fn drop(&mut self) {
|
|
|
|
|
// This is actually safe b/c we know that there is no further usage of
|
|
|
|
|
// this mutex (it's up to the user to arrange for a mutex to get
|
|
|
|
|
// dropped, that's not our job)
|
|
|
|
|
unsafe { self.inner.destroy() }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T: fmt::Debug + 'static> fmt::Debug for ReentrantMutex<T> {
|
2019-03-01 08:34:11 +00:00
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2015-04-03 21:46:54 +00:00
|
|
|
|
match self.try_lock() {
|
2020-03-12 18:39:30 +00:00
|
|
|
|
Some(guard) => f.debug_struct("ReentrantMutex").field("data", &*guard).finish(),
|
|
|
|
|
None => {
|
2017-10-09 18:09:08 +00:00
|
|
|
|
struct LockedPlaceholder;
|
|
|
|
|
impl fmt::Debug for LockedPlaceholder {
|
2019-03-01 08:34:11 +00:00
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
|
f.write_str("<locked>")
|
|
|
|
|
}
|
2017-10-09 18:09:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
f.debug_struct("ReentrantMutex").field("data", &LockedPlaceholder).finish()
|
|
|
|
|
}
|
2015-04-03 21:46:54 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'mutex, T> ReentrantMutexGuard<'mutex, T> {
|
2020-03-12 18:39:30 +00:00
|
|
|
|
fn new(lock: &'mutex ReentrantMutex<T>) -> ReentrantMutexGuard<'mutex, T> {
|
|
|
|
|
ReentrantMutexGuard { __lock: lock }
|
2015-04-03 21:46:54 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-18 03:42:36 +00:00
|
|
|
|
impl<T> Deref for ReentrantMutexGuard<'_, T> {
|
2015-04-03 21:46:54 +00:00
|
|
|
|
type Target = T;
|
|
|
|
|
|
2015-09-07 22:36:29 +00:00
|
|
|
|
fn deref(&self) -> &T {
|
2015-04-03 21:46:54 +00:00
|
|
|
|
&self.__lock.data
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-18 03:42:36 +00:00
|
|
|
|
impl<T> Drop for ReentrantMutexGuard<'_, T> {
|
2015-04-03 21:46:54 +00:00
|
|
|
|
#[inline]
|
|
|
|
|
fn drop(&mut self) {
|
|
|
|
|
unsafe {
|
|
|
|
|
self.__lock.inner.unlock();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|