std/sync/poison/mutex.rs
1use crate::cell::UnsafeCell;
2use crate::fmt;
3use crate::marker::PhantomData;
4use crate::mem::{self, ManuallyDrop};
5use crate::ops::{Deref, DerefMut};
6use crate::ptr::NonNull;
7use crate::sync::{LockResult, PoisonError, TryLockError, TryLockResult, poison};
8use crate::sys::sync as sys;
9
10/// A mutual exclusion primitive useful for protecting shared data
11///
12/// This mutex will block threads waiting for the lock to become available. The
13/// mutex can be created via a [`new`] constructor. Each mutex has a type parameter
14/// which represents the data that it is protecting. The data can only be accessed
15/// through the RAII guards returned from [`lock`] and [`try_lock`], which
16/// guarantees that the data is only ever accessed when the mutex is locked.
17///
18/// # Poisoning
19///
20/// The mutexes in this module implement a strategy called "poisoning" where a
21/// mutex becomes poisoned if it recognizes that the thread holding it has
22/// panicked.
23///
24/// Once a mutex is poisoned, all other threads are unable to access the data by
25/// default as it is likely tainted (some invariant is not being upheld). For a
26/// mutex, this means that the [`lock`] and [`try_lock`] methods return a
27/// [`Result`] which indicates whether a mutex has been poisoned or not. Most
28/// usage of a mutex will simply [`unwrap()`] these results, propagating panics
29/// among threads to ensure that a possibly invalid invariant is not witnessed.
30///
31/// Poisoning is only advisory: the [`PoisonError`] type has an [`into_inner`]
32/// method which will return the guard that would have otherwise been returned
33/// on a successful lock. This allows access to the data, despite the lock being
34/// poisoned.
35///
36/// In addition, the panic detection is not ideal, so even unpoisoned mutexes
37/// need to be handled with care, since certain panics may have been skipped.
38/// Here is a non-exhaustive list of situations where this might occur:
39///
40/// - If a mutex is locked while a panic is underway, e.g. within a [`Drop`]
41/// implementation or a [panic hook], panicking for the second time while the
42/// lock is held will leave the mutex unpoisoned. Note that while double panic
43/// usually aborts the program, [`catch_unwind`] can prevent this.
44///
45/// - Locking and unlocking the mutex across different panic contexts, e.g. by
46/// storing the guard to a [`Cell`] within [`Drop::drop`] and accessing it
47/// outside, or vice versa, can affect poisoning status in an unexpected way.
48///
49/// - Foreign exceptions do not currently trigger poisoning even in absence of
50/// other panics.
51///
52/// While this rarely happens in realistic code, `unsafe` code cannot rely on
53/// poisoning for soundness, since the behavior of poisoning can depend on
54/// outside context. Here's an example of **incorrect** use of poisoning:
55///
56/// ```rust
57/// use std::sync::Mutex;
58///
59/// struct MutexBox<T> {
60/// data: Mutex<*mut T>,
61/// }
62///
63/// impl<T> MutexBox<T> {
64/// pub fn new(value: T) -> Self {
65/// Self {
66/// data: Mutex::new(Box::into_raw(Box::new(value))),
67/// }
68/// }
69///
70/// pub fn replace_with(&self, f: impl FnOnce(T) -> T) {
71/// let ptr = self.data.lock().expect("poisoned");
72/// // While `f` is running, the data is moved out of `*ptr`. If `f`
73/// // panics, `*ptr` keeps pointing at a dropped value. The intention
74/// // is that this will poison the mutex, so the following calls to
75/// // `replace_with` will panic without reading `*ptr`. But since
76/// // poisoning is not guaranteed to occur if this is run from a panic
77/// // hook, this can lead to use-after-free.
78/// unsafe {
79/// (*ptr).write(f((*ptr).read()));
80/// }
81/// }
82/// }
83/// ```
84///
85/// [`new`]: Self::new
86/// [`lock`]: Self::lock
87/// [`try_lock`]: Self::try_lock
88/// [`unwrap()`]: Result::unwrap
89/// [`PoisonError`]: super::PoisonError
90/// [`into_inner`]: super::PoisonError::into_inner
91/// [panic hook]: crate::panic::set_hook
92/// [`catch_unwind`]: crate::panic::catch_unwind
93/// [`Cell`]: crate::cell::Cell
94///
95/// # Examples
96///
97/// ```
98/// use std::sync::{Arc, Mutex};
99/// use std::thread;
100/// use std::sync::mpsc::channel;
101///
102/// const N: usize = 10;
103///
104/// // Spawn a few threads to increment a shared variable (non-atomically), and
105/// // let the main thread know once all increments are done.
106/// //
107/// // Here we're using an Arc to share memory among threads, and the data inside
108/// // the Arc is protected with a mutex.
109/// let data = Arc::new(Mutex::new(0));
110///
111/// let (tx, rx) = channel();
112/// for _ in 0..N {
113/// let (data, tx) = (Arc::clone(&data), tx.clone());
114/// thread::spawn(move || {
115/// // The shared state can only be accessed once the lock is held.
116/// // Our non-atomic increment is safe because we're the only thread
117/// // which can access the shared state when the lock is held.
118/// //
119/// // We unwrap() the return value to assert that we are not expecting
120/// // threads to ever fail while holding the lock.
121/// let mut data = data.lock().unwrap();
122/// *data += 1;
123/// if *data == N {
124/// tx.send(()).unwrap();
125/// }
126/// // the lock is unlocked here when `data` goes out of scope.
127/// });
128/// }
129///
130/// rx.recv().unwrap();
131/// ```
132///
133/// To recover from a poisoned mutex:
134///
135/// ```
136/// use std::sync::{Arc, Mutex};
137/// use std::thread;
138///
139/// let lock = Arc::new(Mutex::new(0_u32));
140/// let lock2 = Arc::clone(&lock);
141///
142/// let _ = thread::spawn(move || -> () {
143/// // This thread will acquire the mutex first, unwrapping the result of
144/// // `lock` because the lock has not been poisoned.
145/// let _guard = lock2.lock().unwrap();
146///
147/// // This panic while holding the lock (`_guard` is in scope) will poison
148/// // the mutex.
149/// panic!();
150/// }).join();
151///
152/// // The lock is poisoned by this point, but the returned result can be
153/// // pattern matched on to return the underlying guard on both branches.
154/// let mut guard = match lock.lock() {
155/// Ok(guard) => guard,
156/// Err(poisoned) => poisoned.into_inner(),
157/// };
158///
159/// *guard += 1;
160/// ```
161///
162/// To unlock a mutex guard sooner than the end of the enclosing scope,
163/// either create an inner scope or drop the guard manually.
164///
165/// ```
166/// use std::sync::{Arc, Mutex};
167/// use std::thread;
168///
169/// const N: usize = 3;
170///
171/// let data_mutex = Arc::new(Mutex::new(vec![1, 2, 3, 4]));
172/// let res_mutex = Arc::new(Mutex::new(0));
173///
174/// let mut threads = Vec::with_capacity(N);
175/// (0..N).for_each(|_| {
176/// let data_mutex_clone = Arc::clone(&data_mutex);
177/// let res_mutex_clone = Arc::clone(&res_mutex);
178///
179/// threads.push(thread::spawn(move || {
180/// // Here we use a block to limit the lifetime of the lock guard.
181/// let result = {
182/// let mut data = data_mutex_clone.lock().unwrap();
183/// // This is the result of some important and long-ish work.
184/// let result = data.iter().fold(0, |acc, x| acc + x * 2);
185/// data.push(result);
186/// result
187/// // The mutex guard gets dropped here, together with any other values
188/// // created in the critical section.
189/// };
190/// // The guard created here is a temporary dropped at the end of the statement, i.e.
191/// // the lock would not remain being held even if the thread did some additional work.
192/// *res_mutex_clone.lock().unwrap() += result;
193/// }));
194/// });
195///
196/// let mut data = data_mutex.lock().unwrap();
197/// // This is the result of some important and long-ish work.
198/// let result = data.iter().fold(0, |acc, x| acc + x * 2);
199/// data.push(result);
200/// // We drop the `data` explicitly because it's not necessary anymore and the
201/// // thread still has work to do. This allows other threads to start working on
202/// // the data immediately, without waiting for the rest of the unrelated work
203/// // to be done here.
204/// //
205/// // It's even more important here than in the threads because we `.join` the
206/// // threads after that. If we had not dropped the mutex guard, a thread could
207/// // be waiting forever for it, causing a deadlock.
208/// // As in the threads, a block could have been used instead of calling the
209/// // `drop` function.
210/// drop(data);
211/// // Here the mutex guard is not assigned to a variable and so, even if the
212/// // scope does not end after this line, the mutex is still released: there is
213/// // no deadlock.
214/// *res_mutex.lock().unwrap() += result;
215///
216/// threads.into_iter().for_each(|thread| {
217/// thread
218/// .join()
219/// .expect("The thread creating or execution failed !")
220/// });
221///
222/// assert_eq!(*res_mutex.lock().unwrap(), 800);
223/// ```
224///
225#[stable(feature = "rust1", since = "1.0.0")]
226#[cfg_attr(not(test), rustc_diagnostic_item = "Mutex")]
227pub struct Mutex<T: ?Sized> {
228 inner: sys::Mutex,
229 poison: poison::Flag,
230 data: UnsafeCell<T>,
231}
232
233/// `T` must be `Send` for a [`Mutex`] to be `Send` because it is possible to acquire
234/// the owned `T` from the `Mutex` via [`into_inner`].
235///
236/// [`into_inner`]: Mutex::into_inner
237#[stable(feature = "rust1", since = "1.0.0")]
238unsafe impl<T: ?Sized + Send> Send for Mutex<T> {}
239
240/// `T` must be `Send` for [`Mutex`] to be `Sync`.
241/// This ensures that the protected data can be accessed safely from multiple threads
242/// without causing data races or other unsafe behavior.
243///
244/// [`Mutex<T>`] provides mutable access to `T` to one thread at a time. However, it's essential
245/// for `T` to be `Send` because it's not safe for non-`Send` structures to be accessed in
246/// this manner. For instance, consider [`Rc`], a non-atomic reference counted smart pointer,
247/// which is not `Send`. With `Rc`, we can have multiple copies pointing to the same heap
248/// allocation with a non-atomic reference count. If we were to use `Mutex<Rc<_>>`, it would
249/// only protect one instance of `Rc` from shared access, leaving other copies vulnerable
250/// to potential data races.
251///
252/// Also note that it is not necessary for `T` to be `Sync` as `&T` is only made available
253/// to one thread at a time if `T` is not `Sync`.
254///
255/// [`Rc`]: crate::rc::Rc
256#[stable(feature = "rust1", since = "1.0.0")]
257unsafe impl<T: ?Sized + Send> Sync for Mutex<T> {}
258
259/// An RAII implementation of a "scoped lock" of a mutex. When this structure is
260/// dropped (falls out of scope), the lock will be unlocked.
261///
262/// The data protected by the mutex can be accessed through this guard via its
263/// [`Deref`] and [`DerefMut`] implementations.
264///
265/// This structure is created by the [`lock`] and [`try_lock`] methods on
266/// [`Mutex`].
267///
268/// [`lock`]: Mutex::lock
269/// [`try_lock`]: Mutex::try_lock
270#[must_use = "if unused the Mutex will immediately unlock"]
271#[must_not_suspend = "holding a MutexGuard across suspend \
272 points can cause deadlocks, delays, \
273 and cause Futures to not implement `Send`"]
274#[stable(feature = "rust1", since = "1.0.0")]
275#[clippy::has_significant_drop]
276#[cfg_attr(not(test), rustc_diagnostic_item = "MutexGuard")]
277pub struct MutexGuard<'a, T: ?Sized + 'a> {
278 lock: &'a Mutex<T>,
279 poison: poison::Guard,
280}
281
282/// A [`MutexGuard`] is not `Send` to maximize platform portablity.
283///
284/// On platforms that use POSIX threads (commonly referred to as pthreads) there is a requirement to
285/// release mutex locks on the same thread they were acquired.
286/// For this reason, [`MutexGuard`] must not implement `Send` to prevent it being dropped from
287/// another thread.
288#[stable(feature = "rust1", since = "1.0.0")]
289impl<T: ?Sized> !Send for MutexGuard<'_, T> {}
290
291/// `T` must be `Sync` for a [`MutexGuard<T>`] to be `Sync`
292/// because it is possible to get a `&T` from `&MutexGuard` (via `Deref`).
293#[stable(feature = "mutexguard", since = "1.19.0")]
294unsafe impl<T: ?Sized + Sync> Sync for MutexGuard<'_, T> {}
295
296/// An RAII mutex guard returned by `MutexGuard::map`, which can point to a
297/// subfield of the protected data. When this structure is dropped (falls out
298/// of scope), the lock will be unlocked.
299///
300/// The main difference between `MappedMutexGuard` and [`MutexGuard`] is that the
301/// former cannot be used with [`Condvar`], since that
302/// could introduce soundness issues if the locked object is modified by another
303/// thread while the `Mutex` is unlocked.
304///
305/// The data protected by the mutex can be accessed through this guard via its
306/// [`Deref`] and [`DerefMut`] implementations.
307///
308/// This structure is created by the [`map`] and [`filter_map`] methods on
309/// [`MutexGuard`].
310///
311/// [`map`]: MutexGuard::map
312/// [`filter_map`]: MutexGuard::filter_map
313/// [`Condvar`]: crate::sync::Condvar
314#[must_use = "if unused the Mutex will immediately unlock"]
315#[must_not_suspend = "holding a MappedMutexGuard across suspend \
316 points can cause deadlocks, delays, \
317 and cause Futures to not implement `Send`"]
318#[unstable(feature = "mapped_lock_guards", issue = "117108")]
319#[clippy::has_significant_drop]
320pub struct MappedMutexGuard<'a, T: ?Sized + 'a> {
321 // NB: we use a pointer instead of `&'a mut T` to avoid `noalias` violations, because a
322 // `MappedMutexGuard` argument doesn't hold uniqueness for its whole scope, only until it drops.
323 // `NonNull` is covariant over `T`, so we add a `PhantomData<&'a mut T>` field
324 // below for the correct variance over `T` (invariance).
325 data: NonNull<T>,
326 inner: &'a sys::Mutex,
327 poison_flag: &'a poison::Flag,
328 poison: poison::Guard,
329 _variance: PhantomData<&'a mut T>,
330}
331
332#[unstable(feature = "mapped_lock_guards", issue = "117108")]
333impl<T: ?Sized> !Send for MappedMutexGuard<'_, T> {}
334#[unstable(feature = "mapped_lock_guards", issue = "117108")]
335unsafe impl<T: ?Sized + Sync> Sync for MappedMutexGuard<'_, T> {}
336
337impl<T> Mutex<T> {
338 /// Creates a new mutex in an unlocked state ready for use.
339 ///
340 /// # Examples
341 ///
342 /// ```
343 /// use std::sync::Mutex;
344 ///
345 /// let mutex = Mutex::new(0);
346 /// ```
347 #[stable(feature = "rust1", since = "1.0.0")]
348 #[rustc_const_stable(feature = "const_locks", since = "1.63.0")]
349 #[inline]
350 pub const fn new(t: T) -> Mutex<T> {
351 Mutex { inner: sys::Mutex::new(), poison: poison::Flag::new(), data: UnsafeCell::new(t) }
352 }
353
354 /// Returns the contained value by cloning it.
355 ///
356 /// # Errors
357 ///
358 /// If another user of this mutex panicked while holding the mutex, then
359 /// this call will return an error instead.
360 ///
361 /// # Examples
362 ///
363 /// ```
364 /// #![feature(lock_value_accessors)]
365 ///
366 /// use std::sync::Mutex;
367 ///
368 /// let mut mutex = Mutex::new(7);
369 ///
370 /// assert_eq!(mutex.get_cloned().unwrap(), 7);
371 /// ```
372 #[unstable(feature = "lock_value_accessors", issue = "133407")]
373 pub fn get_cloned(&self) -> Result<T, PoisonError<()>>
374 where
375 T: Clone,
376 {
377 match self.lock() {
378 Ok(guard) => Ok((*guard).clone()),
379 Err(_) => Err(PoisonError::new(())),
380 }
381 }
382
383 /// Sets the contained value.
384 ///
385 /// # Errors
386 ///
387 /// If another user of this mutex panicked while holding the mutex, then
388 /// this call will return an error containing the provided `value` instead.
389 ///
390 /// # Examples
391 ///
392 /// ```
393 /// #![feature(lock_value_accessors)]
394 ///
395 /// use std::sync::Mutex;
396 ///
397 /// let mut mutex = Mutex::new(7);
398 ///
399 /// assert_eq!(mutex.get_cloned().unwrap(), 7);
400 /// mutex.set(11).unwrap();
401 /// assert_eq!(mutex.get_cloned().unwrap(), 11);
402 /// ```
403 #[unstable(feature = "lock_value_accessors", issue = "133407")]
404 pub fn set(&self, value: T) -> Result<(), PoisonError<T>> {
405 if mem::needs_drop::<T>() {
406 // If the contained value has non-trivial destructor, we
407 // call that destructor after the lock being released.
408 self.replace(value).map(drop)
409 } else {
410 match self.lock() {
411 Ok(mut guard) => {
412 *guard = value;
413
414 Ok(())
415 }
416 Err(_) => Err(PoisonError::new(value)),
417 }
418 }
419 }
420
421 /// Replaces the contained value with `value`, and returns the old contained value.
422 ///
423 /// # Errors
424 ///
425 /// If another user of this mutex panicked while holding the mutex, then
426 /// this call will return an error containing the provided `value` instead.
427 ///
428 /// # Examples
429 ///
430 /// ```
431 /// #![feature(lock_value_accessors)]
432 ///
433 /// use std::sync::Mutex;
434 ///
435 /// let mut mutex = Mutex::new(7);
436 ///
437 /// assert_eq!(mutex.replace(11).unwrap(), 7);
438 /// assert_eq!(mutex.get_cloned().unwrap(), 11);
439 /// ```
440 #[unstable(feature = "lock_value_accessors", issue = "133407")]
441 pub fn replace(&self, value: T) -> LockResult<T> {
442 match self.lock() {
443 Ok(mut guard) => Ok(mem::replace(&mut *guard, value)),
444 Err(_) => Err(PoisonError::new(value)),
445 }
446 }
447}
448
449impl<T: ?Sized> Mutex<T> {
450 /// Acquires a mutex, blocking the current thread until it is able to do so.
451 ///
452 /// This function will block the local thread until it is available to acquire
453 /// the mutex. Upon returning, the thread is the only thread with the lock
454 /// held. An RAII guard is returned to allow scoped unlock of the lock. When
455 /// the guard goes out of scope, the mutex will be unlocked.
456 ///
457 /// The exact behavior on locking a mutex in the thread which already holds
458 /// the lock is left unspecified. However, this function will not return on
459 /// the second call (it might panic or deadlock, for example).
460 ///
461 /// # Errors
462 ///
463 /// If another user of this mutex panicked while holding the mutex, then
464 /// this call will return an error once the mutex is acquired. The acquired
465 /// mutex guard will be contained in the returned error.
466 ///
467 /// # Panics
468 ///
469 /// This function might panic when called if the lock is already held by
470 /// the current thread.
471 ///
472 /// # Examples
473 ///
474 /// ```
475 /// use std::sync::{Arc, Mutex};
476 /// use std::thread;
477 ///
478 /// let mutex = Arc::new(Mutex::new(0));
479 /// let c_mutex = Arc::clone(&mutex);
480 ///
481 /// thread::spawn(move || {
482 /// *c_mutex.lock().unwrap() = 10;
483 /// }).join().expect("thread::spawn failed");
484 /// assert_eq!(*mutex.lock().unwrap(), 10);
485 /// ```
486 #[stable(feature = "rust1", since = "1.0.0")]
487 pub fn lock(&self) -> LockResult<MutexGuard<'_, T>> {
488 unsafe {
489 self.inner.lock();
490 MutexGuard::new(self)
491 }
492 }
493
494 /// Attempts to acquire this lock.
495 ///
496 /// If the lock could not be acquired at this time, then [`Err`] is returned.
497 /// Otherwise, an RAII guard is returned. The lock will be unlocked when the
498 /// guard is dropped.
499 ///
500 /// This function does not block.
501 ///
502 /// # Errors
503 ///
504 /// If another user of this mutex panicked while holding the mutex, then
505 /// this call will return the [`Poisoned`] error if the mutex would
506 /// otherwise be acquired. An acquired lock guard will be contained
507 /// in the returned error.
508 ///
509 /// If the mutex could not be acquired because it is already locked, then
510 /// this call will return the [`WouldBlock`] error.
511 ///
512 /// [`Poisoned`]: TryLockError::Poisoned
513 /// [`WouldBlock`]: TryLockError::WouldBlock
514 ///
515 /// # Examples
516 ///
517 /// ```
518 /// use std::sync::{Arc, Mutex};
519 /// use std::thread;
520 ///
521 /// let mutex = Arc::new(Mutex::new(0));
522 /// let c_mutex = Arc::clone(&mutex);
523 ///
524 /// thread::spawn(move || {
525 /// let mut lock = c_mutex.try_lock();
526 /// if let Ok(ref mut mutex) = lock {
527 /// **mutex = 10;
528 /// } else {
529 /// println!("try_lock failed");
530 /// }
531 /// }).join().expect("thread::spawn failed");
532 /// assert_eq!(*mutex.lock().unwrap(), 10);
533 /// ```
534 #[stable(feature = "rust1", since = "1.0.0")]
535 pub fn try_lock(&self) -> TryLockResult<MutexGuard<'_, T>> {
536 unsafe {
537 if self.inner.try_lock() {
538 Ok(MutexGuard::new(self)?)
539 } else {
540 Err(TryLockError::WouldBlock)
541 }
542 }
543 }
544
545 /// Determines whether the mutex is poisoned.
546 ///
547 /// If another thread is active, the mutex can still become poisoned at any
548 /// time. You should not trust a `false` value for program correctness
549 /// without additional synchronization.
550 ///
551 /// # Examples
552 ///
553 /// ```
554 /// use std::sync::{Arc, Mutex};
555 /// use std::thread;
556 ///
557 /// let mutex = Arc::new(Mutex::new(0));
558 /// let c_mutex = Arc::clone(&mutex);
559 ///
560 /// let _ = thread::spawn(move || {
561 /// let _lock = c_mutex.lock().unwrap();
562 /// panic!(); // the mutex gets poisoned
563 /// }).join();
564 /// assert_eq!(mutex.is_poisoned(), true);
565 /// ```
566 #[inline]
567 #[stable(feature = "sync_poison", since = "1.2.0")]
568 pub fn is_poisoned(&self) -> bool {
569 self.poison.get()
570 }
571
572 /// Clear the poisoned state from a mutex.
573 ///
574 /// If the mutex is poisoned, it will remain poisoned until this function is called. This
575 /// allows recovering from a poisoned state and marking that it has recovered. For example, if
576 /// the value is overwritten by a known-good value, then the mutex can be marked as
577 /// un-poisoned. Or possibly, the value could be inspected to determine if it is in a
578 /// consistent state, and if so the poison is removed.
579 ///
580 /// # Examples
581 ///
582 /// ```
583 /// use std::sync::{Arc, Mutex};
584 /// use std::thread;
585 ///
586 /// let mutex = Arc::new(Mutex::new(0));
587 /// let c_mutex = Arc::clone(&mutex);
588 ///
589 /// let _ = thread::spawn(move || {
590 /// let _lock = c_mutex.lock().unwrap();
591 /// panic!(); // the mutex gets poisoned
592 /// }).join();
593 ///
594 /// assert_eq!(mutex.is_poisoned(), true);
595 /// let x = mutex.lock().unwrap_or_else(|mut e| {
596 /// **e.get_mut() = 1;
597 /// mutex.clear_poison();
598 /// e.into_inner()
599 /// });
600 /// assert_eq!(mutex.is_poisoned(), false);
601 /// assert_eq!(*x, 1);
602 /// ```
603 #[inline]
604 #[stable(feature = "mutex_unpoison", since = "1.77.0")]
605 pub fn clear_poison(&self) {
606 self.poison.clear();
607 }
608
609 /// Consumes this mutex, returning the underlying data.
610 ///
611 /// # Errors
612 ///
613 /// If another user of this mutex panicked while holding the mutex, then
614 /// this call will return an error containing the underlying data
615 /// instead.
616 ///
617 /// # Examples
618 ///
619 /// ```
620 /// use std::sync::Mutex;
621 ///
622 /// let mutex = Mutex::new(0);
623 /// assert_eq!(mutex.into_inner().unwrap(), 0);
624 /// ```
625 #[stable(feature = "mutex_into_inner", since = "1.6.0")]
626 pub fn into_inner(self) -> LockResult<T>
627 where
628 T: Sized,
629 {
630 let data = self.data.into_inner();
631 poison::map_result(self.poison.borrow(), |()| data)
632 }
633
634 /// Returns a mutable reference to the underlying data.
635 ///
636 /// Since this call borrows the `Mutex` mutably, no actual locking needs to
637 /// take place -- the mutable borrow statically guarantees no new locks can be acquired
638 /// while this reference exists. Note that this method does not clear any previous abandoned locks
639 /// (e.g., via [`forget()`] on a [`MutexGuard`]).
640 ///
641 /// # Errors
642 ///
643 /// If another user of this mutex panicked while holding the mutex, then
644 /// this call will return an error containing a mutable reference to the
645 /// underlying data instead.
646 ///
647 /// # Examples
648 ///
649 /// ```
650 /// use std::sync::Mutex;
651 ///
652 /// let mut mutex = Mutex::new(0);
653 /// *mutex.get_mut().unwrap() = 10;
654 /// assert_eq!(*mutex.lock().unwrap(), 10);
655 /// ```
656 ///
657 /// [`forget()`]: mem::forget
658 #[stable(feature = "mutex_get_mut", since = "1.6.0")]
659 pub fn get_mut(&mut self) -> LockResult<&mut T> {
660 let data = self.data.get_mut();
661 poison::map_result(self.poison.borrow(), |()| data)
662 }
663
664 /// Returns a raw pointer to the underlying data.
665 ///
666 /// The returned pointer is always non-null and properly aligned, but it is
667 /// the user's responsibility to ensure that any reads and writes through it
668 /// are properly synchronized to avoid data races, and that it is not read
669 /// or written through after the mutex is dropped.
670 #[unstable(feature = "mutex_data_ptr", issue = "140368")]
671 pub fn data_ptr(&self) -> *mut T {
672 self.data.get()
673 }
674}
675
676#[stable(feature = "mutex_from", since = "1.24.0")]
677impl<T> From<T> for Mutex<T> {
678 /// Creates a new mutex in an unlocked state ready for use.
679 /// This is equivalent to [`Mutex::new`].
680 fn from(t: T) -> Self {
681 Mutex::new(t)
682 }
683}
684
685#[stable(feature = "mutex_default", since = "1.10.0")]
686impl<T: ?Sized + Default> Default for Mutex<T> {
687 /// Creates a `Mutex<T>`, with the `Default` value for T.
688 fn default() -> Mutex<T> {
689 Mutex::new(Default::default())
690 }
691}
692
693#[stable(feature = "rust1", since = "1.0.0")]
694impl<T: ?Sized + fmt::Debug> fmt::Debug for Mutex<T> {
695 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
696 let mut d = f.debug_struct("Mutex");
697 match self.try_lock() {
698 Ok(guard) => {
699 d.field("data", &&*guard);
700 }
701 Err(TryLockError::Poisoned(err)) => {
702 d.field("data", &&**err.get_ref());
703 }
704 Err(TryLockError::WouldBlock) => {
705 d.field("data", &"<locked>");
706 }
707 }
708 d.field("poisoned", &self.poison.get());
709 d.finish_non_exhaustive()
710 }
711}
712
713impl<'mutex, T: ?Sized> MutexGuard<'mutex, T> {
714 unsafe fn new(lock: &'mutex Mutex<T>) -> LockResult<MutexGuard<'mutex, T>> {
715 poison::map_result(lock.poison.guard(), |guard| MutexGuard { lock, poison: guard })
716 }
717}
718
719#[stable(feature = "rust1", since = "1.0.0")]
720impl<T: ?Sized> Deref for MutexGuard<'_, T> {
721 type Target = T;
722
723 fn deref(&self) -> &T {
724 unsafe { &*self.lock.data.get() }
725 }
726}
727
728#[stable(feature = "rust1", since = "1.0.0")]
729impl<T: ?Sized> DerefMut for MutexGuard<'_, T> {
730 fn deref_mut(&mut self) -> &mut T {
731 unsafe { &mut *self.lock.data.get() }
732 }
733}
734
735#[stable(feature = "rust1", since = "1.0.0")]
736impl<T: ?Sized> Drop for MutexGuard<'_, T> {
737 #[inline]
738 fn drop(&mut self) {
739 unsafe {
740 self.lock.poison.done(&self.poison);
741 self.lock.inner.unlock();
742 }
743 }
744}
745
746#[stable(feature = "std_debug", since = "1.16.0")]
747impl<T: ?Sized + fmt::Debug> fmt::Debug for MutexGuard<'_, T> {
748 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
749 fmt::Debug::fmt(&**self, f)
750 }
751}
752
753#[stable(feature = "std_guard_impls", since = "1.20.0")]
754impl<T: ?Sized + fmt::Display> fmt::Display for MutexGuard<'_, T> {
755 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
756 (**self).fmt(f)
757 }
758}
759
760pub fn guard_lock<'a, T: ?Sized>(guard: &MutexGuard<'a, T>) -> &'a sys::Mutex {
761 &guard.lock.inner
762}
763
764pub fn guard_poison<'a, T: ?Sized>(guard: &MutexGuard<'a, T>) -> &'a poison::Flag {
765 &guard.lock.poison
766}
767
768impl<'a, T: ?Sized> MutexGuard<'a, T> {
769 /// Makes a [`MappedMutexGuard`] for a component of the borrowed data, e.g.
770 /// an enum variant.
771 ///
772 /// The `Mutex` is already locked, so this cannot fail.
773 ///
774 /// This is an associated function that needs to be used as
775 /// `MutexGuard::map(...)`. A method would interfere with methods of the
776 /// same name on the contents of the `MutexGuard` used through `Deref`.
777 #[unstable(feature = "mapped_lock_guards", issue = "117108")]
778 pub fn map<U, F>(orig: Self, f: F) -> MappedMutexGuard<'a, U>
779 where
780 F: FnOnce(&mut T) -> &mut U,
781 U: ?Sized,
782 {
783 // SAFETY: the conditions of `MutexGuard::new` were satisfied when the original guard
784 // was created, and have been upheld throughout `map` and/or `filter_map`.
785 // The signature of the closure guarantees that it will not "leak" the lifetime of the reference
786 // passed to it. If the closure panics, the guard will be dropped.
787 let data = NonNull::from(f(unsafe { &mut *orig.lock.data.get() }));
788 let orig = ManuallyDrop::new(orig);
789 MappedMutexGuard {
790 data,
791 inner: &orig.lock.inner,
792 poison_flag: &orig.lock.poison,
793 poison: orig.poison.clone(),
794 _variance: PhantomData,
795 }
796 }
797
798 /// Makes a [`MappedMutexGuard`] for a component of the borrowed data. The
799 /// original guard is returned as an `Err(...)` if the closure returns
800 /// `None`.
801 ///
802 /// The `Mutex` is already locked, so this cannot fail.
803 ///
804 /// This is an associated function that needs to be used as
805 /// `MutexGuard::filter_map(...)`. A method would interfere with methods of the
806 /// same name on the contents of the `MutexGuard` used through `Deref`.
807 #[unstable(feature = "mapped_lock_guards", issue = "117108")]
808 pub fn filter_map<U, F>(orig: Self, f: F) -> Result<MappedMutexGuard<'a, U>, Self>
809 where
810 F: FnOnce(&mut T) -> Option<&mut U>,
811 U: ?Sized,
812 {
813 // SAFETY: the conditions of `MutexGuard::new` were satisfied when the original guard
814 // was created, and have been upheld throughout `map` and/or `filter_map`.
815 // The signature of the closure guarantees that it will not "leak" the lifetime of the reference
816 // passed to it. If the closure panics, the guard will be dropped.
817 match f(unsafe { &mut *orig.lock.data.get() }) {
818 Some(data) => {
819 let data = NonNull::from(data);
820 let orig = ManuallyDrop::new(orig);
821 Ok(MappedMutexGuard {
822 data,
823 inner: &orig.lock.inner,
824 poison_flag: &orig.lock.poison,
825 poison: orig.poison.clone(),
826 _variance: PhantomData,
827 })
828 }
829 None => Err(orig),
830 }
831 }
832}
833
834#[unstable(feature = "mapped_lock_guards", issue = "117108")]
835impl<T: ?Sized> Deref for MappedMutexGuard<'_, T> {
836 type Target = T;
837
838 fn deref(&self) -> &T {
839 unsafe { self.data.as_ref() }
840 }
841}
842
843#[unstable(feature = "mapped_lock_guards", issue = "117108")]
844impl<T: ?Sized> DerefMut for MappedMutexGuard<'_, T> {
845 fn deref_mut(&mut self) -> &mut T {
846 unsafe { self.data.as_mut() }
847 }
848}
849
850#[unstable(feature = "mapped_lock_guards", issue = "117108")]
851impl<T: ?Sized> Drop for MappedMutexGuard<'_, T> {
852 #[inline]
853 fn drop(&mut self) {
854 unsafe {
855 self.poison_flag.done(&self.poison);
856 self.inner.unlock();
857 }
858 }
859}
860
861#[unstable(feature = "mapped_lock_guards", issue = "117108")]
862impl<T: ?Sized + fmt::Debug> fmt::Debug for MappedMutexGuard<'_, T> {
863 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
864 fmt::Debug::fmt(&**self, f)
865 }
866}
867
868#[unstable(feature = "mapped_lock_guards", issue = "117108")]
869impl<T: ?Sized + fmt::Display> fmt::Display for MappedMutexGuard<'_, T> {
870 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
871 (**self).fmt(f)
872 }
873}
874
875impl<'a, T: ?Sized> MappedMutexGuard<'a, T> {
876 /// Makes a [`MappedMutexGuard`] for a component of the borrowed data, e.g.
877 /// an enum variant.
878 ///
879 /// The `Mutex` is already locked, so this cannot fail.
880 ///
881 /// This is an associated function that needs to be used as
882 /// `MappedMutexGuard::map(...)`. A method would interfere with methods of the
883 /// same name on the contents of the `MutexGuard` used through `Deref`.
884 #[unstable(feature = "mapped_lock_guards", issue = "117108")]
885 pub fn map<U, F>(mut orig: Self, f: F) -> MappedMutexGuard<'a, U>
886 where
887 F: FnOnce(&mut T) -> &mut U,
888 U: ?Sized,
889 {
890 // SAFETY: the conditions of `MutexGuard::new` were satisfied when the original guard
891 // was created, and have been upheld throughout `map` and/or `filter_map`.
892 // The signature of the closure guarantees that it will not "leak" the lifetime of the reference
893 // passed to it. If the closure panics, the guard will be dropped.
894 let data = NonNull::from(f(unsafe { orig.data.as_mut() }));
895 let orig = ManuallyDrop::new(orig);
896 MappedMutexGuard {
897 data,
898 inner: orig.inner,
899 poison_flag: orig.poison_flag,
900 poison: orig.poison.clone(),
901 _variance: PhantomData,
902 }
903 }
904
905 /// Makes a [`MappedMutexGuard`] for a component of the borrowed data. The
906 /// original guard is returned as an `Err(...)` if the closure returns
907 /// `None`.
908 ///
909 /// The `Mutex` is already locked, so this cannot fail.
910 ///
911 /// This is an associated function that needs to be used as
912 /// `MappedMutexGuard::filter_map(...)`. A method would interfere with methods of the
913 /// same name on the contents of the `MutexGuard` used through `Deref`.
914 #[unstable(feature = "mapped_lock_guards", issue = "117108")]
915 pub fn filter_map<U, F>(mut orig: Self, f: F) -> Result<MappedMutexGuard<'a, U>, Self>
916 where
917 F: FnOnce(&mut T) -> Option<&mut U>,
918 U: ?Sized,
919 {
920 // SAFETY: the conditions of `MutexGuard::new` were satisfied when the original guard
921 // was created, and have been upheld throughout `map` and/or `filter_map`.
922 // The signature of the closure guarantees that it will not "leak" the lifetime of the reference
923 // passed to it. If the closure panics, the guard will be dropped.
924 match f(unsafe { orig.data.as_mut() }) {
925 Some(data) => {
926 let data = NonNull::from(data);
927 let orig = ManuallyDrop::new(orig);
928 Ok(MappedMutexGuard {
929 data,
930 inner: orig.inner,
931 poison_flag: orig.poison_flag,
932 poison: orig.poison.clone(),
933 _variance: PhantomData,
934 })
935 }
936 None => Err(orig),
937 }
938 }
939}