1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
15 use ops
::{Deref, DerefMut}
;
17 use sys_common
::poison
::{self, LockResult, TryLockError, TryLockResult}
;
18 use sys_common
::rwlock
as sys
;
20 /// A reader-writer lock
22 /// This type of lock allows a number of readers or at most one writer at any
23 /// point in time. The write portion of this lock typically allows modification
24 /// of the underlying data (exclusive access) and the read portion of this lock
25 /// typically allows for read-only access (shared access).
27 /// The priority policy of the lock is dependent on the underlying operating
28 /// system's implementation, and this type does not guarantee that any
29 /// particular policy will be used.
31 /// The type parameter `T` represents the data that this lock protects. It is
32 /// required that `T` satisfies `Send` to be shared across threads and `Sync` to
33 /// allow concurrent access through readers. The RAII guards returned from the
34 /// locking methods implement `Deref` (and `DerefMut` for the `write` methods)
35 /// to allow access to the contained of the lock.
39 /// An `RwLock`, like `Mutex`, will become poisoned on a panic. Note, however,
40 /// that an `RwLock` may only be poisoned if a panic occurs while it is locked
41 /// exclusively (write mode). If a panic occurs in any reader, then the lock
42 /// will not be poisoned.
47 /// use std::sync::RwLock;
49 /// let lock = RwLock::new(5);
51 /// // many reader locks can be held at once
53 /// let r1 = lock.read().unwrap();
54 /// let r2 = lock.read().unwrap();
55 /// assert_eq!(*r1, 5);
56 /// assert_eq!(*r2, 5);
57 /// } // read locks are dropped at this point
59 /// // only one write lock may be held, however
61 /// let mut w = lock.write().unwrap();
63 /// assert_eq!(*w, 6);
64 /// } // write lock is dropped here
66 #[stable(feature = "rust1", since = "1.0.0")]
67 pub struct RwLock
<T
: ?Sized
> {
68 inner
: Box
<sys
::RWLock
>,
73 #[stable(feature = "rust1", since = "1.0.0")]
74 unsafe impl<T
: ?Sized
+ Send
+ Sync
> Send
for RwLock
<T
> {}
75 #[stable(feature = "rust1", since = "1.0.0")]
76 unsafe impl<T
: ?Sized
+ Send
+ Sync
> Sync
for RwLock
<T
> {}
78 /// RAII structure used to release the shared read access of a lock when
81 /// This structure is created by the [`read()`] and [`try_read()`] methods on
84 /// [`read()`]: struct.RwLock.html#method.read
85 /// [`try_read()`]: struct.RwLock.html#method.try_read
86 /// [`RwLock`]: struct.RwLock.html
88 #[stable(feature = "rust1", since = "1.0.0")]
89 pub struct RwLockReadGuard
<'a
, T
: ?Sized
+ 'a
> {
90 __lock
: &'a RwLock
<T
>,
93 #[stable(feature = "rust1", since = "1.0.0")]
94 impl<'a
, T
: ?Sized
> !marker
::Send
for RwLockReadGuard
<'a
, T
> {}
96 /// RAII structure used to release the exclusive write access of a lock when
99 /// This structure is created by the [`write()`] and [`try_write()`] methods
102 /// [`write()`]: struct.RwLock.html#method.write
103 /// [`try_write()`]: struct.RwLock.html#method.try_write
104 /// [`RwLock`]: struct.RwLock.html
106 #[stable(feature = "rust1", since = "1.0.0")]
107 pub struct RwLockWriteGuard
<'a
, T
: ?Sized
+ 'a
> {
108 __lock
: &'a RwLock
<T
>,
109 __poison
: poison
::Guard
,
112 #[stable(feature = "rust1", since = "1.0.0")]
113 impl<'a
, T
: ?Sized
> !marker
::Send
for RwLockWriteGuard
<'a
, T
> {}
116 /// Creates a new instance of an `RwLock<T>` which is unlocked.
121 /// use std::sync::RwLock;
123 /// let lock = RwLock::new(5);
125 #[stable(feature = "rust1", since = "1.0.0")]
126 pub fn new(t
: T
) -> RwLock
<T
> {
128 inner
: box sys
::RWLock
::new(),
129 poison
: poison
::Flag
::new(),
130 data
: UnsafeCell
::new(t
),
135 impl<T
: ?Sized
> RwLock
<T
> {
136 /// Locks this rwlock with shared read access, blocking the current thread
137 /// until it can be acquired.
139 /// The calling thread will be blocked until there are no more writers which
140 /// hold the lock. There may be other readers currently inside the lock when
141 /// this method returns. This method does not provide any guarantees with
142 /// respect to the ordering of whether contentious readers or writers will
143 /// acquire the lock first.
145 /// Returns an RAII guard which will release this thread's shared access
146 /// once it is dropped.
150 /// This function will return an error if the RwLock is poisoned. An RwLock
151 /// is poisoned whenever a writer panics while holding an exclusive lock.
152 /// The failure will occur immediately after the lock has been acquired.
156 /// This function might panic when called if the lock is already held by the current thread.
158 #[stable(feature = "rust1", since = "1.0.0")]
159 pub fn read(&self) -> LockResult
<RwLockReadGuard
<T
>> {
162 RwLockReadGuard
::new(self)
166 /// Attempts to acquire this rwlock with shared read access.
168 /// If the access could not be granted at this time, then `Err` is returned.
169 /// Otherwise, an RAII guard is returned which will release the shared access
170 /// when it is dropped.
172 /// This function does not block.
174 /// This function does not provide any guarantees with respect to the ordering
175 /// of whether contentious readers or writers will acquire the lock first.
179 /// This function will return an error if the RwLock is poisoned. An RwLock
180 /// is poisoned whenever a writer panics while holding an exclusive lock. An
181 /// error will only be returned if the lock would have otherwise been
184 #[stable(feature = "rust1", since = "1.0.0")]
185 pub fn try_read(&self) -> TryLockResult
<RwLockReadGuard
<T
>> {
187 if self.inner
.try_read() {
188 Ok(RwLockReadGuard
::new(self)?
)
190 Err(TryLockError
::WouldBlock
)
195 /// Locks this rwlock with exclusive write access, blocking the current
196 /// thread until it can be acquired.
198 /// This function will not return while other writers or other readers
199 /// currently have access to the lock.
201 /// Returns an RAII guard which will drop the write access of this rwlock
206 /// This function will return an error if the RwLock is poisoned. An RwLock
207 /// is poisoned whenever a writer panics while holding an exclusive lock.
208 /// An error will be returned when the lock is acquired.
212 /// This function might panic when called if the lock is already held by the current thread.
214 #[stable(feature = "rust1", since = "1.0.0")]
215 pub fn write(&self) -> LockResult
<RwLockWriteGuard
<T
>> {
218 RwLockWriteGuard
::new(self)
222 /// Attempts to lock this rwlock with exclusive write access.
224 /// If the lock could not be acquired at this time, then `Err` is returned.
225 /// Otherwise, an RAII guard is returned which will release the lock when
228 /// This function does not block.
230 /// This function does not provide any guarantees with respect to the ordering
231 /// of whether contentious readers or writers will acquire the lock first.
235 /// This function will return an error if the RwLock is poisoned. An RwLock
236 /// is poisoned whenever a writer panics while holding an exclusive lock. An
237 /// error will only be returned if the lock would have otherwise been
240 #[stable(feature = "rust1", since = "1.0.0")]
241 pub fn try_write(&self) -> TryLockResult
<RwLockWriteGuard
<T
>> {
243 if self.inner
.try_write() {
244 Ok(RwLockWriteGuard
::new(self)?
)
246 Err(TryLockError
::WouldBlock
)
251 /// Determines whether the lock is poisoned.
253 /// If another thread is active, the lock can still become poisoned at any
254 /// time. You should not trust a `false` value for program correctness
255 /// without additional synchronization.
257 #[stable(feature = "sync_poison", since = "1.2.0")]
258 pub fn is_poisoned(&self) -> bool
{
262 /// Consumes this `RwLock`, returning the underlying data.
266 /// This function will return an error if the RwLock is poisoned. An RwLock
267 /// is poisoned whenever a writer panics while holding an exclusive lock. An
268 /// error will only be returned if the lock would have otherwise been
270 #[stable(feature = "rwlock_into_inner", since = "1.6.0")]
271 pub fn into_inner(self) -> LockResult
<T
> where T
: Sized
{
272 // We know statically that there are no outstanding references to
273 // `self` so there's no need to lock the inner lock.
275 // To get the inner value, we'd like to call `data.into_inner()`,
276 // but because `RwLock` impl-s `Drop`, we can't move out of it, so
277 // we'll have to destructure it manually instead.
279 // Like `let RwLock { inner, poison, data } = self`.
280 let (inner
, poison
, data
) = {
281 let RwLock { ref inner, ref poison, ref data }
= self;
282 (ptr
::read(inner
), ptr
::read(poison
), ptr
::read(data
))
285 inner
.destroy(); // Keep in sync with the `Drop` impl.
288 poison
::map_result(poison
.borrow(), |_
| data
.into_inner())
292 /// Returns a mutable reference to the underlying data.
294 /// Since this call borrows the `RwLock` mutably, no actual locking needs to
295 /// take place---the mutable borrow statically guarantees no locks exist.
299 /// This function will return an error if the RwLock is poisoned. An RwLock
300 /// is poisoned whenever a writer panics while holding an exclusive lock. An
301 /// error will only be returned if the lock would have otherwise been
303 #[stable(feature = "rwlock_get_mut", since = "1.6.0")]
304 pub fn get_mut(&mut self) -> LockResult
<&mut T
> {
305 // We know statically that there are no other references to `self`, so
306 // there's no need to lock the inner lock.
307 let data
= unsafe { &mut *self.data.get() }
;
308 poison
::map_result(self.poison
.borrow(), |_
| data
)
312 #[stable(feature = "rust1", since = "1.0.0")]
313 unsafe impl<#[may_dangle] T: ?Sized> Drop for RwLock<T> {
315 // IMPORTANT: This code needs to be kept in sync with `RwLock::into_inner`.
316 unsafe { self.inner.destroy() }
320 #[stable(feature = "rust1", since = "1.0.0")]
321 impl<T
: ?Sized
+ fmt
::Debug
> fmt
::Debug
for RwLock
<T
> {
322 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
323 match self.try_read() {
324 Ok(guard
) => write
!(f
, "RwLock {{ data: {:?} }}", &*guard
),
325 Err(TryLockError
::Poisoned(err
)) => {
326 write
!(f
, "RwLock {{ data: Poisoned({:?}) }}", &**err
.get_ref())
328 Err(TryLockError
::WouldBlock
) => write
!(f
, "RwLock {{ <locked> }}")
333 #[stable(feature = "rw_lock_default", since = "1.9.0")]
334 impl<T
: Default
> Default
for RwLock
<T
> {
335 /// Creates a new `RwLock<T>`, with the `Default` value for T.
336 fn default() -> RwLock
<T
> {
337 RwLock
::new(Default
::default())
341 impl<'rwlock
, T
: ?Sized
> RwLockReadGuard
<'rwlock
, T
> {
342 unsafe fn new(lock
: &'rwlock RwLock
<T
>)
343 -> LockResult
<RwLockReadGuard
<'rwlock
, T
>> {
344 poison
::map_result(lock
.poison
.borrow(), |_
| {
352 impl<'rwlock
, T
: ?Sized
> RwLockWriteGuard
<'rwlock
, T
> {
353 unsafe fn new(lock
: &'rwlock RwLock
<T
>)
354 -> LockResult
<RwLockWriteGuard
<'rwlock
, T
>> {
355 poison
::map_result(lock
.poison
.borrow(), |guard
| {
364 #[stable(feature = "std_debug", since = "1.15.0")]
365 impl<'a
, T
: fmt
::Debug
> fmt
::Debug
for RwLockReadGuard
<'a
, T
> {
366 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
367 f
.debug_struct("RwLockReadGuard")
368 .field("lock", &self.__lock
)
373 #[stable(feature = "std_debug", since = "1.15.0")]
374 impl<'a
, T
: fmt
::Debug
> fmt
::Debug
for RwLockWriteGuard
<'a
, T
> {
375 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
376 f
.debug_struct("RwLockWriteGuard")
377 .field("lock", &self.__lock
)
382 #[stable(feature = "rust1", since = "1.0.0")]
383 impl<'rwlock
, T
: ?Sized
> Deref
for RwLockReadGuard
<'rwlock
, T
> {
386 fn deref(&self) -> &T
{
387 unsafe { &*self.__lock.data.get() }
391 #[stable(feature = "rust1", since = "1.0.0")]
392 impl<'rwlock
, T
: ?Sized
> Deref
for RwLockWriteGuard
<'rwlock
, T
> {
395 fn deref(&self) -> &T
{
396 unsafe { &*self.__lock.data.get() }
400 #[stable(feature = "rust1", since = "1.0.0")]
401 impl<'rwlock
, T
: ?Sized
> DerefMut
for RwLockWriteGuard
<'rwlock
, T
> {
402 fn deref_mut(&mut self) -> &mut T
{
403 unsafe { &mut *self.__lock.data.get() }
407 #[stable(feature = "rust1", since = "1.0.0")]
408 impl<'a
, T
: ?Sized
> Drop
for RwLockReadGuard
<'a
, T
> {
410 unsafe { self.__lock.inner.read_unlock(); }
414 #[stable(feature = "rust1", since = "1.0.0")]
415 impl<'a
, T
: ?Sized
> Drop
for RwLockWriteGuard
<'a
, T
> {
417 self.__lock
.poison
.done(&self.__poison
);
418 unsafe { self.__lock.inner.write_unlock(); }
422 #[cfg(all(test, not(target_os = "emscripten")))]
424 #![allow(deprecated)] // rand
426 use rand
::{self, Rng}
;
427 use sync
::mpsc
::channel
;
429 use sync
::{Arc, RwLock, TryLockError}
;
430 use sync
::atomic
::{AtomicUsize, Ordering}
;
432 #[derive(Eq, PartialEq, Debug)]
437 let l
= RwLock
::new(());
438 drop(l
.read().unwrap());
439 drop(l
.write().unwrap());
440 drop((l
.read().unwrap(), l
.read().unwrap()));
441 drop(l
.write().unwrap());
447 const M
: usize = 1000;
449 let r
= Arc
::new(RwLock
::new(()));
451 let (tx
, rx
) = channel
::<()>();
455 thread
::spawn(move || {
456 let mut rng
= rand
::thread_rng();
458 if rng
.gen_weighted_bool(N
) {
459 drop(r
.write().unwrap());
461 drop(r
.read().unwrap());
472 fn test_rw_arc_poison_wr() {
473 let arc
= Arc
::new(RwLock
::new(1));
474 let arc2
= arc
.clone();
475 let _
: Result
<(), _
> = thread
::spawn(move|| {
476 let _lock
= arc2
.write().unwrap();
479 assert
!(arc
.read().is_err());
483 fn test_rw_arc_poison_ww() {
484 let arc
= Arc
::new(RwLock
::new(1));
485 assert
!(!arc
.is_poisoned());
486 let arc2
= arc
.clone();
487 let _
: Result
<(), _
> = thread
::spawn(move|| {
488 let _lock
= arc2
.write().unwrap();
491 assert
!(arc
.write().is_err());
492 assert
!(arc
.is_poisoned());
496 fn test_rw_arc_no_poison_rr() {
497 let arc
= Arc
::new(RwLock
::new(1));
498 let arc2
= arc
.clone();
499 let _
: Result
<(), _
> = thread
::spawn(move|| {
500 let _lock
= arc2
.read().unwrap();
503 let lock
= arc
.read().unwrap();
504 assert_eq
!(*lock
, 1);
507 fn test_rw_arc_no_poison_rw() {
508 let arc
= Arc
::new(RwLock
::new(1));
509 let arc2
= arc
.clone();
510 let _
: Result
<(), _
> = thread
::spawn(move|| {
511 let _lock
= arc2
.read().unwrap();
514 let lock
= arc
.write().unwrap();
515 assert_eq
!(*lock
, 1);
520 let arc
= Arc
::new(RwLock
::new(0));
521 let arc2
= arc
.clone();
522 let (tx
, rx
) = channel();
524 thread
::spawn(move|| {
525 let mut lock
= arc2
.write().unwrap();
532 tx
.send(()).unwrap();
535 // Readers try to catch the writer in the act
536 let mut children
= Vec
::new();
538 let arc3
= arc
.clone();
539 children
.push(thread
::spawn(move|| {
540 let lock
= arc3
.read().unwrap();
545 // Wait for children to pass their asserts
547 assert
!(r
.join().is_ok());
550 // Wait for writer to finish
552 let lock
= arc
.read().unwrap();
553 assert_eq
!(*lock
, 10);
557 fn test_rw_arc_access_in_unwind() {
558 let arc
= Arc
::new(RwLock
::new(1));
559 let arc2
= arc
.clone();
560 let _
= thread
::spawn(move|| -> () {
562 i
: Arc
<RwLock
<isize>>,
564 impl Drop
for Unwinder
{
566 let mut lock
= self.i
.write().unwrap();
570 let _u
= Unwinder { i: arc2 }
;
573 let lock
= arc
.read().unwrap();
574 assert_eq
!(*lock
, 2);
578 fn test_rwlock_unsized() {
579 let rw
: &RwLock
<[i32]> = &RwLock
::new([1, 2, 3]);
581 let b
= &mut *rw
.write().unwrap();
585 let comp
: &[i32] = &[4, 2, 5];
586 assert_eq
!(&*rw
.read().unwrap(), comp
);
590 fn test_rwlock_try_write() {
591 let lock
= RwLock
::new(0isize
);
592 let read_guard
= lock
.read().unwrap();
594 let write_result
= lock
.try_write();
596 Err(TryLockError
::WouldBlock
) => (),
597 Ok(_
) => assert
!(false, "try_write should not succeed while read_guard is in scope"),
598 Err(_
) => assert
!(false, "unexpected error"),
605 fn test_into_inner() {
606 let m
= RwLock
::new(NonCopy(10));
607 assert_eq
!(m
.into_inner().unwrap(), NonCopy(10));
611 fn test_into_inner_drop() {
612 struct Foo(Arc
<AtomicUsize
>);
615 self.0.fetch_add
(1, Ordering
::SeqCst
);
618 let num_drops
= Arc
::new(AtomicUsize
::new(0));
619 let m
= RwLock
::new(Foo(num_drops
.clone()));
620 assert_eq
!(num_drops
.load(Ordering
::SeqCst
), 0);
622 let _inner
= m
.into_inner().unwrap();
623 assert_eq
!(num_drops
.load(Ordering
::SeqCst
), 0);
625 assert_eq
!(num_drops
.load(Ordering
::SeqCst
), 1);
629 fn test_into_inner_poison() {
630 let m
= Arc
::new(RwLock
::new(NonCopy(10)));
632 let _
= thread
::spawn(move || {
633 let _lock
= m2
.write().unwrap();
634 panic
!("test panic in inner thread to poison RwLock");
637 assert
!(m
.is_poisoned());
638 match Arc
::try_unwrap(m
).unwrap().into_inner() {
639 Err(e
) => assert_eq
!(e
.into_inner(), NonCopy(10)),
640 Ok(x
) => panic
!("into_inner of poisoned RwLock is Ok: {:?}", x
),
646 let mut m
= RwLock
::new(NonCopy(10));
647 *m
.get_mut().unwrap() = NonCopy(20);
648 assert_eq
!(m
.into_inner().unwrap(), NonCopy(20));
652 fn test_get_mut_poison() {
653 let m
= Arc
::new(RwLock
::new(NonCopy(10)));
655 let _
= thread
::spawn(move || {
656 let _lock
= m2
.write().unwrap();
657 panic
!("test panic in inner thread to poison RwLock");
660 assert
!(m
.is_poisoned());
661 match Arc
::try_unwrap(m
).unwrap().get_mut() {
662 Err(e
) => assert_eq
!(*e
.into_inner(), NonCopy(10)),
663 Ok(x
) => panic
!("get_mut of poisoned RwLock is Ok: {:?}", x
),