提交 30b883b9 编写于 作者: J John Gallagher

Make RwLock::try_write try to obtain a write lock

上级 7bd71637
......@@ -232,7 +232,7 @@ pub fn write(&self) -> LockResult<RwLockWriteGuard<T>> {
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn try_write(&self) -> TryLockResult<RwLockWriteGuard<T>> {
if unsafe { self.inner.lock.try_read() } {
if unsafe { self.inner.lock.try_write() } {
Ok(try!(RwLockWriteGuard::new(&*self.inner, &self.data)))
} else {
Err(TryLockError::WouldBlock)
......@@ -413,7 +413,7 @@ mod tests {
use rand::{self, Rng};
use sync::mpsc::channel;
use thread;
use sync::{Arc, RwLock, StaticRwLock, RW_LOCK_INIT};
use sync::{Arc, RwLock, StaticRwLock, TryLockError, RW_LOCK_INIT};
#[test]
fn smoke() {
......@@ -577,4 +577,21 @@ fn test_rwlock_unsized() {
let comp: &[i32] = &[4, 2, 5];
assert_eq!(&*rw.read().unwrap(), comp);
}
#[test]
fn test_rwlock_try_write() {
use mem::drop;
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);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册