• 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
147 // See also `MutexLock`, below, for scoped `Mutex` acquisition.
149 class ABSL_LOCKABLE Mutex {
151 // Creates a `Mutex` that is not held by anyone. This constructor is
154 // To create `Mutex` instances with static storage duration
156 // `Mutex::Mutex(absl::kConstInit)` below instead.
157 Mutex();
159 // Creates a mutex with static storage duration. A global variable
168 // ABSL_CONST_INIT absl::Mutex mu(absl::kConstInit);
170 explicit constexpr Mutex(absl::ConstInitType);
172 ~Mutex();
174 // Mutex::Lock()
176 // Blocks the calling thread, if necessary, until this `Mutex` is free, and
180 // Mutex::Unlock()
182 // Releases this `Mutex` and returns it from the exclusive/write state to the
183 // free state. Calling thread must hold the `Mutex` exclusively.
186 // Mutex::TryLock()
188 // If the mutex can be acquired without blocking, does so exclusively and
190 // probability if the `Mutex` was free.
193 // Mutex::AssertHeld()
195 // Require that the mutex be held exclusively (write mode) by this thread.
197 // If the mutex is not currently held by this thread, this function may report
207 // A Mutex can also be used as a starvation-free reader-writer lock.
211 // The Mutex API provides `Writer*()` aliases for the existing `Lock()`,
216 // Introducing reader locks necessarily complicates the `Mutex` state
218 // of a mutex in such cases. Note that ReaderLock() may block even if the lock
235 // Mutex::ReaderLock()
237 // Blocks the calling thread, if necessary, until this `Mutex` is either free,
240 // on the mutex.
244 // Mutex::ReaderUnlock()
246 // Releases a read share of this `Mutex`. `ReaderUnlock` may return a mutex to
247 // the free state if this thread holds the last reader lock on the mutex. Note
248 // that you cannot call `ReaderUnlock()` on a mutex held in write mode.
251 // Mutex::ReaderTryLock()
253 // If the mutex can be acquired without blocking, acquires this mutex for
255 // `true` with high probability if the `Mutex` was free or shared.
258 // Mutex::AssertReaderHeld()
260 // Require that the mutex be held at least in shared mode (read mode) by this
263 // If the mutex is not currently held by this thread, this function may report
269 // Mutex::WriterLock()
270 // Mutex::WriterUnlock()
271 // Mutex::WriterTryLock()
273 // Aliases for `Mutex::Lock()`, `Mutex::Unlock()`, and `Mutex::TryLock()`.
276 // methods) to distingish simple exclusive `Mutex` usage (`Lock()`,
290 // Conditional usage of a `Mutex` can occur using two distinct paradigms:
292 // * Use of `Mutex` member functions with `Condition` objects.
295 // In general, prefer use of `Condition` and the `Mutex` member functions
300 // `Mutex` contains member functions for performing lock operations only under
303 // the `Mutex`. The condition must be invariant w.r.t. environmental state
305 // always be invoked with the mutex held in at least read mode, so you should
318 // Mutex::Await()
320 // Unlocks this `Mutex` and blocks until simultaneously both `cond` is `true`
321 // and this `Mutex` can be reacquired, then reacquires this `Mutex` in the
325 // `Await()` requires that this thread holds this `Mutex` in some mode.
328 // Mutex::LockWhen()
329 // Mutex::ReaderLockWhen()
330 // Mutex::WriterLockWhen()
332 // Blocks until simultaneously both `cond` is `true` and this `Mutex` can
333 // be acquired, then atomically acquires this `Mutex`. `LockWhen()` is
345 // Mutex Variants with Timeouts/Deadlines
348 // Mutex::AwaitWithTimeout()
349 // Mutex::AwaitWithDeadline()
351 // Unlocks this `Mutex` and blocks until simultaneously:
354 // - this `Mutex` can be reacquired,
355 // then reacquire this `Mutex` in the same mode in which it was previously
364 // This method requires that this thread holds this `Mutex` in some mode.
369 // Mutex::LockWhenWithTimeout()
370 // Mutex::ReaderLockWhenWithTimeout()
371 // Mutex::WriterLockWhenWithTimeout()
375 // - this `Mutex` can be acquired,
376 // then atomically acquires this `Mutex`, returning `true` iff `cond` is
389 // Mutex::LockWhenWithDeadline()
390 // Mutex::ReaderLockWhenWithDeadline()
391 // Mutex::WriterLockWhenWithDeadline()
395 // - this `Mutex` can be acquired,
396 // then atomically acquires this Mutex, returning `true` iff `cond` is `true`
413 // Mutex::EnableInvariantDebugging()
417 // this `Mutex` should hold (for example: just after acquire, just before
423 // substantially reduce `Mutex` performance; it should be set only for
428 // Mutex::EnableDebugLog()
430 // Cause all subsequent uses of this `Mutex` to be logged via
434 // Note: This method substantially reduces `Mutex` performance.
439 // Mutex::ForgetDeadlockInfo()
442 // about this `Mutex`. Call this method in debug mode when the lock ordering
443 // of a `Mutex` changes.
446 // Mutex::AssertNotHeld()
448 // Return immediately if this thread does not hold this `Mutex` in any
465 // Mutex::InternalAttemptToUseMutexInFatalSignalHandler()
467 // Causes the `Mutex` implementation to prepare itself for re-entry caused by
468 // future use of `Mutex` within a fatal signal handler. This method is
481 std::atomic<intptr_t> mu_; // The Mutex state.
485 static void IncrementSynchSem(Mutex *mu, base_internal::PerThreadSynch *w);
486 static bool DecrementSynchSem(Mutex *mu, base_internal::PerThreadSynch *w,
504 // Block a thread on mutex.
510 void Trans(MuHow how); // used for CondVar->Mutex transfer
512 base_internal::PerThreadSynch *w); // used for CondVar->Mutex transfer
514 // Catch the error of writing Mutex when intending MutexLock.
515 Mutex(const volatile Mutex * /*ignored*/) {} // NOLINT(runtime/explicit) in Mutex() argument
517 Mutex(const Mutex&) = delete;
518 Mutex& operator=(const Mutex&) = delete;
522 // Mutex RAII Wrappers
527 // `MutexLock` is a helper class, which acquires and releases a `Mutex` via
541 // Mutex mu_;
550 explicit MutexLock(Mutex *mu) ABSL_EXCLUSIVE_LOCK_FUNCTION(mu) : mu_(mu) { in MutexLock()
557 explicit MutexLock(Mutex *mu, const Condition &cond) in MutexLock()
563 MutexLock(const MutexLock &) = delete; // NOLINT(runtime/mutex)
564 MutexLock(MutexLock&&) = delete; // NOLINT(runtime/mutex)
571 Mutex *const mu_;
577 // releases a shared lock on a `Mutex` via RAII.
580 explicit ReaderMutexLock(Mutex *mu) ABSL_SHARED_LOCK_FUNCTION(mu) : mu_(mu) { in ReaderMutexLock()
584 explicit ReaderMutexLock(Mutex *mu, const Condition &cond) in ReaderMutexLock()
598 Mutex *const mu_;
604 // releases a write (exclusive) lock on a `Mutex` via RAII.
607 explicit WriterMutexLock(Mutex *mu) ABSL_EXCLUSIVE_LOCK_FUNCTION(mu) in WriterMutexLock()
612 explicit WriterMutexLock(Mutex *mu, const Condition &cond) in WriterMutexLock()
626 Mutex *const mu_;
633 // `Mutex` contains a number of member functions which take a `Condition` as an
635 // to acquire the mutex. These sections are known as "condition critical"
637 // within an appropriate `Mutex` member function; everything else in the
645 // constant while the mutex is blocked on the condition (e.g. a stack variable),
646 // or objects of state protected explicitly by the mutex.
650 // exceptions. Correctness of `Mutex` / `Condition` is not guaranteed in
657 // suitable `Mutex' member function, such as `Mutex::Await()`, or to the
679 // + arg, or same pointer to object + method), so that the mutex implementation
731 // the lambda as it may be called when the mutex is being unlocked from a
818 // `Mutex` object, which can be signaled to wake callers.
819 // This class is not normally needed; use `Mutex` member functions such as
820 // `Mutex::Await()` and intrinsic `Condition` abstractions. In rare cases
830 // Usage for a thread waiting for some condition C protected by mutex mu:
857 // Atomically releases a `Mutex` and blocks on this condition variable.
859 // spurious wakeup), then reacquires the `Mutex` and returns.
861 // Requires and ensures that the current thread holds the `Mutex`.
862 void Wait(Mutex *mu);
866 // Atomically releases a `Mutex` and blocks on this condition variable.
869 // the `Mutex` and returns.
876 // Requires and ensures that the current thread holds the `Mutex`.
877 bool WaitWithTimeout(Mutex *mu, absl::Duration timeout);
881 // Atomically releases a `Mutex` and blocks on this condition variable.
884 // the `Mutex` and returns.
893 // Requires and ensures that the current thread holds the `Mutex`.
894 bool WaitWithDeadline(Mutex *mu, absl::Time deadline);
914 bool WaitCommon(Mutex *mutex, synchronization_internal::KernelTimeout t);
926 // Mutex::Unlock() and/or if-statements for clarity.
933 explicit MutexLockMaybe(Mutex *mu) ABSL_EXCLUSIVE_LOCK_FUNCTION(mu) in MutexLockMaybe()
940 explicit MutexLockMaybe(Mutex *mu, const Condition &cond) in MutexLockMaybe()
953 Mutex *const mu_;
963 // mutex before destruction. `Release()` may be called at most once.
966 explicit ReleasableMutexLock(Mutex *mu) ABSL_EXCLUSIVE_LOCK_FUNCTION(mu) in ReleasableMutexLock()
971 explicit ReleasableMutexLock(Mutex *mu, const Condition &cond) in ReleasableMutexLock()
984 Mutex *mu_;
991 inline Mutex::Mutex() : mu_(0) { in Mutex() function
995 inline constexpr Mutex::Mutex(absl::ConstInitType) : mu_(0) {} in Mutex() function
1047 // The function pointer registered here will be called whenever a mutex is
1059 // Register a hook for Mutex tracing.
1061 // The function pointer registered here will be called whenever a mutex is
1062 // contended. The callback is given an opaque handle to the contended mutex,
1107 // Enable or disable global support for Mutex invariant debugging. If enabled,
1108 // then invariant predicates can be registered per-Mutex for debug checking.
1109 // See Mutex::EnableInvariantDebugging().
1126 // due to Mutex lock ordering inversions. When set to 'kIgnore', tracking of