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
83 // Mutex
86 // A `Mutex` is a non-reentrant (aka non-recursive) Mutually Exclusive lock
91 // A `Mutex` has two basic operations: `Mutex::Lock()` and `Mutex::Unlock()`.
92 // The `Lock()` operation *acquires* a `Mutex` (in a state known as an
94 // Mutex. During the span of time between the Lock() and Unlock() operations,
95 // a mutex is said to be *held*. By design all mutexes support exclusive/write
96 // locks, as this is the most common way to use a mutex.
98 // The `Mutex` state machine for basic lock/unlock operations is quite simple:
108 // An "invalid" operation is disallowed by the API. The `Mutex` implementation
114 // `Mutex` is not guaranteed to be "fair" in prioritizing waiting threads; it
129 // See also `MutexLock`, below, for scoped `Mutex` acquisition.
131 class ABSL_LOCKABLE Mutex {
133 // Creates a `Mutex` that is not held by anyone. This constructor is
136 // To create `Mutex` instances with static storage duration
138 // `Mutex::Mutex(absl::kConstInit)` below instead.
139 Mutex();
141 // Creates a mutex with static storage duration. A global variable
150 // ABSL_CONST_INIT absl::Mutex mu(absl::kConstInit);
152 explicit constexpr Mutex(absl::ConstInitType);
154 ~Mutex();
156 // Mutex::Lock()
158 // Blocks the calling thread, if necessary, until this `Mutex` is free, and
162 // Mutex::Unlock()
164 // Releases this `Mutex` and returns it from the exclusive/write state to the
165 // free state. Calling thread must hold the `Mutex` exclusively.
168 // Mutex::TryLock()
170 // If the mutex can be acquired without blocking, does so exclusively and
172 // probability if the `Mutex` was free.
175 // Mutex::AssertHeld()
177 // Return immediately if this thread holds the `Mutex` exclusively (in write
186 // A Mutex can also be used as a starvation-free reader-writer lock.
190 // The Mutex API provides `Writer*()` aliases for the existing `Lock()`,
195 // Introducing reader locks necessarily complicates the `Mutex` state
197 // of a mutex in such cases. Note that ReaderLock() may block even if the lock
214 // Mutex::ReaderLock()
216 // Blocks the calling thread, if necessary, until this `Mutex` is either free,
219 // on the mutex.
223 // Mutex::ReaderUnlock()
225 // Releases a read share of this `Mutex`. `ReaderUnlock` may return a mutex to
226 // the free state if this thread holds the last reader lock on the mutex. Note
227 // that you cannot call `ReaderUnlock()` on a mutex held in write mode.
230 // Mutex::ReaderTryLock()
232 // If the mutex can be acquired without blocking, acquires this mutex for
234 // `true` with high probability if the `Mutex` was free or shared.
237 // Mutex::AssertReaderHeld()
239 // Returns immediately if this thread holds the `Mutex` in at least shared
244 // Mutex::WriterLock()
245 // Mutex::WriterUnlock()
246 // Mutex::WriterTryLock()
248 // Aliases for `Mutex::Lock()`, `Mutex::Unlock()`, and `Mutex::TryLock()`.
251 // methods) to distingish simple exclusive `Mutex` usage (`Lock()`,
265 // Conditional usage of a `Mutex` can occur using two distinct paradigms:
267 // * Use of `Mutex` member functions with `Condition` objects.
270 // In general, prefer use of `Condition` and the `Mutex` member functions
275 // `Mutex` contains member functions for performing lock operations only under
278 // the `Mutex`. The condition must be invariant w.r.t. environmental state
280 // always be invoked with the mutex held in at least read mode, so you should
293 // Mutex::Await()
295 // Unlocks this `Mutex` and blocks until simultaneously both `cond` is `true`
296 // and this `Mutex` can be reacquired, then reacquires this `Mutex` in the
300 // `Await()` requires that this thread holds this `Mutex` in some mode.
303 // Mutex::LockWhen()
304 // Mutex::ReaderLockWhen()
305 // Mutex::WriterLockWhen()
307 // Blocks until simultaneously both `cond` is `true` and this `Mutex` can
308 // be acquired, then atomically acquires this `Mutex`. `LockWhen()` is
320 // Mutex Variants with Timeouts/Deadlines
323 // Mutex::AwaitWithTimeout()
324 // Mutex::AwaitWithDeadline()
326 // Unlocks this `Mutex` and blocks until simultaneously:
329 // - this `Mutex` can be reacquired,
330 // then reacquire this `Mutex` in the same mode in which it was previously
339 // This method requires that this thread holds this `Mutex` in some mode.
344 // Mutex::LockWhenWithTimeout()
345 // Mutex::ReaderLockWhenWithTimeout()
346 // Mutex::WriterLockWhenWithTimeout()
350 // - this `Mutex` can be acquired,
351 // then atomically acquires this `Mutex`, returning `true` iff `cond` is
364 // Mutex::LockWhenWithDeadline()
365 // Mutex::ReaderLockWhenWithDeadline()
366 // Mutex::WriterLockWhenWithDeadline()
370 // - this `Mutex` can be acquired,
371 // then atomically acquires this Mutex, returning `true` iff `cond` is `true`
388 // Mutex::EnableInvariantDebugging()
392 // this `Mutex` should hold (for example: just after acquire, just before
398 // substantially reduce `Mutex` performance; it should be set only for
403 // Mutex::EnableDebugLog()
405 // Cause all subsequent uses of this `Mutex` to be logged via
409 // Note: This method substantially reduces `Mutex` performance.
414 // Mutex::ForgetDeadlockInfo()
417 // about this `Mutex`. Call this method in debug mode when the lock ordering
418 // of a `Mutex` changes.
421 // Mutex::AssertNotHeld()
423 // Return immediately if this thread does not hold this `Mutex` in any
440 // Mutex::InternalAttemptToUseMutexInFatalSignalHandler()
442 // Causes the `Mutex` implementation to prepare itself for re-entry caused by
443 // future use of `Mutex` within a fatal signal handler. This method is
456 std::atomic<intptr_t> mu_; // The Mutex state.
460 static void IncrementSynchSem(Mutex *mu, base_internal::PerThreadSynch *w);
461 static bool DecrementSynchSem(Mutex *mu, base_internal::PerThreadSynch *w,
479 // Block a thread on mutex.
485 void Trans(MuHow how); // used for CondVar->Mutex transfer
487 base_internal::PerThreadSynch *w); // used for CondVar->Mutex transfer
489 // Catch the error of writing Mutex when intending MutexLock.
490 Mutex(const volatile Mutex * /*ignored*/) {} // NOLINT(runtime/explicit) in Mutex() function
492 Mutex(const Mutex&) = delete;
493 Mutex& operator=(const Mutex&) = delete;
497 // Mutex RAII Wrappers
502 // `MutexLock` is a helper class, which acquires and releases a `Mutex` via
516 // Mutex mu_;
525 explicit MutexLock(Mutex *mu) ABSL_EXCLUSIVE_LOCK_FUNCTION(mu) : mu_(mu) { in MutexLock()
532 explicit MutexLock(Mutex *mu, const Condition &cond) in MutexLock()
538 MutexLock(const MutexLock &) = delete; // NOLINT(runtime/mutex)
539 MutexLock(MutexLock&&) = delete; // NOLINT(runtime/mutex)
546 Mutex *const mu_;
552 // releases a shared lock on a `Mutex` via RAII.
555 explicit ReaderMutexLock(Mutex *mu) ABSL_SHARED_LOCK_FUNCTION(mu) : mu_(mu) { in ReaderMutexLock()
559 explicit ReaderMutexLock(Mutex *mu, const Condition &cond) in ReaderMutexLock()
573 Mutex *const mu_;
579 // releases a write (exclusive) lock on a `Mutex` via RAII.
582 explicit WriterMutexLock(Mutex *mu) ABSL_EXCLUSIVE_LOCK_FUNCTION(mu) in WriterMutexLock()
587 explicit WriterMutexLock(Mutex *mu, const Condition &cond) in WriterMutexLock()
601 Mutex *const mu_;
608 // As noted above, `Mutex` contains a number of member functions which take a
610 // before attempting to acquire the mutex. These sections are known as
612 // construct it, and use within an appropriate `Mutex` member function;
620 // constant while the mutex is blocked on the condition (e.g. a stack variable),
621 // or objects of state protected explicitly by the mutex.
625 // exceptions. Correctness of `Mutex` / `Condition` is not guaranteed in
632 // suitable `Mutex' member function, such as `Mutex::Await()`, or to the
654 // + arg, or same pointer to object + method), so that the mutex implementation
706 // the lambda as it may be called when the mutex is being unlocked from a
761 // `Mutex` object, which can be signaled to wake callers.
762 // This class is not normally needed; use `Mutex` member functions such as
763 // `Mutex::Await()` and intrinsic `Condition` abstractions. In rare cases
773 // Usage for a thread waiting for some condition C protected by mutex mu:
800 // Atomically releases a `Mutex` and blocks on this condition variable.
802 // spurious wakeup), then reacquires the `Mutex` and returns.
804 // Requires and ensures that the current thread holds the `Mutex`.
805 void Wait(Mutex *mu);
809 // Atomically releases a `Mutex` and blocks on this condition variable.
812 // the `Mutex` and returns.
819 // Requires and ensures that the current thread holds the `Mutex`.
820 bool WaitWithTimeout(Mutex *mu, absl::Duration timeout);
824 // Atomically releases a `Mutex` and blocks on this condition variable.
827 // the `Mutex` and returns.
836 // Requires and ensures that the current thread holds the `Mutex`.
837 bool WaitWithDeadline(Mutex *mu, absl::Time deadline);
857 bool WaitCommon(Mutex *mutex, synchronization_internal::KernelTimeout t);
869 // Mutex::Unlock() and/or if-statements for clarity.
876 explicit MutexLockMaybe(Mutex *mu) ABSL_EXCLUSIVE_LOCK_FUNCTION(mu) in MutexLockMaybe()
883 explicit MutexLockMaybe(Mutex *mu, const Condition &cond) in MutexLockMaybe()
896 Mutex *const mu_;
906 // mutex before destruction. `Release()` may be called at most once.
909 explicit ReleasableMutexLock(Mutex *mu) ABSL_EXCLUSIVE_LOCK_FUNCTION(mu) in ReleasableMutexLock()
914 explicit ReleasableMutexLock(Mutex *mu, const Condition &cond) in ReleasableMutexLock()
927 Mutex *mu_;
934 inline Mutex::Mutex() : mu_(0) { in Mutex() function
938 inline constexpr Mutex::Mutex(absl::ConstInitType) : mu_(0) {} in Mutex() function
986 // The function pointer registered here will be called whenever a mutex is
996 // Register a hook for Mutex tracing.
998 // The function pointer registered here will be called whenever a mutex is
999 // contended. The callback is given an opaque handle to the contended mutex,
1044 // Enable or disable global support for Mutex invariant debugging. If enabled,
1045 // then invariant predicates can be registered per-Mutex for debug checking.
1046 // See Mutex::EnableInvariantDebugging().
1063 // due to Mutex lock ordering inversions. When set to 'kIgnore', tracking of