Lines Matching full:mutex
13 /// An asynchronous `Mutex`-like type.
15 /// This type acts similarly to [`std::sync::Mutex`], with two major
19 /// # Which kind of mutex should you use?
22 /// [`Mutex`][std] from the standard library in asynchronous code.
24 /// The feature that the async mutex offers over the blocking mutex is the
26 /// mutex more expensive than the blocking mutex, so the blocking mutex should
28 /// async mutex is to provide shared mutable access to IO resources such as a
29 /// database connection. If the value behind the mutex is just data, it's
30 /// usually appropriate to use a blocking mutex such as the one in the standard
33 /// Note that, although the compiler will not prevent the std `Mutex` from holding
38 /// A common pattern is to wrap the `Arc<Mutex<...>>` in a struct that provides
40 /// lock the mutex inside these methods. The [mini-redis] example provides an
47 /// [std]: std::sync::Mutex
54 /// use tokio::sync::Mutex;
59 /// let data1 = Arc::new(Mutex::new(0));
74 /// use tokio::sync::Mutex;
79 /// let count = Arc::new(Mutex::new(0));
101 /// 1. The mutex is wrapped in an [`Arc`] to allow it to be shared across
104 /// 3. Mutation of the data protected by the Mutex is done by de-referencing
107 /// Tokio's Mutex works in a simple FIFO (first in, first out) style where all
109 /// Mutex is "fair" and predictable in how it distributes the locks to inner
117 /// Note that in contrast to [`std::sync::Mutex`], this implementation does not
118 /// poison the mutex when a thread holding the [`MutexGuard`] panics. In such a
119 /// case, the mutex will be unlocked. If the panic is caught, this might leave
120 /// the data protected by the mutex in an inconsistent state.
122 /// [`Mutex`]: struct@Mutex
125 /// [`std::sync::Mutex`]: struct@std::sync::Mutex
127 /// [`lock`]: method@Mutex::lock
128 pub struct Mutex<T: ?Sized> { struct
135 /// A handle to a held `Mutex`. The guard can be held across any `.await` point argument
139 /// `T`. The guard internally borrows the `Mutex`, so the mutex will not be
144 #[must_use = "if unused the Mutex will immediately unlock"]
148 lock: &'a Mutex<T>,
151 /// An owned handle to a held `Mutex`.
153 /// This guard is only available from a `Mutex` that is wrapped in an [`Arc`]. It
154 /// is identical to `MutexGuard`, except that rather than borrowing the `Mutex`,
160 /// `Mutex`, so even if the lock goes away, the guard remains valid.
169 lock: Arc<Mutex<T>>,
172 /// A handle to a held `Mutex` that has had a function applied to it via [`MutexGuard::map`].
177 #[must_use = "if unused the Mutex will immediately unlock"]
185 // As long as T: Send, it's fine to send and share Mutex<T> between threads.
186 // If T was not Send, sending and sharing a Mutex<T> would be bad, since you can
187 // access T through Mutex<T>.
188 unsafe impl<T> Send for Mutex<T> where T: ?Sized + Send {} implementation
189 unsafe impl<T> Sync for Mutex<T> where T: ?Sized + Send {} implementation
195 /// Error returned from the [`Mutex::try_lock`], [`RwLock::try_read`] and
198 /// `Mutex::try_lock` operation will only fail if the mutex is already locked.
206 /// [`Mutex::try_lock`]: Mutex::try_lock
233 check_unpin::<Mutex<u32>>(); in bounds()
234 check_send_sync::<Mutex<u32>>(); in bounds()
237 let mutex = Mutex::new(1); in bounds() localVariable
238 check_send_sync_val(mutex.lock()); in bounds()
239 let arc_mutex = Arc::new(Mutex::new(1)); in bounds()
244 impl<T: ?Sized> Mutex<T> { impl
250 /// use tokio::sync::Mutex;
252 /// let lock = Mutex::new(5);
265 concrete_type = "Mutex", in new()
298 /// use tokio::sync::Mutex;
300 /// static LOCK: Mutex<i32> = Mutex::const_new(5);
316 /// Locks this mutex, causing the current task to yield until the lock has
329 /// use tokio::sync::Mutex;
333 /// let mutex = Mutex::new(1);
335 /// let mut n = mutex.lock().await;
344 "Mutex::lock", in lock()
368 /// Blockingly locks this `Mutex`. When the lock has been acquired, function returns a
372 /// need to use this mutex in asynchronous code as well as in synchronous code.
388 /// use tokio::sync::Mutex;
392 /// let mutex = Arc::new(Mutex::new(1));
393 /// let lock = mutex.lock().await;
395 /// let mutex1 = Arc::clone(&mutex);
410 /// let n = mutex.try_lock().unwrap();
421 /// Blockingly locks this `Mutex`. When the lock has been acquired, function returns an
424 /// This method is identical to [`Mutex::blocking_lock`], except that the returned
425 /// guard references the `Mutex` with an [`Arc`] rather than by borrowing
426 /// it. Therefore, the `Mutex` must be wrapped in an `Arc` to call this
428 /// the `Mutex` alive by holding an `Arc`.
444 /// use tokio::sync::Mutex;
448 /// let mutex = Arc::new(Mutex::new(1));
449 /// let lock = mutex.lock().await;
451 /// let mutex1 = Arc::clone(&mutex);
466 /// let n = mutex.try_lock().unwrap();
477 /// Locks this mutex, causing the current task to yield until the lock has
481 /// This method is identical to [`Mutex::lock`], except that the returned
482 /// guard references the `Mutex` with an [`Arc`] rather than by borrowing
483 /// it. Therefore, the `Mutex` must be wrapped in an `Arc` to call this
485 /// the `Mutex` alive by holding an `Arc`.
496 /// use tokio::sync::Mutex;
501 /// let mutex = Arc::new(Mutex::new(1));
503 /// let mut n = mutex.clone().lock_owned().await;
514 "Mutex::lock_owned", in lock_owned()
556 /// use tokio::sync::Mutex;
559 /// let mutex = Mutex::new(1);
561 /// let n = mutex.try_lock()?;
589 /// Since this call borrows the `Mutex` mutably, no actual locking needs to
595 /// use tokio::sync::Mutex;
598 /// let mut mutex = Mutex::new(1);
600 /// let n = mutex.get_mut();
614 /// This method is identical to [`Mutex::try_lock`], except that the
615 /// returned guard references the `Mutex` with an [`Arc`] rather than by
616 /// borrowing it. Therefore, the `Mutex` must be wrapped in an `Arc` to call
618 /// keeps the `Mutex` alive by holding an `Arc`.
625 /// use tokio::sync::Mutex;
629 /// let mutex = Arc::new(Mutex::new(1));
631 /// let n = mutex.clone().try_lock_owned()?;
659 /// Consumes the mutex, returning the underlying data.
663 /// use tokio::sync::Mutex;
667 /// let mutex = Mutex::new(1);
669 /// let n = mutex.into_inner();
681 impl<T> From<T> for Mutex<T> { implementation
687 impl<T> Default for Mutex<T> implementation
696 impl<T: ?Sized> std::fmt::Debug for Mutex<T> implementation
701 let mut d = f.debug_struct("Mutex"); in fmt()
715 /// This operation cannot fail as the [`MutexGuard`] passed in already locked the mutex.
723 /// use tokio::sync::{Mutex, MutexGuard};
730 /// let foo = Mutex::new(Foo(1));
761 /// This operation cannot fail as the [`MutexGuard`] passed in already locked the mutex.
769 /// use tokio::sync::{Mutex, MutexGuard};
776 /// let foo = Mutex::new(Foo(1));
808 /// Returns a reference to the original `Mutex`.
811 /// use tokio::sync::{Mutex, MutexGuard};
815 /// let mutex = MutexGuard::mutex(&guard);
817 /// let guard = mutex.lock().await;
824 /// # let mutex = Mutex::new(0u32);
825 /// # let guard = mutex.lock().await;
830 pub fn mutex(this: &Self) -> &'a Mutex<T> { in mutex() method
876 /// Returns a reference to the original `Arc<Mutex>`.
880 /// use tokio::sync::{Mutex, OwnedMutexGuard};
884 /// let mutex: Arc<Mutex<u32>> = OwnedMutexGuard::mutex(&guard).clone();
886 /// let guard = mutex.lock_owned().await;
893 /// # let mutex = Arc::new(Mutex::new(0u32));
894 /// # let guard = mutex.lock_owned().await;
899 pub fn mutex(this: &Self) -> &Arc<Mutex<T>> { in mutex() method
947 /// This operation cannot fail as the [`MappedMutexGuard`] passed in already locked the mutex.
971 /// This operation cannot fail as the [`MappedMutexGuard`] passed in already locked the mutex.