Skip to content

Commit c7c9f7d

Browse files
hydhknnalexcrichton
authored andcommitted
Make RwLock::try_write try to obtain a write lock
Conflicts: src/libstd/sync/rwlock.rs
1 parent 6a6c44e commit c7c9f7d

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

src/libstd/sync/rwlock.rs

+19-2
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ impl<T> RwLock<T> {
230230
#[inline]
231231
#[stable(feature = "rust1", since = "1.0.0")]
232232
pub fn try_write(&self) -> TryLockResult<RwLockWriteGuard<T>> {
233-
if unsafe { self.inner.lock.try_read() } {
233+
if unsafe { self.inner.lock.try_write() } {
234234
Ok(try!(RwLockWriteGuard::new(&*self.inner, &self.data)))
235235
} else {
236236
Err(TryLockError::WouldBlock)
@@ -413,7 +413,7 @@ mod tests {
413413
use rand::{self, Rng};
414414
use sync::mpsc::channel;
415415
use thread;
416-
use sync::{Arc, RwLock, StaticRwLock, RW_LOCK_INIT};
416+
use sync::{Arc, RwLock, StaticRwLock, TryLockError, RW_LOCK_INIT};
417417

418418
#[test]
419419
fn smoke() {
@@ -565,4 +565,21 @@ mod tests {
565565
let lock = arc.read().unwrap();
566566
assert_eq!(*lock, 2);
567567
}
568+
569+
#[test]
570+
fn test_rwlock_try_write() {
571+
use mem::drop;
572+
573+
let lock = RwLock::new(0isize);
574+
let read_guard = lock.read().unwrap();
575+
576+
let write_result = lock.try_write();
577+
match write_result {
578+
Err(TryLockError::WouldBlock) => (),
579+
Ok(_) => assert!(false, "try_write should not succeed while read_guard is in scope"),
580+
Err(_) => assert!(false, "unexpected error"),
581+
}
582+
583+
drop(read_guard);
584+
}
568585
}

0 commit comments

Comments
 (0)