Lines Matching full:mutex
27 /// Basic operations for a mutex.
29 /// Types implementing this trait can be used by `Mutex` to form a safe and
30 /// fully-functioning mutex type.
34 /// Implementations of this trait must ensure that the mutex is actually
35 /// exclusive: a lock can't be acquired while the mutex is already locked.
37 /// Initial value for an unlocked mutex.
47 /// Acquires this mutex, blocking the current thread until it is able to do so.
50 /// Attempts to acquire this mutex without blocking. Returns `true`
54 /// Unlocks this mutex.
58 /// This method may only be called if the mutex is held in the current context, i.e. it must
67 /// Checks whether the mutex is currently locked.
88 /// Unlocks this mutex using a fair unlock protocol.
92 /// This method may only be called if the mutex is held in the current context, see
98 /// Temporarily yields the mutex to a waiting thread if there is one.
106 /// This method may only be called if the mutex is held in the current context, see
136 /// This mutex will block threads waiting for the lock to become available. The
137 /// mutex can also be statically initialized or created via a `new`
138 /// constructor. Each mutex has a type parameter which represents the data that
141 /// ever accessed when the mutex is locked.
142 pub struct Mutex<R, T: ?Sized> { struct
147 unsafe impl<R: RawMutex + Send, T: ?Sized + Send> Send for Mutex<R, T> {} implementation
148 unsafe impl<R: RawMutex + Sync, T: ?Sized + Send> Sync for Mutex<R, T> {} implementation
150 impl<R: RawMutex, T> Mutex<R, T> { impl
151 /// Creates a new mutex in an unlocked state ready for use.
154 pub const fn new(val: T) -> Mutex<R, T> { in new()
155 Mutex { in new()
161 /// Creates a new mutex in an unlocked state ready for use.
164 pub fn new(val: T) -> Mutex<R, T> { in new()
165 Mutex { in new()
171 /// Consumes this mutex, returning the underlying data.
178 impl<R, T> Mutex<R, T> { implementation
179 /// Creates a new mutex based on a pre-existing raw mutex.
181 /// This allows creating a mutex in a constant context on stable Rust.
183 pub const fn const_new(raw_mutex: R, val: T) -> Mutex<R, T> { in const_new()
184 Mutex { in const_new()
191 impl<R: RawMutex, T: ?Sized> Mutex<R, T> { impl
192 /// Creates a new `MutexGuard` without checking if the mutex is locked.
203 mutex: self, in make_guard_unchecked()
208 /// Acquires a mutex, blocking the current thread until it is able to do so.
211 /// the mutex. Upon returning, the thread is the only thread with the mutex
213 /// the guard goes out of scope, the mutex will be unlocked.
215 /// Attempts to lock a mutex in the thread which already holds the lock will
243 /// Since this call borrows the `Mutex` mutably, no actual locking needs to
250 /// Checks whether the mutex is currently locked.
256 /// Forcibly unlocks the mutex.
266 /// Behavior is undefined if a mutex is unlocked when not locked.
272 /// Returns the underlying raw mutex object.
275 /// `lock_api` to be able to call functions on the raw mutex.
279 /// This method is unsafe because it allows unlocking a mutex while
302 /// Creates a new `ArcMutexGuard` without checking if the mutex is locked.
314 mutex: self.clone(), in make_arc_guard_unchecked()
321 …/// This method is similar to the `lock` method; however, it requires the `Mutex` to be inside of …
322 /// and the resulting mutex guard has no lifetime requirements.
333 …/// This method is similar to the `try_lock` method; however, it requires the `Mutex` to be inside…
334 /// `Arc` and the resulting mutex guard has no lifetime requirements.
347 impl<R: RawMutexFair, T: ?Sized> Mutex<R, T> { implementation
348 /// Forcibly unlocks the mutex using a fair unlock procotol.
358 /// Behavior is undefined if a mutex is unlocked when not locked.
365 impl<R: RawMutexTimed, T: ?Sized> Mutex<R, T> { implementation
398 …/// This method is similar to the `try_lock_for` method; however, it requires the `Mutex` to be in…
399 /// `Arc` and the resulting mutex guard has no lifetime requirements.
413 …/// This method is similar to the `try_lock_until` method; however, it requires the `Mutex` to be …
414 /// an `Arc` and the resulting mutex guard has no lifetime requirements.
430 impl<R: RawMutex, T: ?Sized + Default> Default for Mutex<R, T> { implementation
432 fn default() -> Mutex<R, T> { in default()
433 Mutex::new(Default::default()) in default()
437 impl<R: RawMutex, T> From<T> for Mutex<R, T> { implementation
439 fn from(t: T) -> Mutex<R, T> { in from()
440 Mutex::new(t) in from()
444 impl<R: RawMutex, T: ?Sized + fmt::Debug> fmt::Debug for Mutex<R, T> { implementation
447 Some(guard) => f.debug_struct("Mutex").field("data", &&*guard).finish(), in fmt()
456 f.debug_struct("Mutex") in fmt()
466 impl<R, T> Serialize for Mutex<R, T> implementation
480 impl<'de, R, T> Deserialize<'de> for Mutex<R, T> implementation
489 Deserialize::deserialize(deserializer).map(Mutex::new) in deserialize()
493 /// An RAII implementation of a "scoped lock" of a mutex. When this structure is
496 /// The data protected by the mutex can be accessed through this guard via its
499 #[must_use = "if unused the Mutex will immediately unlock"]
501 mutex: &'a Mutex<R, T>, field
508 /// Returns a reference to the original `Mutex` object.
509 pub fn mutex(s: &Self) -> &'a Mutex<R, T> { in mutex() method
510 s.mutex in mutex()
516 /// in already locked the mutex.
526 let raw = &s.mutex.raw; in map()
527 let data = f(unsafe { &mut *s.mutex.data.get() }); in map()
540 /// in already locked the mutex.
550 let raw = &s.mutex.raw; in try_map()
551 let data = match f(unsafe { &mut *s.mutex.data.get() }) { in try_map()
563 /// Temporarily unlocks the mutex to execute the given function.
566 /// references to the data protected by the mutex.
574 s.mutex.raw.unlock(); in unlocked()
576 defer!(s.mutex.raw.lock()); in unlocked()
580 /// Leaks the mutex guard and returns a mutable reference to the data
581 /// protected by the mutex.
583 /// This will leave the `Mutex` in a locked state.
586 let r = unsafe { &mut *s.mutex.data.get() }; in leak()
593 /// Unlocks the mutex using a fair unlock protocol.
596 /// the mutex before another has the chance to acquire the lock, even if
597 /// that thread has been blocked on the mutex for a long time. This is the
599 /// context switch on every mutex unlock. This can result in one thread
600 /// acquiring a mutex many more times than other threads.
609 s.mutex.raw.unlock_fair(); in unlock_fair()
614 /// Temporarily unlocks the mutex to execute the given function.
616 /// The mutex is unlocked using a fair unlock protocol.
619 /// references to the data protected by the mutex.
627 s.mutex.raw.unlock_fair(); in unlocked_fair()
629 defer!(s.mutex.raw.lock()); in unlocked_fair()
633 /// Temporarily yields the mutex to a waiting thread if there is one.
642 s.mutex.raw.bump(); in bump()
651 unsafe { &*self.mutex.data.get() } in deref()
658 unsafe { &mut *self.mutex.data.get() } in deref_mut()
667 self.mutex.raw.unlock(); in drop()
687 /// An RAII mutex guard returned by the `Arc` locking operations on `Mutex`.
689 …is similar to the `MutexGuard` struct, except instead of using a reference to unlock the `Mutex` it
690 /// uses an `Arc<Mutex>`. This has several advantages, most notably that it has an `'static` lifeti…
693 #[must_use = "if unused the Mutex will immediately unlock"]
695 mutex: Arc<Mutex<R, T>>, field
712 /// Returns a reference to the `Mutex` this is guarding, contained in its `Arc`.
714 pub fn mutex(s: &Self) -> &Arc<Mutex<R, T>> { in mutex() method
715 &s.mutex in mutex()
718 /// Unlocks the mutex and returns the `Arc` that was held by the [`ArcMutexGuard`].
720 pub fn into_arc(s: Self) -> Arc<Mutex<R, T>> { in into_arc()
721 // Safety: Skip our Drop impl and manually unlock the mutex. in into_arc()
722 let arc = unsafe { ptr::read(&s.mutex) }; in into_arc()
730 /// Temporarily unlocks the mutex to execute the given function.
733 /// references to the data protected by the mutex.
741 s.mutex.raw.unlock(); in unlocked()
743 defer!(s.mutex.raw.lock()); in unlocked()
750 /// Unlocks the mutex using a fair unlock protocol.
757 s.mutex.raw.unlock_fair(); in unlock_fair()
762 unsafe { ptr::drop_in_place(&mut s.mutex) }; in unlock_fair()
765 /// Temporarily unlocks the mutex to execute the given function.
775 s.mutex.raw.unlock_fair(); in unlocked_fair()
777 defer!(s.mutex.raw.lock()); in unlocked_fair()
781 /// Temporarily yields the mutex to a waiting thread if there is one.
788 s.mutex.raw.bump(); in bump()
798 unsafe { &*self.mutex.data.get() } in deref()
806 unsafe { &mut *self.mutex.data.get() } in deref_mut()
816 self.mutex.raw.unlock(); in drop()
821 /// An RAII mutex guard returned by `MutexGuard::map`, which can point to a
829 #[must_use = "if unused the Mutex will immediately unlock"]
849 /// in already locked the mutex.
873 /// in already locked the mutex.
898 /// Unlocks the mutex using a fair unlock protocol.
901 /// the mutex before another has the chance to acquire the lock, even if
902 /// that thread has been blocked on the mutex for a long time. This is the
904 /// context switch on every mutex unlock. This can result in one thread
905 /// acquiring a mutex many more times than other threads.