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> {} argument
148 unsafe impl<R: RawMutex + Sync, T: ?Sized + Send> Sync for Mutex<R, T> {} implementation
150 impl<R: RawMutex, T> Mutex<R, T> { implementation
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
198 mutex: self, in guard()
203 /// Acquires a mutex, blocking the current thread until it is able to do so.
206 /// the mutex. Upon returning, the thread is the only thread with the mutex
208 /// the guard goes out of scope, the mutex will be unlocked.
210 /// Attempts to lock a mutex in the thread which already holds the lock will
238 /// Since this call borrows the `Mutex` mutably, no actual locking needs to
245 /// Checks whether the mutex is currently locked.
251 /// Forcibly unlocks the mutex.
261 /// Behavior is undefined if a mutex is unlocked when not locked.
267 /// Returns the underlying raw mutex object.
270 /// `lock_api` to be able to call functions on the raw mutex.
274 /// This method is unsafe because it allows unlocking a mutex while
304 mutex: self.clone(), in guard_arc()
311 …/// This method is similar to the `lock` method; however, it requires the `Mutex` to be inside of …
312 /// and the resulting mutex guard has no lifetime requirements.
323 …/// This method is similar to the `try_lock` method; however, it requires the `Mutex` to be inside…
324 /// `Arc` and the resulting mutex guard has no lifetime requirements.
337 impl<R: RawMutexFair, T: ?Sized> Mutex<R, T> { implementation
338 /// Forcibly unlocks the mutex using a fair unlock procotol.
348 /// Behavior is undefined if a mutex is unlocked when not locked.
355 impl<R: RawMutexTimed, T: ?Sized> Mutex<R, T> { implementation
388 …/// This method is similar to the `try_lock_for` method; however, it requires the `Mutex` to be in…
389 /// `Arc` and the resulting mutex guard has no lifetime requirements.
403 …/// This method is similar to the `try_lock_until` method; however, it requires the `Mutex` to be …
404 /// an `Arc` and the resulting mutex guard has no lifetime requirements.
420 impl<R: RawMutex, T: ?Sized + Default> Default for Mutex<R, T> { implementation
422 fn default() -> Mutex<R, T> { in default()
423 Mutex::new(Default::default()) in default()
427 impl<R: RawMutex, T> From<T> for Mutex<R, T> { implementation
429 fn from(t: T) -> Mutex<R, T> { in from()
430 Mutex::new(t) in from()
434 impl<R: RawMutex, T: ?Sized + fmt::Debug> fmt::Debug for Mutex<R, T> { implementation
437 Some(guard) => f.debug_struct("Mutex").field("data", &&*guard).finish(), in fmt()
446 f.debug_struct("Mutex") in fmt()
456 impl<R, T> Serialize for Mutex<R, T> implementation
470 impl<'de, R, T> Deserialize<'de> for Mutex<R, T> implementation
479 Deserialize::deserialize(deserializer).map(Mutex::new) in deserialize()
483 /// An RAII implementation of a "scoped lock" of a mutex. When this structure is
486 /// The data protected by the mutex can be accessed through this guard via its
488 #[must_use = "if unused the Mutex will immediately unlock"]
490 mutex: &'a Mutex<R, T>, field
497 /// Returns a reference to the original `Mutex` object.
498 pub fn mutex(s: &Self) -> &'a Mutex<R, T> { in mutex() method
499 s.mutex in mutex()
505 /// in already locked the mutex.
515 let raw = &s.mutex.raw; in map()
516 let data = f(unsafe { &mut *s.mutex.data.get() }); in map()
529 /// in already locked the mutex.
539 let raw = &s.mutex.raw; in try_map()
540 let data = match f(unsafe { &mut *s.mutex.data.get() }) { in try_map()
552 /// Temporarily unlocks the mutex to execute the given function.
555 /// references to the data protected by the mutex.
563 s.mutex.raw.unlock(); in unlocked()
565 defer!(s.mutex.raw.lock()); in unlocked()
569 /// Leaks the mutex guard and returns a mutable reference to the data
570 /// protected by the mutex.
572 /// This will leave the `Mutex` in a locked state.
575 let r = unsafe { &mut *s.mutex.data.get() }; in leak()
582 /// Unlocks the mutex using a fair unlock protocol.
585 /// the mutex before another has the chance to acquire the lock, even if
586 /// that thread has been blocked on the mutex for a long time. This is the
588 /// context switch on every mutex unlock. This can result in one thread
589 /// acquiring a mutex many more times than other threads.
598 s.mutex.raw.unlock_fair(); in unlock_fair()
603 /// Temporarily unlocks the mutex to execute the given function.
605 /// The mutex is unlocked using a fair unlock protocol.
608 /// references to the data protected by the mutex.
616 s.mutex.raw.unlock_fair(); in unlocked_fair()
618 defer!(s.mutex.raw.lock()); in unlocked_fair()
622 /// Temporarily yields the mutex to a waiting thread if there is one.
631 s.mutex.raw.bump(); in bump()
640 unsafe { &*self.mutex.data.get() } in deref()
647 unsafe { &mut *self.mutex.data.get() } in deref_mut()
656 self.mutex.raw.unlock(); in drop()
676 /// An RAII mutex guard returned by the `Arc` locking operations on `Mutex`.
678 …is similar to the `MutexGuard` struct, except instead of using a reference to unlock the `Mutex` it
679 /// uses an `Arc<Mutex>`. This has several advantages, most notably that it has an `'static` lifeti…
681 #[must_use = "if unused the Mutex will immediately unlock"]
683 mutex: Arc<Mutex<R, T>>, field
700 /// Returns a reference to the `Mutex` this is guarding, contained in its `Arc`.
702 pub fn mutex(s: &Self) -> &Arc<Mutex<R, T>> { in mutex() method
703 &s.mutex in mutex()
706 /// Unlocks the mutex and returns the `Arc` that was held by the [`ArcMutexGuard`].
708 pub fn into_arc(s: Self) -> Arc<Mutex<R, T>> { in into_arc()
709 // Safety: Skip our Drop impl and manually unlock the mutex. in into_arc()
710 let arc = unsafe { ptr::read(&s.mutex) }; in into_arc()
718 /// Temporarily unlocks the mutex to execute the given function.
721 /// references to the data protected by the mutex.
729 s.mutex.raw.unlock(); in unlocked()
731 defer!(s.mutex.raw.lock()); in unlocked()
738 /// Unlocks the mutex using a fair unlock protocol.
745 s.mutex.raw.unlock_fair(); in unlock_fair()
750 unsafe { ptr::drop_in_place(&mut s.mutex) }; in unlock_fair()
753 /// Temporarily unlocks the mutex to execute the given function.
763 s.mutex.raw.unlock_fair(); in unlocked_fair()
765 defer!(s.mutex.raw.lock()); in unlocked_fair()
769 /// Temporarily yields the mutex to a waiting thread if there is one.
776 s.mutex.raw.bump(); in bump()
786 unsafe { &*self.mutex.data.get() } in deref()
794 unsafe { &mut *self.mutex.data.get() } in deref_mut()
804 self.mutex.raw.unlock(); in drop()
809 /// An RAII mutex guard returned by `MutexGuard::map`, which can point to a
816 #[must_use = "if unused the Mutex will immediately unlock"]
836 /// in already locked the mutex.
860 /// in already locked the mutex.
885 /// Unlocks the mutex using a fair unlock protocol.
888 /// the mutex before another has the chance to acquire the lock, even if
889 /// that thread has been blocked on the mutex for a long time. This is the
891 /// context switch on every mutex unlock. This can result in one thread
892 /// acquiring a mutex many more times than other threads.