Lines Matching full:mutex
16 // mutex.h
19 // This header file defines a `Mutex` -- a mutually exclusive lock -- and the
21 // shared resources. A mutex is used to prevent multiple threads from accessing
24 // Unlike a `std::mutex`, the Abseil `Mutex` provides the following additional
26 // * Conditional predicates intrinsic to the `Mutex` object
32 // MutexLock - An RAII wrapper to acquire and release a `Mutex` for exclusive/
36 // - An RAII wrapper to acquire and release a `Mutex` for shared/read
43 // In addition to simple mutex locks, this file also defines ways to perform
47 // depends on state protected by the `Mutex` to become true.
54 // Mutexes and mutex behavior can be quite complicated. The information within
55 // this header file is limited, as a result. Please consult the Mutex guide for
86 // Mutex
89 // A `Mutex` is a non-reentrant (aka non-recursive) Mutually Exclusive lock
94 // A `Mutex` has two basic operations: `Mutex::Lock()` and `Mutex::Unlock()`.
95 // The `Lock()` operation *acquires* a `Mutex` (in a state known as an
97 // Mutex. During the span of time between the Lock() and Unlock() operations,
98 // a mutex is said to be *held*. By design, all mutexes support exclusive/write
99 // locks, as this is the most common way to use a mutex.
101 // Mutex operations are only allowed under certain conditions; otherwise an
103 // both the current state of the mutex and the identity of the threads that
106 // The `Mutex` state machine for basic lock/unlock operations is quite simple:
115 // * Calls to `Unlock()` require that the mutex be held, and must be made in the
117 // acquired the mutex; otherwise the call is invalid.
119 // * The mutex being non-reentrant (or non-recursive) means that a call to
121 // mutex; such a call is invalid.
125 // the mutex is held *by a particular thread*.
127 // An "invalid" operation has undefined behavior. The `Mutex` implementation
133 // `Mutex` is not guaranteed to be "fair" in prioritizing waiting threads; it
149 // See also `MutexLock`, below, for scoped `Mutex` acquisition.
151 class ABSL_LOCKABLE Mutex {
153 // Creates a `Mutex` that is not held by anyone. This constructor is
156 // To create `Mutex` instances with static storage duration
158 // `Mutex::Mutex(absl::kConstInit)` below instead.
159 Mutex();
161 // Creates a mutex with static storage duration. A global variable
170 // ABSL_CONST_INIT absl::Mutex mu(absl::kConstInit);
172 explicit constexpr Mutex(absl::ConstInitType);
174 ~Mutex();
176 // Mutex::Lock()
178 // Blocks the calling thread, if necessary, until this `Mutex` is free, and
182 // Mutex::Unlock()
184 // Releases this `Mutex` and returns it from the exclusive/write state to the
185 // free state. Calling thread must hold the `Mutex` exclusively.
188 // Mutex::TryLock()
190 // If the mutex can be acquired without blocking, does so exclusively and
192 // probability if the `Mutex` was free.
195 // Mutex::AssertHeld()
197 // Require that the mutex be held exclusively (write mode) by this thread.
199 // If the mutex is not currently held by this thread, this function may report
209 // A Mutex can also be used as a starvation-free reader-writer lock.
213 // The Mutex API provides `Writer*()` aliases for the existing `Lock()`,
218 // Introducing reader locks necessarily complicates the `Mutex` state
220 // of a mutex in such cases. Note that ReaderLock() may block even if the lock
237 // Mutex::ReaderLock()
239 // Blocks the calling thread, if necessary, until this `Mutex` is either free,
242 // on the mutex.
246 // Mutex::ReaderUnlock()
248 // Releases a read share of this `Mutex`. `ReaderUnlock` may return a mutex to
249 // the free state if this thread holds the last reader lock on the mutex. Note
250 // that you cannot call `ReaderUnlock()` on a mutex held in write mode.
253 // Mutex::ReaderTryLock()
255 // If the mutex can be acquired without blocking, acquires this mutex for
257 // `true` with high probability if the `Mutex` was free or shared.
260 // Mutex::AssertReaderHeld()
262 // Require that the mutex be held at least in shared mode (read mode) by this
265 // If the mutex is not currently held by this thread, this function may report
271 // Mutex::WriterLock()
272 // Mutex::WriterUnlock()
273 // Mutex::WriterTryLock()
275 // Aliases for `Mutex::Lock()`, `Mutex::Unlock()`, and `Mutex::TryLock()`.
278 // methods) to distinguish simple exclusive `Mutex` usage (`Lock()`,
292 // Conditional usage of a `Mutex` can occur using two distinct paradigms:
294 // * Use of `Mutex` member functions with `Condition` objects.
297 // In general, prefer use of `Condition` and the `Mutex` member functions
302 // `Mutex` contains member functions for performing lock operations only under
305 // the `Mutex`. The condition must be invariant w.r.t. environmental state
307 // always be invoked with the mutex held in at least read mode, so you should
320 // Mutex::Await()
322 // Unlocks this `Mutex` and blocks until simultaneously both `cond` is `true`
323 // and this `Mutex` can be reacquired, then reacquires this `Mutex` in the
327 // `Await()` requires that this thread holds this `Mutex` in some mode.
332 // Mutex::LockWhen()
333 // Mutex::ReaderLockWhen()
334 // Mutex::WriterLockWhen()
336 // Blocks until simultaneously both `cond` is `true` and this `Mutex` can
337 // be acquired, then atomically acquires this `Mutex`. `LockWhen()` is
355 // Mutex Variants with Timeouts/Deadlines
358 // Mutex::AwaitWithTimeout()
359 // Mutex::AwaitWithDeadline()
361 // Unlocks this `Mutex` and blocks until simultaneously:
364 // - this `Mutex` can be reacquired,
365 // then reacquire this `Mutex` in the same mode in which it was previously
374 // This method requires that this thread holds this `Mutex` in some mode.
383 // Mutex::LockWhenWithTimeout()
384 // Mutex::ReaderLockWhenWithTimeout()
385 // Mutex::WriterLockWhenWithTimeout()
389 // - this `Mutex` can be acquired,
390 // then atomically acquires this `Mutex`, returning `true` iff `cond` is
409 // Mutex::LockWhenWithDeadline()
410 // Mutex::ReaderLockWhenWithDeadline()
411 // Mutex::WriterLockWhenWithDeadline()
415 // - this `Mutex` can be acquired,
416 // then atomically acquires this Mutex, returning `true` iff `cond` is `true`
439 // Mutex::EnableInvariantDebugging()
443 // this `Mutex` should hold (for example: just after acquire, just before
449 // substantially reduce `Mutex` performance; it should be set only for
454 // Mutex::EnableDebugLog()
456 // Cause all subsequent uses of this `Mutex` to be logged via
460 // Note: This method substantially reduces `Mutex` performance.
465 // Mutex::ForgetDeadlockInfo()
468 // about this `Mutex`. Call this method in debug mode when the lock ordering
469 // of a `Mutex` changes.
472 // Mutex::AssertNotHeld()
474 // Return immediately if this thread does not hold this `Mutex` in any
491 // Mutex::InternalAttemptToUseMutexInFatalSignalHandler()
493 // Causes the `Mutex` implementation to prepare itself for re-entry caused by
494 // future use of `Mutex` within a fatal signal handler. This method is
507 std::atomic<intptr_t> mu_; // The Mutex state.
511 static void IncrementSynchSem(Mutex* mu, base_internal::PerThreadSynch* w);
512 static bool DecrementSynchSem(Mutex* mu, base_internal::PerThreadSynch* w,
536 // Block a thread on mutex.
543 void Trans(MuHow how); // used for CondVar->Mutex transfer
545 base_internal::PerThreadSynch* w); // used for CondVar->Mutex transfer
547 // Catch the error of writing Mutex when intending MutexLock.
548 explicit Mutex(const volatile Mutex* /*ignored*/) {} in Mutex() argument
550 Mutex(const Mutex&) = delete;
551 Mutex& operator=(const Mutex&) = delete;
555 // Mutex RAII Wrappers
560 // `MutexLock` is a helper class, which acquires and releases a `Mutex` via
574 // Mutex mu_;
583 explicit MutexLock(Mutex* mu) ABSL_EXCLUSIVE_LOCK_FUNCTION(mu) : mu_(mu) { in MutexLock()
590 explicit MutexLock(Mutex* mu, const Condition& cond) in MutexLock()
596 MutexLock(const MutexLock&) = delete; // NOLINT(runtime/mutex)
597 MutexLock(MutexLock&&) = delete; // NOLINT(runtime/mutex)
604 Mutex* const mu_;
610 // releases a shared lock on a `Mutex` via RAII.
613 explicit ReaderMutexLock(Mutex* mu) ABSL_SHARED_LOCK_FUNCTION(mu) : mu_(mu) { in ReaderMutexLock()
617 explicit ReaderMutexLock(Mutex* mu, const Condition& cond) in ReaderMutexLock()
631 Mutex* const mu_;
637 // releases a write (exclusive) lock on a `Mutex` via RAII.
640 explicit WriterMutexLock(Mutex* mu) ABSL_EXCLUSIVE_LOCK_FUNCTION(mu) in WriterMutexLock()
645 explicit WriterMutexLock(Mutex* mu, const Condition& cond) in WriterMutexLock()
659 Mutex* const mu_;
666 // `Mutex` contains a number of member functions which take a `Condition` as an
668 // to acquire the mutex. These sections are known as "condition critical"
670 // within an appropriate `Mutex` member function; everything else in the
678 // constant while the mutex is blocked on the condition (e.g. a stack variable),
679 // or objects of state protected explicitly by the mutex.
683 // exceptions. Correctness of `Mutex` / `Condition` is not guaranteed in
690 // suitable `Mutex' member function, such as `Mutex::Await()`, or to the
712 // + arg, or same pointer to object + method), so that the mutex implementation
780 // the lambda as it may be called when the mutex is being unlocked from a
804 // the Mutex becomes available. The return value of these methods does
881 // `Mutex` object, which can be signaled to wake callers.
882 // This class is not normally needed; use `Mutex` member functions such as
883 // `Mutex::Await()` and intrinsic `Condition` abstractions. In rare cases
893 // Usage for a thread waiting for some condition C protected by mutex mu:
919 // Atomically releases a `Mutex` and blocks on this condition variable.
921 // spurious wakeup), then reacquires the `Mutex` and returns.
923 // Requires and ensures that the current thread holds the `Mutex`.
924 void Wait(Mutex* mu) { in Wait()
930 // Atomically releases a `Mutex` and blocks on this condition variable.
933 // the `Mutex` and returns.
940 // Requires and ensures that the current thread holds the `Mutex`.
941 bool WaitWithTimeout(Mutex* mu, absl::Duration timeout) { in WaitWithTimeout()
947 // Atomically releases a `Mutex` and blocks on this condition variable.
950 // the `Mutex` and returns.
959 // Requires and ensures that the current thread holds the `Mutex`.
960 bool WaitWithDeadline(Mutex* mu, absl::Time deadline) { in WaitWithDeadline()
982 bool WaitCommon(Mutex* mutex, synchronization_internal::KernelTimeout t);
992 // Mutex::Unlock() and/or if-statements for clarity.
999 explicit MutexLockMaybe(Mutex* mu) ABSL_EXCLUSIVE_LOCK_FUNCTION(mu) in MutexLockMaybe()
1006 explicit MutexLockMaybe(Mutex* mu, const Condition& cond) in MutexLockMaybe()
1021 Mutex* const mu_;
1031 // mutex before destruction. `Release()` may be called at most once.
1034 explicit ReleasableMutexLock(Mutex* mu) ABSL_EXCLUSIVE_LOCK_FUNCTION(mu) in ReleasableMutexLock()
1039 explicit ReleasableMutexLock(Mutex* mu, const Condition& cond) in ReleasableMutexLock()
1054 Mutex* mu_;
1061 inline Mutex::Mutex() : mu_(0) { in Mutex() function
1065 inline constexpr Mutex::Mutex(absl::ConstInitType) : mu_(0) {} in Mutex() function
1069 inline Mutex::~Mutex() { Dtor(); } in ~Mutex()
1074 // We need to mark both Dtor and ~Mutex as always inline for inconsistent
1079 inline void Mutex::Dtor() {} in Dtor()
1137 // The function pointer registered here will be called whenever a mutex is
1149 // Register a hook for Mutex tracing.
1151 // The function pointer registered here will be called whenever a mutex is
1152 // contended. The callback is given an opaque handle to the contended mutex,
1180 // Enable or disable global support for Mutex invariant debugging. If enabled,
1181 // then invariant predicates can be registered per-Mutex for debug checking.
1182 // See Mutex::EnableInvariantDebugging().
1199 // due to Mutex lock ordering inversions. When set to 'kIgnore', tracking of