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