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/
35 // - An RAII wrapper to acquire and release a `Mutex` for shared/read
42 // In addition to simple mutex locks, this file also defines ways to perform
46 // depends on state protected by the `Mutex` to become true.
53 // Mutexes and mutex behavior can be quite complicated. The information within
54 // this header file is limited, as a result. Please consult the Mutex guide for
91 // Mutex
94 // A `Mutex` is a non-reentrant (aka non-recursive) Mutually Exclusive lock
99 // A `Mutex` has two basic operations: `Mutex::Lock()` and `Mutex::Unlock()`.
100 // The `Lock()` operation *acquires* a `Mutex` (in a state known as an
102 // Mutex. During the span of time between the Lock() and Unlock() operations,
103 // a mutex is said to be *held*. By design all mutexes support exclusive/write
104 // locks, as this is the most common way to use a mutex.
106 // The `Mutex` state machine for basic lock/unlock operations is quite simple:
116 // An "invalid" operation is disallowed by the API. The `Mutex` implementation
122 // `Mutex` is not guaranteed to be "fair" in prioritizing waiting threads; it
137 // See also `MutexLock`, below, for scoped `Mutex` acquisition.
139 class ABSL_LOCKABLE Mutex {
141 // Creates a `Mutex` that is not held by anyone. This constructor is
144 // To create `Mutex` instances with static storage duration
146 // `Mutex::Mutex(absl::kConstInit)` below instead.
147 Mutex();
149 // Creates a mutex with static storage duration. A global variable
158 // ABSL_CONST_INIT Mutex mu(absl::kConstInit);
160 explicit constexpr Mutex(absl::ConstInitType);
162 ~Mutex();
164 // Mutex::Lock()
166 // Blocks the calling thread, if necessary, until this `Mutex` is free, and
170 // Mutex::Unlock()
172 // Releases this `Mutex` and returns it from the exclusive/write state to the
173 // free state. Caller must hold the `Mutex` exclusively.
176 // Mutex::TryLock()
178 // If the mutex can be acquired without blocking, does so exclusively and
180 // probability if the `Mutex` was free.
183 // Mutex::AssertHeld()
185 // Return immediately if this thread holds the `Mutex` exclusively (in write
194 // A Mutex can also be used as a starvation-free reader-writer lock.
198 // The Mutex API provides `Writer*()` aliases for the existing `Lock()`,
203 // Introducing reader locks necessarily complicates the `Mutex` state
205 // of a mutex in such cases. Note that ReaderLock() may block even if the lock
222 // Mutex::ReaderLock()
224 // Blocks the calling thread, if necessary, until this `Mutex` is either free,
227 // on the mutex.
231 // Mutex::ReaderUnlock()
233 // Releases a read share of this `Mutex`. `ReaderUnlock` may return a mutex to
234 // the free state if this thread holds the last reader lock on the mutex. Note
235 // that you cannot call `ReaderUnlock()` on a mutex held in write mode.
238 // Mutex::ReaderTryLock()
240 // If the mutex can be acquired without blocking, acquires this mutex for
242 // `true` with high probability if the `Mutex` was free or shared.
245 // Mutex::AssertReaderHeld()
247 // Returns immediately if this thread holds the `Mutex` in at least shared
252 // Mutex::WriterLock()
253 // Mutex::WriterUnlock()
254 // Mutex::WriterTryLock()
256 // Aliases for `Mutex::Lock()`, `Mutex::Unlock()`, and `Mutex::TryLock()`.
259 // methods) to distingish simple exclusive `Mutex` usage (`Lock()`,
273 // Conditional usage of a `Mutex` can occur using two distinct paradigms:
275 // * Use of `Mutex` member functions with `Condition` objects.
278 // In general, prefer use of `Condition` and the `Mutex` member functions
283 // `Mutex` contains member functions for performing lock operations only under
286 // the `Mutex`. The condition must be invariant w.r.t. environmental state
288 // always be invoked with the mutex held in at least read mode, so you should
301 // Mutex::Await()
303 // Unlocks this `Mutex` and blocks until simultaneously both `cond` is `true`
304 // and this `Mutex` can be reacquired, then reacquires this `Mutex` in the
308 // `Await()` requires that this thread holds this `Mutex` in some mode.
311 // Mutex::LockWhen()
312 // Mutex::ReaderLockWhen()
313 // Mutex::WriterLockWhen()
315 // Blocks until simultaneously both `cond` is `true` and this `Mutex` can
316 // be acquired, then atomically acquires this `Mutex`. `LockWhen()` is
328 // Mutex Variants with Timeouts/Deadlines
331 // Mutex::AwaitWithTimeout()
332 // Mutex::AwaitWithDeadline()
337 // If `cond` is initially false, unlock this `Mutex` and block until
341 // - this `Mutex` can be reacquired,
342 // then reacquire this `Mutex` in the same mode in which it was previously
348 // This method requires that this thread holds this `Mutex` in some mode.
353 // Mutex::LockWhenWithTimeout()
354 // Mutex::ReaderLockWhenWithTimeout()
355 // Mutex::WriterLockWhenWithTimeout()
359 // - this `Mutex` can be acquired,
360 // then atomically acquires this `Mutex`, returning `true` iff `cond` is
373 // Mutex::LockWhenWithDeadline()
374 // Mutex::ReaderLockWhenWithDeadline()
375 // Mutex::WriterLockWhenWithDeadline()
379 // - this `Mutex` can be acquired,
380 // then atomically acquires this Mutex, returning `true` iff `cond` is `true`
397 // Mutex::EnableInvariantDebugging()
401 // this `Mutex` should hold (for example: just after acquire, just before
407 // substantially reduce `Mutex` performance; it should be set only for
412 // Mutex::EnableDebugLog()
414 // Cause all subsequent uses of this `Mutex` to be logged via
418 // Note: This method substantially reduces `Mutex` performance.
423 // Mutex::ForgetDeadlockInfo()
426 // about this `Mutex`. Call this method in debug mode when the lock ordering
427 // of a `Mutex` changes.
430 // Mutex::AssertNotHeld()
432 // Return immediately if this thread does not hold this `Mutex` in any
449 // Mutex::InternalAttemptToUseMutexInFatalSignalHandler()
451 // Causes the `Mutex` implementation to prepare itself for re-entry caused by
452 // future use of `Mutex` within a fatal signal handler. This method is
474 std::atomic<intptr_t> mu_; // The Mutex state.
478 static inline void IncrementSynchSem(Mutex *mu,
481 Mutex *mu, base_internal::PerThreadSynch *w,
499 // Block a thread on mutex.
505 void Trans(MuHow how); // used for CondVar->Mutex transfer
507 base_internal::PerThreadSynch *w); // used for CondVar->Mutex transfer
510 // Catch the error of writing Mutex when intending MutexLock.
511 Mutex(const volatile Mutex * /*ignored*/) {} // NOLINT(runtime/explicit) in Mutex() argument
513 Mutex(const Mutex&) = delete;
514 Mutex& operator=(const Mutex&) = delete;
518 // Mutex RAII Wrappers
523 // `MutexLock` is a helper class, which acquires and releases a `Mutex` via
537 // Mutex lock_;
541 explicit MutexLock(Mutex *mu) ABSL_EXCLUSIVE_LOCK_FUNCTION(mu) : mu_(mu) { in MutexLock()
545 MutexLock(const MutexLock &) = delete; // NOLINT(runtime/mutex)
546 MutexLock(MutexLock&&) = delete; // NOLINT(runtime/mutex)
553 Mutex *const mu_;
559 // releases a shared lock on a `Mutex` via RAII.
562 explicit ReaderMutexLock(Mutex *mu) ABSL_SHARED_LOCK_FUNCTION(mu) : mu_(mu) { in ReaderMutexLock()
574 Mutex *const mu_;
580 // releases a write (exclusive) lock on a `Mutex` via RAII.
583 explicit WriterMutexLock(Mutex *mu) ABSL_EXCLUSIVE_LOCK_FUNCTION(mu) in WriterMutexLock()
596 Mutex *const mu_;
603 // As noted above, `Mutex` contains a number of member functions which take a
605 // before attempting to acquire the mutex. These sections are known as
607 // construct it, and use within an appropriate `Mutex` member function;
615 // constant while the mutex is blocked on the condition (e.g. a stack variable),
616 // or objects of state protected explicitly by the mutex.
620 // exceptions. Correctness of `Mutex` / `Condition` is not guaranteed in
627 // appropriate `Mutex' member function, such as `Mutex::Await()`.
639 // + arg, or same pointer to object + method), so that the mutex implementation
741 // `Mutex` object, which can be signaled to wake callers.
742 // This class is not normally needed; use `Mutex` member functions such as
743 // `Mutex::Await()` and intrinsic `Condition` abstractions. In rare cases
753 // Usage for a thread waiting for some condition C protected by mutex mu:
778 // Atomically releases a `Mutex` and blocks on this condition variable.
780 // spurious wakeup), then reacquires the `Mutex` and returns.
782 // Requires and ensures that the current thread holds the `Mutex`.
783 void Wait(Mutex *mu);
787 // Atomically releases a `Mutex` and blocks on this condition variable.
790 // the `Mutex` and returns.
797 // Requires and ensures that the current thread holds the `Mutex`.
798 bool WaitWithTimeout(Mutex *mu, absl::Duration timeout);
802 // Atomically releases a `Mutex` and blocks on this condition variable.
805 // the `Mutex` and returns.
814 // Requires and ensures that the current thread holds the `Mutex`.
815 bool WaitWithDeadline(Mutex *mu, absl::Time deadline);
841 bool WaitCommon(Mutex *mutex, synchronization_internal::KernelTimeout t);
854 // Mutex::Unlock() and/or if-statements for clarity.
861 explicit MutexLockMaybe(Mutex *mu) ABSL_EXCLUSIVE_LOCK_FUNCTION(mu) in MutexLockMaybe()
872 Mutex *const mu_;
882 // mutex before destruction. `Release()` may be called at most once.
885 explicit ReleasableMutexLock(Mutex *mu) ABSL_EXCLUSIVE_LOCK_FUNCTION(mu) in ReleasableMutexLock()
896 Mutex *mu_;
904 inline constexpr Mutex::Mutex(absl::ConstInitType) : impl_(absl::kConstInit) {} in Mutex() function
907 inline Mutex::Mutex() : mu_(0) { in Mutex() function
911 inline constexpr Mutex::Mutex(absl::ConstInitType) : mu_(0) {} in Mutex() function
960 // The function pointer registered here will be called whenever a mutex is
970 // Register a hook for Mutex tracing.
972 // The function pointer registered here will be called whenever a mutex is
973 // contended. The callback is given an opaque handle to the contended mutex,
1018 // Enable or disable global support for Mutex invariant debugging. If enabled,
1019 // then invariant predicates can be registered per-Mutex for debug checking.
1020 // See Mutex::EnableInvariantDebugging().
1037 // due to Mutex lock ordering inversions. When set to 'kIgnore', tracking of