1 #[cfg(all(test, not(target_os = "emscripten")))]
4 use crate::cell::UnsafeCell;
6 use crate::ops::{Deref, DerefMut};
7 use crate::ptr::NonNull;
8 use crate::sync::{poison, LockResult, TryLockError, TryLockResult};
9 use crate::sys_common::rwlock as sys;
11 /// A reader-writer lock
13 /// This type of lock allows a number of readers or at most one writer at any
14 /// point in time. The write portion of this lock typically allows modification
15 /// of the underlying data (exclusive access) and the read portion of this lock
16 /// typically allows for read-only access (shared access).
18 /// In comparison, a [`Mutex`] does not distinguish between readers or writers
19 /// that acquire the lock, therefore blocking any threads waiting for the lock to
20 /// become available. An `RwLock` will allow any number of readers to acquire the
21 /// lock as long as a writer is not holding the lock.
23 /// The priority policy of the lock is dependent on the underlying operating
24 /// system's implementation, and this type does not guarantee that any
25 /// particular policy will be used. In particular, a writer which is waiting to
26 /// acquire the lock in `write` might or might not block concurrent calls to
29 /// <details><summary>Potential deadlock example</summary>
32 /// // Thread 1 | // Thread 2
33 /// let _rg = lock.read(); |
35 /// | let _wg = lock.write();
37 /// let _rg = lock.read(); |
41 /// The type parameter `T` represents the data that this lock protects. It is
42 /// required that `T` satisfies [`Send`] to be shared across threads and
43 /// [`Sync`] to allow concurrent access through readers. The RAII guards
44 /// returned from the locking methods implement [`Deref`] (and [`DerefMut`]
45 /// for the `write` methods) to allow access to the content of the lock.
49 /// An `RwLock`, like [`Mutex`], will become poisoned on a panic. Note, however,
50 /// that an `RwLock` may only be poisoned if a panic occurs while it is locked
51 /// exclusively (write mode). If a panic occurs in any reader, then the lock
52 /// will not be poisoned.
57 /// use std::sync::RwLock;
59 /// let lock = RwLock::new(5);
61 /// // many reader locks can be held at once
63 /// let r1 = lock.read().unwrap();
64 /// let r2 = lock.read().unwrap();
65 /// assert_eq!(*r1, 5);
66 /// assert_eq!(*r2, 5);
67 /// } // read locks are dropped at this point
69 /// // only one write lock may be held, however
71 /// let mut w = lock.write().unwrap();
73 /// assert_eq!(*w, 6);
74 /// } // write lock is dropped here
77 /// [`Mutex`]: super::Mutex
78 #[stable(feature = "rust1", since = "1.0.0")]
79 #[cfg_attr(not(test), rustc_diagnostic_item = "RwLock")]
80 pub struct RwLock<T: ?Sized> {
81 inner: sys::MovableRwLock,
86 #[stable(feature = "rust1", since = "1.0.0")]
87 unsafe impl<T: ?Sized + Send> Send for RwLock<T> {}
88 #[stable(feature = "rust1", since = "1.0.0")]
89 unsafe impl<T: ?Sized + Send + Sync> Sync for RwLock<T> {}
91 /// RAII structure used to release the shared read access of a lock when
94 /// This structure is created by the [`read`] and [`try_read`] methods on
97 /// [`read`]: RwLock::read
98 /// [`try_read`]: RwLock::try_read
99 #[must_use = "if unused the RwLock will immediately unlock"]
100 #[must_not_suspend = "holding a RwLockReadGuard across suspend \
101 points can cause deadlocks, delays, \
102 and cause Futures to not implement `Send`"]
103 #[stable(feature = "rust1", since = "1.0.0")]
104 #[clippy::has_significant_drop]
105 #[cfg_attr(not(test), rustc_diagnostic_item = "RwLockReadGuard")]
106 pub struct RwLockReadGuard<'a, T: ?Sized + 'a> {
107 // NB: we use a pointer instead of `&'a T` to avoid `noalias` violations, because a
108 // `Ref` argument doesn't hold immutability for its whole scope, only until it drops.
109 // `NonNull` is also covariant over `T`, just like we would have with `&T`. `NonNull`
110 // is preferable over `const* T` to allow for niche optimization.
112 inner_lock: &'a sys::MovableRwLock,
115 #[stable(feature = "rust1", since = "1.0.0")]
116 impl<T: ?Sized> !Send for RwLockReadGuard<'_, T> {}
118 #[stable(feature = "rwlock_guard_sync", since = "1.23.0")]
119 unsafe impl<T: ?Sized + Sync> Sync for RwLockReadGuard<'_, T> {}
121 /// RAII structure used to release the exclusive write access of a lock when
124 /// This structure is created by the [`write`] and [`try_write`] methods
127 /// [`write`]: RwLock::write
128 /// [`try_write`]: RwLock::try_write
129 #[must_use = "if unused the RwLock will immediately unlock"]
130 #[must_not_suspend = "holding a RwLockWriteGuard across suspend \
131 points can cause deadlocks, delays, \
132 and cause Future's to not implement `Send`"]
133 #[stable(feature = "rust1", since = "1.0.0")]
134 #[clippy::has_significant_drop]
135 #[cfg_attr(not(test), rustc_diagnostic_item = "RwLockWriteGuard")]
136 pub struct RwLockWriteGuard<'a, T: ?Sized + 'a> {
138 poison: poison::Guard,
141 #[stable(feature = "rust1", since = "1.0.0")]
142 impl<T: ?Sized> !Send for RwLockWriteGuard<'_, T> {}
144 #[stable(feature = "rwlock_guard_sync", since = "1.23.0")]
145 unsafe impl<T: ?Sized + Sync> Sync for RwLockWriteGuard<'_, T> {}
148 /// Creates a new instance of an `RwLock<T>` which is unlocked.
153 /// use std::sync::RwLock;
155 /// let lock = RwLock::new(5);
157 #[stable(feature = "rust1", since = "1.0.0")]
158 #[rustc_const_stable(feature = "const_locks", since = "1.63.0")]
160 pub const fn new(t: T) -> RwLock<T> {
162 inner: sys::MovableRwLock::new(),
163 poison: poison::Flag::new(),
164 data: UnsafeCell::new(t),
169 impl<T: ?Sized> RwLock<T> {
170 /// Locks this `RwLock` with shared read access, blocking the current thread
171 /// until it can be acquired.
173 /// The calling thread will be blocked until there are no more writers which
174 /// hold the lock. There may be other readers currently inside the lock when
175 /// this method returns. This method does not provide any guarantees with
176 /// respect to the ordering of whether contentious readers or writers will
177 /// acquire the lock first.
179 /// Returns an RAII guard which will release this thread's shared access
180 /// once it is dropped.
184 /// This function will return an error if the `RwLock` is poisoned. An
185 /// `RwLock` is poisoned whenever a writer panics while holding an exclusive
186 /// lock. The failure will occur immediately after the lock has been
191 /// This function might panic when called if the lock is already held by the current thread.
196 /// use std::sync::{Arc, RwLock};
199 /// let lock = Arc::new(RwLock::new(1));
200 /// let c_lock = Arc::clone(&lock);
202 /// let n = lock.read().unwrap();
203 /// assert_eq!(*n, 1);
205 /// thread::spawn(move || {
206 /// let r = c_lock.read();
207 /// assert!(r.is_ok());
208 /// }).join().unwrap();
211 #[stable(feature = "rust1", since = "1.0.0")]
212 pub fn read(&self) -> LockResult<RwLockReadGuard<'_, T>> {
215 RwLockReadGuard::new(self)
219 /// Attempts to acquire this `RwLock` with shared read access.
221 /// If the access could not be granted at this time, then `Err` is returned.
222 /// Otherwise, an RAII guard is returned which will release the shared access
223 /// when it is dropped.
225 /// This function does not block.
227 /// This function does not provide any guarantees with respect to the ordering
228 /// of whether contentious readers or writers will acquire the lock first.
232 /// This function will return the [`Poisoned`] error if the `RwLock` is
233 /// poisoned. An `RwLock` is poisoned whenever a writer panics while holding
234 /// an exclusive lock. `Poisoned` will only be returned if the lock would
235 /// have otherwise been acquired.
237 /// This function will return the [`WouldBlock`] error if the `RwLock` could
238 /// not be acquired because it was already locked exclusively.
240 /// [`Poisoned`]: TryLockError::Poisoned
241 /// [`WouldBlock`]: TryLockError::WouldBlock
246 /// use std::sync::RwLock;
248 /// let lock = RwLock::new(1);
250 /// match lock.try_read() {
251 /// Ok(n) => assert_eq!(*n, 1),
252 /// Err(_) => unreachable!(),
256 #[stable(feature = "rust1", since = "1.0.0")]
257 pub fn try_read(&self) -> TryLockResult<RwLockReadGuard<'_, T>> {
259 if self.inner.try_read() {
260 Ok(RwLockReadGuard::new(self)?)
262 Err(TryLockError::WouldBlock)
267 /// Locks this `RwLock` with exclusive write access, blocking the current
268 /// thread until it can be acquired.
270 /// This function will not return while other writers or other readers
271 /// currently have access to the lock.
273 /// Returns an RAII guard which will drop the write access of this `RwLock`
278 /// This function will return an error if the `RwLock` is poisoned. An
279 /// `RwLock` is poisoned whenever a writer panics while holding an exclusive
280 /// lock. An error will be returned when the lock is acquired.
284 /// This function might panic when called if the lock is already held by the current thread.
289 /// use std::sync::RwLock;
291 /// let lock = RwLock::new(1);
293 /// let mut n = lock.write().unwrap();
296 /// assert!(lock.try_read().is_err());
299 #[stable(feature = "rust1", since = "1.0.0")]
300 pub fn write(&self) -> LockResult<RwLockWriteGuard<'_, T>> {
303 RwLockWriteGuard::new(self)
307 /// Attempts to lock this `RwLock` with exclusive write access.
309 /// If the lock could not be acquired at this time, then `Err` is returned.
310 /// Otherwise, an RAII guard is returned which will release the lock when
313 /// This function does not block.
315 /// This function does not provide any guarantees with respect to the ordering
316 /// of whether contentious readers or writers will acquire the lock first.
320 /// This function will return the [`Poisoned`] error if the `RwLock` is
321 /// poisoned. An `RwLock` is poisoned whenever a writer panics while holding
322 /// an exclusive lock. `Poisoned` will only be returned if the lock would
323 /// have otherwise been acquired.
325 /// This function will return the [`WouldBlock`] error if the `RwLock` could
326 /// not be acquired because it was already locked exclusively.
328 /// [`Poisoned`]: TryLockError::Poisoned
329 /// [`WouldBlock`]: TryLockError::WouldBlock
335 /// use std::sync::RwLock;
337 /// let lock = RwLock::new(1);
339 /// let n = lock.read().unwrap();
340 /// assert_eq!(*n, 1);
342 /// assert!(lock.try_write().is_err());
345 #[stable(feature = "rust1", since = "1.0.0")]
346 pub fn try_write(&self) -> TryLockResult<RwLockWriteGuard<'_, T>> {
348 if self.inner.try_write() {
349 Ok(RwLockWriteGuard::new(self)?)
351 Err(TryLockError::WouldBlock)
356 /// Determines whether the lock is poisoned.
358 /// If another thread is active, the lock can still become poisoned at any
359 /// time. You should not trust a `false` value for program correctness
360 /// without additional synchronization.
365 /// use std::sync::{Arc, RwLock};
368 /// let lock = Arc::new(RwLock::new(0));
369 /// let c_lock = Arc::clone(&lock);
371 /// let _ = thread::spawn(move || {
372 /// let _lock = c_lock.write().unwrap();
373 /// panic!(); // the lock gets poisoned
375 /// assert_eq!(lock.is_poisoned(), true);
378 #[stable(feature = "sync_poison", since = "1.2.0")]
379 pub fn is_poisoned(&self) -> bool {
383 /// Clear the poisoned state from a lock
385 /// If the lock is poisoned, it will remain poisoned until this function is called. This allows
386 /// recovering from a poisoned state and marking that it has recovered. For example, if the
387 /// value is overwritten by a known-good value, then the mutex can be marked as un-poisoned. Or
388 /// possibly, the value could be inspected to determine if it is in a consistent state, and if
389 /// so the poison is removed.
394 /// #![feature(mutex_unpoison)]
396 /// use std::sync::{Arc, RwLock};
399 /// let lock = Arc::new(RwLock::new(0));
400 /// let c_lock = Arc::clone(&lock);
402 /// let _ = thread::spawn(move || {
403 /// let _lock = c_lock.write().unwrap();
404 /// panic!(); // the mutex gets poisoned
407 /// assert_eq!(lock.is_poisoned(), true);
408 /// let guard = lock.write().unwrap_or_else(|mut e| {
409 /// **e.get_mut() = 1;
410 /// lock.clear_poison();
413 /// assert_eq!(lock.is_poisoned(), false);
414 /// assert_eq!(*guard, 1);
417 #[unstable(feature = "mutex_unpoison", issue = "96469")]
418 pub fn clear_poison(&self) {
422 /// Consumes this `RwLock`, returning the underlying data.
426 /// This function will return an error if the `RwLock` is poisoned. An
427 /// `RwLock` is poisoned whenever a writer panics while holding an exclusive
428 /// lock. An error will only be returned if the lock would have otherwise
434 /// use std::sync::RwLock;
436 /// let lock = RwLock::new(String::new());
438 /// let mut s = lock.write().unwrap();
439 /// *s = "modified".to_owned();
441 /// assert_eq!(lock.into_inner().unwrap(), "modified");
443 #[stable(feature = "rwlock_into_inner", since = "1.6.0")]
444 pub fn into_inner(self) -> LockResult<T>
448 let data = self.data.into_inner();
449 poison::map_result(self.poison.borrow(), |()| data)
452 /// Returns a mutable reference to the underlying data.
454 /// Since this call borrows the `RwLock` mutably, no actual locking needs to
455 /// take place -- the mutable borrow statically guarantees no locks exist.
459 /// This function will return an error if the `RwLock` is poisoned. An
460 /// `RwLock` is poisoned whenever a writer panics while holding an exclusive
461 /// lock. An error will only be returned if the lock would have otherwise
467 /// use std::sync::RwLock;
469 /// let mut lock = RwLock::new(0);
470 /// *lock.get_mut().unwrap() = 10;
471 /// assert_eq!(*lock.read().unwrap(), 10);
473 #[stable(feature = "rwlock_get_mut", since = "1.6.0")]
474 pub fn get_mut(&mut self) -> LockResult<&mut T> {
475 let data = self.data.get_mut();
476 poison::map_result(self.poison.borrow(), |()| data)
480 #[stable(feature = "rust1", since = "1.0.0")]
481 impl<T: ?Sized + fmt::Debug> fmt::Debug for RwLock<T> {
482 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
483 let mut d = f.debug_struct("RwLock");
484 match self.try_read() {
486 d.field("data", &&*guard);
488 Err(TryLockError::Poisoned(err)) => {
489 d.field("data", &&**err.get_ref());
491 Err(TryLockError::WouldBlock) => {
492 struct LockedPlaceholder;
493 impl fmt::Debug for LockedPlaceholder {
494 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
495 f.write_str("<locked>")
498 d.field("data", &LockedPlaceholder);
501 d.field("poisoned", &self.poison.get());
502 d.finish_non_exhaustive()
506 #[stable(feature = "rw_lock_default", since = "1.10.0")]
507 impl<T: Default> Default for RwLock<T> {
508 /// Creates a new `RwLock<T>`, with the `Default` value for T.
509 fn default() -> RwLock<T> {
510 RwLock::new(Default::default())
514 #[stable(feature = "rw_lock_from", since = "1.24.0")]
515 impl<T> From<T> for RwLock<T> {
516 /// Creates a new instance of an `RwLock<T>` which is unlocked.
517 /// This is equivalent to [`RwLock::new`].
518 fn from(t: T) -> Self {
523 impl<'rwlock, T: ?Sized> RwLockReadGuard<'rwlock, T> {
524 /// Create a new instance of `RwLockReadGuard<T>` from a `RwLock<T>`.
525 // SAFETY: if and only if `lock.inner.read()` (or `lock.inner.try_read()`) has been
526 // successfully called from the same thread before instantiating this object.
527 unsafe fn new(lock: &'rwlock RwLock<T>) -> LockResult<RwLockReadGuard<'rwlock, T>> {
528 poison::map_result(lock.poison.borrow(), |()| RwLockReadGuard {
529 data: NonNull::new_unchecked(lock.data.get()),
530 inner_lock: &lock.inner,
535 impl<'rwlock, T: ?Sized> RwLockWriteGuard<'rwlock, T> {
536 /// Create a new instance of `RwLockWriteGuard<T>` from a `RwLock<T>`.
537 // SAFETY: if and only if `lock.inner.write()` (or `lock.inner.try_write()`) has been
538 // successfully called from the same thread before instantiating this object.
539 unsafe fn new(lock: &'rwlock RwLock<T>) -> LockResult<RwLockWriteGuard<'rwlock, T>> {
540 poison::map_result(lock.poison.guard(), |guard| RwLockWriteGuard { lock, poison: guard })
544 #[stable(feature = "std_debug", since = "1.16.0")]
545 impl<T: fmt::Debug> fmt::Debug for RwLockReadGuard<'_, T> {
546 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
551 #[stable(feature = "std_guard_impls", since = "1.20.0")]
552 impl<T: ?Sized + fmt::Display> fmt::Display for RwLockReadGuard<'_, T> {
553 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
558 #[stable(feature = "std_debug", since = "1.16.0")]
559 impl<T: fmt::Debug> fmt::Debug for RwLockWriteGuard<'_, T> {
560 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
565 #[stable(feature = "std_guard_impls", since = "1.20.0")]
566 impl<T: ?Sized + fmt::Display> fmt::Display for RwLockWriteGuard<'_, T> {
567 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
572 #[stable(feature = "rust1", since = "1.0.0")]
573 impl<T: ?Sized> Deref for RwLockReadGuard<'_, T> {
576 fn deref(&self) -> &T {
577 // SAFETY: the conditions of `RwLockGuard::new` were satisfied when created.
578 unsafe { self.data.as_ref() }
582 #[stable(feature = "rust1", since = "1.0.0")]
583 impl<T: ?Sized> Deref for RwLockWriteGuard<'_, T> {
586 fn deref(&self) -> &T {
587 // SAFETY: the conditions of `RwLockWriteGuard::new` were satisfied when created.
588 unsafe { &*self.lock.data.get() }
592 #[stable(feature = "rust1", since = "1.0.0")]
593 impl<T: ?Sized> DerefMut for RwLockWriteGuard<'_, T> {
594 fn deref_mut(&mut self) -> &mut T {
595 // SAFETY: the conditions of `RwLockWriteGuard::new` were satisfied when created.
596 unsafe { &mut *self.lock.data.get() }
600 #[stable(feature = "rust1", since = "1.0.0")]
601 impl<T: ?Sized> Drop for RwLockReadGuard<'_, T> {
603 // SAFETY: the conditions of `RwLockReadGuard::new` were satisfied when created.
605 self.inner_lock.read_unlock();
610 #[stable(feature = "rust1", since = "1.0.0")]
611 impl<T: ?Sized> Drop for RwLockWriteGuard<'_, T> {
613 self.lock.poison.done(&self.poison);
614 // SAFETY: the conditions of `RwLockWriteGuard::new` were satisfied when created.
616 self.lock.inner.write_unlock();