@@ -230,7 +230,7 @@ impl<T> RwLock<T> {
230
230
#[ inline]
231
231
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
232
232
pub fn try_write ( & self ) -> TryLockResult < RwLockWriteGuard < T > > {
233
- if unsafe { self . inner . lock . try_read ( ) } {
233
+ if unsafe { self . inner . lock . try_write ( ) } {
234
234
Ok ( try!( RwLockWriteGuard :: new ( & * self . inner , & self . data ) ) )
235
235
} else {
236
236
Err ( TryLockError :: WouldBlock )
@@ -413,7 +413,7 @@ mod tests {
413
413
use rand:: { self , Rng } ;
414
414
use sync:: mpsc:: channel;
415
415
use thread;
416
- use sync:: { Arc , RwLock , StaticRwLock , RW_LOCK_INIT } ;
416
+ use sync:: { Arc , RwLock , StaticRwLock , TryLockError , RW_LOCK_INIT } ;
417
417
418
418
#[ test]
419
419
fn smoke ( ) {
@@ -565,4 +565,21 @@ mod tests {
565
565
let lock = arc. read ( ) . unwrap ( ) ;
566
566
assert_eq ! ( * lock, 2 ) ;
567
567
}
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
+ }
568
585
}
0 commit comments