1 #[cfg(all(test, not(target_os = "emscripten")))]
4 use crate::cell::UnsafeCell;
7 use crate::ops::{Deref, DerefMut};
9 use crate::sync::{poison, LockResult, TryLockError, TryLockResult};
10 use crate::sys_common::rwlock as sys;
12 /// A reader-writer lock
14 /// This type of lock allows a number of readers or at most one writer at any
15 /// point in time. The write portion of this lock typically allows modification
16 /// of the underlying data (exclusive access) and the read portion of this lock
17 /// typically allows for read-only access (shared access).
19 /// In comparison, a [`Mutex`] does not distinguish between readers or writers
20 /// that acquire the lock, therefore blocking any threads waiting for the lock to
21 /// become available. An `RwLock` will allow any number of readers to acquire the
22 /// lock as long as a writer is not holding the lock.
24 /// The priority policy of the lock is dependent on the underlying operating
25 /// system's implementation, and this type does not guarantee that any
26 /// particular policy will be used. In particular, a writer which is waiting to
27 /// acquire the lock in `write` might or might not block concurrent calls to
30 /// The type parameter `T` represents the data that this lock protects. It is
31 /// required that `T` satisfies [`Send`] to be shared across threads and
32 /// [`Sync`] to allow concurrent access through readers. The RAII guards
33 /// returned from the locking methods implement [`Deref`] (and [`DerefMut`]
34 /// for the `write` methods) to allow access to the content of the lock.
38 /// An `RwLock`, like [`Mutex`], will become poisoned on a panic. Note, however,
39 /// that an `RwLock` may only be poisoned if a panic occurs while it is locked
40 /// exclusively (write mode). If a panic occurs in any reader, then the lock
41 /// will not be poisoned.
46 /// use std::sync::RwLock;
48 /// let lock = RwLock::new(5);
50 /// // many reader locks can be held at once
52 /// let r1 = lock.read().unwrap();
53 /// let r2 = lock.read().unwrap();
54 /// assert_eq!(*r1, 5);
55 /// assert_eq!(*r2, 5);
56 /// } // read locks are dropped at this point
58 /// // only one write lock may be held, however
60 /// let mut w = lock.write().unwrap();
62 /// assert_eq!(*w, 6);
63 /// } // write lock is dropped here
66 /// [`Mutex`]: super::Mutex
67 #[stable(feature = "rust1", since = "1.0.0")]
68 pub struct RwLock<T: ?Sized> {
69 inner: Box<sys::RWLock>,
74 #[stable(feature = "rust1", since = "1.0.0")]
75 unsafe impl<T: ?Sized + Send> Send for RwLock<T> {}
76 #[stable(feature = "rust1", since = "1.0.0")]
77 unsafe impl<T: ?Sized + Send + Sync> Sync for RwLock<T> {}
79 /// RAII structure used to release the shared read access of a lock when
82 /// This structure is created by the [`read`] and [`try_read`] methods on
85 /// [`read`]: RwLock::read
86 /// [`try_read`]: RwLock::try_read
87 #[must_use = "if unused the RwLock will immediately unlock"]
88 #[stable(feature = "rust1", since = "1.0.0")]
89 pub struct RwLockReadGuard<'a, T: ?Sized + 'a> {
93 #[stable(feature = "rust1", since = "1.0.0")]
94 impl<T: ?Sized> !Send for RwLockReadGuard<'_, T> {}
96 #[stable(feature = "rwlock_guard_sync", since = "1.23.0")]
97 unsafe impl<T: ?Sized + Sync> Sync for RwLockReadGuard<'_, T> {}
99 /// RAII structure used to release the exclusive write access of a lock when
102 /// This structure is created by the [`write`] and [`try_write`] methods
105 /// [`write`]: RwLock::write
106 /// [`try_write`]: RwLock::try_write
107 #[must_use = "if unused the RwLock will immediately unlock"]
108 #[stable(feature = "rust1", since = "1.0.0")]
109 pub struct RwLockWriteGuard<'a, T: ?Sized + 'a> {
111 poison: poison::Guard,
114 #[stable(feature = "rust1", since = "1.0.0")]
115 impl<T: ?Sized> !Send for RwLockWriteGuard<'_, T> {}
117 #[stable(feature = "rwlock_guard_sync", since = "1.23.0")]
118 unsafe impl<T: ?Sized + Sync> Sync for RwLockWriteGuard<'_, T> {}
121 /// Creates a new instance of an `RwLock<T>` which is unlocked.
126 /// use std::sync::RwLock;
128 /// let lock = RwLock::new(5);
130 #[stable(feature = "rust1", since = "1.0.0")]
131 pub fn new(t: T) -> RwLock<T> {
133 inner: box sys::RWLock::new(),
134 poison: poison::Flag::new(),
135 data: UnsafeCell::new(t),
140 impl<T: ?Sized> RwLock<T> {
141 /// Locks this rwlock with shared read access, blocking the current thread
142 /// until it can be acquired.
144 /// The calling thread will be blocked until there are no more writers which
145 /// hold the lock. There may be other readers currently inside the lock when
146 /// this method returns. This method does not provide any guarantees with
147 /// respect to the ordering of whether contentious readers or writers will
148 /// acquire the lock first.
150 /// Returns an RAII guard which will release this thread's shared access
151 /// once it is dropped.
155 /// This function will return an error if the RwLock is poisoned. An RwLock
156 /// is poisoned whenever a writer panics while holding an exclusive lock.
157 /// The failure will occur immediately after the lock has been acquired.
161 /// This function might panic when called if the lock is already held by the current thread.
166 /// use std::sync::{Arc, RwLock};
169 /// let lock = Arc::new(RwLock::new(1));
170 /// let c_lock = Arc::clone(&lock);
172 /// let n = lock.read().unwrap();
173 /// assert_eq!(*n, 1);
175 /// thread::spawn(move || {
176 /// let r = c_lock.read();
177 /// assert!(r.is_ok());
178 /// }).join().unwrap();
181 #[stable(feature = "rust1", since = "1.0.0")]
182 pub fn read(&self) -> LockResult<RwLockReadGuard<'_, T>> {
185 RwLockReadGuard::new(self)
189 /// Attempts to acquire this rwlock with shared read access.
191 /// If the access could not be granted at this time, then `Err` is returned.
192 /// Otherwise, an RAII guard is returned which will release the shared access
193 /// when it is dropped.
195 /// This function does not block.
197 /// This function does not provide any guarantees with respect to the ordering
198 /// of whether contentious readers or writers will acquire the lock first.
202 /// This function will return an error if the RwLock is poisoned. An RwLock
203 /// is poisoned whenever a writer panics while holding an exclusive lock. An
204 /// error will only be returned if the lock would have otherwise been
210 /// use std::sync::RwLock;
212 /// let lock = RwLock::new(1);
214 /// match lock.try_read() {
215 /// Ok(n) => assert_eq!(*n, 1),
216 /// Err(_) => unreachable!(),
220 #[stable(feature = "rust1", since = "1.0.0")]
221 pub fn try_read(&self) -> TryLockResult<RwLockReadGuard<'_, T>> {
223 if self.inner.try_read() {
224 Ok(RwLockReadGuard::new(self)?)
226 Err(TryLockError::WouldBlock)
231 /// Locks this rwlock with exclusive write access, blocking the current
232 /// thread until it can be acquired.
234 /// This function will not return while other writers or other readers
235 /// currently have access to the lock.
237 /// Returns an RAII guard which will drop the write access of this rwlock
242 /// This function will return an error if the RwLock is poisoned. An RwLock
243 /// is poisoned whenever a writer panics while holding an exclusive lock.
244 /// An error will be returned when the lock is acquired.
248 /// This function might panic when called if the lock is already held by the current thread.
253 /// use std::sync::RwLock;
255 /// let lock = RwLock::new(1);
257 /// let mut n = lock.write().unwrap();
260 /// assert!(lock.try_read().is_err());
263 #[stable(feature = "rust1", since = "1.0.0")]
264 pub fn write(&self) -> LockResult<RwLockWriteGuard<'_, T>> {
267 RwLockWriteGuard::new(self)
271 /// Attempts to lock this rwlock with exclusive write access.
273 /// If the lock could not be acquired at this time, then `Err` is returned.
274 /// Otherwise, an RAII guard is returned which will release the lock when
277 /// This function does not block.
279 /// This function does not provide any guarantees with respect to the ordering
280 /// of whether contentious readers or writers will acquire the lock first.
284 /// This function will return an error if the RwLock is poisoned. An RwLock
285 /// is poisoned whenever a writer panics while holding an exclusive lock. An
286 /// error will only be returned if the lock would have otherwise been
292 /// use std::sync::RwLock;
294 /// let lock = RwLock::new(1);
296 /// let n = lock.read().unwrap();
297 /// assert_eq!(*n, 1);
299 /// assert!(lock.try_write().is_err());
302 #[stable(feature = "rust1", since = "1.0.0")]
303 pub fn try_write(&self) -> TryLockResult<RwLockWriteGuard<'_, T>> {
305 if self.inner.try_write() {
306 Ok(RwLockWriteGuard::new(self)?)
308 Err(TryLockError::WouldBlock)
313 /// Determines whether the lock is poisoned.
315 /// If another thread is active, the lock can still become poisoned at any
316 /// time. You should not trust a `false` value for program correctness
317 /// without additional synchronization.
322 /// use std::sync::{Arc, RwLock};
325 /// let lock = Arc::new(RwLock::new(0));
326 /// let c_lock = Arc::clone(&lock);
328 /// let _ = thread::spawn(move || {
329 /// let _lock = c_lock.write().unwrap();
330 /// panic!(); // the lock gets poisoned
332 /// assert_eq!(lock.is_poisoned(), true);
335 #[stable(feature = "sync_poison", since = "1.2.0")]
336 pub fn is_poisoned(&self) -> bool {
340 /// Consumes this `RwLock`, returning the underlying data.
344 /// This function will return an error if the RwLock is poisoned. An RwLock
345 /// is poisoned whenever a writer panics while holding an exclusive lock. An
346 /// error will only be returned if the lock would have otherwise been
352 /// use std::sync::RwLock;
354 /// let lock = RwLock::new(String::new());
356 /// let mut s = lock.write().unwrap();
357 /// *s = "modified".to_owned();
359 /// assert_eq!(lock.into_inner().unwrap(), "modified");
361 #[stable(feature = "rwlock_into_inner", since = "1.6.0")]
362 pub fn into_inner(self) -> LockResult<T>
366 // We know statically that there are no outstanding references to
367 // `self` so there's no need to lock the inner lock.
369 // To get the inner value, we'd like to call `data.into_inner()`,
370 // but because `RwLock` impl-s `Drop`, we can't move out of it, so
371 // we'll have to destructure it manually instead.
373 // Like `let RwLock { inner, poison, data } = self`.
374 let (inner, poison, data) = {
375 let RwLock { ref inner, ref poison, ref data } = self;
376 (ptr::read(inner), ptr::read(poison), ptr::read(data))
379 inner.destroy(); // Keep in sync with the `Drop` impl.
382 poison::map_result(poison.borrow(), |_| data.into_inner())
386 /// Returns a mutable reference to the underlying data.
388 /// Since this call borrows the `RwLock` mutably, no actual locking needs to
389 /// take place -- the mutable borrow statically guarantees no locks exist.
393 /// This function will return an error if the RwLock is poisoned. An RwLock
394 /// is poisoned whenever a writer panics while holding an exclusive lock. An
395 /// error will only be returned if the lock would have otherwise been
401 /// use std::sync::RwLock;
403 /// let mut lock = RwLock::new(0);
404 /// *lock.get_mut().unwrap() = 10;
405 /// assert_eq!(*lock.read().unwrap(), 10);
407 #[stable(feature = "rwlock_get_mut", since = "1.6.0")]
408 pub fn get_mut(&mut self) -> LockResult<&mut T> {
409 let data = self.data.get_mut();
410 poison::map_result(self.poison.borrow(), |_| data)
414 #[stable(feature = "rust1", since = "1.0.0")]
415 unsafe impl<#[may_dangle] T: ?Sized> Drop for RwLock<T> {
417 // IMPORTANT: This code needs to be kept in sync with `RwLock::into_inner`.
418 unsafe { self.inner.destroy() }
422 #[stable(feature = "rust1", since = "1.0.0")]
423 impl<T: ?Sized + fmt::Debug> fmt::Debug for RwLock<T> {
424 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
425 let mut d = f.debug_struct("RwLock");
426 match self.try_read() {
428 d.field("data", &&*guard);
430 Err(TryLockError::Poisoned(err)) => {
431 d.field("data", &&**err.get_ref());
433 Err(TryLockError::WouldBlock) => {
434 struct LockedPlaceholder;
435 impl fmt::Debug for LockedPlaceholder {
436 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
437 f.write_str("<locked>")
440 d.field("data", &LockedPlaceholder);
443 d.field("poisoned", &self.poison.get());
444 d.finish_non_exhaustive()
448 #[stable(feature = "rw_lock_default", since = "1.10.0")]
449 impl<T: Default> Default for RwLock<T> {
450 /// Creates a new `RwLock<T>`, with the `Default` value for T.
451 fn default() -> RwLock<T> {
452 RwLock::new(Default::default())
456 #[stable(feature = "rw_lock_from", since = "1.24.0")]
457 impl<T> From<T> for RwLock<T> {
458 /// Creates a new instance of an `RwLock<T>` which is unlocked.
459 /// This is equivalent to [`RwLock::new`].
460 fn from(t: T) -> Self {
465 impl<'rwlock, T: ?Sized> RwLockReadGuard<'rwlock, T> {
466 unsafe fn new(lock: &'rwlock RwLock<T>) -> LockResult<RwLockReadGuard<'rwlock, T>> {
467 poison::map_result(lock.poison.borrow(), |_| RwLockReadGuard { lock })
471 impl<'rwlock, T: ?Sized> RwLockWriteGuard<'rwlock, T> {
472 unsafe fn new(lock: &'rwlock RwLock<T>) -> LockResult<RwLockWriteGuard<'rwlock, T>> {
473 poison::map_result(lock.poison.borrow(), |guard| RwLockWriteGuard { lock, poison: guard })
477 #[stable(feature = "std_debug", since = "1.16.0")]
478 impl<T: fmt::Debug> fmt::Debug for RwLockReadGuard<'_, T> {
479 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
484 #[stable(feature = "std_guard_impls", since = "1.20.0")]
485 impl<T: ?Sized + fmt::Display> fmt::Display for RwLockReadGuard<'_, T> {
486 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
491 #[stable(feature = "std_debug", since = "1.16.0")]
492 impl<T: fmt::Debug> fmt::Debug for RwLockWriteGuard<'_, T> {
493 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
498 #[stable(feature = "std_guard_impls", since = "1.20.0")]
499 impl<T: ?Sized + fmt::Display> fmt::Display for RwLockWriteGuard<'_, T> {
500 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
505 #[stable(feature = "rust1", since = "1.0.0")]
506 impl<T: ?Sized> Deref for RwLockReadGuard<'_, T> {
509 fn deref(&self) -> &T {
510 unsafe { &*self.lock.data.get() }
514 #[stable(feature = "rust1", since = "1.0.0")]
515 impl<T: ?Sized> Deref for RwLockWriteGuard<'_, T> {
518 fn deref(&self) -> &T {
519 unsafe { &*self.lock.data.get() }
523 #[stable(feature = "rust1", since = "1.0.0")]
524 impl<T: ?Sized> DerefMut for RwLockWriteGuard<'_, T> {
525 fn deref_mut(&mut self) -> &mut T {
526 unsafe { &mut *self.lock.data.get() }
530 #[stable(feature = "rust1", since = "1.0.0")]
531 impl<T: ?Sized> Drop for RwLockReadGuard<'_, T> {
534 self.lock.inner.read_unlock();
539 #[stable(feature = "rust1", since = "1.0.0")]
540 impl<T: ?Sized> Drop for RwLockWriteGuard<'_, T> {
542 self.lock.poison.done(&self.poison);
544 self.lock.inner.write_unlock();