mutex.h 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  1. // Copyright 2017 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // -----------------------------------------------------------------------------
  16. // mutex.h
  17. // -----------------------------------------------------------------------------
  18. //
  19. // This header file defines a `Mutex` -- a mutually exclusive lock -- and the
  20. // most common type of synchronization primitive for facilitating locks on
  21. // shared resources. A mutex is used to prevent multiple threads from accessing
  22. // and/or writing to a shared resource concurrently.
  23. //
  24. // Unlike a `std::mutex`, the Abseil `Mutex` provides the following additional
  25. // features:
  26. // * Conditional predicates intrinsic to the `Mutex` object
  27. // * Shared/reader locks, in addition to standard exclusive/writer locks
  28. // * Deadlock detection and debug support.
  29. //
  30. // The following helper classes are also defined within this file:
  31. //
  32. // MutexLock - An RAII wrapper to acquire and release a `Mutex` for exclusive/
  33. // write access within the current scope.
  34. // ReaderMutexLock
  35. // - An RAII wrapper to acquire and release a `Mutex` for shared/read
  36. // access within the current scope.
  37. //
  38. // WriterMutexLock
  39. // - Alias for `MutexLock` above, designed for use in distinguishing
  40. // reader and writer locks within code.
  41. //
  42. // In addition to simple mutex locks, this file also defines ways to perform
  43. // locking under certain conditions.
  44. //
  45. // Condition - (Preferred) Used to wait for a particular predicate that
  46. // depends on state protected by the `Mutex` to become true.
  47. // CondVar - A lower-level variant of `Condition` that relies on
  48. // application code to explicitly signal the `CondVar` when
  49. // a condition has been met.
  50. //
  51. // See below for more information on using `Condition` or `CondVar`.
  52. //
  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
  55. // more complete information and examples.
  56. #ifndef ABSL_SYNCHRONIZATION_MUTEX_H_
  57. #define ABSL_SYNCHRONIZATION_MUTEX_H_
  58. #include <atomic>
  59. #include <cstdint>
  60. #include <string>
  61. #include "absl/base/const_init.h"
  62. #include "absl/base/internal/identity.h"
  63. #include "absl/base/internal/low_level_alloc.h"
  64. #include "absl/base/internal/thread_identity.h"
  65. #include "absl/base/internal/tsan_mutex_interface.h"
  66. #include "absl/base/port.h"
  67. #include "absl/base/thread_annotations.h"
  68. #include "absl/synchronization/internal/kernel_timeout.h"
  69. #include "absl/synchronization/internal/per_thread_sem.h"
  70. #include "absl/time/time.h"
  71. // Decide if we should use the non-production implementation because
  72. // the production implementation hasn't been fully ported yet.
  73. #ifdef ABSL_INTERNAL_USE_NONPROD_MUTEX
  74. #error ABSL_INTERNAL_USE_NONPROD_MUTEX cannot be directly set
  75. #elif defined(ABSL_LOW_LEVEL_ALLOC_MISSING)
  76. #define ABSL_INTERNAL_USE_NONPROD_MUTEX 1
  77. #include "absl/synchronization/internal/mutex_nonprod.inc"
  78. #endif
  79. namespace absl {
  80. ABSL_NAMESPACE_BEGIN
  81. class Condition;
  82. struct SynchWaitParams;
  83. // -----------------------------------------------------------------------------
  84. // Mutex
  85. // -----------------------------------------------------------------------------
  86. //
  87. // A `Mutex` is a non-reentrant (aka non-recursive) Mutually Exclusive lock
  88. // on some resource, typically a variable or data structure with associated
  89. // invariants. Proper usage of mutexes prevents concurrent access by different
  90. // threads to the same resource.
  91. //
  92. // A `Mutex` has two basic operations: `Mutex::Lock()` and `Mutex::Unlock()`.
  93. // The `Lock()` operation *acquires* a `Mutex` (in a state known as an
  94. // *exclusive* -- or write -- lock), while the `Unlock()` operation *releases* a
  95. // Mutex. During the span of time between the Lock() and Unlock() operations,
  96. // a mutex is said to be *held*. By design all mutexes support exclusive/write
  97. // locks, as this is the most common way to use a mutex.
  98. //
  99. // The `Mutex` state machine for basic lock/unlock operations is quite simple:
  100. //
  101. // | | Lock() | Unlock() |
  102. // |----------------+------------+----------|
  103. // | Free | Exclusive | invalid |
  104. // | Exclusive | blocks | Free |
  105. //
  106. // Attempts to `Unlock()` must originate from the thread that performed the
  107. // corresponding `Lock()` operation.
  108. //
  109. // An "invalid" operation is disallowed by the API. The `Mutex` implementation
  110. // is allowed to do anything on an invalid call, including but not limited to
  111. // crashing with a useful error message, silently succeeding, or corrupting
  112. // data structures. In debug mode, the implementation attempts to crash with a
  113. // useful error message.
  114. //
  115. // `Mutex` is not guaranteed to be "fair" in prioritizing waiting threads; it
  116. // is, however, approximately fair over long periods, and starvation-free for
  117. // threads at the same priority.
  118. //
  119. // The lock/unlock primitives are now annotated with lock annotations
  120. // defined in (base/thread_annotations.h). When writing multi-threaded code,
  121. // you should use lock annotations whenever possible to document your lock
  122. // synchronization policy. Besides acting as documentation, these annotations
  123. // also help compilers or static analysis tools to identify and warn about
  124. // issues that could potentially result in race conditions and deadlocks.
  125. //
  126. // For more information about the lock annotations, please see
  127. // [Thread Safety Analysis](http://clang.llvm.org/docs/ThreadSafetyAnalysis.html)
  128. // in the Clang documentation.
  129. //
  130. // See also `MutexLock`, below, for scoped `Mutex` acquisition.
  131. class ABSL_LOCKABLE Mutex {
  132. public:
  133. // Creates a `Mutex` that is not held by anyone. This constructor is
  134. // typically used for Mutexes allocated on the heap or the stack.
  135. //
  136. // To create `Mutex` instances with static storage duration
  137. // (e.g. a namespace-scoped or global variable), see
  138. // `Mutex::Mutex(absl::kConstInit)` below instead.
  139. Mutex();
  140. // Creates a mutex with static storage duration. A global variable
  141. // constructed this way avoids the lifetime issues that can occur on program
  142. // startup and shutdown. (See absl/base/const_init.h.)
  143. //
  144. // For Mutexes allocated on the heap and stack, instead use the default
  145. // constructor, which can interact more fully with the thread sanitizer.
  146. //
  147. // Example usage:
  148. // namespace foo {
  149. // ABSL_CONST_INIT Mutex mu(absl::kConstInit);
  150. // }
  151. explicit constexpr Mutex(absl::ConstInitType);
  152. ~Mutex();
  153. // Mutex::Lock()
  154. //
  155. // Blocks the calling thread, if necessary, until this `Mutex` is free, and
  156. // then acquires it exclusively. (This lock is also known as a "write lock.")
  157. void Lock() ABSL_EXCLUSIVE_LOCK_FUNCTION();
  158. // Mutex::Unlock()
  159. //
  160. // Releases this `Mutex` and returns it from the exclusive/write state to the
  161. // free state. Caller must hold the `Mutex` exclusively.
  162. void Unlock() ABSL_UNLOCK_FUNCTION();
  163. // Mutex::TryLock()
  164. //
  165. // If the mutex can be acquired without blocking, does so exclusively and
  166. // returns `true`. Otherwise, returns `false`. Returns `true` with high
  167. // probability if the `Mutex` was free.
  168. bool TryLock() ABSL_EXCLUSIVE_TRYLOCK_FUNCTION(true);
  169. // Mutex::AssertHeld()
  170. //
  171. // Return immediately if this thread holds the `Mutex` exclusively (in write
  172. // mode). Otherwise, may report an error (typically by crashing with a
  173. // diagnostic), or may return immediately.
  174. void AssertHeld() const ABSL_ASSERT_EXCLUSIVE_LOCK();
  175. // ---------------------------------------------------------------------------
  176. // Reader-Writer Locking
  177. // ---------------------------------------------------------------------------
  178. // A Mutex can also be used as a starvation-free reader-writer lock.
  179. // Neither read-locks nor write-locks are reentrant/recursive to avoid
  180. // potential client programming errors.
  181. //
  182. // The Mutex API provides `Writer*()` aliases for the existing `Lock()`,
  183. // `Unlock()` and `TryLock()` methods for use within applications mixing
  184. // reader/writer locks. Using `Reader*()` and `Writer*()` operations in this
  185. // manner can make locking behavior clearer when mixing read and write modes.
  186. //
  187. // Introducing reader locks necessarily complicates the `Mutex` state
  188. // machine somewhat. The table below illustrates the allowed state transitions
  189. // of a mutex in such cases. Note that ReaderLock() may block even if the lock
  190. // is held in shared mode; this occurs when another thread is blocked on a
  191. // call to WriterLock().
  192. //
  193. // ---------------------------------------------------------------------------
  194. // Operation: WriterLock() Unlock() ReaderLock() ReaderUnlock()
  195. // ---------------------------------------------------------------------------
  196. // State
  197. // ---------------------------------------------------------------------------
  198. // Free Exclusive invalid Shared(1) invalid
  199. // Shared(1) blocks invalid Shared(2) or blocks Free
  200. // Shared(n) n>1 blocks invalid Shared(n+1) or blocks Shared(n-1)
  201. // Exclusive blocks Free blocks invalid
  202. // ---------------------------------------------------------------------------
  203. //
  204. // In comments below, "shared" refers to a state of Shared(n) for any n > 0.
  205. // Mutex::ReaderLock()
  206. //
  207. // Blocks the calling thread, if necessary, until this `Mutex` is either free,
  208. // or in shared mode, and then acquires a share of it. Note that
  209. // `ReaderLock()` will block if some other thread has an exclusive/writer lock
  210. // on the mutex.
  211. void ReaderLock() ABSL_SHARED_LOCK_FUNCTION();
  212. // Mutex::ReaderUnlock()
  213. //
  214. // Releases a read share of this `Mutex`. `ReaderUnlock` may return a mutex to
  215. // the free state if this thread holds the last reader lock on the mutex. Note
  216. // that you cannot call `ReaderUnlock()` on a mutex held in write mode.
  217. void ReaderUnlock() ABSL_UNLOCK_FUNCTION();
  218. // Mutex::ReaderTryLock()
  219. //
  220. // If the mutex can be acquired without blocking, acquires this mutex for
  221. // shared access and returns `true`. Otherwise, returns `false`. Returns
  222. // `true` with high probability if the `Mutex` was free or shared.
  223. bool ReaderTryLock() ABSL_SHARED_TRYLOCK_FUNCTION(true);
  224. // Mutex::AssertReaderHeld()
  225. //
  226. // Returns immediately if this thread holds the `Mutex` in at least shared
  227. // mode (read mode). Otherwise, may report an error (typically by
  228. // crashing with a diagnostic), or may return immediately.
  229. void AssertReaderHeld() const ABSL_ASSERT_SHARED_LOCK();
  230. // Mutex::WriterLock()
  231. // Mutex::WriterUnlock()
  232. // Mutex::WriterTryLock()
  233. //
  234. // Aliases for `Mutex::Lock()`, `Mutex::Unlock()`, and `Mutex::TryLock()`.
  235. //
  236. // These methods may be used (along with the complementary `Reader*()`
  237. // methods) to distingish simple exclusive `Mutex` usage (`Lock()`,
  238. // etc.) from reader/writer lock usage.
  239. void WriterLock() ABSL_EXCLUSIVE_LOCK_FUNCTION() { this->Lock(); }
  240. void WriterUnlock() ABSL_UNLOCK_FUNCTION() { this->Unlock(); }
  241. bool WriterTryLock() ABSL_EXCLUSIVE_TRYLOCK_FUNCTION(true) {
  242. return this->TryLock();
  243. }
  244. // ---------------------------------------------------------------------------
  245. // Conditional Critical Regions
  246. // ---------------------------------------------------------------------------
  247. // Conditional usage of a `Mutex` can occur using two distinct paradigms:
  248. //
  249. // * Use of `Mutex` member functions with `Condition` objects.
  250. // * Use of the separate `CondVar` abstraction.
  251. //
  252. // In general, prefer use of `Condition` and the `Mutex` member functions
  253. // listed below over `CondVar`. When there are multiple threads waiting on
  254. // distinctly different conditions, however, a battery of `CondVar`s may be
  255. // more efficient. This section discusses use of `Condition` objects.
  256. //
  257. // `Mutex` contains member functions for performing lock operations only under
  258. // certain conditions, of class `Condition`. For correctness, the `Condition`
  259. // must return a boolean that is a pure function, only of state protected by
  260. // the `Mutex`. The condition must be invariant w.r.t. environmental state
  261. // such as thread, cpu id, or time, and must be `noexcept`. The condition will
  262. // always be invoked with the mutex held in at least read mode, so you should
  263. // not block it for long periods or sleep it on a timer.
  264. //
  265. // Since a condition must not depend directly on the current time, use
  266. // `*WithTimeout()` member function variants to make your condition
  267. // effectively true after a given duration, or `*WithDeadline()` variants to
  268. // make your condition effectively true after a given time.
  269. //
  270. // The condition function should have no side-effects aside from debug
  271. // logging; as a special exception, the function may acquire other mutexes
  272. // provided it releases all those that it acquires. (This exception was
  273. // required to allow logging.)
  274. // Mutex::Await()
  275. //
  276. // Unlocks this `Mutex` and blocks until simultaneously both `cond` is `true`
  277. // and this `Mutex` can be reacquired, then reacquires this `Mutex` in the
  278. // same mode in which it was previously held. If the condition is initially
  279. // `true`, `Await()` *may* skip the release/re-acquire step.
  280. //
  281. // `Await()` requires that this thread holds this `Mutex` in some mode.
  282. void Await(const Condition &cond);
  283. // Mutex::LockWhen()
  284. // Mutex::ReaderLockWhen()
  285. // Mutex::WriterLockWhen()
  286. //
  287. // Blocks until simultaneously both `cond` is `true` and this `Mutex` can
  288. // be acquired, then atomically acquires this `Mutex`. `LockWhen()` is
  289. // logically equivalent to `*Lock(); Await();` though they may have different
  290. // performance characteristics.
  291. void LockWhen(const Condition &cond) ABSL_EXCLUSIVE_LOCK_FUNCTION();
  292. void ReaderLockWhen(const Condition &cond) ABSL_SHARED_LOCK_FUNCTION();
  293. void WriterLockWhen(const Condition &cond) ABSL_EXCLUSIVE_LOCK_FUNCTION() {
  294. this->LockWhen(cond);
  295. }
  296. // ---------------------------------------------------------------------------
  297. // Mutex Variants with Timeouts/Deadlines
  298. // ---------------------------------------------------------------------------
  299. // Mutex::AwaitWithTimeout()
  300. // Mutex::AwaitWithDeadline()
  301. //
  302. // Unlocks this `Mutex` and blocks until simultaneously:
  303. // - either `cond` is true or the {timeout has expired, deadline has passed}
  304. // and
  305. // - this `Mutex` can be reacquired,
  306. // then reacquire this `Mutex` in the same mode in which it was previously
  307. // held, returning `true` iff `cond` is `true` on return.
  308. //
  309. // If the condition is initially `true`, the implementation *may* skip the
  310. // release/re-acquire step and return immediately.
  311. //
  312. // Deadlines in the past are equivalent to an immediate deadline.
  313. // Negative timeouts are equivalent to a zero timeout.
  314. //
  315. // This method requires that this thread holds this `Mutex` in some mode.
  316. bool AwaitWithTimeout(const Condition &cond, absl::Duration timeout);
  317. bool AwaitWithDeadline(const Condition &cond, absl::Time deadline);
  318. // Mutex::LockWhenWithTimeout()
  319. // Mutex::ReaderLockWhenWithTimeout()
  320. // Mutex::WriterLockWhenWithTimeout()
  321. //
  322. // Blocks until simultaneously both:
  323. // - either `cond` is `true` or the timeout has expired, and
  324. // - this `Mutex` can be acquired,
  325. // then atomically acquires this `Mutex`, returning `true` iff `cond` is
  326. // `true` on return.
  327. //
  328. // Negative timeouts are equivalent to a zero timeout.
  329. bool LockWhenWithTimeout(const Condition &cond, absl::Duration timeout)
  330. ABSL_EXCLUSIVE_LOCK_FUNCTION();
  331. bool ReaderLockWhenWithTimeout(const Condition &cond, absl::Duration timeout)
  332. ABSL_SHARED_LOCK_FUNCTION();
  333. bool WriterLockWhenWithTimeout(const Condition &cond, absl::Duration timeout)
  334. ABSL_EXCLUSIVE_LOCK_FUNCTION() {
  335. return this->LockWhenWithTimeout(cond, timeout);
  336. }
  337. // Mutex::LockWhenWithDeadline()
  338. // Mutex::ReaderLockWhenWithDeadline()
  339. // Mutex::WriterLockWhenWithDeadline()
  340. //
  341. // Blocks until simultaneously both:
  342. // - either `cond` is `true` or the deadline has been passed, and
  343. // - this `Mutex` can be acquired,
  344. // then atomically acquires this Mutex, returning `true` iff `cond` is `true`
  345. // on return.
  346. //
  347. // Deadlines in the past are equivalent to an immediate deadline.
  348. bool LockWhenWithDeadline(const Condition &cond, absl::Time deadline)
  349. ABSL_EXCLUSIVE_LOCK_FUNCTION();
  350. bool ReaderLockWhenWithDeadline(const Condition &cond, absl::Time deadline)
  351. ABSL_SHARED_LOCK_FUNCTION();
  352. bool WriterLockWhenWithDeadline(const Condition &cond, absl::Time deadline)
  353. ABSL_EXCLUSIVE_LOCK_FUNCTION() {
  354. return this->LockWhenWithDeadline(cond, deadline);
  355. }
  356. // ---------------------------------------------------------------------------
  357. // Debug Support: Invariant Checking, Deadlock Detection, Logging.
  358. // ---------------------------------------------------------------------------
  359. // Mutex::EnableInvariantDebugging()
  360. //
  361. // If `invariant`!=null and if invariant debugging has been enabled globally,
  362. // cause `(*invariant)(arg)` to be called at moments when the invariant for
  363. // this `Mutex` should hold (for example: just after acquire, just before
  364. // release).
  365. //
  366. // The routine `invariant` should have no side-effects since it is not
  367. // guaranteed how many times it will be called; it should check the invariant
  368. // and crash if it does not hold. Enabling global invariant debugging may
  369. // substantially reduce `Mutex` performance; it should be set only for
  370. // non-production runs. Optimization options may also disable invariant
  371. // checks.
  372. void EnableInvariantDebugging(void (*invariant)(void *), void *arg);
  373. // Mutex::EnableDebugLog()
  374. //
  375. // Cause all subsequent uses of this `Mutex` to be logged via
  376. // `ABSL_RAW_LOG(INFO)`. Log entries are tagged with `name` if no previous
  377. // call to `EnableInvariantDebugging()` or `EnableDebugLog()` has been made.
  378. //
  379. // Note: This method substantially reduces `Mutex` performance.
  380. void EnableDebugLog(const char *name);
  381. // Deadlock detection
  382. // Mutex::ForgetDeadlockInfo()
  383. //
  384. // Forget any deadlock-detection information previously gathered
  385. // about this `Mutex`. Call this method in debug mode when the lock ordering
  386. // of a `Mutex` changes.
  387. void ForgetDeadlockInfo();
  388. // Mutex::AssertNotHeld()
  389. //
  390. // Return immediately if this thread does not hold this `Mutex` in any
  391. // mode; otherwise, may report an error (typically by crashing with a
  392. // diagnostic), or may return immediately.
  393. //
  394. // Currently this check is performed only if all of:
  395. // - in debug mode
  396. // - SetMutexDeadlockDetectionMode() has been set to kReport or kAbort
  397. // - number of locks concurrently held by this thread is not large.
  398. // are true.
  399. void AssertNotHeld() const;
  400. // Special cases.
  401. // A `MuHow` is a constant that indicates how a lock should be acquired.
  402. // Internal implementation detail. Clients should ignore.
  403. typedef const struct MuHowS *MuHow;
  404. // Mutex::InternalAttemptToUseMutexInFatalSignalHandler()
  405. //
  406. // Causes the `Mutex` implementation to prepare itself for re-entry caused by
  407. // future use of `Mutex` within a fatal signal handler. This method is
  408. // intended for use only for last-ditch attempts to log crash information.
  409. // It does not guarantee that attempts to use Mutexes within the handler will
  410. // not deadlock; it merely makes other faults less likely.
  411. //
  412. // WARNING: This routine must be invoked from a signal handler, and the
  413. // signal handler must either loop forever or terminate the process.
  414. // Attempts to return from (or `longjmp` out of) the signal handler once this
  415. // call has been made may cause arbitrary program behaviour including
  416. // crashes and deadlocks.
  417. static void InternalAttemptToUseMutexInFatalSignalHandler();
  418. private:
  419. #ifdef ABSL_INTERNAL_USE_NONPROD_MUTEX
  420. friend class CondVar;
  421. synchronization_internal::MutexImpl *impl() { return impl_.get(); }
  422. synchronization_internal::SynchronizationStorage<
  423. synchronization_internal::MutexImpl>
  424. impl_;
  425. #else
  426. std::atomic<intptr_t> mu_; // The Mutex state.
  427. // Post()/Wait() versus associated PerThreadSem; in class for required
  428. // friendship with PerThreadSem.
  429. static inline void IncrementSynchSem(Mutex *mu,
  430. base_internal::PerThreadSynch *w);
  431. static inline bool DecrementSynchSem(
  432. Mutex *mu, base_internal::PerThreadSynch *w,
  433. synchronization_internal::KernelTimeout t);
  434. // slow path acquire
  435. void LockSlowLoop(SynchWaitParams *waitp, int flags);
  436. // wrappers around LockSlowLoop()
  437. bool LockSlowWithDeadline(MuHow how, const Condition *cond,
  438. synchronization_internal::KernelTimeout t,
  439. int flags);
  440. void LockSlow(MuHow how, const Condition *cond,
  441. int flags) ABSL_ATTRIBUTE_COLD;
  442. // slow path release
  443. void UnlockSlow(SynchWaitParams *waitp) ABSL_ATTRIBUTE_COLD;
  444. // Common code between Await() and AwaitWithTimeout/Deadline()
  445. bool AwaitCommon(const Condition &cond,
  446. synchronization_internal::KernelTimeout t);
  447. // Attempt to remove thread s from queue.
  448. void TryRemove(base_internal::PerThreadSynch *s);
  449. // Block a thread on mutex.
  450. void Block(base_internal::PerThreadSynch *s);
  451. // Wake a thread; return successor.
  452. base_internal::PerThreadSynch *Wakeup(base_internal::PerThreadSynch *w);
  453. friend class CondVar; // for access to Trans()/Fer().
  454. void Trans(MuHow how); // used for CondVar->Mutex transfer
  455. void Fer(
  456. base_internal::PerThreadSynch *w); // used for CondVar->Mutex transfer
  457. #endif
  458. // Catch the error of writing Mutex when intending MutexLock.
  459. Mutex(const volatile Mutex * /*ignored*/) {} // NOLINT(runtime/explicit)
  460. Mutex(const Mutex&) = delete;
  461. Mutex& operator=(const Mutex&) = delete;
  462. };
  463. // -----------------------------------------------------------------------------
  464. // Mutex RAII Wrappers
  465. // -----------------------------------------------------------------------------
  466. // MutexLock
  467. //
  468. // `MutexLock` is a helper class, which acquires and releases a `Mutex` via
  469. // RAII.
  470. //
  471. // Example:
  472. //
  473. // Class Foo {
  474. //
  475. // Foo::Bar* Baz() {
  476. // MutexLock l(&lock_);
  477. // ...
  478. // return bar;
  479. // }
  480. //
  481. // private:
  482. // Mutex lock_;
  483. // };
  484. class ABSL_SCOPED_LOCKABLE MutexLock {
  485. public:
  486. explicit MutexLock(Mutex *mu) ABSL_EXCLUSIVE_LOCK_FUNCTION(mu) : mu_(mu) {
  487. this->mu_->Lock();
  488. }
  489. MutexLock(const MutexLock &) = delete; // NOLINT(runtime/mutex)
  490. MutexLock(MutexLock&&) = delete; // NOLINT(runtime/mutex)
  491. MutexLock& operator=(const MutexLock&) = delete;
  492. MutexLock& operator=(MutexLock&&) = delete;
  493. ~MutexLock() ABSL_UNLOCK_FUNCTION() { this->mu_->Unlock(); }
  494. private:
  495. Mutex *const mu_;
  496. };
  497. // ReaderMutexLock
  498. //
  499. // The `ReaderMutexLock` is a helper class, like `MutexLock`, which acquires and
  500. // releases a shared lock on a `Mutex` via RAII.
  501. class ABSL_SCOPED_LOCKABLE ReaderMutexLock {
  502. public:
  503. explicit ReaderMutexLock(Mutex *mu) ABSL_SHARED_LOCK_FUNCTION(mu) : mu_(mu) {
  504. mu->ReaderLock();
  505. }
  506. ReaderMutexLock(const ReaderMutexLock&) = delete;
  507. ReaderMutexLock(ReaderMutexLock&&) = delete;
  508. ReaderMutexLock& operator=(const ReaderMutexLock&) = delete;
  509. ReaderMutexLock& operator=(ReaderMutexLock&&) = delete;
  510. ~ReaderMutexLock() ABSL_UNLOCK_FUNCTION() { this->mu_->ReaderUnlock(); }
  511. private:
  512. Mutex *const mu_;
  513. };
  514. // WriterMutexLock
  515. //
  516. // The `WriterMutexLock` is a helper class, like `MutexLock`, which acquires and
  517. // releases a write (exclusive) lock on a `Mutex` via RAII.
  518. class ABSL_SCOPED_LOCKABLE WriterMutexLock {
  519. public:
  520. explicit WriterMutexLock(Mutex *mu) ABSL_EXCLUSIVE_LOCK_FUNCTION(mu)
  521. : mu_(mu) {
  522. mu->WriterLock();
  523. }
  524. WriterMutexLock(const WriterMutexLock&) = delete;
  525. WriterMutexLock(WriterMutexLock&&) = delete;
  526. WriterMutexLock& operator=(const WriterMutexLock&) = delete;
  527. WriterMutexLock& operator=(WriterMutexLock&&) = delete;
  528. ~WriterMutexLock() ABSL_UNLOCK_FUNCTION() { this->mu_->WriterUnlock(); }
  529. private:
  530. Mutex *const mu_;
  531. };
  532. // -----------------------------------------------------------------------------
  533. // Condition
  534. // -----------------------------------------------------------------------------
  535. //
  536. // As noted above, `Mutex` contains a number of member functions which take a
  537. // `Condition` as an argument; clients can wait for conditions to become `true`
  538. // before attempting to acquire the mutex. These sections are known as
  539. // "condition critical" sections. To use a `Condition`, you simply need to
  540. // construct it, and use within an appropriate `Mutex` member function;
  541. // everything else in the `Condition` class is an implementation detail.
  542. //
  543. // A `Condition` is specified as a function pointer which returns a boolean.
  544. // `Condition` functions should be pure functions -- their results should depend
  545. // only on passed arguments, should not consult any external state (such as
  546. // clocks), and should have no side-effects, aside from debug logging. Any
  547. // objects that the function may access should be limited to those which are
  548. // constant while the mutex is blocked on the condition (e.g. a stack variable),
  549. // or objects of state protected explicitly by the mutex.
  550. //
  551. // No matter which construction is used for `Condition`, the underlying
  552. // function pointer / functor / callable must not throw any
  553. // exceptions. Correctness of `Mutex` / `Condition` is not guaranteed in
  554. // the face of a throwing `Condition`. (When Abseil is allowed to depend
  555. // on C++17, these function pointers will be explicitly marked
  556. // `noexcept`; until then this requirement cannot be enforced in the
  557. // type system.)
  558. //
  559. // Note: to use a `Condition`, you need only construct it and pass it within the
  560. // appropriate `Mutex' member function, such as `Mutex::Await()`.
  561. //
  562. // Example:
  563. //
  564. // // assume count_ is not internal reference count
  565. // int count_ ABSL_GUARDED_BY(mu_);
  566. //
  567. // mu_.LockWhen(Condition(+[](int* count) { return *count == 0; },
  568. // &count_));
  569. //
  570. // When multiple threads are waiting on exactly the same condition, make sure
  571. // that they are constructed with the same parameters (same pointer to function
  572. // + arg, or same pointer to object + method), so that the mutex implementation
  573. // can avoid redundantly evaluating the same condition for each thread.
  574. class Condition {
  575. public:
  576. // A Condition that returns the result of "(*func)(arg)"
  577. Condition(bool (*func)(void *), void *arg);
  578. // Templated version for people who are averse to casts.
  579. //
  580. // To use a lambda, prepend it with unary plus, which converts the lambda
  581. // into a function pointer:
  582. // Condition(+[](T* t) { return ...; }, arg).
  583. //
  584. // Note: lambdas in this case must contain no bound variables.
  585. //
  586. // See class comment for performance advice.
  587. template<typename T>
  588. Condition(bool (*func)(T *), T *arg);
  589. // Templated version for invoking a method that returns a `bool`.
  590. //
  591. // `Condition(object, &Class::Method)` constructs a `Condition` that evaluates
  592. // `object->Method()`.
  593. //
  594. // Implementation Note: `absl::internal::identity` is used to allow methods to
  595. // come from base classes. A simpler signature like
  596. // `Condition(T*, bool (T::*)())` does not suffice.
  597. template<typename T>
  598. Condition(T *object, bool (absl::internal::identity<T>::type::* method)());
  599. // Same as above, for const members
  600. template<typename T>
  601. Condition(const T *object,
  602. bool (absl::internal::identity<T>::type::* method)() const);
  603. // A Condition that returns the value of `*cond`
  604. explicit Condition(const bool *cond);
  605. // Templated version for invoking a functor that returns a `bool`.
  606. // This approach accepts pointers to non-mutable lambdas, `std::function`,
  607. // the result of` std::bind` and user-defined functors that define
  608. // `bool F::operator()() const`.
  609. //
  610. // Example:
  611. //
  612. // auto reached = [this, current]() {
  613. // mu_.AssertReaderHeld(); // For annotalysis.
  614. // return processed_ >= current;
  615. // };
  616. // mu_.Await(Condition(&reached));
  617. //
  618. // NOTE: never use "mu_.AssertHeld()" instead of "mu_.AssertReadHeld()" in the
  619. // lambda as it may be called when the mutex is being unlocked from a scope
  620. // holding only a reader lock, which will make the assertion not fulfilled and
  621. // crash the binary.
  622. // See class comment for performance advice. In particular, if there
  623. // might be more than one waiter for the same condition, make sure
  624. // that all waiters construct the condition with the same pointers.
  625. // Implementation note: The second template parameter ensures that this
  626. // constructor doesn't participate in overload resolution if T doesn't have
  627. // `bool operator() const`.
  628. template <typename T, typename E = decltype(
  629. static_cast<bool (T::*)() const>(&T::operator()))>
  630. explicit Condition(const T *obj)
  631. : Condition(obj, static_cast<bool (T::*)() const>(&T::operator())) {}
  632. // A Condition that always returns `true`.
  633. static const Condition kTrue;
  634. // Evaluates the condition.
  635. bool Eval() const;
  636. // Returns `true` if the two conditions are guaranteed to return the same
  637. // value if evaluated at the same time, `false` if the evaluation *may* return
  638. // different results.
  639. //
  640. // Two `Condition` values are guaranteed equal if both their `func` and `arg`
  641. // components are the same. A null pointer is equivalent to a `true`
  642. // condition.
  643. static bool GuaranteedEqual(const Condition *a, const Condition *b);
  644. private:
  645. typedef bool (*InternalFunctionType)(void * arg);
  646. typedef bool (Condition::*InternalMethodType)();
  647. typedef bool (*InternalMethodCallerType)(void * arg,
  648. InternalMethodType internal_method);
  649. bool (*eval_)(const Condition*); // Actual evaluator
  650. InternalFunctionType function_; // function taking pointer returning bool
  651. InternalMethodType method_; // method returning bool
  652. void *arg_; // arg of function_ or object of method_
  653. Condition(); // null constructor used only to create kTrue
  654. // Various functions eval_ can point to:
  655. static bool CallVoidPtrFunction(const Condition*);
  656. template <typename T> static bool CastAndCallFunction(const Condition* c);
  657. template <typename T> static bool CastAndCallMethod(const Condition* c);
  658. };
  659. // -----------------------------------------------------------------------------
  660. // CondVar
  661. // -----------------------------------------------------------------------------
  662. //
  663. // A condition variable, reflecting state evaluated separately outside of the
  664. // `Mutex` object, which can be signaled to wake callers.
  665. // This class is not normally needed; use `Mutex` member functions such as
  666. // `Mutex::Await()` and intrinsic `Condition` abstractions. In rare cases
  667. // with many threads and many conditions, `CondVar` may be faster.
  668. //
  669. // The implementation may deliver signals to any condition variable at
  670. // any time, even when no call to `Signal()` or `SignalAll()` is made; as a
  671. // result, upon being awoken, you must check the logical condition you have
  672. // been waiting upon.
  673. //
  674. // Examples:
  675. //
  676. // Usage for a thread waiting for some condition C protected by mutex mu:
  677. // mu.Lock();
  678. // while (!C) { cv->Wait(&mu); } // releases and reacquires mu
  679. // // C holds; process data
  680. // mu.Unlock();
  681. //
  682. // Usage to wake T is:
  683. // mu.Lock();
  684. // // process data, possibly establishing C
  685. // if (C) { cv->Signal(); }
  686. // mu.Unlock();
  687. //
  688. // If C may be useful to more than one waiter, use `SignalAll()` instead of
  689. // `Signal()`.
  690. //
  691. // With this implementation it is efficient to use `Signal()/SignalAll()` inside
  692. // the locked region; this usage can make reasoning about your program easier.
  693. //
  694. class CondVar {
  695. public:
  696. // A `CondVar` allocated on the heap or on the stack can use the this
  697. // constructor.
  698. CondVar();
  699. ~CondVar();
  700. // CondVar::Wait()
  701. //
  702. // Atomically releases a `Mutex` and blocks on this condition variable.
  703. // Waits until awakened by a call to `Signal()` or `SignalAll()` (or a
  704. // spurious wakeup), then reacquires the `Mutex` and returns.
  705. //
  706. // Requires and ensures that the current thread holds the `Mutex`.
  707. void Wait(Mutex *mu);
  708. // CondVar::WaitWithTimeout()
  709. //
  710. // Atomically releases a `Mutex` and blocks on this condition variable.
  711. // Waits until awakened by a call to `Signal()` or `SignalAll()` (or a
  712. // spurious wakeup), or until the timeout has expired, then reacquires
  713. // the `Mutex` and returns.
  714. //
  715. // Returns true if the timeout has expired without this `CondVar`
  716. // being signalled in any manner. If both the timeout has expired
  717. // and this `CondVar` has been signalled, the implementation is free
  718. // to return `true` or `false`.
  719. //
  720. // Requires and ensures that the current thread holds the `Mutex`.
  721. bool WaitWithTimeout(Mutex *mu, absl::Duration timeout);
  722. // CondVar::WaitWithDeadline()
  723. //
  724. // Atomically releases a `Mutex` and blocks on this condition variable.
  725. // Waits until awakened by a call to `Signal()` or `SignalAll()` (or a
  726. // spurious wakeup), or until the deadline has passed, then reacquires
  727. // the `Mutex` and returns.
  728. //
  729. // Deadlines in the past are equivalent to an immediate deadline.
  730. //
  731. // Returns true if the deadline has passed without this `CondVar`
  732. // being signalled in any manner. If both the deadline has passed
  733. // and this `CondVar` has been signalled, the implementation is free
  734. // to return `true` or `false`.
  735. //
  736. // Requires and ensures that the current thread holds the `Mutex`.
  737. bool WaitWithDeadline(Mutex *mu, absl::Time deadline);
  738. // CondVar::Signal()
  739. //
  740. // Signal this `CondVar`; wake at least one waiter if one exists.
  741. void Signal();
  742. // CondVar::SignalAll()
  743. //
  744. // Signal this `CondVar`; wake all waiters.
  745. void SignalAll();
  746. // CondVar::EnableDebugLog()
  747. //
  748. // Causes all subsequent uses of this `CondVar` to be logged via
  749. // `ABSL_RAW_LOG(INFO)`. Log entries are tagged with `name` if `name != 0`.
  750. // Note: this method substantially reduces `CondVar` performance.
  751. void EnableDebugLog(const char *name);
  752. private:
  753. #ifdef ABSL_INTERNAL_USE_NONPROD_MUTEX
  754. synchronization_internal::CondVarImpl *impl() { return impl_.get(); }
  755. synchronization_internal::SynchronizationStorage<
  756. synchronization_internal::CondVarImpl>
  757. impl_;
  758. #else
  759. bool WaitCommon(Mutex *mutex, synchronization_internal::KernelTimeout t);
  760. void Remove(base_internal::PerThreadSynch *s);
  761. void Wakeup(base_internal::PerThreadSynch *w);
  762. std::atomic<intptr_t> cv_; // Condition variable state.
  763. #endif
  764. CondVar(const CondVar&) = delete;
  765. CondVar& operator=(const CondVar&) = delete;
  766. };
  767. // Variants of MutexLock.
  768. //
  769. // If you find yourself using one of these, consider instead using
  770. // Mutex::Unlock() and/or if-statements for clarity.
  771. // MutexLockMaybe
  772. //
  773. // MutexLockMaybe is like MutexLock, but is a no-op when mu is null.
  774. class ABSL_SCOPED_LOCKABLE MutexLockMaybe {
  775. public:
  776. explicit MutexLockMaybe(Mutex *mu) ABSL_EXCLUSIVE_LOCK_FUNCTION(mu)
  777. : mu_(mu) {
  778. if (this->mu_ != nullptr) {
  779. this->mu_->Lock();
  780. }
  781. }
  782. ~MutexLockMaybe() ABSL_UNLOCK_FUNCTION() {
  783. if (this->mu_ != nullptr) { this->mu_->Unlock(); }
  784. }
  785. private:
  786. Mutex *const mu_;
  787. MutexLockMaybe(const MutexLockMaybe&) = delete;
  788. MutexLockMaybe(MutexLockMaybe&&) = delete;
  789. MutexLockMaybe& operator=(const MutexLockMaybe&) = delete;
  790. MutexLockMaybe& operator=(MutexLockMaybe&&) = delete;
  791. };
  792. // ReleasableMutexLock
  793. //
  794. // ReleasableMutexLock is like MutexLock, but permits `Release()` of its
  795. // mutex before destruction. `Release()` may be called at most once.
  796. class ABSL_SCOPED_LOCKABLE ReleasableMutexLock {
  797. public:
  798. explicit ReleasableMutexLock(Mutex *mu) ABSL_EXCLUSIVE_LOCK_FUNCTION(mu)
  799. : mu_(mu) {
  800. this->mu_->Lock();
  801. }
  802. ~ReleasableMutexLock() ABSL_UNLOCK_FUNCTION() {
  803. if (this->mu_ != nullptr) { this->mu_->Unlock(); }
  804. }
  805. void Release() ABSL_UNLOCK_FUNCTION();
  806. private:
  807. Mutex *mu_;
  808. ReleasableMutexLock(const ReleasableMutexLock&) = delete;
  809. ReleasableMutexLock(ReleasableMutexLock&&) = delete;
  810. ReleasableMutexLock& operator=(const ReleasableMutexLock&) = delete;
  811. ReleasableMutexLock& operator=(ReleasableMutexLock&&) = delete;
  812. };
  813. #ifdef ABSL_INTERNAL_USE_NONPROD_MUTEX
  814. inline constexpr Mutex::Mutex(absl::ConstInitType) : impl_(absl::kConstInit) {}
  815. #else
  816. inline Mutex::Mutex() : mu_(0) {
  817. ABSL_TSAN_MUTEX_CREATE(this, __tsan_mutex_not_static);
  818. }
  819. inline constexpr Mutex::Mutex(absl::ConstInitType) : mu_(0) {}
  820. inline CondVar::CondVar() : cv_(0) {}
  821. #endif // ABSL_INTERNAL_USE_NONPROD_MUTEX
  822. // static
  823. template <typename T>
  824. bool Condition::CastAndCallMethod(const Condition *c) {
  825. typedef bool (T::*MemberType)();
  826. MemberType rm = reinterpret_cast<MemberType>(c->method_);
  827. T *x = static_cast<T *>(c->arg_);
  828. return (x->*rm)();
  829. }
  830. // static
  831. template <typename T>
  832. bool Condition::CastAndCallFunction(const Condition *c) {
  833. typedef bool (*FuncType)(T *);
  834. FuncType fn = reinterpret_cast<FuncType>(c->function_);
  835. T *x = static_cast<T *>(c->arg_);
  836. return (*fn)(x);
  837. }
  838. template <typename T>
  839. inline Condition::Condition(bool (*func)(T *), T *arg)
  840. : eval_(&CastAndCallFunction<T>),
  841. function_(reinterpret_cast<InternalFunctionType>(func)),
  842. method_(nullptr),
  843. arg_(const_cast<void *>(static_cast<const void *>(arg))) {}
  844. template <typename T>
  845. inline Condition::Condition(T *object,
  846. bool (absl::internal::identity<T>::type::*method)())
  847. : eval_(&CastAndCallMethod<T>),
  848. function_(nullptr),
  849. method_(reinterpret_cast<InternalMethodType>(method)),
  850. arg_(object) {}
  851. template <typename T>
  852. inline Condition::Condition(const T *object,
  853. bool (absl::internal::identity<T>::type::*method)()
  854. const)
  855. : eval_(&CastAndCallMethod<T>),
  856. function_(nullptr),
  857. method_(reinterpret_cast<InternalMethodType>(method)),
  858. arg_(reinterpret_cast<void *>(const_cast<T *>(object))) {}
  859. // Register a hook for profiling support.
  860. //
  861. // The function pointer registered here will be called whenever a mutex is
  862. // contended. The callback is given the absl/base/cycleclock.h timestamp when
  863. // waiting began.
  864. //
  865. // Calls to this function do not race or block, but there is no ordering
  866. // guaranteed between calls to this function and call to the provided hook.
  867. // In particular, the previously registered hook may still be called for some
  868. // time after this function returns.
  869. void RegisterMutexProfiler(void (*fn)(int64_t wait_timestamp));
  870. // Register a hook for Mutex tracing.
  871. //
  872. // The function pointer registered here will be called whenever a mutex is
  873. // contended. The callback is given an opaque handle to the contended mutex,
  874. // an event name, and the number of wait cycles (as measured by
  875. // //absl/base/internal/cycleclock.h, and which may not be real
  876. // "cycle" counts.)
  877. //
  878. // The only event name currently sent is "slow release".
  879. //
  880. // This has the same memory ordering concerns as RegisterMutexProfiler() above.
  881. void RegisterMutexTracer(void (*fn)(const char *msg, const void *obj,
  882. int64_t wait_cycles));
  883. // TODO(gfalcon): Combine RegisterMutexProfiler() and RegisterMutexTracer()
  884. // into a single interface, since they are only ever called in pairs.
  885. // Register a hook for CondVar tracing.
  886. //
  887. // The function pointer registered here will be called here on various CondVar
  888. // events. The callback is given an opaque handle to the CondVar object and
  889. // a string identifying the event. This is thread-safe, but only a single
  890. // tracer can be registered.
  891. //
  892. // Events that can be sent are "Wait", "Unwait", "Signal wakeup", and
  893. // "SignalAll wakeup".
  894. //
  895. // This has the same memory ordering concerns as RegisterMutexProfiler() above.
  896. void RegisterCondVarTracer(void (*fn)(const char *msg, const void *cv));
  897. // Register a hook for symbolizing stack traces in deadlock detector reports.
  898. //
  899. // 'pc' is the program counter being symbolized, 'out' is the buffer to write
  900. // into, and 'out_size' is the size of the buffer. This function can return
  901. // false if symbolizing failed, or true if a NUL-terminated symbol was written
  902. // to 'out.'
  903. //
  904. // This has the same memory ordering concerns as RegisterMutexProfiler() above.
  905. //
  906. // DEPRECATED: The default symbolizer function is absl::Symbolize() and the
  907. // ability to register a different hook for symbolizing stack traces will be
  908. // removed on or after 2023-05-01.
  909. ABSL_DEPRECATED("absl::RegisterSymbolizer() is deprecated and will be removed "
  910. "on or after 2023-05-01")
  911. void RegisterSymbolizer(bool (*fn)(const void *pc, char *out, int out_size));
  912. // EnableMutexInvariantDebugging()
  913. //
  914. // Enable or disable global support for Mutex invariant debugging. If enabled,
  915. // then invariant predicates can be registered per-Mutex for debug checking.
  916. // See Mutex::EnableInvariantDebugging().
  917. void EnableMutexInvariantDebugging(bool enabled);
  918. // When in debug mode, and when the feature has been enabled globally, the
  919. // implementation will keep track of lock ordering and complain (or optionally
  920. // crash) if a cycle is detected in the acquired-before graph.
  921. // Possible modes of operation for the deadlock detector in debug mode.
  922. enum class OnDeadlockCycle {
  923. kIgnore, // Neither report on nor attempt to track cycles in lock ordering
  924. kReport, // Report lock cycles to stderr when detected
  925. kAbort, // Report lock cycles to stderr when detected, then abort
  926. };
  927. // SetMutexDeadlockDetectionMode()
  928. //
  929. // Enable or disable global support for detection of potential deadlocks
  930. // due to Mutex lock ordering inversions. When set to 'kIgnore', tracking of
  931. // lock ordering is disabled. Otherwise, in debug builds, a lock ordering graph
  932. // will be maintained internally, and detected cycles will be reported in
  933. // the manner chosen here.
  934. void SetMutexDeadlockDetectionMode(OnDeadlockCycle mode);
  935. ABSL_NAMESPACE_END
  936. } // namespace absl
  937. // In some build configurations we pass --detect-odr-violations to the
  938. // gold linker. This causes it to flag weak symbol overrides as ODR
  939. // violations. Because ODR only applies to C++ and not C,
  940. // --detect-odr-violations ignores symbols not mangled with C++ names.
  941. // By changing our extension points to be extern "C", we dodge this
  942. // check.
  943. extern "C" {
  944. void AbslInternalMutexYield();
  945. } // extern "C"
  946. #endif // ABSL_SYNCHRONIZATION_MUTEX_H_