use parking_lot::{
lock_api::{
RawRwLock as _,
RawRwLockFair as _,
RawRwLockDowngrade as _,
RawRwLockTimed as _,
RawRwLockRecursive as _,
RawRwLockRecursiveTimed as _,
RawRwLockUpgrade as _,
RawRwLockUpgradeFair as _,
RawRwLockUpgradeDowngrade as _,
RawRwLockUpgradeTimed as _
},
RawRwLock,
};
use core::cell::UnsafeCell;
use core::fmt;
use core::marker::PhantomData;
use core::mem;
use core::time::Duration;
use core::ops::{Deref, DerefMut};
use std::time::Instant;
#[cfg(feature = "owning_ref_support")]
use owning_ref::StableAddress;
#[cfg(feature = "serde_support")]
use serde::{Deserialize, Deserializer, Serialize, Serializer};
pub struct RwLock<T: ?Sized> {
raw: RawRwLock,
data: UnsafeCell<T>,
}
#[cfg(feature = "serde_support")]
impl<T> Serialize for RwLock<T>
where
T: Serialize + ?Sized,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
self.read().serialize(serializer)
}
}
#[cfg(feature = "serde_support")]
impl<'de, T> Deserialize<'de> for RwLock<T>
where
T: Deserialize<'de> + ?Sized,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
Deserialize::deserialize(deserializer).map(RwLock::new)
}
}
unsafe impl<T: ?Sized + Send> Send for RwLock<T> {}
unsafe impl<T: ?Sized + Send + Sync> Sync for RwLock<T> {}
impl<T> RwLock<T> {
#[inline]
pub const fn new(val: T) -> RwLock<T> {
RwLock {
data: UnsafeCell::new(val),
raw: RawRwLock::INIT,
}
}
#[inline]
#[allow(unused_unsafe)]
pub fn into_inner(self) -> T {
unsafe { self.data.into_inner() }
}
}
impl<T: ?Sized> RwLock<T> {
#[inline]
unsafe fn read_guard(&self) -> RwLockReadGuard<'_, T> {
RwLockReadGuard {
rwlock: self,
marker: PhantomData,
}
}
#[inline]
unsafe fn write_guard(&self) -> RwLockWriteGuard<'_, T> {
RwLockWriteGuard {
rwlock: self,
marker: PhantomData,
}
}
#[inline]
pub fn read(&self) -> RwLockReadGuard<'_, T> {
self.raw.lock_shared();
unsafe { self.read_guard() }
}
#[inline]
pub fn try_read(&self) -> Option<RwLockReadGuard<'_, T>> {
if self.raw.try_lock_shared() {
Some(unsafe { self.read_guard() })
} else {
None
}
}
#[inline]
pub fn write(&self) -> RwLockWriteGuard<'_, T> {
self.raw.lock_exclusive();
unsafe { self.write_guard() }
}
#[inline]
pub fn try_write(&self) -> Option<RwLockWriteGuard<'_, T>> {
if self.raw.try_lock_exclusive() {
Some(unsafe { self.write_guard() })
} else {
None
}
}
#[inline]
pub fn get_mut(&mut self) -> &mut T {
unsafe { &mut *self.data.get() }
}
#[inline]
pub unsafe fn force_unlock_read(&self) {
self.raw.unlock_shared();
}
#[inline]
pub unsafe fn force_unlock_write(&self) {
self.raw.unlock_exclusive();
}
pub unsafe fn raw(&self) -> &RawRwLock {
&self.raw
}
}
impl<T: ?Sized> RwLock<T> {
#[inline]
pub unsafe fn force_unlock_read_fair(&self) {
self.raw.unlock_shared_fair();
}
#[inline]
pub unsafe fn force_unlock_write_fair(&self) {
self.raw.unlock_exclusive_fair();
}
}
impl<T: ?Sized> RwLock<T> {
#[inline]
pub fn try_read_for(&self, timeout: Duration) -> Option<RwLockReadGuard<'_, T>> {
if self.raw.try_lock_shared_for(timeout) {
Some(unsafe { self.read_guard() })
} else {
None
}
}
#[inline]
pub fn try_read_until(&self, timeout: Instant) -> Option<RwLockReadGuard<'_, T>> {
if self.raw.try_lock_shared_until(timeout) {
Some(unsafe { self.read_guard() })
} else {
None
}
}
#[inline]
pub fn try_write_for(&self, timeout: Duration) -> Option<RwLockWriteGuard<'_, T>> {
if self.raw.try_lock_exclusive_for(timeout) {
Some(unsafe { self.write_guard() })
} else {
None
}
}
#[inline]
pub fn try_write_until(&self, timeout: Instant) -> Option<RwLockWriteGuard<'_, T>> {
if self.raw.try_lock_exclusive_until(timeout) {
Some(unsafe { self.write_guard() })
} else {
None
}
}
}
impl<T: ?Sized> RwLock<T> {
#[inline]
pub fn read_recursive(&self) -> RwLockReadGuard<'_, T> {
self.raw.lock_shared_recursive();
unsafe { self.read_guard() }
}
#[inline]
pub fn try_read_recursive(&self) -> Option<RwLockReadGuard<'_, T>> {
if self.raw.try_lock_shared_recursive() {
Some(unsafe { self.read_guard() })
} else {
None
}
}
}
impl<T: ?Sized> RwLock<T> {
#[inline]
pub fn try_read_recursive_for(
&self,
timeout: Duration,
) -> Option<RwLockReadGuard<'_, T>> {
if self.raw.try_lock_shared_recursive_for(timeout) {
Some(unsafe { self.read_guard() })
} else {
None
}
}
#[inline]
pub fn try_read_recursive_until(
&self,
timeout: Instant,
) -> Option<RwLockReadGuard<'_, T>> {
if self.raw.try_lock_shared_recursive_until(timeout) {
Some(unsafe { self.read_guard() })
} else {
None
}
}
}
impl<T: ?Sized> RwLock<T> {
#[inline]
unsafe fn upgradable_guard(&self) -> RwLockUpgradableReadGuard<'_, T> {
RwLockUpgradableReadGuard {
rwlock: self,
marker: PhantomData,
}
}
#[inline]
pub fn upgradable_read(&self) -> RwLockUpgradableReadGuard<'_, T> {
self.raw.lock_upgradable();
unsafe { self.upgradable_guard() }
}
#[inline]
pub fn try_upgradable_read(&self) -> Option<RwLockUpgradableReadGuard<'_, T>> {
if self.raw.try_lock_upgradable() {
Some(unsafe { self.upgradable_guard() })
} else {
None
}
}
}
impl<T: ?Sized> RwLock<T> {
#[inline]
pub fn try_upgradable_read_for(
&self,
timeout: Duration,
) -> Option<RwLockUpgradableReadGuard<'_, T>> {
if self.raw.try_lock_upgradable_for(timeout) {
Some(unsafe { self.upgradable_guard() })
} else {
None
}
}
#[inline]
pub fn try_upgradable_read_until(
&self,
timeout: Instant,
) -> Option<RwLockUpgradableReadGuard<'_, T>> {
if self.raw.try_lock_upgradable_until(timeout) {
Some(unsafe { self.upgradable_guard() })
} else {
None
}
}
}
impl<T: ?Sized + Default> Default for RwLock<T> {
#[inline]
fn default() -> RwLock<T> {
RwLock::new(Default::default())
}
}
impl<T> From<T> for RwLock<T> {
#[inline]
fn from(t: T) -> RwLock<T> {
RwLock::new(t)
}
}
impl<T: ?Sized + fmt::Debug> fmt::Debug for RwLock<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.try_read() {
Some(guard) => f.debug_struct("RwLock").field("data", &&*guard).finish(),
None => {
struct LockedPlaceholder;
impl fmt::Debug for LockedPlaceholder {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("<locked>")
}
}
f.debug_struct("RwLock")
.field("data", &LockedPlaceholder)
.finish()
}
}
}
}
#[must_use = "if unused the RwLock will immediately unlock"]
pub struct RwLockReadGuard<'a, T: ?Sized> {
rwlock: &'a RwLock<T>,
marker: PhantomData<(&'a T, *mut ())>,
}
unsafe impl<'a, T: ?Sized + Sync + 'a> Sync for RwLockReadGuard<'a, T> {}
impl<'a, T: ?Sized + 'a> RwLockReadGuard<'a, T> {
pub fn rwlock(s: &Self) -> &'a RwLock<T> {
s.rwlock
}
#[inline]
pub fn map<U: ?Sized, F>(s: Self, f: F) -> MappedRwLockReadGuard<'a, U>
where
F: FnOnce(&T) -> &U,
{
let raw = &s.rwlock.raw;
let data = f(unsafe { &*s.rwlock.data.get() });
mem::forget(s);
MappedRwLockReadGuard {
raw,
data,
marker: PhantomData,
}
}
#[inline]
pub fn try_map<U: ?Sized, F>(s: Self, f: F) -> Result<MappedRwLockReadGuard<'a, U>, Self>
where
F: FnOnce(&T) -> Option<&U>,
{
let raw = &s.rwlock.raw;
let data = match f(unsafe { &*s.rwlock.data.get() }) {
Some(data) => data,
None => return Err(s),
};
mem::forget(s);
Ok(MappedRwLockReadGuard {
raw,
data,
marker: PhantomData,
})
}
#[inline]
pub fn unlocked<F, U>(s: &mut Self, f: F) -> U
where
F: FnOnce() -> U,
{
s.rwlock.raw.unlock_shared();
defer!(s.rwlock.raw.lock_shared());
f()
}
}
impl<'a, T: ?Sized + 'a> RwLockReadGuard<'a, T> {
#[inline]
pub fn unlock_fair(s: Self) {
s.rwlock.raw.unlock_shared_fair();
mem::forget(s);
}
#[inline]
pub fn unlocked_fair<F, U>(s: &mut Self, f: F) -> U
where
F: FnOnce() -> U,
{
s.rwlock.raw.unlock_shared_fair();
defer!(s.rwlock.raw.lock_shared());
f()
}
#[inline]
pub fn bump(s: &mut Self) {
s.rwlock.raw.bump_shared();
}
}
impl<'a, T: ?Sized + 'a> Deref for RwLockReadGuard<'a, T> {
type Target = T;
#[inline]
fn deref(&self) -> &T {
unsafe { &*self.rwlock.data.get() }
}
}
impl<'a, T: ?Sized + 'a> Drop for RwLockReadGuard<'a, T> {
#[inline]
fn drop(&mut self) {
self.rwlock.raw.unlock_shared();
}
}
impl<'a, T: fmt::Debug + ?Sized + 'a> fmt::Debug for RwLockReadGuard<'a, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&**self, f)
}
}
impl<'a, T: fmt::Display + ?Sized + 'a> fmt::Display
for RwLockReadGuard<'a, T>
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
(**self).fmt(f)
}
}
#[cfg(feature = "owning_ref_support")]
unsafe impl<'a, T: ?Sized + 'a> StableAddress for RwLockReadGuard<'a, T> {}
#[must_use = "if unused the RwLock will immediately unlock"]
pub struct RwLockWriteGuard<'a, T: ?Sized> {
rwlock: &'a RwLock<T>,
marker: PhantomData<(&'a mut T, *mut ())>,
}
unsafe impl<'a, T: ?Sized + Sync + 'a> Sync for RwLockWriteGuard<'a, T> {}
impl<'a, T: ?Sized + 'a> RwLockWriteGuard<'a, T> {
pub fn rwlock(s: &Self) -> &'a RwLock<T> {
s.rwlock
}
#[inline]
pub fn map<U: ?Sized, F>(s: Self, f: F) -> MappedRwLockWriteGuard<'a, U>
where
F: FnOnce(&mut T) -> &mut U,
{
let raw = &s.rwlock.raw;
let data = f(unsafe { &mut *s.rwlock.data.get() });
mem::forget(s);
MappedRwLockWriteGuard {
raw,
data,
marker: PhantomData,
}
}
#[inline]
pub fn try_map<U: ?Sized, F>(s: Self, f: F) -> Result<MappedRwLockWriteGuard<'a, U>, Self>
where
F: FnOnce(&mut T) -> Option<&mut U>,
{
let raw = &s.rwlock.raw;
let data = match f(unsafe { &mut *s.rwlock.data.get() }) {
Some(data) => data,
None => return Err(s),
};
mem::forget(s);
Ok(MappedRwLockWriteGuard {
raw,
data,
marker: PhantomData,
})
}
#[inline]
pub fn unlocked<F, U>(s: &mut Self, f: F) -> U
where
F: FnOnce() -> U,
{
s.rwlock.raw.unlock_exclusive();
defer!(s.rwlock.raw.lock_exclusive());
f()
}
}
impl<'a, T: ?Sized + 'a> RwLockWriteGuard<'a, T> {
pub fn downgrade(s: Self) -> RwLockReadGuard<'a, T> {
s.rwlock.raw.downgrade();
let rwlock = s.rwlock;
mem::forget(s);
RwLockReadGuard {
rwlock,
marker: PhantomData,
}
}
}
impl<'a, T: ?Sized + 'a> RwLockWriteGuard<'a, T> {
pub fn downgrade_to_upgradable(s: Self) -> RwLockUpgradableReadGuard<'a, T> {
s.rwlock.raw.downgrade_to_upgradable();
let rwlock = s.rwlock;
mem::forget(s);
RwLockUpgradableReadGuard {
rwlock,
marker: PhantomData,
}
}
}
impl<'a, T: ?Sized + 'a> RwLockWriteGuard<'a, T> {
#[inline]
pub fn unlock_fair(s: Self) {
s.rwlock.raw.unlock_exclusive_fair();
mem::forget(s);
}
#[inline]
pub fn unlocked_fair<F, U>(s: &mut Self, f: F) -> U
where
F: FnOnce() -> U,
{
s.rwlock.raw.unlock_exclusive_fair();
defer!(s.rwlock.raw.lock_exclusive());
f()
}
#[inline]
pub fn bump(s: &mut Self) {
s.rwlock.raw.bump_exclusive();
}
}
impl<'a, T: ?Sized + 'a> Deref for RwLockWriteGuard<'a, T> {
type Target = T;
#[inline]
fn deref(&self) -> &T {
unsafe { &*self.rwlock.data.get() }
}
}
impl<'a, T: ?Sized + 'a> DerefMut for RwLockWriteGuard<'a, T> {
#[inline]
fn deref_mut(&mut self) -> &mut T {
unsafe { &mut *self.rwlock.data.get() }
}
}
impl<'a, T: ?Sized + 'a> Drop for RwLockWriteGuard<'a, T> {
#[inline]
fn drop(&mut self) {
self.rwlock.raw.unlock_exclusive();
}
}
impl<'a, T: fmt::Debug + ?Sized + 'a> fmt::Debug for RwLockWriteGuard<'a, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&**self, f)
}
}
impl<'a, T: fmt::Display + ?Sized + 'a> fmt::Display
for RwLockWriteGuard<'a, T>
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
(**self).fmt(f)
}
}
#[cfg(feature = "owning_ref_support")]
unsafe impl<'a, T: ?Sized + 'a> StableAddress for RwLockWriteGuard<'a, T> {}
#[must_use = "if unused the RwLock will immediately unlock"]
pub struct RwLockUpgradableReadGuard<'a, T: ?Sized> {
rwlock: &'a RwLock<T>,
marker: PhantomData<(&'a T, *mut ())>,
}
unsafe impl<'a, T: ?Sized + Sync + 'a> Sync
for RwLockUpgradableReadGuard<'a, T>
{
}
impl<'a, T: ?Sized + 'a> RwLockUpgradableReadGuard<'a, T> {
pub fn rwlock(s: &Self) -> &'a RwLock<T> {
s.rwlock
}
#[inline]
pub fn unlocked<F, U>(s: &mut Self, f: F) -> U
where
F: FnOnce() -> U,
{
s.rwlock.raw.unlock_upgradable();
defer!(s.rwlock.raw.lock_upgradable());
f()
}
pub fn upgrade(s: Self) -> RwLockWriteGuard<'a, T> {
s.rwlock.raw.upgrade();
let rwlock = s.rwlock;
mem::forget(s);
RwLockWriteGuard {
rwlock,
marker: PhantomData,
}
}
pub fn try_upgrade(s: Self) -> Result<RwLockWriteGuard<'a, T>, Self> {
if s.rwlock.raw.try_upgrade() {
let rwlock = s.rwlock;
mem::forget(s);
Ok(RwLockWriteGuard {
rwlock,
marker: PhantomData,
})
} else {
Err(s)
}
}
}
impl<'a, T: ?Sized + 'a> RwLockUpgradableReadGuard<'a, T> {
#[inline]
pub fn unlock_fair(s: Self) {
s.rwlock.raw.unlock_upgradable_fair();
mem::forget(s);
}
#[inline]
pub fn unlocked_fair<F, U>(s: &mut Self, f: F) -> U
where
F: FnOnce() -> U,
{
s.rwlock.raw.unlock_upgradable_fair();
defer!(s.rwlock.raw.lock_upgradable());
f()
}
#[inline]
pub fn bump(s: &mut Self) {
s.rwlock.raw.bump_upgradable();
}
}
impl<'a, T: ?Sized + 'a> RwLockUpgradableReadGuard<'a, T> {
pub fn downgrade(s: Self) -> RwLockReadGuard<'a, T> {
s.rwlock.raw.downgrade_upgradable();
let rwlock = s.rwlock;
mem::forget(s);
RwLockReadGuard {
rwlock,
marker: PhantomData,
}
}
}
impl<'a, T: ?Sized + 'a> RwLockUpgradableReadGuard<'a, T> {
pub fn try_upgrade_for(
s: Self,
timeout: Duration,
) -> Result<RwLockWriteGuard<'a, T>, Self> {
if s.rwlock.raw.try_upgrade_for(timeout) {
let rwlock = s.rwlock;
mem::forget(s);
Ok(RwLockWriteGuard {
rwlock,
marker: PhantomData,
})
} else {
Err(s)
}
}
#[inline]
pub fn try_upgrade_until(
s: Self,
timeout: Instant,
) -> Result<RwLockWriteGuard<'a, T>, Self> {
if s.rwlock.raw.try_upgrade_until(timeout) {
let rwlock = s.rwlock;
mem::forget(s);
Ok(RwLockWriteGuard {
rwlock,
marker: PhantomData,
})
} else {
Err(s)
}
}
}
impl<'a, T: ?Sized + 'a> Deref for RwLockUpgradableReadGuard<'a, T> {
type Target = T;
#[inline]
fn deref(&self) -> &T {
unsafe { &*self.rwlock.data.get() }
}
}
impl<'a, T: ?Sized + 'a> Drop for RwLockUpgradableReadGuard<'a, T> {
#[inline]
fn drop(&mut self) {
self.rwlock.raw.unlock_upgradable();
}
}
impl<'a, T: fmt::Debug + ?Sized + 'a> fmt::Debug
for RwLockUpgradableReadGuard<'a, T>
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&**self, f)
}
}
impl<'a, T: fmt::Display + ?Sized + 'a> fmt::Display
for RwLockUpgradableReadGuard<'a, T>
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
(**self).fmt(f)
}
}
#[cfg(feature = "owning_ref_support")]
unsafe impl<'a, T: ?Sized + 'a> StableAddress
for RwLockUpgradableReadGuard<'a, T>
{
}
#[must_use = "if unused the RwLock will immediately unlock"]
pub struct MappedRwLockReadGuard<'a, T: ?Sized> {
raw: &'a RawRwLock,
data: *const T,
marker: PhantomData<&'a T>,
}
unsafe impl<'a, T: ?Sized + Sync + 'a> Sync for MappedRwLockReadGuard<'a, T> {}
impl<'a, T: ?Sized + 'a> MappedRwLockReadGuard<'a, T> {
#[inline]
pub fn map<U: ?Sized, F>(s: Self, f: F) -> MappedRwLockReadGuard<'a, U>
where
F: FnOnce(&T) -> &U,
{
let raw = s.raw;
let data = f(unsafe { &*s.data });
mem::forget(s);
MappedRwLockReadGuard {
raw,
data,
marker: PhantomData,
}
}
#[inline]
pub fn try_map<U: ?Sized, F>(s: Self, f: F) -> Result<MappedRwLockReadGuard<'a, U>, Self>
where
F: FnOnce(&T) -> Option<&U>,
{
let raw = s.raw;
let data = match f(unsafe { &*s.data }) {
Some(data) => data,
None => return Err(s),
};
mem::forget(s);
Ok(MappedRwLockReadGuard {
raw,
data,
marker: PhantomData,
})
}
}
impl<'a, T: ?Sized + 'a> MappedRwLockReadGuard<'a, T> {
#[inline]
pub fn unlock_fair(s: Self) {
s.raw.unlock_shared_fair();
mem::forget(s);
}
}
impl<'a, T: ?Sized + 'a> Deref for MappedRwLockReadGuard<'a, T> {
type Target = T;
#[inline]
fn deref(&self) -> &T {
unsafe { &*self.data }
}
}
impl<'a, T: ?Sized + 'a> Drop for MappedRwLockReadGuard<'a, T> {
#[inline]
fn drop(&mut self) {
self.raw.unlock_shared();
}
}
impl<'a, T: fmt::Debug + ?Sized + 'a> fmt::Debug
for MappedRwLockReadGuard<'a, T>
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&**self, f)
}
}
impl<'a, T: fmt::Display + ?Sized + 'a> fmt::Display
for MappedRwLockReadGuard<'a, T>
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
(**self).fmt(f)
}
}
#[cfg(feature = "owning_ref_support")]
unsafe impl<'a, T: ?Sized + 'a> StableAddress
for MappedRwLockReadGuard<'a, T>
{
}
#[must_use = "if unused the RwLock will immediately unlock"]
pub struct MappedRwLockWriteGuard<'a, T: ?Sized> {
raw: &'a RawRwLock,
data: *mut T,
marker: PhantomData<&'a mut T>,
}
unsafe impl<'a, T: ?Sized + Sync + 'a> Sync
for MappedRwLockWriteGuard<'a, T>
{
}
impl<'a, T: ?Sized + 'a> MappedRwLockWriteGuard<'a, T> {
#[inline]
pub fn map<U: ?Sized, F>(s: Self, f: F) -> MappedRwLockWriteGuard<'a, U>
where
F: FnOnce(&mut T) -> &mut U,
{
let raw = s.raw;
let data = f(unsafe { &mut *s.data });
mem::forget(s);
MappedRwLockWriteGuard {
raw,
data,
marker: PhantomData,
}
}
#[inline]
pub fn try_map<U: ?Sized, F>(s: Self, f: F) -> Result<MappedRwLockWriteGuard<'a, U>, Self>
where
F: FnOnce(&mut T) -> Option<&mut U>,
{
let raw = s.raw;
let data = match f(unsafe { &mut *s.data }) {
Some(data) => data,
None => return Err(s),
};
mem::forget(s);
Ok(MappedRwLockWriteGuard {
raw,
data,
marker: PhantomData,
})
}
}
impl<'a, T: ?Sized + 'a> MappedRwLockWriteGuard<'a, T> {
pub fn downgrade(s: Self) -> MappedRwLockReadGuard<'a, T> {
s.raw.downgrade();
let raw = s.raw;
let data = s.data;
mem::forget(s);
MappedRwLockReadGuard {
raw,
data,
marker: PhantomData,
}
}
}
impl<'a, T: ?Sized + 'a> MappedRwLockWriteGuard<'a, T> {
#[inline]
pub fn unlock_fair(s: Self) {
s.raw.unlock_exclusive_fair();
mem::forget(s);
}
}
impl<'a, T: ?Sized + 'a> Deref for MappedRwLockWriteGuard<'a, T> {
type Target = T;
#[inline]
fn deref(&self) -> &T {
unsafe { &*self.data }
}
}
impl<'a, T: ?Sized + 'a> DerefMut for MappedRwLockWriteGuard<'a, T> {
#[inline]
fn deref_mut(&mut self) -> &mut T {
unsafe { &mut *self.data }
}
}
impl<'a, T: ?Sized + 'a> Drop for MappedRwLockWriteGuard<'a, T> {
#[inline]
fn drop(&mut self) {
self.raw.unlock_exclusive();
}
}
impl<'a, T: fmt::Debug + ?Sized + 'a> fmt::Debug
for MappedRwLockWriteGuard<'a, T>
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&**self, f)
}
}
impl<'a, T: fmt::Display + ?Sized + 'a> fmt::Display
for MappedRwLockWriteGuard<'a, T>
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
(**self).fmt(f)
}
}
#[cfg(feature = "owning_ref_support")]
unsafe impl<'a, T: ?Sized + 'a> StableAddress
for MappedRwLockWriteGuard<'a, T>
{
}
#[cfg(test)]
mod tests {
use crate::{RwLock, RwLockUpgradableReadGuard, RwLockWriteGuard};
use rand::Rng;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::mpsc::channel;
use std::sync::Arc;
use std::thread;
use std::time::Duration;
#[cfg(feature = "serde_support")]
use bincode::{deserialize, serialize};
#[derive(Eq, PartialEq, Debug)]
struct NonCopy(i32);
#[test]
fn smoke() {
let l = RwLock::new(());
drop(l.read());
drop(l.write());
drop(l.upgradable_read());
drop((l.read(), l.read()));
drop((l.read(), l.upgradable_read()));
drop(l.write());
}
#[test]
fn frob() {
const N: u32 = 10;
const M: u32 = 1000;
let r = Arc::new(RwLock::new(()));
let (tx, rx) = channel::<()>();
for _ in 0..N {
let tx = tx.clone();
let r = r.clone();
thread::spawn(move || {
let mut rng = rand::thread_rng();
for _ in 0..M {
if rng.gen_bool(1.0 / N as f64) {
drop(r.write());
} else {
drop(r.read());
}
}
drop(tx);
});
}
drop(tx);
let _ = rx.recv();
}
#[test]
fn test_rw_arc_no_poison_wr() {
let arc = Arc::new(RwLock::new(1));
let arc2 = arc.clone();
let _: Result<(), _> = thread::spawn(move || {
let _lock = arc2.write();
panic!();
})
.join();
let lock = arc.read();
assert_eq!(*lock, 1);
}
#[test]
fn test_rw_arc_no_poison_ww() {
let arc = Arc::new(RwLock::new(1));
let arc2 = arc.clone();
let _: Result<(), _> = thread::spawn(move || {
let _lock = arc2.write();
panic!();
})
.join();
let lock = arc.write();
assert_eq!(*lock, 1);
}
#[test]
fn test_rw_arc_no_poison_rr() {
let arc = Arc::new(RwLock::new(1));
let arc2 = arc.clone();
let _: Result<(), _> = thread::spawn(move || {
let _lock = arc2.read();
panic!();
})
.join();
let lock = arc.read();
assert_eq!(*lock, 1);
}
#[test]
fn test_rw_arc_no_poison_rw() {
let arc = Arc::new(RwLock::new(1));
let arc2 = arc.clone();
let _: Result<(), _> = thread::spawn(move || {
let _lock = arc2.read();
panic!()
})
.join();
let lock = arc.write();
assert_eq!(*lock, 1);
}
#[test]
fn test_ruw_arc() {
let arc = Arc::new(RwLock::new(0));
let arc2 = arc.clone();
let (tx, rx) = channel();
thread::spawn(move || {
for _ in 0..10 {
let mut lock = arc2.write();
let tmp = *lock;
*lock = -1;
thread::yield_now();
*lock = tmp + 1;
}
tx.send(()).unwrap();
});
let mut children = Vec::new();
for _ in 0..5 {
let arc3 = arc.clone();
children.push(thread::spawn(move || {
let lock = arc3.upgradable_read();
let tmp = *lock;
assert!(tmp >= 0);
thread::yield_now();
let mut lock = RwLockUpgradableReadGuard::upgrade(lock);
assert_eq!(tmp, *lock);
*lock = -1;
thread::yield_now();
*lock = tmp + 1;
}));
}
for _ in 0..5 {
let arc4 = arc.clone();
children.push(thread::spawn(move || {
let lock = arc4.read();
assert!(*lock >= 0);
}));
}
for r in children {
assert!(r.join().is_ok());
}
rx.recv().unwrap();
let lock = arc.read();
assert_eq!(*lock, 15);
}
#[test]
fn test_rw_arc() {
let arc = Arc::new(RwLock::new(0));
let arc2 = arc.clone();
let (tx, rx) = channel();
thread::spawn(move || {
let mut lock = arc2.write();
for _ in 0..10 {
let tmp = *lock;
*lock = -1;
thread::yield_now();
*lock = tmp + 1;
}
tx.send(()).unwrap();
});
let mut children = Vec::new();
for _ in 0..5 {
let arc3 = arc.clone();
children.push(thread::spawn(move || {
let lock = arc3.read();
assert!(*lock >= 0);
}));
}
for r in children {
assert!(r.join().is_ok());
}
rx.recv().unwrap();
let lock = arc.read();
assert_eq!(*lock, 10);
}
#[test]
fn test_rw_arc_access_in_unwind() {
let arc = Arc::new(RwLock::new(1));
let arc2 = arc.clone();
let _ = thread::spawn(move || {
struct Unwinder {
i: Arc<RwLock<isize>>,
}
impl Drop for Unwinder {
fn drop(&mut self) {
let mut lock = self.i.write();
*lock += 1;
}
}
let _u = Unwinder { i: arc2 };
panic!();
})
.join();
let lock = arc.read();
assert_eq!(*lock, 2);
}
#[test]
fn test_rwlock_unsized() {
let rw: &RwLock<[i32]> = &RwLock::new([1, 2, 3]);
{
let b = &mut *rw.write();
b[0] = 4;
b[2] = 5;
}
let comp: &[i32] = &[4, 2, 5];
assert_eq!(&*rw.read(), comp);
}
#[test]
fn test_rwlock_try_read() {
let lock = RwLock::new(0isize);
{
let read_guard = lock.read();
let read_result = lock.try_read();
assert!(
read_result.is_some(),
"try_read should succeed while read_guard is in scope"
);
drop(read_guard);
}
{
let upgrade_guard = lock.upgradable_read();
let read_result = lock.try_read();
assert!(
read_result.is_some(),
"try_read should succeed while upgrade_guard is in scope"
);
drop(upgrade_guard);
}
{
let write_guard = lock.write();
let read_result = lock.try_read();
assert!(
read_result.is_none(),
"try_read should fail while write_guard is in scope"
);
drop(write_guard);
}
}
#[test]
fn test_rwlock_try_write() {
let lock = RwLock::new(0isize);
{
let read_guard = lock.read();
let write_result = lock.try_write();
assert!(
write_result.is_none(),
"try_write should fail while read_guard is in scope"
);
drop(read_guard);
}
{
let upgrade_guard = lock.upgradable_read();
let write_result = lock.try_write();
assert!(
write_result.is_none(),
"try_write should fail while upgrade_guard is in scope"
);
drop(upgrade_guard);
}
{
let write_guard = lock.write();
let write_result = lock.try_write();
assert!(
write_result.is_none(),
"try_write should fail while write_guard is in scope"
);
drop(write_guard);
}
}
#[test]
fn test_rwlock_try_upgrade() {
let lock = RwLock::new(0isize);
{
let read_guard = lock.read();
let upgrade_result = lock.try_upgradable_read();
assert!(
upgrade_result.is_some(),
"try_upgradable_read should succeed while read_guard is in scope"
);
drop(read_guard);
}
{
let upgrade_guard = lock.upgradable_read();
let upgrade_result = lock.try_upgradable_read();
assert!(
upgrade_result.is_none(),
"try_upgradable_read should fail while upgrade_guard is in scope"
);
drop(upgrade_guard);
}
{
let write_guard = lock.write();
let upgrade_result = lock.try_upgradable_read();
assert!(
upgrade_result.is_none(),
"try_upgradable should fail while write_guard is in scope"
);
drop(write_guard);
}
}
#[test]
fn test_into_inner() {
let m = RwLock::new(NonCopy(10));
assert_eq!(m.into_inner(), NonCopy(10));
}
#[test]
fn test_into_inner_drop() {
struct Foo(Arc<AtomicUsize>);
impl Drop for Foo {
fn drop(&mut self) {
self.0.fetch_add(1, Ordering::SeqCst);
}
}
let num_drops = Arc::new(AtomicUsize::new(0));
let m = RwLock::new(Foo(num_drops.clone()));
assert_eq!(num_drops.load(Ordering::SeqCst), 0);
{
let _inner = m.into_inner();
assert_eq!(num_drops.load(Ordering::SeqCst), 0);
}
assert_eq!(num_drops.load(Ordering::SeqCst), 1);
}
#[test]
fn test_get_mut() {
let mut m = RwLock::new(NonCopy(10));
*m.get_mut() = NonCopy(20);
assert_eq!(m.into_inner(), NonCopy(20));
}
#[test]
fn test_rwlockguard_sync() {
fn sync<T: Sync>(_: T) {}
let rwlock = RwLock::new(());
sync(rwlock.read());
sync(rwlock.write());
}
#[test]
fn test_rwlock_downgrade() {
let x = Arc::new(RwLock::new(0));
let mut handles = Vec::new();
for _ in 0..8 {
let x = x.clone();
handles.push(thread::spawn(move || {
for _ in 0..100 {
let mut writer = x.write();
*writer += 1;
let cur_val = *writer;
let reader = RwLockWriteGuard::downgrade(writer);
assert_eq!(cur_val, *reader);
}
}));
}
for handle in handles {
handle.join().unwrap()
}
assert_eq!(*x.read(), 800);
}
#[test]
fn test_rwlock_recursive() {
let arc = Arc::new(RwLock::new(1));
let arc2 = arc.clone();
let _lock1 = arc.read();
thread::spawn(move || {
let _lock = arc2.write();
});
if cfg!(not(all(target_env = "sgx", target_vendor = "fortanix"))) {
thread::sleep(Duration::from_millis(100));
} else {
for _ in 0..100 {
thread::yield_now();
}
}
let _lock2 = arc.read_recursive();
}
#[test]
fn test_rwlock_debug() {
let x = RwLock::new(vec![0u8, 10]);
assert_eq!(format!("{:?}", x), "RwLock { data: [0, 10] }");
let _lock = x.write();
assert_eq!(format!("{:?}", x), "RwLock { data: <locked> }");
}
#[test]
fn test_clone() {
let rwlock = RwLock::new(Arc::new(1));
let a = rwlock.read_recursive();
let b = a.clone();
assert_eq!(Arc::strong_count(&b), 2);
}
#[cfg(feature = "serde_support")]
#[test]
fn test_serde() {
let contents: Vec<u8> = vec![0, 1, 2];
let mutex = RwLock::new(contents.clone());
let serialized = serialize(&mutex).unwrap();
let deserialized: RwLock<Vec<u8>> = deserialize(&serialized).unwrap();
assert_eq!(*(mutex.read()), *(deserialized.read()));
assert_eq!(contents, *(deserialized.read()));
}
}