• Home
  • Raw
  • Download

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
85 // Mutex
88 // A `Mutex` is a non-reentrant (aka non-recursive) Mutually Exclusive lock
93 // A `Mutex` has two basic operations: `Mutex::Lock()` and `Mutex::Unlock()`.
94 // The `Lock()` operation *acquires* a `Mutex` (in a state known as an
96 // Mutex. During the span of time between the Lock() and Unlock() operations,
97 // a mutex is said to be *held*. By design, all mutexes support exclusive/write
98 // locks, as this is the most common way to use a mutex.
100 // Mutex operations are only allowed under certain conditions; otherwise an
102 // both the current state of the mutex and the identity of the threads that
105 // The `Mutex` state machine for basic lock/unlock operations is quite simple:
114 // * Calls to `Unlock()` require that the mutex be held, and must be made in the
116 // acquired the mutex; otherwise the call is invalid.
118 // * The mutex being non-reentrant (or non-recursive) means that a call to
120 // mutex; such a call is invalid.
124 // the mutex is held *by a particular thread*.
126 // An "invalid" operation has undefined behavior. The `Mutex` implementation
132 // `Mutex` is not guaranteed to be "fair" in prioritizing waiting threads; it
148 // See also `MutexLock`, below, for scoped `Mutex` acquisition.
150 class ABSL_LOCKABLE Mutex {
152 // Creates a `Mutex` that is not held by anyone. This constructor is
155 // To create `Mutex` instances with static storage duration
157 // `Mutex::Mutex(absl::kConstInit)` below instead.
158 Mutex();
160 // Creates a mutex with static storage duration. A global variable
169 // ABSL_CONST_INIT absl::Mutex mu(absl::kConstInit);
171 explicit constexpr Mutex(absl::ConstInitType);
173 ~Mutex();
175 // Mutex::Lock()
177 // Blocks the calling thread, if necessary, until this `Mutex` is free, and
181 // Mutex::Unlock()
183 // Releases this `Mutex` and returns it from the exclusive/write state to the
184 // free state. Calling thread must hold the `Mutex` exclusively.
187 // Mutex::TryLock()
189 // If the mutex can be acquired without blocking, does so exclusively and
191 // probability if the `Mutex` was free.
194 // Mutex::AssertHeld()
196 // Require that the mutex be held exclusively (write mode) by this thread.
198 // If the mutex is not currently held by this thread, this function may report
208 // A Mutex can also be used as a starvation-free reader-writer lock.
212 // The Mutex API provides `Writer*()` aliases for the existing `Lock()`,
217 // Introducing reader locks necessarily complicates the `Mutex` state
219 // of a mutex in such cases. Note that ReaderLock() may block even if the lock
236 // Mutex::ReaderLock()
238 // Blocks the calling thread, if necessary, until this `Mutex` is either free,
241 // on the mutex.
245 // Mutex::ReaderUnlock()
247 // Releases a read share of this `Mutex`. `ReaderUnlock` may return a mutex to
248 // the free state if this thread holds the last reader lock on the mutex. Note
249 // that you cannot call `ReaderUnlock()` on a mutex held in write mode.
252 // Mutex::ReaderTryLock()
254 // If the mutex can be acquired without blocking, acquires this mutex for
256 // `true` with high probability if the `Mutex` was free or shared.
259 // Mutex::AssertReaderHeld()
261 // Require that the mutex be held at least in shared mode (read mode) by this
264 // If the mutex is not currently held by this thread, this function may report
270 // Mutex::WriterLock()
271 // Mutex::WriterUnlock()
272 // Mutex::WriterTryLock()
274 // Aliases for `Mutex::Lock()`, `Mutex::Unlock()`, and `Mutex::TryLock()`.
277 // methods) to distinguish simple exclusive `Mutex` usage (`Lock()`,
291 // Conditional usage of a `Mutex` can occur using two distinct paradigms:
293 // * Use of `Mutex` member functions with `Condition` objects.
296 // In general, prefer use of `Condition` and the `Mutex` member functions
301 // `Mutex` contains member functions for performing lock operations only under
304 // the `Mutex`. The condition must be invariant w.r.t. environmental state
306 // always be invoked with the mutex held in at least read mode, so you should
319 // Mutex::Await()
321 // Unlocks this `Mutex` and blocks until simultaneously both `cond` is `true`
322 // and this `Mutex` can be reacquired, then reacquires this `Mutex` in the
326 // `Await()` requires that this thread holds this `Mutex` in some mode.
329 // Mutex::LockWhen()
330 // Mutex::ReaderLockWhen()
331 // Mutex::WriterLockWhen()
333 // Blocks until simultaneously both `cond` is `true` and this `Mutex` can
334 // be acquired, then atomically acquires this `Mutex`. `LockWhen()` is
346 // Mutex Variants with Timeouts/Deadlines
349 // Mutex::AwaitWithTimeout()
350 // Mutex::AwaitWithDeadline()
352 // Unlocks this `Mutex` and blocks until simultaneously:
355 // - this `Mutex` can be reacquired,
356 // then reacquire this `Mutex` in the same mode in which it was previously
365 // This method requires that this thread holds this `Mutex` in some mode.
370 // Mutex::LockWhenWithTimeout()
371 // Mutex::ReaderLockWhenWithTimeout()
372 // Mutex::WriterLockWhenWithTimeout()
376 // - this `Mutex` can be acquired,
377 // then atomically acquires this `Mutex`, returning `true` iff `cond` is
390 // Mutex::LockWhenWithDeadline()
391 // Mutex::ReaderLockWhenWithDeadline()
392 // Mutex::WriterLockWhenWithDeadline()
396 // - this `Mutex` can be acquired,
397 // then atomically acquires this Mutex, returning `true` iff `cond` is `true`
414 // Mutex::EnableInvariantDebugging()
418 // this `Mutex` should hold (for example: just after acquire, just before
424 // substantially reduce `Mutex` performance; it should be set only for
429 // Mutex::EnableDebugLog()
431 // Cause all subsequent uses of this `Mutex` to be logged via
435 // Note: This method substantially reduces `Mutex` performance.
440 // Mutex::ForgetDeadlockInfo()
443 // about this `Mutex`. Call this method in debug mode when the lock ordering
444 // of a `Mutex` changes.
447 // Mutex::AssertNotHeld()
449 // Return immediately if this thread does not hold this `Mutex` in any
466 // Mutex::InternalAttemptToUseMutexInFatalSignalHandler()
468 // Causes the `Mutex` implementation to prepare itself for re-entry caused by
469 // future use of `Mutex` within a fatal signal handler. This method is
482 std::atomic<intptr_t> mu_; // The Mutex state.
486 static void IncrementSynchSem(Mutex* mu, base_internal::PerThreadSynch* w);
487 static bool DecrementSynchSem(Mutex* mu, base_internal::PerThreadSynch* w,
505 // Block a thread on mutex.
511 void Trans(MuHow how); // used for CondVar->Mutex transfer
513 base_internal::PerThreadSynch* w); // used for CondVar->Mutex transfer
515 // Catch the error of writing Mutex when intending MutexLock.
516 explicit Mutex(const volatile Mutex* /*ignored*/) {} in Mutex() argument
518 Mutex(const Mutex&) = delete;
519 Mutex& operator=(const Mutex&) = delete;
523 // Mutex RAII Wrappers
528 // `MutexLock` is a helper class, which acquires and releases a `Mutex` via
542 // Mutex mu_;
551 explicit MutexLock(Mutex* mu) ABSL_EXCLUSIVE_LOCK_FUNCTION(mu) : mu_(mu) { in MutexLock()
558 explicit MutexLock(Mutex* mu, const Condition& cond) in MutexLock()
564 MutexLock(const MutexLock&) = delete; // NOLINT(runtime/mutex)
565 MutexLock(MutexLock&&) = delete; // NOLINT(runtime/mutex)
572 Mutex* const mu_;
578 // releases a shared lock on a `Mutex` via RAII.
581 explicit ReaderMutexLock(Mutex* mu) ABSL_SHARED_LOCK_FUNCTION(mu) : mu_(mu) { in ReaderMutexLock()
585 explicit ReaderMutexLock(Mutex* mu, const Condition& cond) in ReaderMutexLock()
599 Mutex* const mu_;
605 // releases a write (exclusive) lock on a `Mutex` via RAII.
608 explicit WriterMutexLock(Mutex* mu) ABSL_EXCLUSIVE_LOCK_FUNCTION(mu) in WriterMutexLock()
613 explicit WriterMutexLock(Mutex* mu, const Condition& cond) in WriterMutexLock()
627 Mutex* const mu_;
634 // `Mutex` contains a number of member functions which take a `Condition` as an
636 // to acquire the mutex. These sections are known as "condition critical"
638 // within an appropriate `Mutex` member function; everything else in the
646 // constant while the mutex is blocked on the condition (e.g. a stack variable),
647 // or objects of state protected explicitly by the mutex.
651 // exceptions. Correctness of `Mutex` / `Condition` is not guaranteed in
658 // suitable `Mutex' member function, such as `Mutex::Await()`, or to the
680 // + arg, or same pointer to object + method), so that the mutex implementation
746 // the lambda as it may be called when the mutex is being unlocked from a
770 // the Mutex becomes available. The return value of these methods does
845 // `Mutex` object, which can be signaled to wake callers.
846 // This class is not normally needed; use `Mutex` member functions such as
847 // `Mutex::Await()` and intrinsic `Condition` abstractions. In rare cases
857 // Usage for a thread waiting for some condition C protected by mutex mu:
884 // Atomically releases a `Mutex` and blocks on this condition variable.
886 // spurious wakeup), then reacquires the `Mutex` and returns.
888 // Requires and ensures that the current thread holds the `Mutex`.
889 void Wait(Mutex* mu);
893 // Atomically releases a `Mutex` and blocks on this condition variable.
896 // the `Mutex` and returns.
903 // Requires and ensures that the current thread holds the `Mutex`.
904 bool WaitWithTimeout(Mutex* mu, absl::Duration timeout);
908 // Atomically releases a `Mutex` and blocks on this condition variable.
911 // the `Mutex` and returns.
920 // Requires and ensures that the current thread holds the `Mutex`.
921 bool WaitWithDeadline(Mutex* mu, absl::Time deadline);
941 bool WaitCommon(Mutex* mutex, synchronization_internal::KernelTimeout t);
952 // Mutex::Unlock() and/or if-statements for clarity.
959 explicit MutexLockMaybe(Mutex* mu) ABSL_EXCLUSIVE_LOCK_FUNCTION(mu) in MutexLockMaybe()
966 explicit MutexLockMaybe(Mutex* mu, const Condition& cond) in MutexLockMaybe()
981 Mutex* const mu_;
991 // mutex before destruction. `Release()` may be called at most once.
994 explicit ReleasableMutexLock(Mutex* mu) ABSL_EXCLUSIVE_LOCK_FUNCTION(mu) in ReleasableMutexLock()
999 explicit ReleasableMutexLock(Mutex* mu, const Condition& cond) in ReleasableMutexLock()
1014 Mutex* mu_;
1021 inline Mutex::Mutex() : mu_(0) { in Mutex() function
1025 inline constexpr Mutex::Mutex(absl::ConstInitType) : mu_(0) {} in Mutex() function
1082 // The function pointer registered here will be called whenever a mutex is
1094 // Register a hook for Mutex tracing.
1096 // The function pointer registered here will be called whenever a mutex is
1097 // contended. The callback is given an opaque handle to the contended mutex,
1125 // Enable or disable global support for Mutex invariant debugging. If enabled,
1126 // then invariant predicates can be registered per-Mutex for debug checking.
1127 // See Mutex::EnableInvariantDebugging().
1144 // due to Mutex lock ordering inversions. When set to 'kIgnore', tracking of