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()
334 // Unlocks this `Mutex` and blocks until simultaneously:
337 // - this `Mutex` can be reacquired,
338 // then reacquire this `Mutex` in the same mode in which it was previously
347 // This method requires that this thread holds this `Mutex` in some mode.
352 // Mutex::LockWhenWithTimeout()
353 // Mutex::ReaderLockWhenWithTimeout()
354 // Mutex::WriterLockWhenWithTimeout()
358 // - this `Mutex` can be acquired,
359 // then atomically acquires this `Mutex`, returning `true` iff `cond` is
372 // Mutex::LockWhenWithDeadline()
373 // Mutex::ReaderLockWhenWithDeadline()
374 // Mutex::WriterLockWhenWithDeadline()
378 // - this `Mutex` can be acquired,
379 // then atomically acquires this Mutex, returning `true` iff `cond` is `true`
396 // Mutex::EnableInvariantDebugging()
400 // this `Mutex` should hold (for example: just after acquire, just before
406 // substantially reduce `Mutex` performance; it should be set only for
411 // Mutex::EnableDebugLog()
413 // Cause all subsequent uses of this `Mutex` to be logged via
417 // Note: This method substantially reduces `Mutex` performance.
422 // Mutex::ForgetDeadlockInfo()
425 // about this `Mutex`. Call this method in debug mode when the lock ordering
426 // of a `Mutex` changes.
429 // Mutex::AssertNotHeld()
431 // Return immediately if this thread does not hold this `Mutex` in any
448 // Mutex::InternalAttemptToUseMutexInFatalSignalHandler()
450 // Causes the `Mutex` implementation to prepare itself for re-entry caused by
451 // future use of `Mutex` within a fatal signal handler. This method is
473 std::atomic<intptr_t> mu_; // The Mutex state.
477 static inline void IncrementSynchSem(Mutex *mu,
480 Mutex *mu, base_internal::PerThreadSynch *w,
498 // Block a thread on mutex.
504 void Trans(MuHow how); // used for CondVar->Mutex transfer
506 base_internal::PerThreadSynch *w); // used for CondVar->Mutex transfer
509 // Catch the error of writing Mutex when intending MutexLock.
510 Mutex(const volatile Mutex * /*ignored*/) {} // NOLINT(runtime/explicit) in Mutex() argument
512 Mutex(const Mutex&) = delete;
513 Mutex& operator=(const Mutex&) = delete;
517 // Mutex RAII Wrappers
522 // `MutexLock` is a helper class, which acquires and releases a `Mutex` via
536 // Mutex lock_;
540 explicit MutexLock(Mutex *mu) ABSL_EXCLUSIVE_LOCK_FUNCTION(mu) : mu_(mu) { in MutexLock()
544 MutexLock(const MutexLock &) = delete; // NOLINT(runtime/mutex)
545 MutexLock(MutexLock&&) = delete; // NOLINT(runtime/mutex)
552 Mutex *const mu_;
558 // releases a shared lock on a `Mutex` via RAII.
561 explicit ReaderMutexLock(Mutex *mu) ABSL_SHARED_LOCK_FUNCTION(mu) : mu_(mu) { 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()
595 Mutex *const mu_;
602 // As noted above, `Mutex` contains a number of member functions which take a
604 // before attempting to acquire the mutex. These sections are known as
606 // construct it, and use within an appropriate `Mutex` member function;
614 // constant while the mutex is blocked on the condition (e.g. a stack variable),
615 // or objects of state protected explicitly by the mutex.
619 // exceptions. Correctness of `Mutex` / `Condition` is not guaranteed in
626 // appropriate `Mutex' member function, such as `Mutex::Await()`.
638 // + arg, or same pointer to object + method), so that the mutex implementation
690 // lambda as it may be called when the mutex is being unlocked from a scope
745 // `Mutex` object, which can be signaled to wake callers.
746 // This class is not normally needed; use `Mutex` member functions such as
747 // `Mutex::Await()` and intrinsic `Condition` abstractions. In rare cases
757 // Usage for a thread waiting for some condition C protected by mutex mu:
784 // Atomically releases a `Mutex` and blocks on this condition variable.
786 // spurious wakeup), then reacquires the `Mutex` and returns.
788 // Requires and ensures that the current thread holds the `Mutex`.
789 void Wait(Mutex *mu);
793 // Atomically releases a `Mutex` and blocks on this condition variable.
796 // the `Mutex` and returns.
803 // Requires and ensures that the current thread holds the `Mutex`.
804 bool WaitWithTimeout(Mutex *mu, absl::Duration timeout);
808 // Atomically releases a `Mutex` and blocks on this condition variable.
811 // the `Mutex` and returns.
820 // Requires and ensures that the current thread holds the `Mutex`.
821 bool WaitWithDeadline(Mutex *mu, absl::Time deadline);
847 bool WaitCommon(Mutex *mutex, synchronization_internal::KernelTimeout t);
860 // Mutex::Unlock() and/or if-statements for clarity.
867 explicit MutexLockMaybe(Mutex *mu) ABSL_EXCLUSIVE_LOCK_FUNCTION(mu) in MutexLockMaybe()
878 Mutex *const mu_;
888 // mutex before destruction. `Release()` may be called at most once.
891 explicit ReleasableMutexLock(Mutex *mu) ABSL_EXCLUSIVE_LOCK_FUNCTION(mu) in ReleasableMutexLock()
902 Mutex *mu_;
911 inline constexpr Mutex::Mutex(absl::ConstInitType) : impl_(absl::kConstInit) {} in Mutex() function
915 inline Mutex::Mutex() : mu_(0) { in Mutex() function
919 inline constexpr Mutex::Mutex(absl::ConstInitType) : mu_(0) {} in Mutex() function
969 // The function pointer registered here will be called whenever a mutex is
979 // Register a hook for Mutex tracing.
981 // The function pointer registered here will be called whenever a mutex is
982 // contended. The callback is given an opaque handle to the contended mutex,
1027 // Enable or disable global support for Mutex invariant debugging. If enabled,
1028 // then invariant predicates can be registered per-Mutex for debug checking.
1029 // See Mutex::EnableInvariantDebugging().
1046 // due to Mutex lock ordering inversions. When set to 'kIgnore', tracking of