use std::cell::UnsafeCell;
use std::{fmt, marker, mem};
use std::ops::{Deref, DerefMut};
use std::ptr;
use poison::{self, LockResult, TryLockError, TryLockResult};
use cross as sys;
pub struct RwLock<T: ?Sized> {
inner: Box<StaticRwLock>,
data: UnsafeCell<T>,
}
unsafe impl<T: ?Sized + Send + Sync> Send for RwLock<T> {}
unsafe impl<T: ?Sized + Send + Sync> Sync for RwLock<T> {}
struct StaticRwLock {
lock: sys::RWLock,
poison: poison::Flag,
}
#[must_use]
pub struct RwLockReadGuard<'a, T: ?Sized + 'a> {
__lock: &'a StaticRwLock,
__data: &'a T,
__marker: marker::PhantomData<*mut ()>
}
#[must_use]
pub struct RwLockWriteGuard<'a, T: ?Sized + 'a> {
__lock: &'a StaticRwLock,
__data: &'a UnsafeCell<T>,
__poison: poison::Guard,
__marker: marker::PhantomData<*mut ()>
}
impl<T> RwLock<T> {
pub fn new(t: T) -> RwLock<T> {
RwLock { inner: Box::new(StaticRwLock::new()), data: UnsafeCell::new(t) }
}
}
impl<T: ?Sized> RwLock<T> {
#[inline]
pub fn read(&self) -> LockResult<RwLockReadGuard<T>> {
unsafe { self.inner.lock.read() }
RwLockReadGuard::new(&*self.inner, &self.data)
}
#[inline]
pub fn try_read(&self) -> TryLockResult<RwLockReadGuard<T>> {
if unsafe { self.inner.lock.try_read() } {
Ok(try!(RwLockReadGuard::new(&*self.inner, &self.data)))
} else {
Err(TryLockError::WouldBlock)
}
}
#[inline]
pub fn write(&self) -> LockResult<RwLockWriteGuard<T>> {
unsafe { self.inner.lock.write() }
RwLockWriteGuard::new(&*self.inner, &self.data)
}
#[inline]
pub fn try_write(&self) -> TryLockResult<RwLockWriteGuard<T>> {
if unsafe { self.inner.lock.try_write() } {
Ok(try!(RwLockWriteGuard::new(&*self.inner, &self.data)))
} else {
Err(TryLockError::WouldBlock)
}
}
#[inline]
pub fn is_poisoned(&self) -> bool {
self.inner.poison.get()
}
pub fn into_inner(self) -> LockResult<T> where T: Sized {
unsafe {
let (inner, data) = {
let RwLock { ref inner, ref data } = self;
(ptr::read(inner), ptr::read(data))
};
mem::forget(self);
inner.lock.destroy();
poison::map_result(inner.poison.borrow(), |_| data.into_inner())
}
}
pub fn get_mut(&mut self) -> LockResult<&mut T> {
let data = unsafe { &mut *self.data.get() };
poison::map_result(self.inner.poison.borrow(), |_| data )
}
}
impl<T: ?Sized> Drop for RwLock<T> {
fn drop(&mut self) {
unsafe { self.inner.lock.destroy() }
}
}
impl<T: ?Sized + fmt::Debug> fmt::Debug for RwLock<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.try_read() {
Ok(guard) => write!(f, "RwLock {{ data: {:?} }}", &*guard),
Err(TryLockError::Poisoned(err)) => {
write!(f, "RwLock {{ data: Poisoned({:?}) }}", &**err.get_ref())
},
Err(TryLockError::WouldBlock) => write!(f, "RwLock {{ <locked> }}")
}
}
}
impl StaticRwLock {
pub fn new() -> StaticRwLock {
StaticRwLock {
lock: sys::RWLock::new(),
poison: poison::Flag::new(),
}
}
}
impl<'rwlock, T: ?Sized> RwLockReadGuard<'rwlock, T> {
fn new(lock: &'rwlock StaticRwLock, data: &'rwlock UnsafeCell<T>)
-> LockResult<RwLockReadGuard<'rwlock, T>> {
poison::map_result(lock.poison.borrow(), |_| {
RwLockReadGuard {
__lock: lock,
__data: unsafe { &*data.get() },
__marker: marker::PhantomData
}
})
}
pub fn map<U: ?Sized, F>(this: Self, cb: F) -> RwLockReadGuard<'rwlock, U>
where F: FnOnce(&'rwlock T) -> &'rwlock U
{
let new = RwLockReadGuard {
__lock: this.__lock,
__data: cb(this.__data),
__marker: marker::PhantomData
};
mem::forget(this);
new
}
pub fn filter_map<U: ?Sized, E, F>(this: Self, cb: F) -> Result<RwLockReadGuard<'rwlock, U>, (Self, E)>
where F: FnOnce(&'rwlock T) -> Result<&'rwlock U, E>
{
match cb(this.__data) {
Ok(new_data) => {
let new = RwLockReadGuard {
__lock: this.__lock,
__data: new_data,
__marker: marker::PhantomData
};
mem::forget(this);
Ok(new)
},
Err(e) => Err((this, e))
}
}
}
impl<'rwlock, T: ?Sized> RwLockWriteGuard<'rwlock, T> {
fn new(lock: &'rwlock StaticRwLock, data: &'rwlock UnsafeCell<T>)
-> LockResult<RwLockWriteGuard<'rwlock, T>> {
poison::map_result(lock.poison.borrow(), |guard| {
RwLockWriteGuard {
__lock: lock,
__data: data,
__poison: guard,
__marker: marker::PhantomData
}
})
}
pub fn map<U: ?Sized, F>(this: Self, cb: F) -> RwLockWriteGuard<'rwlock, U>
where F: FnOnce(&'rwlock mut T) -> &'rwlock mut U
{
let data = unsafe { ptr::read(&this.__data) };
let new_data = cb(unsafe { &mut *data.get() });
let (poison, lock) = unsafe {
(ptr::read(&this.__poison), ptr::read(&this.__lock))
};
mem::forget(this);
RwLockWriteGuard {
__lock: lock,
__data: unsafe { mem::transmute::<&mut U, &UnsafeCell<U>>(new_data) },
__poison: poison,
__marker: marker::PhantomData
}
}
pub fn filter_map<U: ?Sized, E, F>(this: Self, cb: F) -> Result<RwLockWriteGuard<'rwlock, U>, (Self, E)>
where F: FnOnce(&'rwlock mut T) -> Result<&'rwlock mut U, E>
{
let data = unsafe { ptr::read(&this.__data) };
match cb(unsafe { &mut *data.get() }) {
Ok(new_data) => {
let (poison, lock) = unsafe {
(ptr::read(&this.__poison), ptr::read(&this.__lock))
};
mem::forget(this);
Ok(RwLockWriteGuard {
__lock: lock,
__data: unsafe { mem::transmute::<&mut U, &UnsafeCell<U>>(new_data) },
__poison: poison,
__marker: marker::PhantomData
})
},
Err(e) => Err((this, e))
}
}
}
impl<'rwlock, T: ?Sized> Deref for RwLockReadGuard<'rwlock, T> {
type Target = T;
fn deref(&self) -> &T { self.__data }
}
impl<'rwlock, T: ?Sized> Deref for RwLockWriteGuard<'rwlock, T> {
type Target = T;
fn deref(&self) -> &T { unsafe { &*self.__data.get() } }
}
impl<'rwlock, T: ?Sized> DerefMut for RwLockWriteGuard<'rwlock, T> {
fn deref_mut(&mut self) -> &mut T {
unsafe { &mut *self.__data.get() }
}
}
impl<'a, T: ?Sized> Drop for RwLockReadGuard<'a, T> {
fn drop(&mut self) {
unsafe { self.__lock.lock.read_unlock(); }
}
}
impl<'a, T: ?Sized> Drop for RwLockWriteGuard<'a, T> {
fn drop(&mut self) {
self.__lock.poison.done(&self.__poison);
unsafe { self.__lock.lock.write_unlock(); }
}
}
#[cfg(test)]
mod tests {
use rand::{self, Rng};
use std::sync::mpsc::channel;
use std::thread;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use poison::TryLockError;
use super::RwLock;
#[derive(Eq, PartialEq, Debug)]
struct NonCopy(i32);
#[test]
fn smoke() {
let l = RwLock::new(());
drop(l.read().unwrap());
drop(l.write().unwrap());
drop((l.read().unwrap(), l.read().unwrap()));
drop(l.write().unwrap());
}
#[test]
fn frob() {
const N: usize = 10;
const M: usize = 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_weighted_bool(N as u32) {
drop(r.write().unwrap());
} else {
drop(r.read().unwrap());
}
}
drop(tx);
});
}
drop(tx);
let _ = rx.recv();
}
#[test]
fn test_rw_arc_poison_wr() {
let arc = Arc::new(RwLock::new(1));
let arc2 = arc.clone();
let _: Result<(), _> = thread::spawn(move|| {
let _lock = arc2.write().unwrap();
panic!();
}).join();
assert!(arc.read().is_err());
}
#[test]
fn test_rw_arc_poison_ww() {
let arc = Arc::new(RwLock::new(1));
assert!(!arc.is_poisoned());
let arc2 = arc.clone();
let _: Result<(), _> = thread::spawn(move|| {
let _lock = arc2.write().unwrap();
panic!();
}).join();
assert!(arc.write().is_err());
assert!(arc.is_poisoned());
}
#[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().unwrap();
panic!();
}).join();
let lock = arc.read().unwrap();
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().unwrap();
panic!()
}).join();
let lock = arc.write().unwrap();
assert_eq!(*lock, 1);
}
#[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().unwrap();
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().unwrap();
assert!(*lock >= 0);
}));
}
for r in children {
assert!(r.join().is_ok());
}
rx.recv().unwrap();
let lock = arc.read().unwrap();
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().unwrap();
*lock += 1;
}
}
let _u = Unwinder { i: arc2 };
panic!();
}).join();
let lock = arc.read().unwrap();
assert_eq!(*lock, 2);
}
#[test]
fn test_rwlock_unsized() {
let rw: &RwLock<[i32]> = &RwLock::new([1, 2, 3]);
{
let b = &mut *rw.write().unwrap();
b[0] = 4;
b[2] = 5;
}
let comp: &[i32] = &[4, 2, 5];
assert_eq!(&*rw.read().unwrap(), comp);
}
#[test]
fn test_rwlock_try_write() {
let lock = RwLock::new(0isize);
let read_guard = lock.read().unwrap();
let write_result = lock.try_write();
match write_result {
Err(TryLockError::WouldBlock) => (),
Ok(_) => assert!(false, "try_write should not succeed while read_guard is in scope"),
Err(_) => assert!(false, "unexpected error"),
}
drop(read_guard);
}
#[test]
fn test_into_inner() {
let m = RwLock::new(NonCopy(10));
assert_eq!(m.into_inner().unwrap(), 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().unwrap();
assert_eq!(num_drops.load(Ordering::SeqCst), 0);
}
assert_eq!(num_drops.load(Ordering::SeqCst), 1);
}
#[test]
fn test_into_inner_poison() {
let m = Arc::new(RwLock::new(NonCopy(10)));
let m2 = m.clone();
let _ = thread::spawn(move || {
let _lock = m2.write().unwrap();
panic!("test panic in inner thread to poison RwLock");
}).join();
assert!(m.is_poisoned());
match Arc::try_unwrap(m).unwrap().into_inner() {
Err(e) => assert_eq!(e.into_inner(), NonCopy(10)),
Ok(x) => panic!("into_inner of poisoned RwLock is Ok: {:?}", x),
}
}
#[test]
fn test_get_mut() {
let mut m = RwLock::new(NonCopy(10));
*m.get_mut().unwrap() = NonCopy(20);
assert_eq!(m.into_inner().unwrap(), NonCopy(20));
}
#[test]
fn test_get_mut_poison() {
let m = Arc::new(RwLock::new(NonCopy(10)));
let m2 = m.clone();
let _ = thread::spawn(move || {
let _lock = m2.write().unwrap();
panic!("test panic in inner thread to poison RwLock");
}).join();
assert!(m.is_poisoned());
match Arc::try_unwrap(m).unwrap().get_mut() {
Err(e) => assert_eq!(*e.into_inner(), NonCopy(10)),
Ok(x) => panic!("get_mut of poisoned RwLock is Ok: {:?}", x),
}
}
}