xref: /aosp_15_r20/external/webrtc/third_party/abseil-cpp/absl/synchronization/mutex.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 #include "absl/synchronization/mutex.h"
16 
17 #ifdef _WIN32
18 #include <windows.h>
19 #ifdef ERROR
20 #undef ERROR
21 #endif
22 #else
23 #include <fcntl.h>
24 #include <pthread.h>
25 #include <sched.h>
26 #include <sys/time.h>
27 #endif
28 
29 #include <assert.h>
30 #include <errno.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <time.h>
35 
36 #include <algorithm>
37 #include <atomic>
38 #include <cinttypes>
39 #include <cstddef>
40 #include <thread>  // NOLINT(build/c++11)
41 
42 #include "absl/base/attributes.h"
43 #include "absl/base/call_once.h"
44 #include "absl/base/config.h"
45 #include "absl/base/dynamic_annotations.h"
46 #include "absl/base/internal/atomic_hook.h"
47 #include "absl/base/internal/cycleclock.h"
48 #include "absl/base/internal/hide_ptr.h"
49 #include "absl/base/internal/low_level_alloc.h"
50 #include "absl/base/internal/raw_logging.h"
51 #include "absl/base/internal/spinlock.h"
52 #include "absl/base/internal/sysinfo.h"
53 #include "absl/base/internal/thread_identity.h"
54 #include "absl/base/internal/tsan_mutex_interface.h"
55 #include "absl/base/port.h"
56 #include "absl/debugging/stacktrace.h"
57 #include "absl/debugging/symbolize.h"
58 #include "absl/synchronization/internal/graphcycles.h"
59 #include "absl/synchronization/internal/per_thread_sem.h"
60 #include "absl/time/time.h"
61 
62 using absl::base_internal::CurrentThreadIdentityIfPresent;
63 using absl::base_internal::PerThreadSynch;
64 using absl::base_internal::SchedulingGuard;
65 using absl::base_internal::ThreadIdentity;
66 using absl::synchronization_internal::GetOrCreateCurrentThreadIdentity;
67 using absl::synchronization_internal::GraphCycles;
68 using absl::synchronization_internal::GraphId;
69 using absl::synchronization_internal::InvalidGraphId;
70 using absl::synchronization_internal::KernelTimeout;
71 using absl::synchronization_internal::PerThreadSem;
72 
73 extern "C" {
ABSL_INTERNAL_C_SYMBOL(AbslInternalMutexYield)74 ABSL_ATTRIBUTE_WEAK void ABSL_INTERNAL_C_SYMBOL(AbslInternalMutexYield)() {
75   std::this_thread::yield();
76 }
77 }  // extern "C"
78 
79 namespace absl {
80 ABSL_NAMESPACE_BEGIN
81 
82 namespace {
83 
84 #if defined(ABSL_HAVE_THREAD_SANITIZER)
85 constexpr OnDeadlockCycle kDeadlockDetectionDefault = OnDeadlockCycle::kIgnore;
86 #else
87 constexpr OnDeadlockCycle kDeadlockDetectionDefault = OnDeadlockCycle::kAbort;
88 #endif
89 
90 ABSL_CONST_INIT std::atomic<OnDeadlockCycle> synch_deadlock_detection(
91     kDeadlockDetectionDefault);
92 ABSL_CONST_INIT std::atomic<bool> synch_check_invariants(false);
93 
94 ABSL_INTERNAL_ATOMIC_HOOK_ATTRIBUTES
95 absl::base_internal::AtomicHook<void (*)(int64_t wait_cycles)>
96     submit_profile_data;
97 ABSL_INTERNAL_ATOMIC_HOOK_ATTRIBUTES absl::base_internal::AtomicHook<void (*)(
98     const char *msg, const void *obj, int64_t wait_cycles)>
99     mutex_tracer;
100 ABSL_INTERNAL_ATOMIC_HOOK_ATTRIBUTES
101     absl::base_internal::AtomicHook<void (*)(const char *msg, const void *cv)>
102         cond_var_tracer;
103 ABSL_INTERNAL_ATOMIC_HOOK_ATTRIBUTES absl::base_internal::AtomicHook<
104     bool (*)(const void *pc, char *out, int out_size)>
105     symbolizer(absl::Symbolize);
106 
107 }  // namespace
108 
109 static inline bool EvalConditionAnnotated(const Condition *cond, Mutex *mu,
110                                           bool locking, bool trylock,
111                                           bool read_lock);
112 
RegisterMutexProfiler(void (* fn)(int64_t wait_cycles))113 void RegisterMutexProfiler(void (*fn)(int64_t wait_cycles)) {
114   submit_profile_data.Store(fn);
115 }
116 
RegisterMutexTracer(void (* fn)(const char * msg,const void * obj,int64_t wait_cycles))117 void RegisterMutexTracer(void (*fn)(const char *msg, const void *obj,
118                                     int64_t wait_cycles)) {
119   mutex_tracer.Store(fn);
120 }
121 
RegisterCondVarTracer(void (* fn)(const char * msg,const void * cv))122 void RegisterCondVarTracer(void (*fn)(const char *msg, const void *cv)) {
123   cond_var_tracer.Store(fn);
124 }
125 
RegisterSymbolizer(bool (* fn)(const void * pc,char * out,int out_size))126 void RegisterSymbolizer(bool (*fn)(const void *pc, char *out, int out_size)) {
127   symbolizer.Store(fn);
128 }
129 
130 namespace {
131 // Represents the strategy for spin and yield.
132 // See the comment in GetMutexGlobals() for more information.
133 enum DelayMode { AGGRESSIVE, GENTLE };
134 
135 struct ABSL_CACHELINE_ALIGNED MutexGlobals {
136   absl::once_flag once;
137   int spinloop_iterations = 0;
138   int32_t mutex_sleep_spins[2] = {};
139   absl::Duration mutex_sleep_time;
140 };
141 
MeasureTimeToYield()142 absl::Duration MeasureTimeToYield() {
143   absl::Time before = absl::Now();
144   ABSL_INTERNAL_C_SYMBOL(AbslInternalMutexYield)();
145   return absl::Now() - before;
146 }
147 
GetMutexGlobals()148 const MutexGlobals &GetMutexGlobals() {
149   ABSL_CONST_INIT static MutexGlobals data;
150   absl::base_internal::LowLevelCallOnce(&data.once, [&]() {
151     const int num_cpus = absl::base_internal::NumCPUs();
152     data.spinloop_iterations = num_cpus > 1 ? 1500 : 0;
153     // If this a uniprocessor, only yield/sleep.
154     // Real-time threads are often unable to yield, so the sleep time needs
155     // to be long enough to keep the calling thread asleep until scheduling
156     // happens.
157     // If this is multiprocessor, allow spinning. If the mode is
158     // aggressive then spin many times before yielding.  If the mode is
159     // gentle then spin only a few times before yielding.  Aggressive spinning
160     // is used to ensure that an Unlock() call, which must get the spin lock
161     // for any thread to make progress gets it without undue delay.
162     if (num_cpus > 1) {
163       data.mutex_sleep_spins[AGGRESSIVE] = 5000;
164       data.mutex_sleep_spins[GENTLE] = 250;
165       data.mutex_sleep_time = absl::Microseconds(10);
166     } else {
167       data.mutex_sleep_spins[AGGRESSIVE] = 0;
168       data.mutex_sleep_spins[GENTLE] = 0;
169       data.mutex_sleep_time = MeasureTimeToYield() * 5;
170       data.mutex_sleep_time =
171           std::min(data.mutex_sleep_time, absl::Milliseconds(1));
172       data.mutex_sleep_time =
173           std::max(data.mutex_sleep_time, absl::Microseconds(10));
174     }
175   });
176   return data;
177 }
178 }  // namespace
179 
180 namespace synchronization_internal {
181 // Returns the Mutex delay on iteration `c` depending on the given `mode`.
182 // The returned value should be used as `c` for the next call to `MutexDelay`.
MutexDelay(int32_t c,int mode)183 int MutexDelay(int32_t c, int mode) {
184   const int32_t limit = GetMutexGlobals().mutex_sleep_spins[mode];
185   const absl::Duration sleep_time = GetMutexGlobals().mutex_sleep_time;
186   if (c < limit) {
187     // Spin.
188     c++;
189   } else {
190     SchedulingGuard::ScopedEnable enable_rescheduling;
191     ABSL_TSAN_MUTEX_PRE_DIVERT(nullptr, 0);
192     if (c == limit) {
193       // Yield once.
194       ABSL_INTERNAL_C_SYMBOL(AbslInternalMutexYield)();
195       c++;
196     } else {
197       // Then wait.
198       absl::SleepFor(sleep_time);
199       c = 0;
200     }
201     ABSL_TSAN_MUTEX_POST_DIVERT(nullptr, 0);
202   }
203   return c;
204 }
205 }  // namespace synchronization_internal
206 
207 // --------------------------Generic atomic ops
208 // Ensure that "(*pv & bits) == bits" by doing an atomic update of "*pv" to
209 // "*pv | bits" if necessary.  Wait until (*pv & wait_until_clear)==0
210 // before making any change.
211 // This is used to set flags in mutex and condition variable words.
AtomicSetBits(std::atomic<intptr_t> * pv,intptr_t bits,intptr_t wait_until_clear)212 static void AtomicSetBits(std::atomic<intptr_t>* pv, intptr_t bits,
213                           intptr_t wait_until_clear) {
214   intptr_t v;
215   do {
216     v = pv->load(std::memory_order_relaxed);
217   } while ((v & bits) != bits &&
218            ((v & wait_until_clear) != 0 ||
219             !pv->compare_exchange_weak(v, v | bits,
220                                        std::memory_order_release,
221                                        std::memory_order_relaxed)));
222 }
223 
224 // Ensure that "(*pv & bits) == 0" by doing an atomic update of "*pv" to
225 // "*pv & ~bits" if necessary.  Wait until (*pv & wait_until_clear)==0
226 // before making any change.
227 // This is used to unset flags in mutex and condition variable words.
AtomicClearBits(std::atomic<intptr_t> * pv,intptr_t bits,intptr_t wait_until_clear)228 static void AtomicClearBits(std::atomic<intptr_t>* pv, intptr_t bits,
229                             intptr_t wait_until_clear) {
230   intptr_t v;
231   do {
232     v = pv->load(std::memory_order_relaxed);
233   } while ((v & bits) != 0 &&
234            ((v & wait_until_clear) != 0 ||
235             !pv->compare_exchange_weak(v, v & ~bits,
236                                        std::memory_order_release,
237                                        std::memory_order_relaxed)));
238 }
239 
240 //------------------------------------------------------------------
241 
242 // Data for doing deadlock detection.
243 ABSL_CONST_INIT static absl::base_internal::SpinLock deadlock_graph_mu(
244     absl::kConstInit, base_internal::SCHEDULE_KERNEL_ONLY);
245 
246 // Graph used to detect deadlocks.
247 ABSL_CONST_INIT static GraphCycles *deadlock_graph
248     ABSL_GUARDED_BY(deadlock_graph_mu) ABSL_PT_GUARDED_BY(deadlock_graph_mu);
249 
250 //------------------------------------------------------------------
251 // An event mechanism for debugging mutex use.
252 // It also allows mutexes to be given names for those who can't handle
253 // addresses, and instead like to give their data structures names like
254 // "Henry", "Fido", or "Rupert IV, King of Yondavia".
255 
256 namespace {  // to prevent name pollution
257 enum {       // Mutex and CondVar events passed as "ev" to PostSynchEvent
258              // Mutex events
259   SYNCH_EV_TRYLOCK_SUCCESS,
260   SYNCH_EV_TRYLOCK_FAILED,
261   SYNCH_EV_READERTRYLOCK_SUCCESS,
262   SYNCH_EV_READERTRYLOCK_FAILED,
263   SYNCH_EV_LOCK,
264   SYNCH_EV_LOCK_RETURNING,
265   SYNCH_EV_READERLOCK,
266   SYNCH_EV_READERLOCK_RETURNING,
267   SYNCH_EV_UNLOCK,
268   SYNCH_EV_READERUNLOCK,
269 
270   // CondVar events
271   SYNCH_EV_WAIT,
272   SYNCH_EV_WAIT_RETURNING,
273   SYNCH_EV_SIGNAL,
274   SYNCH_EV_SIGNALALL,
275 };
276 
277 enum {                    // Event flags
278   SYNCH_F_R = 0x01,       // reader event
279   SYNCH_F_LCK = 0x02,     // PostSynchEvent called with mutex held
280   SYNCH_F_TRY = 0x04,     // TryLock or ReaderTryLock
281   SYNCH_F_UNLOCK = 0x08,  // Unlock or ReaderUnlock
282 
283   SYNCH_F_LCK_W = SYNCH_F_LCK,
284   SYNCH_F_LCK_R = SYNCH_F_LCK | SYNCH_F_R,
285 };
286 }  // anonymous namespace
287 
288 // Properties of the events.
289 static const struct {
290   int flags;
291   const char *msg;
292 } event_properties[] = {
293     {SYNCH_F_LCK_W | SYNCH_F_TRY, "TryLock succeeded "},
294     {0, "TryLock failed "},
295     {SYNCH_F_LCK_R | SYNCH_F_TRY, "ReaderTryLock succeeded "},
296     {0, "ReaderTryLock failed "},
297     {0, "Lock blocking "},
298     {SYNCH_F_LCK_W, "Lock returning "},
299     {0, "ReaderLock blocking "},
300     {SYNCH_F_LCK_R, "ReaderLock returning "},
301     {SYNCH_F_LCK_W | SYNCH_F_UNLOCK, "Unlock "},
302     {SYNCH_F_LCK_R | SYNCH_F_UNLOCK, "ReaderUnlock "},
303     {0, "Wait on "},
304     {0, "Wait unblocked "},
305     {0, "Signal on "},
306     {0, "SignalAll on "},
307 };
308 
309 ABSL_CONST_INIT static absl::base_internal::SpinLock synch_event_mu(
310     absl::kConstInit, base_internal::SCHEDULE_KERNEL_ONLY);
311 
312 // Hash table size; should be prime > 2.
313 // Can't be too small, as it's used for deadlock detection information.
314 static constexpr uint32_t kNSynchEvent = 1031;
315 
316 static struct SynchEvent {     // this is a trivial hash table for the events
317   // struct is freed when refcount reaches 0
318   int refcount ABSL_GUARDED_BY(synch_event_mu);
319 
320   // buckets have linear, 0-terminated  chains
321   SynchEvent *next ABSL_GUARDED_BY(synch_event_mu);
322 
323   // Constant after initialization
324   uintptr_t masked_addr;  // object at this address is called "name"
325 
326   // No explicit synchronization used.  Instead we assume that the
327   // client who enables/disables invariants/logging on a Mutex does so
328   // while the Mutex is not being concurrently accessed by others.
329   void (*invariant)(void *arg);  // called on each event
330   void *arg;            // first arg to (*invariant)()
331   bool log;             // logging turned on
332 
333   // Constant after initialization
334   char name[1];         // actually longer---NUL-terminated string
335 } * synch_event[kNSynchEvent] ABSL_GUARDED_BY(synch_event_mu);
336 
337 // Ensure that the object at "addr" has a SynchEvent struct associated with it,
338 // set "bits" in the word there (waiting until lockbit is clear before doing
339 // so), and return a refcounted reference that will remain valid until
340 // UnrefSynchEvent() is called.  If a new SynchEvent is allocated,
341 // the string name is copied into it.
342 // When used with a mutex, the caller should also ensure that kMuEvent
343 // is set in the mutex word, and similarly for condition variables and kCVEvent.
EnsureSynchEvent(std::atomic<intptr_t> * addr,const char * name,intptr_t bits,intptr_t lockbit)344 static SynchEvent *EnsureSynchEvent(std::atomic<intptr_t> *addr,
345                                     const char *name, intptr_t bits,
346                                     intptr_t lockbit) {
347   uint32_t h = reinterpret_cast<uintptr_t>(addr) % kNSynchEvent;
348   SynchEvent *e;
349   // first look for existing SynchEvent struct..
350   synch_event_mu.Lock();
351   for (e = synch_event[h];
352        e != nullptr && e->masked_addr != base_internal::HidePtr(addr);
353        e = e->next) {
354   }
355   if (e == nullptr) {  // no SynchEvent struct found; make one.
356     if (name == nullptr) {
357       name = "";
358     }
359     size_t l = strlen(name);
360     e = reinterpret_cast<SynchEvent *>(
361         base_internal::LowLevelAlloc::Alloc(sizeof(*e) + l));
362     e->refcount = 2;    // one for return value, one for linked list
363     e->masked_addr = base_internal::HidePtr(addr);
364     e->invariant = nullptr;
365     e->arg = nullptr;
366     e->log = false;
367     strcpy(e->name, name);  // NOLINT(runtime/printf)
368     e->next = synch_event[h];
369     AtomicSetBits(addr, bits, lockbit);
370     synch_event[h] = e;
371   } else {
372     e->refcount++;      // for return value
373   }
374   synch_event_mu.Unlock();
375   return e;
376 }
377 
378 // Deallocate the SynchEvent *e, whose refcount has fallen to zero.
DeleteSynchEvent(SynchEvent * e)379 static void DeleteSynchEvent(SynchEvent *e) {
380   base_internal::LowLevelAlloc::Free(e);
381 }
382 
383 // Decrement the reference count of *e, or do nothing if e==null.
UnrefSynchEvent(SynchEvent * e)384 static void UnrefSynchEvent(SynchEvent *e) {
385   if (e != nullptr) {
386     synch_event_mu.Lock();
387     bool del = (--(e->refcount) == 0);
388     synch_event_mu.Unlock();
389     if (del) {
390       DeleteSynchEvent(e);
391     }
392   }
393 }
394 
395 // Forget the mapping from the object (Mutex or CondVar) at address addr
396 // to SynchEvent object, and clear "bits" in its word (waiting until lockbit
397 // is clear before doing so).
ForgetSynchEvent(std::atomic<intptr_t> * addr,intptr_t bits,intptr_t lockbit)398 static void ForgetSynchEvent(std::atomic<intptr_t> *addr, intptr_t bits,
399                              intptr_t lockbit) {
400   uint32_t h = reinterpret_cast<uintptr_t>(addr) % kNSynchEvent;
401   SynchEvent **pe;
402   SynchEvent *e;
403   synch_event_mu.Lock();
404   for (pe = &synch_event[h];
405        (e = *pe) != nullptr && e->masked_addr != base_internal::HidePtr(addr);
406        pe = &e->next) {
407   }
408   bool del = false;
409   if (e != nullptr) {
410     *pe = e->next;
411     del = (--(e->refcount) == 0);
412   }
413   AtomicClearBits(addr, bits, lockbit);
414   synch_event_mu.Unlock();
415   if (del) {
416     DeleteSynchEvent(e);
417   }
418 }
419 
420 // Return a refcounted reference to the SynchEvent of the object at address
421 // "addr", if any.  The pointer returned is valid until the UnrefSynchEvent() is
422 // called.
GetSynchEvent(const void * addr)423 static SynchEvent *GetSynchEvent(const void *addr) {
424   uint32_t h = reinterpret_cast<uintptr_t>(addr) % kNSynchEvent;
425   SynchEvent *e;
426   synch_event_mu.Lock();
427   for (e = synch_event[h];
428        e != nullptr && e->masked_addr != base_internal::HidePtr(addr);
429        e = e->next) {
430   }
431   if (e != nullptr) {
432     e->refcount++;
433   }
434   synch_event_mu.Unlock();
435   return e;
436 }
437 
438 // Called when an event "ev" occurs on a Mutex of CondVar "obj"
439 // if event recording is on
PostSynchEvent(void * obj,int ev)440 static void PostSynchEvent(void *obj, int ev) {
441   SynchEvent *e = GetSynchEvent(obj);
442   // logging is on if event recording is on and either there's no event struct,
443   // or it explicitly says to log
444   if (e == nullptr || e->log) {
445     void *pcs[40];
446     int n = absl::GetStackTrace(pcs, ABSL_ARRAYSIZE(pcs), 1);
447     // A buffer with enough space for the ASCII for all the PCs, even on a
448     // 64-bit machine.
449     char buffer[ABSL_ARRAYSIZE(pcs) * 24];
450     int pos = snprintf(buffer, sizeof (buffer), " @");
451     for (int i = 0; i != n; i++) {
452       int b = snprintf(&buffer[pos], sizeof(buffer) - static_cast<size_t>(pos),
453                        " %p", pcs[i]);
454       if (b < 0 ||
455           static_cast<size_t>(b) >= sizeof(buffer) - static_cast<size_t>(pos)) {
456         break;
457       }
458       pos += b;
459     }
460     ABSL_RAW_LOG(INFO, "%s%p %s %s", event_properties[ev].msg, obj,
461                  (e == nullptr ? "" : e->name), buffer);
462   }
463   const int flags = event_properties[ev].flags;
464   if ((flags & SYNCH_F_LCK) != 0 && e != nullptr && e->invariant != nullptr) {
465     // Calling the invariant as is causes problems under ThreadSanitizer.
466     // We are currently inside of Mutex Lock/Unlock and are ignoring all
467     // memory accesses and synchronization. If the invariant transitively
468     // synchronizes something else and we ignore the synchronization, we will
469     // get false positive race reports later.
470     // Reuse EvalConditionAnnotated to properly call into user code.
471     struct local {
472       static bool pred(SynchEvent *ev) {
473         (*ev->invariant)(ev->arg);
474         return false;
475       }
476     };
477     Condition cond(&local::pred, e);
478     Mutex *mu = static_cast<Mutex *>(obj);
479     const bool locking = (flags & SYNCH_F_UNLOCK) == 0;
480     const bool trylock = (flags & SYNCH_F_TRY) != 0;
481     const bool read_lock = (flags & SYNCH_F_R) != 0;
482     EvalConditionAnnotated(&cond, mu, locking, trylock, read_lock);
483   }
484   UnrefSynchEvent(e);
485 }
486 
487 //------------------------------------------------------------------
488 
489 // The SynchWaitParams struct encapsulates the way in which a thread is waiting:
490 // whether it has a timeout, the condition, exclusive/shared, and whether a
491 // condition variable wait has an associated Mutex (as opposed to another
492 // type of lock).  It also points to the PerThreadSynch struct of its thread.
493 // cv_word tells Enqueue() to enqueue on a CondVar using CondVarEnqueue().
494 //
495 // This structure is held on the stack rather than directly in
496 // PerThreadSynch because a thread can be waiting on multiple Mutexes if,
497 // while waiting on one Mutex, the implementation calls a client callback
498 // (such as a Condition function) that acquires another Mutex. We don't
499 // strictly need to allow this, but programmers become confused if we do not
500 // allow them to use functions such a LOG() within Condition functions.  The
501 // PerThreadSynch struct points at the most recent SynchWaitParams struct when
502 // the thread is on a Mutex's waiter queue.
503 struct SynchWaitParams {
SynchWaitParamsabsl::SynchWaitParams504   SynchWaitParams(Mutex::MuHow how_arg, const Condition *cond_arg,
505                   KernelTimeout timeout_arg, Mutex *cvmu_arg,
506                   PerThreadSynch *thread_arg,
507                   std::atomic<intptr_t> *cv_word_arg)
508       : how(how_arg),
509         cond(cond_arg),
510         timeout(timeout_arg),
511         cvmu(cvmu_arg),
512         thread(thread_arg),
513         cv_word(cv_word_arg),
514         contention_start_cycles(base_internal::CycleClock::Now()),
515         should_submit_contention_data(false) {}
516 
517   const Mutex::MuHow how;  // How this thread needs to wait.
518   const Condition *cond;  // The condition that this thread is waiting for.
519                           // In Mutex, this field is set to zero if a timeout
520                           // expires.
521   KernelTimeout timeout;  // timeout expiry---absolute time
522                           // In Mutex, this field is set to zero if a timeout
523                           // expires.
524   Mutex *const cvmu;      // used for transfer from cond var to mutex
525   PerThreadSynch *const thread;  // thread that is waiting
526 
527   // If not null, thread should be enqueued on the CondVar whose state
528   // word is cv_word instead of queueing normally on the Mutex.
529   std::atomic<intptr_t> *cv_word;
530 
531   int64_t contention_start_cycles;  // Time (in cycles) when this thread started
532                                     // to contend for the mutex.
533   bool should_submit_contention_data;
534 };
535 
536 struct SynchLocksHeld {
537   int n;              // number of valid entries in locks[]
538   bool overflow;      // true iff we overflowed the array at some point
539   struct {
540     Mutex *mu;        // lock acquired
541     int32_t count;      // times acquired
542     GraphId id;       // deadlock_graph id of acquired lock
543   } locks[40];
544   // If a thread overfills the array during deadlock detection, we
545   // continue, discarding information as needed.  If no overflow has
546   // taken place, we can provide more error checking, such as
547   // detecting when a thread releases a lock it does not hold.
548 };
549 
550 // A sentinel value in lists that is not 0.
551 // A 0 value is used to mean "not on a list".
552 static PerThreadSynch *const kPerThreadSynchNull =
553   reinterpret_cast<PerThreadSynch *>(1);
554 
LocksHeldAlloc()555 static SynchLocksHeld *LocksHeldAlloc() {
556   SynchLocksHeld *ret = reinterpret_cast<SynchLocksHeld *>(
557       base_internal::LowLevelAlloc::Alloc(sizeof(SynchLocksHeld)));
558   ret->n = 0;
559   ret->overflow = false;
560   return ret;
561 }
562 
563 // Return the PerThreadSynch-struct for this thread.
Synch_GetPerThread()564 static PerThreadSynch *Synch_GetPerThread() {
565   ThreadIdentity *identity = GetOrCreateCurrentThreadIdentity();
566   return &identity->per_thread_synch;
567 }
568 
Synch_GetPerThreadAnnotated(Mutex * mu)569 static PerThreadSynch *Synch_GetPerThreadAnnotated(Mutex *mu) {
570   if (mu) {
571     ABSL_TSAN_MUTEX_PRE_DIVERT(mu, 0);
572   }
573   PerThreadSynch *w = Synch_GetPerThread();
574   if (mu) {
575     ABSL_TSAN_MUTEX_POST_DIVERT(mu, 0);
576   }
577   return w;
578 }
579 
Synch_GetAllLocks()580 static SynchLocksHeld *Synch_GetAllLocks() {
581   PerThreadSynch *s = Synch_GetPerThread();
582   if (s->all_locks == nullptr) {
583     s->all_locks = LocksHeldAlloc();  // Freed by ReclaimThreadIdentity.
584   }
585   return s->all_locks;
586 }
587 
588 // Post on "w"'s associated PerThreadSem.
IncrementSynchSem(Mutex * mu,PerThreadSynch * w)589 void Mutex::IncrementSynchSem(Mutex *mu, PerThreadSynch *w) {
590   if (mu) {
591     ABSL_TSAN_MUTEX_PRE_DIVERT(mu, 0);
592   }
593   PerThreadSem::Post(w->thread_identity());
594   if (mu) {
595     ABSL_TSAN_MUTEX_POST_DIVERT(mu, 0);
596   }
597 }
598 
599 // Wait on "w"'s associated PerThreadSem; returns false if timeout expired.
DecrementSynchSem(Mutex * mu,PerThreadSynch * w,KernelTimeout t)600 bool Mutex::DecrementSynchSem(Mutex *mu, PerThreadSynch *w, KernelTimeout t) {
601   if (mu) {
602     ABSL_TSAN_MUTEX_PRE_DIVERT(mu, 0);
603   }
604   assert(w == Synch_GetPerThread());
605   static_cast<void>(w);
606   bool res = PerThreadSem::Wait(t);
607   if (mu) {
608     ABSL_TSAN_MUTEX_POST_DIVERT(mu, 0);
609   }
610   return res;
611 }
612 
613 // We're in a fatal signal handler that hopes to use Mutex and to get
614 // lucky by not deadlocking.  We try to improve its chances of success
615 // by effectively disabling some of the consistency checks.  This will
616 // prevent certain ABSL_RAW_CHECK() statements from being triggered when
617 // re-rentry is detected.  The ABSL_RAW_CHECK() statements are those in the
618 // Mutex code checking that the "waitp" field has not been reused.
InternalAttemptToUseMutexInFatalSignalHandler()619 void Mutex::InternalAttemptToUseMutexInFatalSignalHandler() {
620   // Fix the per-thread state only if it exists.
621   ThreadIdentity *identity = CurrentThreadIdentityIfPresent();
622   if (identity != nullptr) {
623     identity->per_thread_synch.suppress_fatal_errors = true;
624   }
625   // Don't do deadlock detection when we are already failing.
626   synch_deadlock_detection.store(OnDeadlockCycle::kIgnore,
627                                  std::memory_order_release);
628 }
629 
630 // --------------------------time support
631 
632 // Return the current time plus the timeout.  Use the same clock as
633 // PerThreadSem::Wait() for consistency.  Unfortunately, we don't have
634 // such a choice when a deadline is given directly.
DeadlineFromTimeout(absl::Duration timeout)635 static absl::Time DeadlineFromTimeout(absl::Duration timeout) {
636 #ifndef _WIN32
637   struct timeval tv;
638   gettimeofday(&tv, nullptr);
639   return absl::TimeFromTimeval(tv) + timeout;
640 #else
641   return absl::Now() + timeout;
642 #endif
643 }
644 
645 // --------------------------Mutexes
646 
647 // In the layout below, the msb of the bottom byte is currently unused.  Also,
648 // the following constraints were considered in choosing the layout:
649 //  o Both the debug allocator's "uninitialized" and "freed" patterns (0xab and
650 //    0xcd) are illegal: reader and writer lock both held.
651 //  o kMuWriter and kMuEvent should exceed kMuDesig and kMuWait, to enable the
652 //    bit-twiddling trick in Mutex::Unlock().
653 //  o kMuWriter / kMuReader == kMuWrWait / kMuWait,
654 //    to enable the bit-twiddling trick in CheckForMutexCorruption().
655 static const intptr_t kMuReader      = 0x0001L;  // a reader holds the lock
656 static const intptr_t kMuDesig       = 0x0002L;  // there's a designated waker
657 static const intptr_t kMuWait        = 0x0004L;  // threads are waiting
658 static const intptr_t kMuWriter      = 0x0008L;  // a writer holds the lock
659 static const intptr_t kMuEvent       = 0x0010L;  // record this mutex's events
660 // INVARIANT1:  there's a thread that was blocked on the mutex, is
661 // no longer, yet has not yet acquired the mutex.  If there's a
662 // designated waker, all threads can avoid taking the slow path in
663 // unlock because the designated waker will subsequently acquire
664 // the lock and wake someone.  To maintain INVARIANT1 the bit is
665 // set when a thread is unblocked(INV1a), and threads that were
666 // unblocked reset the bit when they either acquire or re-block
667 // (INV1b).
668 static const intptr_t kMuWrWait      = 0x0020L;  // runnable writer is waiting
669                                                  // for a reader
670 static const intptr_t kMuSpin        = 0x0040L;  // spinlock protects wait list
671 static const intptr_t kMuLow         = 0x00ffL;  // mask all mutex bits
672 static const intptr_t kMuHigh        = ~kMuLow;  // mask pointer/reader count
673 
674 // Hack to make constant values available to gdb pretty printer
675 enum {
676   kGdbMuSpin = kMuSpin,
677   kGdbMuEvent = kMuEvent,
678   kGdbMuWait = kMuWait,
679   kGdbMuWriter = kMuWriter,
680   kGdbMuDesig = kMuDesig,
681   kGdbMuWrWait = kMuWrWait,
682   kGdbMuReader = kMuReader,
683   kGdbMuLow = kMuLow,
684 };
685 
686 // kMuWrWait implies kMuWait.
687 // kMuReader and kMuWriter are mutually exclusive.
688 // If kMuReader is zero, there are no readers.
689 // Otherwise, if kMuWait is zero, the high order bits contain a count of the
690 // number of readers.  Otherwise, the reader count is held in
691 // PerThreadSynch::readers of the most recently queued waiter, again in the
692 // bits above kMuLow.
693 static const intptr_t kMuOne = 0x0100;  // a count of one reader
694 
695 // flags passed to Enqueue and LockSlow{,WithTimeout,Loop}
696 static const int kMuHasBlocked = 0x01;  // already blocked (MUST == 1)
697 static const int kMuIsCond = 0x02;      // conditional waiter (CV or Condition)
698 
699 static_assert(PerThreadSynch::kAlignment > kMuLow,
700               "PerThreadSynch::kAlignment must be greater than kMuLow");
701 
702 // This struct contains various bitmasks to be used in
703 // acquiring and releasing a mutex in a particular mode.
704 struct MuHowS {
705   // if all the bits in fast_need_zero are zero, the lock can be acquired by
706   // adding fast_add and oring fast_or.  The bit kMuDesig should be reset iff
707   // this is the designated waker.
708   intptr_t fast_need_zero;
709   intptr_t fast_or;
710   intptr_t fast_add;
711 
712   intptr_t slow_need_zero;  // fast_need_zero with events (e.g. logging)
713 
714   intptr_t slow_inc_need_zero;  // if all the bits in slow_inc_need_zero are
715                                 // zero a reader can acquire a read share by
716                                 // setting the reader bit and incrementing
717                                 // the reader count (in last waiter since
718                                 // we're now slow-path).  kMuWrWait be may
719                                 // be ignored if we already waited once.
720 };
721 
722 static const MuHowS kSharedS = {
723     // shared or read lock
724     kMuWriter | kMuWait | kMuEvent,   // fast_need_zero
725     kMuReader,                        // fast_or
726     kMuOne,                           // fast_add
727     kMuWriter | kMuWait,              // slow_need_zero
728     kMuSpin | kMuWriter | kMuWrWait,  // slow_inc_need_zero
729 };
730 static const MuHowS kExclusiveS = {
731     // exclusive or write lock
732     kMuWriter | kMuReader | kMuEvent,  // fast_need_zero
733     kMuWriter,                         // fast_or
734     0,                                 // fast_add
735     kMuWriter | kMuReader,             // slow_need_zero
736     ~static_cast<intptr_t>(0),         // slow_inc_need_zero
737 };
738 static const Mutex::MuHow kShared = &kSharedS;        // shared lock
739 static const Mutex::MuHow kExclusive = &kExclusiveS;  // exclusive lock
740 
741 #ifdef NDEBUG
742 static constexpr bool kDebugMode = false;
743 #else
744 static constexpr bool kDebugMode = true;
745 #endif
746 
747 #ifdef ABSL_INTERNAL_HAVE_TSAN_INTERFACE
TsanFlags(Mutex::MuHow how)748 static unsigned TsanFlags(Mutex::MuHow how) {
749   return how == kShared ? __tsan_mutex_read_lock : 0;
750 }
751 #endif
752 
DebugOnlyIsExiting()753 static bool DebugOnlyIsExiting() {
754   return false;
755 }
756 
~Mutex()757 Mutex::~Mutex() {
758   intptr_t v = mu_.load(std::memory_order_relaxed);
759   if ((v & kMuEvent) != 0 && !DebugOnlyIsExiting()) {
760     ForgetSynchEvent(&this->mu_, kMuEvent, kMuSpin);
761   }
762   if (kDebugMode) {
763     this->ForgetDeadlockInfo();
764   }
765   ABSL_TSAN_MUTEX_DESTROY(this, __tsan_mutex_not_static);
766 }
767 
EnableDebugLog(const char * name)768 void Mutex::EnableDebugLog(const char *name) {
769   SynchEvent *e = EnsureSynchEvent(&this->mu_, name, kMuEvent, kMuSpin);
770   e->log = true;
771   UnrefSynchEvent(e);
772 }
773 
EnableMutexInvariantDebugging(bool enabled)774 void EnableMutexInvariantDebugging(bool enabled) {
775   synch_check_invariants.store(enabled, std::memory_order_release);
776 }
777 
EnableInvariantDebugging(void (* invariant)(void *),void * arg)778 void Mutex::EnableInvariantDebugging(void (*invariant)(void *),
779                                      void *arg) {
780   if (synch_check_invariants.load(std::memory_order_acquire) &&
781       invariant != nullptr) {
782     SynchEvent *e = EnsureSynchEvent(&this->mu_, nullptr, kMuEvent, kMuSpin);
783     e->invariant = invariant;
784     e->arg = arg;
785     UnrefSynchEvent(e);
786   }
787 }
788 
SetMutexDeadlockDetectionMode(OnDeadlockCycle mode)789 void SetMutexDeadlockDetectionMode(OnDeadlockCycle mode) {
790   synch_deadlock_detection.store(mode, std::memory_order_release);
791 }
792 
793 // Return true iff threads x and y are part of the same equivalence
794 // class of waiters. An equivalence class is defined as the set of
795 // waiters with the same condition, type of lock, and thread priority.
796 //
797 // Requires that x and y be waiting on the same Mutex queue.
MuEquivalentWaiter(PerThreadSynch * x,PerThreadSynch * y)798 static bool MuEquivalentWaiter(PerThreadSynch *x, PerThreadSynch *y) {
799   return x->waitp->how == y->waitp->how && x->priority == y->priority &&
800          Condition::GuaranteedEqual(x->waitp->cond, y->waitp->cond);
801 }
802 
803 // Given the contents of a mutex word containing a PerThreadSynch pointer,
804 // return the pointer.
GetPerThreadSynch(intptr_t v)805 static inline PerThreadSynch *GetPerThreadSynch(intptr_t v) {
806   return reinterpret_cast<PerThreadSynch *>(v & kMuHigh);
807 }
808 
809 // The next several routines maintain the per-thread next and skip fields
810 // used in the Mutex waiter queue.
811 // The queue is a circular singly-linked list, of which the "head" is the
812 // last element, and head->next if the first element.
813 // The skip field has the invariant:
814 //   For thread x, x->skip is one of:
815 //     - invalid (iff x is not in a Mutex wait queue),
816 //     - null, or
817 //     - a pointer to a distinct thread waiting later in the same Mutex queue
818 //       such that all threads in [x, x->skip] have the same condition, priority
819 //       and lock type (MuEquivalentWaiter() is true for all pairs in [x,
820 //       x->skip]).
821 // In addition, if x->skip is  valid, (x->may_skip || x->skip == null)
822 //
823 // By the spec of MuEquivalentWaiter(), it is not necessary when removing the
824 // first runnable thread y from the front a Mutex queue to adjust the skip
825 // field of another thread x because if x->skip==y, x->skip must (have) become
826 // invalid before y is removed.  The function TryRemove can remove a specified
827 // thread from an arbitrary position in the queue whether runnable or not, so
828 // it fixes up skip fields that would otherwise be left dangling.
829 // The statement
830 //     if (x->may_skip && MuEquivalentWaiter(x, x->next)) { x->skip = x->next; }
831 // maintains the invariant provided x is not the last waiter in a Mutex queue
832 // The statement
833 //          if (x->skip != null) { x->skip = x->skip->skip; }
834 // maintains the invariant.
835 
836 // Returns the last thread y in a mutex waiter queue such that all threads in
837 // [x, y] inclusive share the same condition.  Sets skip fields of some threads
838 // in that range to optimize future evaluation of Skip() on x values in
839 // the range.  Requires thread x is in a mutex waiter queue.
840 // The locking is unusual.  Skip() is called under these conditions:
841 //   - spinlock is held in call from Enqueue(), with maybe_unlocking == false
842 //   - Mutex is held in call from UnlockSlow() by last unlocker, with
843 //     maybe_unlocking == true
844 //   - both Mutex and spinlock are held in call from DequeueAllWakeable() (from
845 //     UnlockSlow()) and TryRemove()
846 // These cases are mutually exclusive, so Skip() never runs concurrently
847 // with itself on the same Mutex.   The skip chain is used in these other places
848 // that cannot occur concurrently:
849 //   - FixSkip() (from TryRemove()) - spinlock and Mutex are held)
850 //   - Dequeue() (with spinlock and Mutex held)
851 //   - UnlockSlow() (with spinlock and Mutex held)
852 // A more complex case is Enqueue()
853 //   - Enqueue() (with spinlock held and maybe_unlocking == false)
854 //               This is the first case in which Skip is called, above.
855 //   - Enqueue() (without spinlock held; but queue is empty and being freshly
856 //                formed)
857 //   - Enqueue() (with spinlock held and maybe_unlocking == true)
858 // The first case has mutual exclusion, and the second isolation through
859 // working on an otherwise unreachable data structure.
860 // In the last case, Enqueue() is required to change no skip/next pointers
861 // except those in the added node and the former "head" node.  This implies
862 // that the new node is added after head, and so must be the new head or the
863 // new front of the queue.
Skip(PerThreadSynch * x)864 static PerThreadSynch *Skip(PerThreadSynch *x) {
865   PerThreadSynch *x0 = nullptr;
866   PerThreadSynch *x1 = x;
867   PerThreadSynch *x2 = x->skip;
868   if (x2 != nullptr) {
869     // Each iteration attempts to advance sequence (x0,x1,x2) to next sequence
870     // such that   x1 == x0->skip && x2 == x1->skip
871     while ((x0 = x1, x1 = x2, x2 = x2->skip) != nullptr) {
872       x0->skip = x2;      // short-circuit skip from x0 to x2
873     }
874     x->skip = x1;         // short-circuit skip from x to result
875   }
876   return x1;
877 }
878 
879 // "ancestor" appears before "to_be_removed" in the same Mutex waiter queue.
880 // The latter is going to be removed out of order, because of a timeout.
881 // Check whether "ancestor" has a skip field pointing to "to_be_removed",
882 // and fix it if it does.
FixSkip(PerThreadSynch * ancestor,PerThreadSynch * to_be_removed)883 static void FixSkip(PerThreadSynch *ancestor, PerThreadSynch *to_be_removed) {
884   if (ancestor->skip == to_be_removed) {  // ancestor->skip left dangling
885     if (to_be_removed->skip != nullptr) {
886       ancestor->skip = to_be_removed->skip;  // can skip past to_be_removed
887     } else if (ancestor->next != to_be_removed) {  // they are not adjacent
888       ancestor->skip = ancestor->next;             // can skip one past ancestor
889     } else {
890       ancestor->skip = nullptr;  // can't skip at all
891     }
892   }
893 }
894 
895 static void CondVarEnqueue(SynchWaitParams *waitp);
896 
897 // Enqueue thread "waitp->thread" on a waiter queue.
898 // Called with mutex spinlock held if head != nullptr
899 // If head==nullptr and waitp->cv_word==nullptr, then Enqueue() is
900 // idempotent; it alters no state associated with the existing (empty)
901 // queue.
902 //
903 // If waitp->cv_word == nullptr, queue the thread at either the front or
904 // the end (according to its priority) of the circular mutex waiter queue whose
905 // head is "head", and return the new head.  mu is the previous mutex state,
906 // which contains the reader count (perhaps adjusted for the operation in
907 // progress) if the list was empty and a read lock held, and the holder hint if
908 // the list was empty and a write lock held.  (flags & kMuIsCond) indicates
909 // whether this thread was transferred from a CondVar or is waiting for a
910 // non-trivial condition.  In this case, Enqueue() never returns nullptr
911 //
912 // If waitp->cv_word != nullptr, CondVarEnqueue() is called, and "head" is
913 // returned. This mechanism is used by CondVar to queue a thread on the
914 // condition variable queue instead of the mutex queue in implementing Wait().
915 // In this case, Enqueue() can return nullptr (if head==nullptr).
Enqueue(PerThreadSynch * head,SynchWaitParams * waitp,intptr_t mu,int flags)916 static PerThreadSynch *Enqueue(PerThreadSynch *head,
917                                SynchWaitParams *waitp, intptr_t mu, int flags) {
918   // If we have been given a cv_word, call CondVarEnqueue() and return
919   // the previous head of the Mutex waiter queue.
920   if (waitp->cv_word != nullptr) {
921     CondVarEnqueue(waitp);
922     return head;
923   }
924 
925   PerThreadSynch *s = waitp->thread;
926   ABSL_RAW_CHECK(
927       s->waitp == nullptr ||    // normal case
928           s->waitp == waitp ||  // Fer()---transfer from condition variable
929           s->suppress_fatal_errors,
930       "detected illegal recursion into Mutex code");
931   s->waitp = waitp;
932   s->skip = nullptr;             // maintain skip invariant (see above)
933   s->may_skip = true;            // always true on entering queue
934   s->wake = false;               // not being woken
935   s->cond_waiter = ((flags & kMuIsCond) != 0);
936   if (head == nullptr) {         // s is the only waiter
937     s->next = s;                 // it's the only entry in the cycle
938     s->readers = mu;             // reader count is from mu word
939     s->maybe_unlocking = false;  // no one is searching an empty list
940     head = s;                    // s is new head
941   } else {
942     PerThreadSynch *enqueue_after = nullptr;  // we'll put s after this element
943 #ifdef ABSL_HAVE_PTHREAD_GETSCHEDPARAM
944     int64_t now_cycles = base_internal::CycleClock::Now();
945     if (s->next_priority_read_cycles < now_cycles) {
946       // Every so often, update our idea of the thread's priority.
947       // pthread_getschedparam() is 5% of the block/wakeup time;
948       // base_internal::CycleClock::Now() is 0.5%.
949       int policy;
950       struct sched_param param;
951       const int err = pthread_getschedparam(pthread_self(), &policy, &param);
952       if (err != 0) {
953         ABSL_RAW_LOG(ERROR, "pthread_getschedparam failed: %d", err);
954       } else {
955         s->priority = param.sched_priority;
956         s->next_priority_read_cycles =
957             now_cycles +
958             static_cast<int64_t>(base_internal::CycleClock::Frequency());
959       }
960     }
961     if (s->priority > head->priority) {  // s's priority is above head's
962       // try to put s in priority-fifo order, or failing that at the front.
963       if (!head->maybe_unlocking) {
964         // No unlocker can be scanning the queue, so we can insert into the
965         // middle of the queue.
966         //
967         // Within a skip chain, all waiters have the same priority, so we can
968         // skip forward through the chains until we find one with a lower
969         // priority than the waiter to be enqueued.
970         PerThreadSynch *advance_to = head;    // next value of enqueue_after
971         do {
972           enqueue_after = advance_to;
973           // (side-effect: optimizes skip chain)
974           advance_to = Skip(enqueue_after->next);
975         } while (s->priority <= advance_to->priority);
976               // termination guaranteed because s->priority > head->priority
977               // and head is the end of a skip chain
978       } else if (waitp->how == kExclusive &&
979                  Condition::GuaranteedEqual(waitp->cond, nullptr)) {
980         // An unlocker could be scanning the queue, but we know it will recheck
981         // the queue front for writers that have no condition, which is what s
982         // is, so an insert at front is safe.
983         enqueue_after = head;       // add after head, at front
984       }
985     }
986 #endif
987     if (enqueue_after != nullptr) {
988       s->next = enqueue_after->next;
989       enqueue_after->next = s;
990 
991       // enqueue_after can be: head, Skip(...), or cur.
992       // The first two imply enqueue_after->skip == nullptr, and
993       // the last is used only if MuEquivalentWaiter(s, cur).
994       // We require this because clearing enqueue_after->skip
995       // is impossible; enqueue_after's predecessors might also
996       // incorrectly skip over s if we were to allow other
997       // insertion points.
998       ABSL_RAW_CHECK(enqueue_after->skip == nullptr ||
999                          MuEquivalentWaiter(enqueue_after, s),
1000                      "Mutex Enqueue failure");
1001 
1002       if (enqueue_after != head && enqueue_after->may_skip &&
1003           MuEquivalentWaiter(enqueue_after, enqueue_after->next)) {
1004         // enqueue_after can skip to its new successor, s
1005         enqueue_after->skip = enqueue_after->next;
1006       }
1007       if (MuEquivalentWaiter(s, s->next)) {  // s->may_skip is known to be true
1008         s->skip = s->next;                // s may skip to its successor
1009       }
1010     } else {   // enqueue not done any other way, so
1011                // we're inserting s at the back
1012       // s will become new head; copy data from head into it
1013       s->next = head->next;        // add s after head
1014       head->next = s;
1015       s->readers = head->readers;  // reader count is from previous head
1016       s->maybe_unlocking = head->maybe_unlocking;  // same for unlock hint
1017       if (head->may_skip && MuEquivalentWaiter(head, s)) {
1018         // head now has successor; may skip
1019         head->skip = s;
1020       }
1021       head = s;  // s is new head
1022     }
1023   }
1024   s->state.store(PerThreadSynch::kQueued, std::memory_order_relaxed);
1025   return head;
1026 }
1027 
1028 // Dequeue the successor pw->next of thread pw from the Mutex waiter queue
1029 // whose last element is head.  The new head element is returned, or null
1030 // if the list is made empty.
1031 // Dequeue is called with both spinlock and Mutex held.
Dequeue(PerThreadSynch * head,PerThreadSynch * pw)1032 static PerThreadSynch *Dequeue(PerThreadSynch *head, PerThreadSynch *pw) {
1033   PerThreadSynch *w = pw->next;
1034   pw->next = w->next;         // snip w out of list
1035   if (head == w) {            // we removed the head
1036     head = (pw == w) ? nullptr : pw;  // either emptied list, or pw is new head
1037   } else if (pw != head && MuEquivalentWaiter(pw, pw->next)) {
1038     // pw can skip to its new successor
1039     if (pw->next->skip !=
1040         nullptr) {  // either skip to its successors skip target
1041       pw->skip = pw->next->skip;
1042     } else {                   // or to pw's successor
1043       pw->skip = pw->next;
1044     }
1045   }
1046   return head;
1047 }
1048 
1049 // Traverse the elements [ pw->next, h] of the circular list whose last element
1050 // is head.
1051 // Remove all elements with wake==true and place them in the
1052 // singly-linked list wake_list in the order found.   Assumes that
1053 // there is only one such element if the element has how == kExclusive.
1054 // Return the new head.
DequeueAllWakeable(PerThreadSynch * head,PerThreadSynch * pw,PerThreadSynch ** wake_tail)1055 static PerThreadSynch *DequeueAllWakeable(PerThreadSynch *head,
1056                                           PerThreadSynch *pw,
1057                                           PerThreadSynch **wake_tail) {
1058   PerThreadSynch *orig_h = head;
1059   PerThreadSynch *w = pw->next;
1060   bool skipped = false;
1061   do {
1062     if (w->wake) {                    // remove this element
1063       ABSL_RAW_CHECK(pw->skip == nullptr, "bad skip in DequeueAllWakeable");
1064       // we're removing pw's successor so either pw->skip is zero or we should
1065       // already have removed pw since if pw->skip!=null, pw has the same
1066       // condition as w.
1067       head = Dequeue(head, pw);
1068       w->next = *wake_tail;           // keep list terminated
1069       *wake_tail = w;                 // add w to wake_list;
1070       wake_tail = &w->next;           // next addition to end
1071       if (w->waitp->how == kExclusive) {  // wake at most 1 writer
1072         break;
1073       }
1074     } else {                // not waking this one; skip
1075       pw = Skip(w);       // skip as much as possible
1076       skipped = true;
1077     }
1078     w = pw->next;
1079     // We want to stop processing after we've considered the original head,
1080     // orig_h.  We can't test for w==orig_h in the loop because w may skip over
1081     // it; we are guaranteed only that w's predecessor will not skip over
1082     // orig_h.  When we've considered orig_h, either we've processed it and
1083     // removed it (so orig_h != head), or we considered it and skipped it (so
1084     // skipped==true && pw == head because skipping from head always skips by
1085     // just one, leaving pw pointing at head).  So we want to
1086     // continue the loop with the negation of that expression.
1087   } while (orig_h == head && (pw != head || !skipped));
1088   return head;
1089 }
1090 
1091 // Try to remove thread s from the list of waiters on this mutex.
1092 // Does nothing if s is not on the waiter list.
TryRemove(PerThreadSynch * s)1093 void Mutex::TryRemove(PerThreadSynch *s) {
1094   SchedulingGuard::ScopedDisable disable_rescheduling;
1095   intptr_t v = mu_.load(std::memory_order_relaxed);
1096   // acquire spinlock & lock
1097   if ((v & (kMuWait | kMuSpin | kMuWriter | kMuReader)) == kMuWait &&
1098       mu_.compare_exchange_strong(v, v | kMuSpin | kMuWriter,
1099                                   std::memory_order_acquire,
1100                                   std::memory_order_relaxed)) {
1101     PerThreadSynch *h = GetPerThreadSynch(v);
1102     if (h != nullptr) {
1103       PerThreadSynch *pw = h;   // pw is w's predecessor
1104       PerThreadSynch *w;
1105       if ((w = pw->next) != s) {  // search for thread,
1106         do {                      // processing at least one element
1107           // If the current element isn't equivalent to the waiter to be
1108           // removed, we can skip the entire chain.
1109           if (!MuEquivalentWaiter(s, w)) {
1110             pw = Skip(w);                // so skip all that won't match
1111             // we don't have to worry about dangling skip fields
1112             // in the threads we skipped; none can point to s
1113             // because they are in a different equivalence class.
1114           } else {          // seeking same condition
1115             FixSkip(w, s);  // fix up any skip pointer from w to s
1116             pw = w;
1117           }
1118           // don't search further if we found the thread, or we're about to
1119           // process the first thread again.
1120         } while ((w = pw->next) != s && pw != h);
1121       }
1122       if (w == s) {                 // found thread; remove it
1123         // pw->skip may be non-zero here; the loop above ensured that
1124         // no ancestor of s can skip to s, so removal is safe anyway.
1125         h = Dequeue(h, pw);
1126         s->next = nullptr;
1127         s->state.store(PerThreadSynch::kAvailable, std::memory_order_release);
1128       }
1129     }
1130     intptr_t nv;
1131     do {                        // release spinlock and lock
1132       v = mu_.load(std::memory_order_relaxed);
1133       nv = v & (kMuDesig | kMuEvent);
1134       if (h != nullptr) {
1135         nv |= kMuWait | reinterpret_cast<intptr_t>(h);
1136         h->readers = 0;            // we hold writer lock
1137         h->maybe_unlocking = false;  // finished unlocking
1138       }
1139     } while (!mu_.compare_exchange_weak(v, nv,
1140                                         std::memory_order_release,
1141                                         std::memory_order_relaxed));
1142   }
1143 }
1144 
1145 // Wait until thread "s", which must be the current thread, is removed from the
1146 // this mutex's waiter queue.  If "s->waitp->timeout" has a timeout, wake up
1147 // if the wait extends past the absolute time specified, even if "s" is still
1148 // on the mutex queue.  In this case, remove "s" from the queue and return
1149 // true, otherwise return false.
Block(PerThreadSynch * s)1150 ABSL_XRAY_LOG_ARGS(1) void Mutex::Block(PerThreadSynch *s) {
1151   while (s->state.load(std::memory_order_acquire) == PerThreadSynch::kQueued) {
1152     if (!DecrementSynchSem(this, s, s->waitp->timeout)) {
1153       // After a timeout, we go into a spin loop until we remove ourselves
1154       // from the queue, or someone else removes us.  We can't be sure to be
1155       // able to remove ourselves in a single lock acquisition because this
1156       // mutex may be held, and the holder has the right to read the centre
1157       // of the waiter queue without holding the spinlock.
1158       this->TryRemove(s);
1159       int c = 0;
1160       while (s->next != nullptr) {
1161         c = synchronization_internal::MutexDelay(c, GENTLE);
1162         this->TryRemove(s);
1163       }
1164       if (kDebugMode) {
1165         // This ensures that we test the case that TryRemove() is called when s
1166         // is not on the queue.
1167         this->TryRemove(s);
1168       }
1169       s->waitp->timeout = KernelTimeout::Never();      // timeout is satisfied
1170       s->waitp->cond = nullptr;  // condition no longer relevant for wakeups
1171     }
1172   }
1173   ABSL_RAW_CHECK(s->waitp != nullptr || s->suppress_fatal_errors,
1174                  "detected illegal recursion in Mutex code");
1175   s->waitp = nullptr;
1176 }
1177 
1178 // Wake thread w, and return the next thread in the list.
Wakeup(PerThreadSynch * w)1179 PerThreadSynch *Mutex::Wakeup(PerThreadSynch *w) {
1180   PerThreadSynch *next = w->next;
1181   w->next = nullptr;
1182   w->state.store(PerThreadSynch::kAvailable, std::memory_order_release);
1183   IncrementSynchSem(this, w);
1184 
1185   return next;
1186 }
1187 
GetGraphIdLocked(Mutex * mu)1188 static GraphId GetGraphIdLocked(Mutex *mu)
1189     ABSL_EXCLUSIVE_LOCKS_REQUIRED(deadlock_graph_mu) {
1190   if (!deadlock_graph) {  // (re)create the deadlock graph.
1191     deadlock_graph =
1192         new (base_internal::LowLevelAlloc::Alloc(sizeof(*deadlock_graph)))
1193             GraphCycles;
1194   }
1195   return deadlock_graph->GetId(mu);
1196 }
1197 
GetGraphId(Mutex * mu)1198 static GraphId GetGraphId(Mutex *mu) ABSL_LOCKS_EXCLUDED(deadlock_graph_mu) {
1199   deadlock_graph_mu.Lock();
1200   GraphId id = GetGraphIdLocked(mu);
1201   deadlock_graph_mu.Unlock();
1202   return id;
1203 }
1204 
1205 // Record a lock acquisition.  This is used in debug mode for deadlock
1206 // detection.  The held_locks pointer points to the relevant data
1207 // structure for each case.
LockEnter(Mutex * mu,GraphId id,SynchLocksHeld * held_locks)1208 static void LockEnter(Mutex* mu, GraphId id, SynchLocksHeld *held_locks) {
1209   int n = held_locks->n;
1210   int i = 0;
1211   while (i != n && held_locks->locks[i].id != id) {
1212     i++;
1213   }
1214   if (i == n) {
1215     if (n == ABSL_ARRAYSIZE(held_locks->locks)) {
1216       held_locks->overflow = true;  // lost some data
1217     } else {                        // we have room for lock
1218       held_locks->locks[i].mu = mu;
1219       held_locks->locks[i].count = 1;
1220       held_locks->locks[i].id = id;
1221       held_locks->n = n + 1;
1222     }
1223   } else {
1224     held_locks->locks[i].count++;
1225   }
1226 }
1227 
1228 // Record a lock release.  Each call to LockEnter(mu, id, x) should be
1229 // eventually followed by a call to LockLeave(mu, id, x) by the same thread.
1230 // It does not process the event if is not needed when deadlock detection is
1231 // disabled.
LockLeave(Mutex * mu,GraphId id,SynchLocksHeld * held_locks)1232 static void LockLeave(Mutex* mu, GraphId id, SynchLocksHeld *held_locks) {
1233   int n = held_locks->n;
1234   int i = 0;
1235   while (i != n && held_locks->locks[i].id != id) {
1236     i++;
1237   }
1238   if (i == n) {
1239     if (!held_locks->overflow) {
1240       // The deadlock id may have been reassigned after ForgetDeadlockInfo,
1241       // but in that case mu should still be present.
1242       i = 0;
1243       while (i != n && held_locks->locks[i].mu != mu) {
1244         i++;
1245       }
1246       if (i == n) {  // mu missing means releasing unheld lock
1247         SynchEvent *mu_events = GetSynchEvent(mu);
1248         ABSL_RAW_LOG(FATAL,
1249                      "thread releasing lock it does not hold: %p %s; "
1250                      ,
1251                      static_cast<void *>(mu),
1252                      mu_events == nullptr ? "" : mu_events->name);
1253       }
1254     }
1255   } else if (held_locks->locks[i].count == 1) {
1256     held_locks->n = n - 1;
1257     held_locks->locks[i] = held_locks->locks[n - 1];
1258     held_locks->locks[n - 1].id = InvalidGraphId();
1259     held_locks->locks[n - 1].mu =
1260         nullptr;  // clear mu to please the leak detector.
1261   } else {
1262     assert(held_locks->locks[i].count > 0);
1263     held_locks->locks[i].count--;
1264   }
1265 }
1266 
1267 // Call LockEnter() if in debug mode and deadlock detection is enabled.
DebugOnlyLockEnter(Mutex * mu)1268 static inline void DebugOnlyLockEnter(Mutex *mu) {
1269   if (kDebugMode) {
1270     if (synch_deadlock_detection.load(std::memory_order_acquire) !=
1271         OnDeadlockCycle::kIgnore) {
1272       LockEnter(mu, GetGraphId(mu), Synch_GetAllLocks());
1273     }
1274   }
1275 }
1276 
1277 // Call LockEnter() if in debug mode and deadlock detection is enabled.
DebugOnlyLockEnter(Mutex * mu,GraphId id)1278 static inline void DebugOnlyLockEnter(Mutex *mu, GraphId id) {
1279   if (kDebugMode) {
1280     if (synch_deadlock_detection.load(std::memory_order_acquire) !=
1281         OnDeadlockCycle::kIgnore) {
1282       LockEnter(mu, id, Synch_GetAllLocks());
1283     }
1284   }
1285 }
1286 
1287 // Call LockLeave() if in debug mode and deadlock detection is enabled.
DebugOnlyLockLeave(Mutex * mu)1288 static inline void DebugOnlyLockLeave(Mutex *mu) {
1289   if (kDebugMode) {
1290     if (synch_deadlock_detection.load(std::memory_order_acquire) !=
1291         OnDeadlockCycle::kIgnore) {
1292       LockLeave(mu, GetGraphId(mu), Synch_GetAllLocks());
1293     }
1294   }
1295 }
1296 
StackString(void ** pcs,int n,char * buf,int maxlen,bool symbolize)1297 static char *StackString(void **pcs, int n, char *buf, int maxlen,
1298                          bool symbolize) {
1299   static const int kSymLen = 200;
1300   char sym[kSymLen];
1301   int len = 0;
1302   for (int i = 0; i != n; i++) {
1303     if (len >= maxlen)
1304       return buf;
1305     size_t count = static_cast<size_t>(maxlen - len);
1306     if (symbolize) {
1307       if (!symbolizer(pcs[i], sym, kSymLen)) {
1308         sym[0] = '\0';
1309       }
1310       snprintf(buf + len, count, "%s\t@ %p %s\n", (i == 0 ? "\n" : ""), pcs[i],
1311                sym);
1312     } else {
1313       snprintf(buf + len, count, " %p", pcs[i]);
1314     }
1315     len += strlen(&buf[len]);
1316   }
1317   return buf;
1318 }
1319 
CurrentStackString(char * buf,int maxlen,bool symbolize)1320 static char *CurrentStackString(char *buf, int maxlen, bool symbolize) {
1321   void *pcs[40];
1322   return StackString(pcs, absl::GetStackTrace(pcs, ABSL_ARRAYSIZE(pcs), 2), buf,
1323                      maxlen, symbolize);
1324 }
1325 
1326 namespace {
1327 enum { kMaxDeadlockPathLen = 10 };  // maximum length of a deadlock cycle;
1328                                     // a path this long would be remarkable
1329 // Buffers required to report a deadlock.
1330 // We do not allocate them on stack to avoid large stack frame.
1331 struct DeadlockReportBuffers {
1332   char buf[6100];
1333   GraphId path[kMaxDeadlockPathLen];
1334 };
1335 
1336 struct ScopedDeadlockReportBuffers {
ScopedDeadlockReportBuffersabsl::__anon2e75745e0a11::ScopedDeadlockReportBuffers1337   ScopedDeadlockReportBuffers() {
1338     b = reinterpret_cast<DeadlockReportBuffers *>(
1339         base_internal::LowLevelAlloc::Alloc(sizeof(*b)));
1340   }
~ScopedDeadlockReportBuffersabsl::__anon2e75745e0a11::ScopedDeadlockReportBuffers1341   ~ScopedDeadlockReportBuffers() { base_internal::LowLevelAlloc::Free(b); }
1342   DeadlockReportBuffers *b;
1343 };
1344 
1345 // Helper to pass to GraphCycles::UpdateStackTrace.
GetStack(void ** stack,int max_depth)1346 int GetStack(void** stack, int max_depth) {
1347   return absl::GetStackTrace(stack, max_depth, 3);
1348 }
1349 }  // anonymous namespace
1350 
1351 // Called in debug mode when a thread is about to acquire a lock in a way that
1352 // may block.
DeadlockCheck(Mutex * mu)1353 static GraphId DeadlockCheck(Mutex *mu) {
1354   if (synch_deadlock_detection.load(std::memory_order_acquire) ==
1355       OnDeadlockCycle::kIgnore) {
1356     return InvalidGraphId();
1357   }
1358 
1359   SynchLocksHeld *all_locks = Synch_GetAllLocks();
1360 
1361   absl::base_internal::SpinLockHolder lock(&deadlock_graph_mu);
1362   const GraphId mu_id = GetGraphIdLocked(mu);
1363 
1364   if (all_locks->n == 0) {
1365     // There are no other locks held. Return now so that we don't need to
1366     // call GetSynchEvent(). This way we do not record the stack trace
1367     // for this Mutex. It's ok, since if this Mutex is involved in a deadlock,
1368     // it can't always be the first lock acquired by a thread.
1369     return mu_id;
1370   }
1371 
1372   // We prefer to keep stack traces that show a thread holding and acquiring
1373   // as many locks as possible.  This increases the chances that a given edge
1374   // in the acquires-before graph will be represented in the stack traces
1375   // recorded for the locks.
1376   deadlock_graph->UpdateStackTrace(mu_id, all_locks->n + 1, GetStack);
1377 
1378   // For each other mutex already held by this thread:
1379   for (int i = 0; i != all_locks->n; i++) {
1380     const GraphId other_node_id = all_locks->locks[i].id;
1381     const Mutex *other =
1382         static_cast<const Mutex *>(deadlock_graph->Ptr(other_node_id));
1383     if (other == nullptr) {
1384       // Ignore stale lock
1385       continue;
1386     }
1387 
1388     // Add the acquired-before edge to the graph.
1389     if (!deadlock_graph->InsertEdge(other_node_id, mu_id)) {
1390       ScopedDeadlockReportBuffers scoped_buffers;
1391       DeadlockReportBuffers *b = scoped_buffers.b;
1392       static int number_of_reported_deadlocks = 0;
1393       number_of_reported_deadlocks++;
1394       // Symbolize only 2 first deadlock report to avoid huge slowdowns.
1395       bool symbolize = number_of_reported_deadlocks <= 2;
1396       ABSL_RAW_LOG(ERROR, "Potential Mutex deadlock: %s",
1397                    CurrentStackString(b->buf, sizeof (b->buf), symbolize));
1398       size_t len = 0;
1399       for (int j = 0; j != all_locks->n; j++) {
1400         void* pr = deadlock_graph->Ptr(all_locks->locks[j].id);
1401         if (pr != nullptr) {
1402           snprintf(b->buf + len, sizeof (b->buf) - len, " %p", pr);
1403           len += strlen(&b->buf[len]);
1404         }
1405       }
1406       ABSL_RAW_LOG(ERROR,
1407                    "Acquiring absl::Mutex %p while holding %s; a cycle in the "
1408                    "historical lock ordering graph has been observed",
1409                    static_cast<void *>(mu), b->buf);
1410       ABSL_RAW_LOG(ERROR, "Cycle: ");
1411       int path_len = deadlock_graph->FindPath(
1412           mu_id, other_node_id, ABSL_ARRAYSIZE(b->path), b->path);
1413       for (int j = 0; j != path_len; j++) {
1414         GraphId id = b->path[j];
1415         Mutex *path_mu = static_cast<Mutex *>(deadlock_graph->Ptr(id));
1416         if (path_mu == nullptr) continue;
1417         void** stack;
1418         int depth = deadlock_graph->GetStackTrace(id, &stack);
1419         snprintf(b->buf, sizeof(b->buf),
1420                  "mutex@%p stack: ", static_cast<void *>(path_mu));
1421         StackString(stack, depth, b->buf + strlen(b->buf),
1422                     static_cast<int>(sizeof(b->buf) - strlen(b->buf)),
1423                     symbolize);
1424         ABSL_RAW_LOG(ERROR, "%s", b->buf);
1425       }
1426       if (synch_deadlock_detection.load(std::memory_order_acquire) ==
1427           OnDeadlockCycle::kAbort) {
1428         deadlock_graph_mu.Unlock();  // avoid deadlock in fatal sighandler
1429         ABSL_RAW_LOG(FATAL, "dying due to potential deadlock");
1430         return mu_id;
1431       }
1432       break;   // report at most one potential deadlock per acquisition
1433     }
1434   }
1435 
1436   return mu_id;
1437 }
1438 
1439 // Invoke DeadlockCheck() iff we're in debug mode and
1440 // deadlock checking has been enabled.
DebugOnlyDeadlockCheck(Mutex * mu)1441 static inline GraphId DebugOnlyDeadlockCheck(Mutex *mu) {
1442   if (kDebugMode && synch_deadlock_detection.load(std::memory_order_acquire) !=
1443                         OnDeadlockCycle::kIgnore) {
1444     return DeadlockCheck(mu);
1445   } else {
1446     return InvalidGraphId();
1447   }
1448 }
1449 
ForgetDeadlockInfo()1450 void Mutex::ForgetDeadlockInfo() {
1451   if (kDebugMode && synch_deadlock_detection.load(std::memory_order_acquire) !=
1452                         OnDeadlockCycle::kIgnore) {
1453     deadlock_graph_mu.Lock();
1454     if (deadlock_graph != nullptr) {
1455       deadlock_graph->RemoveNode(this);
1456     }
1457     deadlock_graph_mu.Unlock();
1458   }
1459 }
1460 
AssertNotHeld() const1461 void Mutex::AssertNotHeld() const {
1462   // We have the data to allow this check only if in debug mode and deadlock
1463   // detection is enabled.
1464   if (kDebugMode &&
1465       (mu_.load(std::memory_order_relaxed) & (kMuWriter | kMuReader)) != 0 &&
1466       synch_deadlock_detection.load(std::memory_order_acquire) !=
1467           OnDeadlockCycle::kIgnore) {
1468     GraphId id = GetGraphId(const_cast<Mutex *>(this));
1469     SynchLocksHeld *locks = Synch_GetAllLocks();
1470     for (int i = 0; i != locks->n; i++) {
1471       if (locks->locks[i].id == id) {
1472         SynchEvent *mu_events = GetSynchEvent(this);
1473         ABSL_RAW_LOG(FATAL, "thread should not hold mutex %p %s",
1474                      static_cast<const void *>(this),
1475                      (mu_events == nullptr ? "" : mu_events->name));
1476       }
1477     }
1478   }
1479 }
1480 
1481 // Attempt to acquire *mu, and return whether successful.  The implementation
1482 // may spin for a short while if the lock cannot be acquired immediately.
TryAcquireWithSpinning(std::atomic<intptr_t> * mu)1483 static bool TryAcquireWithSpinning(std::atomic<intptr_t>* mu) {
1484   int c = GetMutexGlobals().spinloop_iterations;
1485   do {  // do/while somewhat faster on AMD
1486     intptr_t v = mu->load(std::memory_order_relaxed);
1487     if ((v & (kMuReader|kMuEvent)) != 0) {
1488       return false;  // a reader or tracing -> give up
1489     } else if (((v & kMuWriter) == 0) &&  // no holder -> try to acquire
1490                mu->compare_exchange_strong(v, kMuWriter | v,
1491                                            std::memory_order_acquire,
1492                                            std::memory_order_relaxed)) {
1493       return true;
1494     }
1495   } while (--c > 0);
1496   return false;
1497 }
1498 
Lock()1499 ABSL_XRAY_LOG_ARGS(1) void Mutex::Lock() {
1500   ABSL_TSAN_MUTEX_PRE_LOCK(this, 0);
1501   GraphId id = DebugOnlyDeadlockCheck(this);
1502   intptr_t v = mu_.load(std::memory_order_relaxed);
1503   // try fast acquire, then spin loop
1504   if ((v & (kMuWriter | kMuReader | kMuEvent)) != 0 ||
1505       !mu_.compare_exchange_strong(v, kMuWriter | v,
1506                                    std::memory_order_acquire,
1507                                    std::memory_order_relaxed)) {
1508     // try spin acquire, then slow loop
1509     if (!TryAcquireWithSpinning(&this->mu_)) {
1510       this->LockSlow(kExclusive, nullptr, 0);
1511     }
1512   }
1513   DebugOnlyLockEnter(this, id);
1514   ABSL_TSAN_MUTEX_POST_LOCK(this, 0, 0);
1515 }
1516 
ReaderLock()1517 ABSL_XRAY_LOG_ARGS(1) void Mutex::ReaderLock() {
1518   ABSL_TSAN_MUTEX_PRE_LOCK(this, __tsan_mutex_read_lock);
1519   GraphId id = DebugOnlyDeadlockCheck(this);
1520   intptr_t v = mu_.load(std::memory_order_relaxed);
1521   // try fast acquire, then slow loop
1522   if ((v & (kMuWriter | kMuWait | kMuEvent)) != 0 ||
1523       !mu_.compare_exchange_strong(v, (kMuReader | v) + kMuOne,
1524                                    std::memory_order_acquire,
1525                                    std::memory_order_relaxed)) {
1526     this->LockSlow(kShared, nullptr, 0);
1527   }
1528   DebugOnlyLockEnter(this, id);
1529   ABSL_TSAN_MUTEX_POST_LOCK(this, __tsan_mutex_read_lock, 0);
1530 }
1531 
LockWhen(const Condition & cond)1532 void Mutex::LockWhen(const Condition &cond) {
1533   ABSL_TSAN_MUTEX_PRE_LOCK(this, 0);
1534   GraphId id = DebugOnlyDeadlockCheck(this);
1535   this->LockSlow(kExclusive, &cond, 0);
1536   DebugOnlyLockEnter(this, id);
1537   ABSL_TSAN_MUTEX_POST_LOCK(this, 0, 0);
1538 }
1539 
LockWhenWithTimeout(const Condition & cond,absl::Duration timeout)1540 bool Mutex::LockWhenWithTimeout(const Condition &cond, absl::Duration timeout) {
1541   return LockWhenWithDeadline(cond, DeadlineFromTimeout(timeout));
1542 }
1543 
LockWhenWithDeadline(const Condition & cond,absl::Time deadline)1544 bool Mutex::LockWhenWithDeadline(const Condition &cond, absl::Time deadline) {
1545   ABSL_TSAN_MUTEX_PRE_LOCK(this, 0);
1546   GraphId id = DebugOnlyDeadlockCheck(this);
1547   bool res = LockSlowWithDeadline(kExclusive, &cond,
1548                                   KernelTimeout(deadline), 0);
1549   DebugOnlyLockEnter(this, id);
1550   ABSL_TSAN_MUTEX_POST_LOCK(this, 0, 0);
1551   return res;
1552 }
1553 
ReaderLockWhen(const Condition & cond)1554 void Mutex::ReaderLockWhen(const Condition &cond) {
1555   ABSL_TSAN_MUTEX_PRE_LOCK(this, __tsan_mutex_read_lock);
1556   GraphId id = DebugOnlyDeadlockCheck(this);
1557   this->LockSlow(kShared, &cond, 0);
1558   DebugOnlyLockEnter(this, id);
1559   ABSL_TSAN_MUTEX_POST_LOCK(this, __tsan_mutex_read_lock, 0);
1560 }
1561 
ReaderLockWhenWithTimeout(const Condition & cond,absl::Duration timeout)1562 bool Mutex::ReaderLockWhenWithTimeout(const Condition &cond,
1563                                       absl::Duration timeout) {
1564   return ReaderLockWhenWithDeadline(cond, DeadlineFromTimeout(timeout));
1565 }
1566 
ReaderLockWhenWithDeadline(const Condition & cond,absl::Time deadline)1567 bool Mutex::ReaderLockWhenWithDeadline(const Condition &cond,
1568                                        absl::Time deadline) {
1569   ABSL_TSAN_MUTEX_PRE_LOCK(this, __tsan_mutex_read_lock);
1570   GraphId id = DebugOnlyDeadlockCheck(this);
1571   bool res = LockSlowWithDeadline(kShared, &cond, KernelTimeout(deadline), 0);
1572   DebugOnlyLockEnter(this, id);
1573   ABSL_TSAN_MUTEX_POST_LOCK(this, __tsan_mutex_read_lock, 0);
1574   return res;
1575 }
1576 
Await(const Condition & cond)1577 void Mutex::Await(const Condition &cond) {
1578   if (cond.Eval()) {    // condition already true; nothing to do
1579     if (kDebugMode) {
1580       this->AssertReaderHeld();
1581     }
1582   } else {              // normal case
1583     ABSL_RAW_CHECK(this->AwaitCommon(cond, KernelTimeout::Never()),
1584                    "condition untrue on return from Await");
1585   }
1586 }
1587 
AwaitWithTimeout(const Condition & cond,absl::Duration timeout)1588 bool Mutex::AwaitWithTimeout(const Condition &cond, absl::Duration timeout) {
1589   return AwaitWithDeadline(cond, DeadlineFromTimeout(timeout));
1590 }
1591 
AwaitWithDeadline(const Condition & cond,absl::Time deadline)1592 bool Mutex::AwaitWithDeadline(const Condition &cond, absl::Time deadline) {
1593   if (cond.Eval()) {      // condition already true; nothing to do
1594     if (kDebugMode) {
1595       this->AssertReaderHeld();
1596     }
1597     return true;
1598   }
1599 
1600   KernelTimeout t{deadline};
1601   bool res = this->AwaitCommon(cond, t);
1602   ABSL_RAW_CHECK(res || t.has_timeout(),
1603                  "condition untrue on return from Await");
1604   return res;
1605 }
1606 
AwaitCommon(const Condition & cond,KernelTimeout t)1607 bool Mutex::AwaitCommon(const Condition &cond, KernelTimeout t) {
1608   this->AssertReaderHeld();
1609   MuHow how =
1610       (mu_.load(std::memory_order_relaxed) & kMuWriter) ? kExclusive : kShared;
1611   ABSL_TSAN_MUTEX_PRE_UNLOCK(this, TsanFlags(how));
1612   SynchWaitParams waitp(
1613       how, &cond, t, nullptr /*no cvmu*/, Synch_GetPerThreadAnnotated(this),
1614       nullptr /*no cv_word*/);
1615   int flags = kMuHasBlocked;
1616   if (!Condition::GuaranteedEqual(&cond, nullptr)) {
1617     flags |= kMuIsCond;
1618   }
1619   this->UnlockSlow(&waitp);
1620   this->Block(waitp.thread);
1621   ABSL_TSAN_MUTEX_POST_UNLOCK(this, TsanFlags(how));
1622   ABSL_TSAN_MUTEX_PRE_LOCK(this, TsanFlags(how));
1623   this->LockSlowLoop(&waitp, flags);
1624   bool res = waitp.cond != nullptr ||  // => cond known true from LockSlowLoop
1625              EvalConditionAnnotated(&cond, this, true, false, how == kShared);
1626   ABSL_TSAN_MUTEX_POST_LOCK(this, TsanFlags(how), 0);
1627   return res;
1628 }
1629 
TryLock()1630 ABSL_XRAY_LOG_ARGS(1) bool Mutex::TryLock() {
1631   ABSL_TSAN_MUTEX_PRE_LOCK(this, __tsan_mutex_try_lock);
1632   intptr_t v = mu_.load(std::memory_order_relaxed);
1633   if ((v & (kMuWriter | kMuReader | kMuEvent)) == 0 &&  // try fast acquire
1634       mu_.compare_exchange_strong(v, kMuWriter | v,
1635                                   std::memory_order_acquire,
1636                                   std::memory_order_relaxed)) {
1637     DebugOnlyLockEnter(this);
1638     ABSL_TSAN_MUTEX_POST_LOCK(this, __tsan_mutex_try_lock, 0);
1639     return true;
1640   }
1641   if ((v & kMuEvent) != 0) {              // we're recording events
1642     if ((v & kExclusive->slow_need_zero) == 0 &&  // try fast acquire
1643         mu_.compare_exchange_strong(
1644             v, (kExclusive->fast_or | v) + kExclusive->fast_add,
1645             std::memory_order_acquire, std::memory_order_relaxed)) {
1646       DebugOnlyLockEnter(this);
1647       PostSynchEvent(this, SYNCH_EV_TRYLOCK_SUCCESS);
1648       ABSL_TSAN_MUTEX_POST_LOCK(this, __tsan_mutex_try_lock, 0);
1649       return true;
1650     } else {
1651       PostSynchEvent(this, SYNCH_EV_TRYLOCK_FAILED);
1652     }
1653   }
1654   ABSL_TSAN_MUTEX_POST_LOCK(
1655       this, __tsan_mutex_try_lock | __tsan_mutex_try_lock_failed, 0);
1656   return false;
1657 }
1658 
ReaderTryLock()1659 ABSL_XRAY_LOG_ARGS(1) bool Mutex::ReaderTryLock() {
1660   ABSL_TSAN_MUTEX_PRE_LOCK(this,
1661                            __tsan_mutex_read_lock | __tsan_mutex_try_lock);
1662   intptr_t v = mu_.load(std::memory_order_relaxed);
1663   // The while-loops (here and below) iterate only if the mutex word keeps
1664   // changing (typically because the reader count changes) under the CAS.  We
1665   // limit the number of attempts to avoid having to think about livelock.
1666   int loop_limit = 5;
1667   while ((v & (kMuWriter|kMuWait|kMuEvent)) == 0 && loop_limit != 0) {
1668     if (mu_.compare_exchange_strong(v, (kMuReader | v) + kMuOne,
1669                                     std::memory_order_acquire,
1670                                     std::memory_order_relaxed)) {
1671       DebugOnlyLockEnter(this);
1672       ABSL_TSAN_MUTEX_POST_LOCK(
1673           this, __tsan_mutex_read_lock | __tsan_mutex_try_lock, 0);
1674       return true;
1675     }
1676     loop_limit--;
1677     v = mu_.load(std::memory_order_relaxed);
1678   }
1679   if ((v & kMuEvent) != 0) {   // we're recording events
1680     loop_limit = 5;
1681     while ((v & kShared->slow_need_zero) == 0 && loop_limit != 0) {
1682       if (mu_.compare_exchange_strong(v, (kMuReader | v) + kMuOne,
1683                                       std::memory_order_acquire,
1684                                       std::memory_order_relaxed)) {
1685         DebugOnlyLockEnter(this);
1686         PostSynchEvent(this, SYNCH_EV_READERTRYLOCK_SUCCESS);
1687         ABSL_TSAN_MUTEX_POST_LOCK(
1688             this, __tsan_mutex_read_lock | __tsan_mutex_try_lock, 0);
1689         return true;
1690       }
1691       loop_limit--;
1692       v = mu_.load(std::memory_order_relaxed);
1693     }
1694     if ((v & kMuEvent) != 0) {
1695       PostSynchEvent(this, SYNCH_EV_READERTRYLOCK_FAILED);
1696     }
1697   }
1698   ABSL_TSAN_MUTEX_POST_LOCK(this,
1699                             __tsan_mutex_read_lock | __tsan_mutex_try_lock |
1700                                 __tsan_mutex_try_lock_failed,
1701                             0);
1702   return false;
1703 }
1704 
Unlock()1705 ABSL_XRAY_LOG_ARGS(1) void Mutex::Unlock() {
1706   ABSL_TSAN_MUTEX_PRE_UNLOCK(this, 0);
1707   DebugOnlyLockLeave(this);
1708   intptr_t v = mu_.load(std::memory_order_relaxed);
1709 
1710   if (kDebugMode && ((v & (kMuWriter | kMuReader)) != kMuWriter)) {
1711     ABSL_RAW_LOG(FATAL, "Mutex unlocked when destroyed or not locked: v=0x%x",
1712                  static_cast<unsigned>(v));
1713   }
1714 
1715   // should_try_cas is whether we'll try a compare-and-swap immediately.
1716   // NOTE: optimized out when kDebugMode is false.
1717   bool should_try_cas = ((v & (kMuEvent | kMuWriter)) == kMuWriter &&
1718                           (v & (kMuWait | kMuDesig)) != kMuWait);
1719   // But, we can use an alternate computation of it, that compilers
1720   // currently don't find on their own.  When that changes, this function
1721   // can be simplified.
1722   intptr_t x = (v ^ (kMuWriter | kMuWait)) & (kMuWriter | kMuEvent);
1723   intptr_t y = (v ^ (kMuWriter | kMuWait)) & (kMuWait | kMuDesig);
1724   // Claim: "x == 0 && y > 0" is equal to should_try_cas.
1725   // Also, because kMuWriter and kMuEvent exceed kMuDesig and kMuWait,
1726   // all possible non-zero values for x exceed all possible values for y.
1727   // Therefore, (x == 0 && y > 0) == (x < y).
1728   if (kDebugMode && should_try_cas != (x < y)) {
1729     // We would usually use PRIdPTR here, but is not correctly implemented
1730     // within the android toolchain.
1731     ABSL_RAW_LOG(FATAL, "internal logic error %llx %llx %llx\n",
1732                  static_cast<long long>(v), static_cast<long long>(x),
1733                  static_cast<long long>(y));
1734   }
1735   if (x < y &&
1736       mu_.compare_exchange_strong(v, v & ~(kMuWrWait | kMuWriter),
1737                                   std::memory_order_release,
1738                                   std::memory_order_relaxed)) {
1739     // fast writer release (writer with no waiters or with designated waker)
1740   } else {
1741     this->UnlockSlow(nullptr /*no waitp*/);  // take slow path
1742   }
1743   ABSL_TSAN_MUTEX_POST_UNLOCK(this, 0);
1744 }
1745 
1746 // Requires v to represent a reader-locked state.
ExactlyOneReader(intptr_t v)1747 static bool ExactlyOneReader(intptr_t v) {
1748   assert((v & (kMuWriter|kMuReader)) == kMuReader);
1749   assert((v & kMuHigh) != 0);
1750   // The more straightforward "(v & kMuHigh) == kMuOne" also works, but
1751   // on some architectures the following generates slightly smaller code.
1752   // It may be faster too.
1753   constexpr intptr_t kMuMultipleWaitersMask = kMuHigh ^ kMuOne;
1754   return (v & kMuMultipleWaitersMask) == 0;
1755 }
1756 
ReaderUnlock()1757 ABSL_XRAY_LOG_ARGS(1) void Mutex::ReaderUnlock() {
1758   ABSL_TSAN_MUTEX_PRE_UNLOCK(this, __tsan_mutex_read_lock);
1759   DebugOnlyLockLeave(this);
1760   intptr_t v = mu_.load(std::memory_order_relaxed);
1761   assert((v & (kMuWriter|kMuReader)) == kMuReader);
1762   if ((v & (kMuReader|kMuWait|kMuEvent)) == kMuReader) {
1763     // fast reader release (reader with no waiters)
1764     intptr_t clear = ExactlyOneReader(v) ? kMuReader|kMuOne : kMuOne;
1765     if (mu_.compare_exchange_strong(v, v - clear,
1766                                     std::memory_order_release,
1767                                     std::memory_order_relaxed)) {
1768       ABSL_TSAN_MUTEX_POST_UNLOCK(this, __tsan_mutex_read_lock);
1769       return;
1770     }
1771   }
1772   this->UnlockSlow(nullptr /*no waitp*/);  // take slow path
1773   ABSL_TSAN_MUTEX_POST_UNLOCK(this, __tsan_mutex_read_lock);
1774 }
1775 
1776 // Clears the designated waker flag in the mutex if this thread has blocked, and
1777 // therefore may be the designated waker.
ClearDesignatedWakerMask(int flag)1778 static intptr_t ClearDesignatedWakerMask(int flag) {
1779   assert(flag >= 0);
1780   assert(flag <= 1);
1781   switch (flag) {
1782     case 0:  // not blocked
1783       return ~static_cast<intptr_t>(0);
1784     case 1:  // blocked; turn off the designated waker bit
1785       return ~static_cast<intptr_t>(kMuDesig);
1786   }
1787   ABSL_INTERNAL_UNREACHABLE;
1788 }
1789 
1790 // Conditionally ignores the existence of waiting writers if a reader that has
1791 // already blocked once wakes up.
IgnoreWaitingWritersMask(int flag)1792 static intptr_t IgnoreWaitingWritersMask(int flag) {
1793   assert(flag >= 0);
1794   assert(flag <= 1);
1795   switch (flag) {
1796     case 0:  // not blocked
1797       return ~static_cast<intptr_t>(0);
1798     case 1:  // blocked; pretend there are no waiting writers
1799       return ~static_cast<intptr_t>(kMuWrWait);
1800   }
1801   ABSL_INTERNAL_UNREACHABLE;
1802 }
1803 
1804 // Internal version of LockWhen().  See LockSlowWithDeadline()
LockSlow(MuHow how,const Condition * cond,int flags)1805 ABSL_ATTRIBUTE_NOINLINE void Mutex::LockSlow(MuHow how, const Condition *cond,
1806                                              int flags) {
1807   ABSL_RAW_CHECK(
1808       this->LockSlowWithDeadline(how, cond, KernelTimeout::Never(), flags),
1809       "condition untrue on return from LockSlow");
1810 }
1811 
1812 // Compute cond->Eval() and tell race detectors that we do it under mutex mu.
EvalConditionAnnotated(const Condition * cond,Mutex * mu,bool locking,bool trylock,bool read_lock)1813 static inline bool EvalConditionAnnotated(const Condition *cond, Mutex *mu,
1814                                           bool locking, bool trylock,
1815                                           bool read_lock) {
1816   // Delicate annotation dance.
1817   // We are currently inside of read/write lock/unlock operation.
1818   // All memory accesses are ignored inside of mutex operations + for unlock
1819   // operation tsan considers that we've already released the mutex.
1820   bool res = false;
1821 #ifdef ABSL_INTERNAL_HAVE_TSAN_INTERFACE
1822   const uint32_t flags = read_lock ? __tsan_mutex_read_lock : 0;
1823   const uint32_t tryflags = flags | (trylock ? __tsan_mutex_try_lock : 0);
1824 #endif
1825   if (locking) {
1826     // For lock we pretend that we have finished the operation,
1827     // evaluate the predicate, then unlock the mutex and start locking it again
1828     // to match the annotation at the end of outer lock operation.
1829     // Note: we can't simply do POST_LOCK, Eval, PRE_LOCK, because then tsan
1830     // will think the lock acquisition is recursive which will trigger
1831     // deadlock detector.
1832     ABSL_TSAN_MUTEX_POST_LOCK(mu, tryflags, 0);
1833     res = cond->Eval();
1834     // There is no "try" version of Unlock, so use flags instead of tryflags.
1835     ABSL_TSAN_MUTEX_PRE_UNLOCK(mu, flags);
1836     ABSL_TSAN_MUTEX_POST_UNLOCK(mu, flags);
1837     ABSL_TSAN_MUTEX_PRE_LOCK(mu, tryflags);
1838   } else {
1839     // Similarly, for unlock we pretend that we have unlocked the mutex,
1840     // lock the mutex, evaluate the predicate, and start unlocking it again
1841     // to match the annotation at the end of outer unlock operation.
1842     ABSL_TSAN_MUTEX_POST_UNLOCK(mu, flags);
1843     ABSL_TSAN_MUTEX_PRE_LOCK(mu, flags);
1844     ABSL_TSAN_MUTEX_POST_LOCK(mu, flags, 0);
1845     res = cond->Eval();
1846     ABSL_TSAN_MUTEX_PRE_UNLOCK(mu, flags);
1847   }
1848   // Prevent unused param warnings in non-TSAN builds.
1849   static_cast<void>(mu);
1850   static_cast<void>(trylock);
1851   static_cast<void>(read_lock);
1852   return res;
1853 }
1854 
1855 // Compute cond->Eval() hiding it from race detectors.
1856 // We are hiding it because inside of UnlockSlow we can evaluate a predicate
1857 // that was just added by a concurrent Lock operation; Lock adds the predicate
1858 // to the internal Mutex list without actually acquiring the Mutex
1859 // (it only acquires the internal spinlock, which is rightfully invisible for
1860 // tsan). As the result there is no tsan-visible synchronization between the
1861 // addition and this thread. So if we would enable race detection here,
1862 // it would race with the predicate initialization.
EvalConditionIgnored(Mutex * mu,const Condition * cond)1863 static inline bool EvalConditionIgnored(Mutex *mu, const Condition *cond) {
1864   // Memory accesses are already ignored inside of lock/unlock operations,
1865   // but synchronization operations are also ignored. When we evaluate the
1866   // predicate we must ignore only memory accesses but not synchronization,
1867   // because missed synchronization can lead to false reports later.
1868   // So we "divert" (which un-ignores both memory accesses and synchronization)
1869   // and then separately turn on ignores of memory accesses.
1870   ABSL_TSAN_MUTEX_PRE_DIVERT(mu, 0);
1871   ABSL_ANNOTATE_IGNORE_READS_AND_WRITES_BEGIN();
1872   bool res = cond->Eval();
1873   ABSL_ANNOTATE_IGNORE_READS_AND_WRITES_END();
1874   ABSL_TSAN_MUTEX_POST_DIVERT(mu, 0);
1875   static_cast<void>(mu);  // Prevent unused param warning in non-TSAN builds.
1876   return res;
1877 }
1878 
1879 // Internal equivalent of *LockWhenWithDeadline(), where
1880 //   "t" represents the absolute timeout; !t.has_timeout() means "forever".
1881 //   "how" is "kShared" (for ReaderLockWhen) or "kExclusive" (for LockWhen)
1882 // In flags, bits are ored together:
1883 // - kMuHasBlocked indicates that the client has already blocked on the call so
1884 //   the designated waker bit must be cleared and waiting writers should not
1885 //   obstruct this call
1886 // - kMuIsCond indicates that this is a conditional acquire (condition variable,
1887 //   Await,  LockWhen) so contention profiling should be suppressed.
LockSlowWithDeadline(MuHow how,const Condition * cond,KernelTimeout t,int flags)1888 bool Mutex::LockSlowWithDeadline(MuHow how, const Condition *cond,
1889                                  KernelTimeout t, int flags) {
1890   intptr_t v = mu_.load(std::memory_order_relaxed);
1891   bool unlock = false;
1892   if ((v & how->fast_need_zero) == 0 &&  // try fast acquire
1893       mu_.compare_exchange_strong(
1894           v,
1895           (how->fast_or |
1896            (v & ClearDesignatedWakerMask(flags & kMuHasBlocked))) +
1897               how->fast_add,
1898           std::memory_order_acquire, std::memory_order_relaxed)) {
1899     if (cond == nullptr ||
1900         EvalConditionAnnotated(cond, this, true, false, how == kShared)) {
1901       return true;
1902     }
1903     unlock = true;
1904   }
1905   SynchWaitParams waitp(
1906       how, cond, t, nullptr /*no cvmu*/, Synch_GetPerThreadAnnotated(this),
1907       nullptr /*no cv_word*/);
1908   if (!Condition::GuaranteedEqual(cond, nullptr)) {
1909     flags |= kMuIsCond;
1910   }
1911   if (unlock) {
1912     this->UnlockSlow(&waitp);
1913     this->Block(waitp.thread);
1914     flags |= kMuHasBlocked;
1915   }
1916   this->LockSlowLoop(&waitp, flags);
1917   return waitp.cond != nullptr ||  // => cond known true from LockSlowLoop
1918          cond == nullptr ||
1919          EvalConditionAnnotated(cond, this, true, false, how == kShared);
1920 }
1921 
1922 // RAW_CHECK_FMT() takes a condition, a printf-style format string, and
1923 // the printf-style argument list.   The format string must be a literal.
1924 // Arguments after the first are not evaluated unless the condition is true.
1925 #define RAW_CHECK_FMT(cond, ...)                                   \
1926   do {                                                             \
1927     if (ABSL_PREDICT_FALSE(!(cond))) {                             \
1928       ABSL_RAW_LOG(FATAL, "Check " #cond " failed: " __VA_ARGS__); \
1929     }                                                              \
1930   } while (0)
1931 
CheckForMutexCorruption(intptr_t v,const char * label)1932 static void CheckForMutexCorruption(intptr_t v, const char* label) {
1933   // Test for either of two situations that should not occur in v:
1934   //   kMuWriter and kMuReader
1935   //   kMuWrWait and !kMuWait
1936   const uintptr_t w = static_cast<uintptr_t>(v ^ kMuWait);
1937   // By flipping that bit, we can now test for:
1938   //   kMuWriter and kMuReader in w
1939   //   kMuWrWait and kMuWait in w
1940   // We've chosen these two pairs of values to be so that they will overlap,
1941   // respectively, when the word is left shifted by three.  This allows us to
1942   // save a branch in the common (correct) case of them not being coincident.
1943   static_assert(kMuReader << 3 == kMuWriter, "must match");
1944   static_assert(kMuWait << 3 == kMuWrWait, "must match");
1945   if (ABSL_PREDICT_TRUE((w & (w << 3) & (kMuWriter | kMuWrWait)) == 0)) return;
1946   RAW_CHECK_FMT((v & (kMuWriter | kMuReader)) != (kMuWriter | kMuReader),
1947                 "%s: Mutex corrupt: both reader and writer lock held: %p",
1948                 label, reinterpret_cast<void *>(v));
1949   RAW_CHECK_FMT((v & (kMuWait | kMuWrWait)) != kMuWrWait,
1950                 "%s: Mutex corrupt: waiting writer with no waiters: %p",
1951                 label, reinterpret_cast<void *>(v));
1952   assert(false);
1953 }
1954 
LockSlowLoop(SynchWaitParams * waitp,int flags)1955 void Mutex::LockSlowLoop(SynchWaitParams *waitp, int flags) {
1956   SchedulingGuard::ScopedDisable disable_rescheduling;
1957   int c = 0;
1958   intptr_t v = mu_.load(std::memory_order_relaxed);
1959   if ((v & kMuEvent) != 0) {
1960     PostSynchEvent(this,
1961          waitp->how == kExclusive?  SYNCH_EV_LOCK: SYNCH_EV_READERLOCK);
1962   }
1963   ABSL_RAW_CHECK(
1964       waitp->thread->waitp == nullptr || waitp->thread->suppress_fatal_errors,
1965       "detected illegal recursion into Mutex code");
1966   for (;;) {
1967     v = mu_.load(std::memory_order_relaxed);
1968     CheckForMutexCorruption(v, "Lock");
1969     if ((v & waitp->how->slow_need_zero) == 0) {
1970       if (mu_.compare_exchange_strong(
1971               v,
1972               (waitp->how->fast_or |
1973                (v & ClearDesignatedWakerMask(flags & kMuHasBlocked))) +
1974                   waitp->how->fast_add,
1975               std::memory_order_acquire, std::memory_order_relaxed)) {
1976         if (waitp->cond == nullptr ||
1977             EvalConditionAnnotated(waitp->cond, this, true, false,
1978                                    waitp->how == kShared)) {
1979           break;  // we timed out, or condition true, so return
1980         }
1981         this->UnlockSlow(waitp);  // got lock but condition false
1982         this->Block(waitp->thread);
1983         flags |= kMuHasBlocked;
1984         c = 0;
1985       }
1986     } else {                      // need to access waiter list
1987       bool dowait = false;
1988       if ((v & (kMuSpin|kMuWait)) == 0) {   // no waiters
1989         // This thread tries to become the one and only waiter.
1990         PerThreadSynch *new_h = Enqueue(nullptr, waitp, v, flags);
1991         intptr_t nv =
1992             (v & ClearDesignatedWakerMask(flags & kMuHasBlocked) & kMuLow) |
1993             kMuWait;
1994         ABSL_RAW_CHECK(new_h != nullptr, "Enqueue to empty list failed");
1995         if (waitp->how == kExclusive && (v & kMuReader) != 0) {
1996           nv |= kMuWrWait;
1997         }
1998         if (mu_.compare_exchange_strong(
1999                 v, reinterpret_cast<intptr_t>(new_h) | nv,
2000                 std::memory_order_release, std::memory_order_relaxed)) {
2001           dowait = true;
2002         } else {            // attempted Enqueue() failed
2003           // zero out the waitp field set by Enqueue()
2004           waitp->thread->waitp = nullptr;
2005         }
2006       } else if ((v & waitp->how->slow_inc_need_zero &
2007                   IgnoreWaitingWritersMask(flags & kMuHasBlocked)) == 0) {
2008         // This is a reader that needs to increment the reader count,
2009         // but the count is currently held in the last waiter.
2010         if (mu_.compare_exchange_strong(
2011                 v,
2012                 (v & ClearDesignatedWakerMask(flags & kMuHasBlocked)) |
2013                     kMuSpin | kMuReader,
2014                 std::memory_order_acquire, std::memory_order_relaxed)) {
2015           PerThreadSynch *h = GetPerThreadSynch(v);
2016           h->readers += kMuOne;       // inc reader count in waiter
2017           do {                        // release spinlock
2018             v = mu_.load(std::memory_order_relaxed);
2019           } while (!mu_.compare_exchange_weak(v, (v & ~kMuSpin) | kMuReader,
2020                                               std::memory_order_release,
2021                                               std::memory_order_relaxed));
2022           if (waitp->cond == nullptr ||
2023               EvalConditionAnnotated(waitp->cond, this, true, false,
2024                                      waitp->how == kShared)) {
2025             break;  // we timed out, or condition true, so return
2026           }
2027           this->UnlockSlow(waitp);           // got lock but condition false
2028           this->Block(waitp->thread);
2029           flags |= kMuHasBlocked;
2030           c = 0;
2031         }
2032       } else if ((v & kMuSpin) == 0 &&  // attempt to queue ourselves
2033                  mu_.compare_exchange_strong(
2034                      v,
2035                      (v & ClearDesignatedWakerMask(flags & kMuHasBlocked)) |
2036                          kMuSpin | kMuWait,
2037                      std::memory_order_acquire, std::memory_order_relaxed)) {
2038         PerThreadSynch *h = GetPerThreadSynch(v);
2039         PerThreadSynch *new_h = Enqueue(h, waitp, v, flags);
2040         intptr_t wr_wait = 0;
2041         ABSL_RAW_CHECK(new_h != nullptr, "Enqueue to list failed");
2042         if (waitp->how == kExclusive && (v & kMuReader) != 0) {
2043           wr_wait = kMuWrWait;      // give priority to a waiting writer
2044         }
2045         do {                        // release spinlock
2046           v = mu_.load(std::memory_order_relaxed);
2047         } while (!mu_.compare_exchange_weak(
2048             v, (v & (kMuLow & ~kMuSpin)) | kMuWait | wr_wait |
2049             reinterpret_cast<intptr_t>(new_h),
2050             std::memory_order_release, std::memory_order_relaxed));
2051         dowait = true;
2052       }
2053       if (dowait) {
2054         this->Block(waitp->thread);  // wait until removed from list or timeout
2055         flags |= kMuHasBlocked;
2056         c = 0;
2057       }
2058     }
2059     ABSL_RAW_CHECK(
2060         waitp->thread->waitp == nullptr || waitp->thread->suppress_fatal_errors,
2061         "detected illegal recursion into Mutex code");
2062     // delay, then try again
2063     c = synchronization_internal::MutexDelay(c, GENTLE);
2064   }
2065   ABSL_RAW_CHECK(
2066       waitp->thread->waitp == nullptr || waitp->thread->suppress_fatal_errors,
2067       "detected illegal recursion into Mutex code");
2068   if ((v & kMuEvent) != 0) {
2069     PostSynchEvent(this,
2070                    waitp->how == kExclusive? SYNCH_EV_LOCK_RETURNING :
2071                                       SYNCH_EV_READERLOCK_RETURNING);
2072   }
2073 }
2074 
2075 // Unlock this mutex, which is held by the current thread.
2076 // If waitp is non-zero, it must be the wait parameters for the current thread
2077 // which holds the lock but is not runnable because its condition is false
2078 // or it is in the process of blocking on a condition variable; it must requeue
2079 // itself on the mutex/condvar to wait for its condition to become true.
UnlockSlow(SynchWaitParams * waitp)2080 ABSL_ATTRIBUTE_NOINLINE void Mutex::UnlockSlow(SynchWaitParams *waitp) {
2081   SchedulingGuard::ScopedDisable disable_rescheduling;
2082   intptr_t v = mu_.load(std::memory_order_relaxed);
2083   this->AssertReaderHeld();
2084   CheckForMutexCorruption(v, "Unlock");
2085   if ((v & kMuEvent) != 0) {
2086     PostSynchEvent(this,
2087                 (v & kMuWriter) != 0? SYNCH_EV_UNLOCK: SYNCH_EV_READERUNLOCK);
2088   }
2089   int c = 0;
2090   // the waiter under consideration to wake, or zero
2091   PerThreadSynch *w = nullptr;
2092   // the predecessor to w or zero
2093   PerThreadSynch *pw = nullptr;
2094   // head of the list searched previously, or zero
2095   PerThreadSynch *old_h = nullptr;
2096   // a condition that's known to be false.
2097   const Condition *known_false = nullptr;
2098   PerThreadSynch *wake_list = kPerThreadSynchNull;   // list of threads to wake
2099   intptr_t wr_wait = 0;        // set to kMuWrWait if we wake a reader and a
2100                                // later writer could have acquired the lock
2101                                // (starvation avoidance)
2102   ABSL_RAW_CHECK(waitp == nullptr || waitp->thread->waitp == nullptr ||
2103                      waitp->thread->suppress_fatal_errors,
2104                  "detected illegal recursion into Mutex code");
2105   // This loop finds threads wake_list to wakeup if any, and removes them from
2106   // the list of waiters.  In addition, it places waitp.thread on the queue of
2107   // waiters if waitp is non-zero.
2108   for (;;) {
2109     v = mu_.load(std::memory_order_relaxed);
2110     if ((v & kMuWriter) != 0 && (v & (kMuWait | kMuDesig)) != kMuWait &&
2111         waitp == nullptr) {
2112       // fast writer release (writer with no waiters or with designated waker)
2113       if (mu_.compare_exchange_strong(v, v & ~(kMuWrWait | kMuWriter),
2114                                       std::memory_order_release,
2115                                       std::memory_order_relaxed)) {
2116         return;
2117       }
2118     } else if ((v & (kMuReader | kMuWait)) == kMuReader && waitp == nullptr) {
2119       // fast reader release (reader with no waiters)
2120       intptr_t clear = ExactlyOneReader(v) ? kMuReader | kMuOne : kMuOne;
2121       if (mu_.compare_exchange_strong(v, v - clear,
2122                                       std::memory_order_release,
2123                                       std::memory_order_relaxed)) {
2124         return;
2125       }
2126     } else if ((v & kMuSpin) == 0 &&  // attempt to get spinlock
2127                mu_.compare_exchange_strong(v, v | kMuSpin,
2128                                            std::memory_order_acquire,
2129                                            std::memory_order_relaxed)) {
2130       if ((v & kMuWait) == 0) {       // no one to wake
2131         intptr_t nv;
2132         bool do_enqueue = true;  // always Enqueue() the first time
2133         ABSL_RAW_CHECK(waitp != nullptr,
2134                        "UnlockSlow is confused");  // about to sleep
2135         do {    // must loop to release spinlock as reader count may change
2136           v = mu_.load(std::memory_order_relaxed);
2137           // decrement reader count if there are readers
2138           intptr_t new_readers = (v >= kMuOne)?  v - kMuOne : v;
2139           PerThreadSynch *new_h = nullptr;
2140           if (do_enqueue) {
2141             // If we are enqueuing on a CondVar (waitp->cv_word != nullptr) then
2142             // we must not retry here.  The initial attempt will always have
2143             // succeeded, further attempts would enqueue us against *this due to
2144             // Fer() handling.
2145             do_enqueue = (waitp->cv_word == nullptr);
2146             new_h = Enqueue(nullptr, waitp, new_readers, kMuIsCond);
2147           }
2148           intptr_t clear = kMuWrWait | kMuWriter;  // by default clear write bit
2149           if ((v & kMuWriter) == 0 && ExactlyOneReader(v)) {  // last reader
2150             clear = kMuWrWait | kMuReader;                    // clear read bit
2151           }
2152           nv = (v & kMuLow & ~clear & ~kMuSpin);
2153           if (new_h != nullptr) {
2154             nv |= kMuWait | reinterpret_cast<intptr_t>(new_h);
2155           } else {  // new_h could be nullptr if we queued ourselves on a
2156                     // CondVar
2157             // In that case, we must place the reader count back in the mutex
2158             // word, as Enqueue() did not store it in the new waiter.
2159             nv |= new_readers & kMuHigh;
2160           }
2161           // release spinlock & our lock; retry if reader-count changed
2162           // (writer count cannot change since we hold lock)
2163         } while (!mu_.compare_exchange_weak(v, nv,
2164                                             std::memory_order_release,
2165                                             std::memory_order_relaxed));
2166         break;
2167       }
2168 
2169       // There are waiters.
2170       // Set h to the head of the circular waiter list.
2171       PerThreadSynch *h = GetPerThreadSynch(v);
2172       if ((v & kMuReader) != 0 && (h->readers & kMuHigh) > kMuOne) {
2173         // a reader but not the last
2174         h->readers -= kMuOne;  // release our lock
2175         intptr_t nv = v;       // normally just release spinlock
2176         if (waitp != nullptr) {  // but waitp!=nullptr => must queue ourselves
2177           PerThreadSynch *new_h = Enqueue(h, waitp, v, kMuIsCond);
2178           ABSL_RAW_CHECK(new_h != nullptr,
2179                          "waiters disappeared during Enqueue()!");
2180           nv &= kMuLow;
2181           nv |= kMuWait | reinterpret_cast<intptr_t>(new_h);
2182         }
2183         mu_.store(nv, std::memory_order_release);  // release spinlock
2184         // can release with a store because there were waiters
2185         break;
2186       }
2187 
2188       // Either we didn't search before, or we marked the queue
2189       // as "maybe_unlocking" and no one else should have changed it.
2190       ABSL_RAW_CHECK(old_h == nullptr || h->maybe_unlocking,
2191                      "Mutex queue changed beneath us");
2192 
2193       // The lock is becoming free, and there's a waiter
2194       if (old_h != nullptr &&
2195           !old_h->may_skip) {                  // we used old_h as a terminator
2196         old_h->may_skip = true;                // allow old_h to skip once more
2197         ABSL_RAW_CHECK(old_h->skip == nullptr, "illegal skip from head");
2198         if (h != old_h && MuEquivalentWaiter(old_h, old_h->next)) {
2199           old_h->skip = old_h->next;  // old_h not head & can skip to successor
2200         }
2201       }
2202       if (h->next->waitp->how == kExclusive &&
2203           Condition::GuaranteedEqual(h->next->waitp->cond, nullptr)) {
2204         // easy case: writer with no condition; no need to search
2205         pw = h;                       // wake w, the successor of h (=pw)
2206         w = h->next;
2207         w->wake = true;
2208         // We are waking up a writer.  This writer may be racing against
2209         // an already awake reader for the lock.  We want the
2210         // writer to usually win this race,
2211         // because if it doesn't, we can potentially keep taking a reader
2212         // perpetually and writers will starve.  Worse than
2213         // that, this can also starve other readers if kMuWrWait gets set
2214         // later.
2215         wr_wait = kMuWrWait;
2216       } else if (w != nullptr && (w->waitp->how == kExclusive || h == old_h)) {
2217         // we found a waiter w to wake on a previous iteration and either it's
2218         // a writer, or we've searched the entire list so we have all the
2219         // readers.
2220         if (pw == nullptr) {  // if w's predecessor is unknown, it must be h
2221           pw = h;
2222         }
2223       } else {
2224         // At this point we don't know all the waiters to wake, and the first
2225         // waiter has a condition or is a reader.  We avoid searching over
2226         // waiters we've searched on previous iterations by starting at
2227         // old_h if it's set.  If old_h==h, there's no one to wakeup at all.
2228         if (old_h == h) {      // we've searched before, and nothing's new
2229                                // so there's no one to wake.
2230           intptr_t nv = (v & ~(kMuReader|kMuWriter|kMuWrWait));
2231           h->readers = 0;
2232           h->maybe_unlocking = false;   // finished unlocking
2233           if (waitp != nullptr) {       // we must queue ourselves and sleep
2234             PerThreadSynch *new_h = Enqueue(h, waitp, v, kMuIsCond);
2235             nv &= kMuLow;
2236             if (new_h != nullptr) {
2237               nv |= kMuWait | reinterpret_cast<intptr_t>(new_h);
2238             }  // else new_h could be nullptr if we queued ourselves on a
2239                // CondVar
2240           }
2241           // release spinlock & lock
2242           // can release with a store because there were waiters
2243           mu_.store(nv, std::memory_order_release);
2244           break;
2245         }
2246 
2247         // set up to walk the list
2248         PerThreadSynch *w_walk;   // current waiter during list walk
2249         PerThreadSynch *pw_walk;  // previous waiter during list walk
2250         if (old_h != nullptr) {  // we've searched up to old_h before
2251           pw_walk = old_h;
2252           w_walk = old_h->next;
2253         } else {            // no prior search, start at beginning
2254           pw_walk =
2255               nullptr;  // h->next's predecessor may change; don't record it
2256           w_walk = h->next;
2257         }
2258 
2259         h->may_skip = false;  // ensure we never skip past h in future searches
2260                               // even if other waiters are queued after it.
2261         ABSL_RAW_CHECK(h->skip == nullptr, "illegal skip from head");
2262 
2263         h->maybe_unlocking = true;  // we're about to scan the waiter list
2264                                     // without the spinlock held.
2265                                     // Enqueue must be conservative about
2266                                     // priority queuing.
2267 
2268         // We must release the spinlock to evaluate the conditions.
2269         mu_.store(v, std::memory_order_release);  // release just spinlock
2270         // can release with a store because there were waiters
2271 
2272         // h is the last waiter queued, and w_walk the first unsearched waiter.
2273         // Without the spinlock, the locations mu_ and h->next may now change
2274         // underneath us, but since we hold the lock itself, the only legal
2275         // change is to add waiters between h and w_walk.  Therefore, it's safe
2276         // to walk the path from w_walk to h inclusive. (TryRemove() can remove
2277         // a waiter anywhere, but it acquires both the spinlock and the Mutex)
2278 
2279         old_h = h;        // remember we searched to here
2280 
2281         // Walk the path upto and including h looking for waiters we can wake.
2282         while (pw_walk != h) {
2283           w_walk->wake = false;
2284           if (w_walk->waitp->cond ==
2285                   nullptr ||  // no condition => vacuously true OR
2286               (w_walk->waitp->cond != known_false &&
2287                // this thread's condition is not known false, AND
2288                //  is in fact true
2289                EvalConditionIgnored(this, w_walk->waitp->cond))) {
2290             if (w == nullptr) {
2291               w_walk->wake = true;    // can wake this waiter
2292               w = w_walk;
2293               pw = pw_walk;
2294               if (w_walk->waitp->how == kExclusive) {
2295                 wr_wait = kMuWrWait;
2296                 break;                // bail if waking this writer
2297               }
2298             } else if (w_walk->waitp->how == kShared) {  // wake if a reader
2299               w_walk->wake = true;
2300             } else {   // writer with true condition
2301               wr_wait = kMuWrWait;
2302             }
2303           } else {                  // can't wake; condition false
2304             known_false = w_walk->waitp->cond;  // remember last false condition
2305           }
2306           if (w_walk->wake) {   // we're waking reader w_walk
2307             pw_walk = w_walk;   // don't skip similar waiters
2308           } else {              // not waking; skip as much as possible
2309             pw_walk = Skip(w_walk);
2310           }
2311           // If pw_walk == h, then load of pw_walk->next can race with
2312           // concurrent write in Enqueue(). However, at the same time
2313           // we do not need to do the load, because we will bail out
2314           // from the loop anyway.
2315           if (pw_walk != h) {
2316             w_walk = pw_walk->next;
2317           }
2318         }
2319 
2320         continue;  // restart for(;;)-loop to wakeup w or to find more waiters
2321       }
2322       ABSL_RAW_CHECK(pw->next == w, "pw not w's predecessor");
2323       // The first (and perhaps only) waiter we've chosen to wake is w, whose
2324       // predecessor is pw.  If w is a reader, we must wake all the other
2325       // waiters with wake==true as well.  We may also need to queue
2326       // ourselves if waitp != null.  The spinlock and the lock are still
2327       // held.
2328 
2329       // This traverses the list in [ pw->next, h ], where h is the head,
2330       // removing all elements with wake==true and placing them in the
2331       // singly-linked list wake_list.  Returns the new head.
2332       h = DequeueAllWakeable(h, pw, &wake_list);
2333 
2334       intptr_t nv = (v & kMuEvent) | kMuDesig;
2335                                              // assume no waiters left,
2336                                              // set kMuDesig for INV1a
2337 
2338       if (waitp != nullptr) {  // we must queue ourselves and sleep
2339         h = Enqueue(h, waitp, v, kMuIsCond);
2340         // h is new last waiter; could be null if we queued ourselves on a
2341         // CondVar
2342       }
2343 
2344       ABSL_RAW_CHECK(wake_list != kPerThreadSynchNull,
2345                      "unexpected empty wake list");
2346 
2347       if (h != nullptr) {  // there are waiters left
2348         h->readers = 0;
2349         h->maybe_unlocking = false;     // finished unlocking
2350         nv |= wr_wait | kMuWait | reinterpret_cast<intptr_t>(h);
2351       }
2352 
2353       // release both spinlock & lock
2354       // can release with a store because there were waiters
2355       mu_.store(nv, std::memory_order_release);
2356       break;  // out of for(;;)-loop
2357     }
2358     // aggressive here; no one can proceed till we do
2359     c = synchronization_internal::MutexDelay(c, AGGRESSIVE);
2360   }                            // end of for(;;)-loop
2361 
2362   if (wake_list != kPerThreadSynchNull) {
2363     int64_t total_wait_cycles = 0;
2364     int64_t max_wait_cycles = 0;
2365     int64_t now = base_internal::CycleClock::Now();
2366     do {
2367       // Profile lock contention events only if the waiter was trying to acquire
2368       // the lock, not waiting on a condition variable or Condition.
2369       if (!wake_list->cond_waiter) {
2370         int64_t cycles_waited =
2371             (now - wake_list->waitp->contention_start_cycles);
2372         total_wait_cycles += cycles_waited;
2373         if (max_wait_cycles == 0) max_wait_cycles = cycles_waited;
2374         wake_list->waitp->contention_start_cycles = now;
2375         wake_list->waitp->should_submit_contention_data = true;
2376       }
2377       wake_list = Wakeup(wake_list);              // wake waiters
2378     } while (wake_list != kPerThreadSynchNull);
2379     if (total_wait_cycles > 0) {
2380       mutex_tracer("slow release", this, total_wait_cycles);
2381       ABSL_TSAN_MUTEX_PRE_DIVERT(this, 0);
2382       submit_profile_data(total_wait_cycles);
2383       ABSL_TSAN_MUTEX_POST_DIVERT(this, 0);
2384     }
2385   }
2386 }
2387 
2388 // Used by CondVar implementation to reacquire mutex after waking from
2389 // condition variable.  This routine is used instead of Lock() because the
2390 // waiting thread may have been moved from the condition variable queue to the
2391 // mutex queue without a wakeup, by Trans().  In that case, when the thread is
2392 // finally woken, the woken thread will believe it has been woken from the
2393 // condition variable (i.e. its PC will be in when in the CondVar code), when
2394 // in fact it has just been woken from the mutex.  Thus, it must enter the slow
2395 // path of the mutex in the same state as if it had just woken from the mutex.
2396 // That is, it must ensure to clear kMuDesig (INV1b).
Trans(MuHow how)2397 void Mutex::Trans(MuHow how) {
2398   this->LockSlow(how, nullptr, kMuHasBlocked | kMuIsCond);
2399 }
2400 
2401 // Used by CondVar implementation to effectively wake thread w from the
2402 // condition variable.  If this mutex is free, we simply wake the thread.
2403 // It will later acquire the mutex with high probability.  Otherwise, we
2404 // enqueue thread w on this mutex.
Fer(PerThreadSynch * w)2405 void Mutex::Fer(PerThreadSynch *w) {
2406   SchedulingGuard::ScopedDisable disable_rescheduling;
2407   int c = 0;
2408   ABSL_RAW_CHECK(w->waitp->cond == nullptr,
2409                  "Mutex::Fer while waiting on Condition");
2410   ABSL_RAW_CHECK(!w->waitp->timeout.has_timeout(),
2411                  "Mutex::Fer while in timed wait");
2412   ABSL_RAW_CHECK(w->waitp->cv_word == nullptr,
2413                  "Mutex::Fer with pending CondVar queueing");
2414   for (;;) {
2415     intptr_t v = mu_.load(std::memory_order_relaxed);
2416     // Note: must not queue if the mutex is unlocked (nobody will wake it).
2417     // For example, we can have only kMuWait (conditional) or maybe
2418     // kMuWait|kMuWrWait.
2419     // conflicting != 0 implies that the waking thread cannot currently take
2420     // the mutex, which in turn implies that someone else has it and can wake
2421     // us if we queue.
2422     const intptr_t conflicting =
2423         kMuWriter | (w->waitp->how == kShared ? 0 : kMuReader);
2424     if ((v & conflicting) == 0) {
2425       w->next = nullptr;
2426       w->state.store(PerThreadSynch::kAvailable, std::memory_order_release);
2427       IncrementSynchSem(this, w);
2428       return;
2429     } else {
2430       if ((v & (kMuSpin|kMuWait)) == 0) {       // no waiters
2431         // This thread tries to become the one and only waiter.
2432         PerThreadSynch *new_h = Enqueue(nullptr, w->waitp, v, kMuIsCond);
2433         ABSL_RAW_CHECK(new_h != nullptr,
2434                        "Enqueue failed");  // we must queue ourselves
2435         if (mu_.compare_exchange_strong(
2436                 v, reinterpret_cast<intptr_t>(new_h) | (v & kMuLow) | kMuWait,
2437                 std::memory_order_release, std::memory_order_relaxed)) {
2438           return;
2439         }
2440       } else if ((v & kMuSpin) == 0 &&
2441                  mu_.compare_exchange_strong(v, v | kMuSpin | kMuWait)) {
2442         PerThreadSynch *h = GetPerThreadSynch(v);
2443         PerThreadSynch *new_h = Enqueue(h, w->waitp, v, kMuIsCond);
2444         ABSL_RAW_CHECK(new_h != nullptr,
2445                        "Enqueue failed");  // we must queue ourselves
2446         do {
2447           v = mu_.load(std::memory_order_relaxed);
2448         } while (!mu_.compare_exchange_weak(
2449             v,
2450             (v & kMuLow & ~kMuSpin) | kMuWait |
2451                 reinterpret_cast<intptr_t>(new_h),
2452             std::memory_order_release, std::memory_order_relaxed));
2453         return;
2454       }
2455     }
2456     c = synchronization_internal::MutexDelay(c, GENTLE);
2457   }
2458 }
2459 
AssertHeld() const2460 void Mutex::AssertHeld() const {
2461   if ((mu_.load(std::memory_order_relaxed) & kMuWriter) == 0) {
2462     SynchEvent *e = GetSynchEvent(this);
2463     ABSL_RAW_LOG(FATAL, "thread should hold write lock on Mutex %p %s",
2464                  static_cast<const void *>(this),
2465                  (e == nullptr ? "" : e->name));
2466   }
2467 }
2468 
AssertReaderHeld() const2469 void Mutex::AssertReaderHeld() const {
2470   if ((mu_.load(std::memory_order_relaxed) & (kMuReader | kMuWriter)) == 0) {
2471     SynchEvent *e = GetSynchEvent(this);
2472     ABSL_RAW_LOG(
2473         FATAL, "thread should hold at least a read lock on Mutex %p %s",
2474         static_cast<const void *>(this), (e == nullptr ? "" : e->name));
2475   }
2476 }
2477 
2478 // -------------------------------- condition variables
2479 static const intptr_t kCvSpin = 0x0001L;   // spinlock protects waiter list
2480 static const intptr_t kCvEvent = 0x0002L;  // record events
2481 
2482 static const intptr_t kCvLow = 0x0003L;  // low order bits of CV
2483 
2484 // Hack to make constant values available to gdb pretty printer
2485 enum { kGdbCvSpin = kCvSpin, kGdbCvEvent = kCvEvent, kGdbCvLow = kCvLow, };
2486 
2487 static_assert(PerThreadSynch::kAlignment > kCvLow,
2488               "PerThreadSynch::kAlignment must be greater than kCvLow");
2489 
EnableDebugLog(const char * name)2490 void CondVar::EnableDebugLog(const char *name) {
2491   SynchEvent *e = EnsureSynchEvent(&this->cv_, name, kCvEvent, kCvSpin);
2492   e->log = true;
2493   UnrefSynchEvent(e);
2494 }
2495 
~CondVar()2496 CondVar::~CondVar() {
2497   if ((cv_.load(std::memory_order_relaxed) & kCvEvent) != 0) {
2498     ForgetSynchEvent(&this->cv_, kCvEvent, kCvSpin);
2499   }
2500 }
2501 
2502 
2503 // Remove thread s from the list of waiters on this condition variable.
Remove(PerThreadSynch * s)2504 void CondVar::Remove(PerThreadSynch *s) {
2505   SchedulingGuard::ScopedDisable disable_rescheduling;
2506   intptr_t v;
2507   int c = 0;
2508   for (v = cv_.load(std::memory_order_relaxed);;
2509        v = cv_.load(std::memory_order_relaxed)) {
2510     if ((v & kCvSpin) == 0 &&  // attempt to acquire spinlock
2511         cv_.compare_exchange_strong(v, v | kCvSpin,
2512                                     std::memory_order_acquire,
2513                                     std::memory_order_relaxed)) {
2514       PerThreadSynch *h = reinterpret_cast<PerThreadSynch *>(v & ~kCvLow);
2515       if (h != nullptr) {
2516         PerThreadSynch *w = h;
2517         while (w->next != s && w->next != h) {  // search for thread
2518           w = w->next;
2519         }
2520         if (w->next == s) {           // found thread; remove it
2521           w->next = s->next;
2522           if (h == s) {
2523             h = (w == s) ? nullptr : w;
2524           }
2525           s->next = nullptr;
2526           s->state.store(PerThreadSynch::kAvailable, std::memory_order_release);
2527         }
2528       }
2529                                       // release spinlock
2530       cv_.store((v & kCvEvent) | reinterpret_cast<intptr_t>(h),
2531                 std::memory_order_release);
2532       return;
2533     } else {
2534       // try again after a delay
2535       c = synchronization_internal::MutexDelay(c, GENTLE);
2536     }
2537   }
2538 }
2539 
2540 // Queue thread waitp->thread on condition variable word cv_word using
2541 // wait parameters waitp.
2542 // We split this into a separate routine, rather than simply doing it as part
2543 // of WaitCommon().  If we were to queue ourselves on the condition variable
2544 // before calling Mutex::UnlockSlow(), the Mutex code might be re-entered (via
2545 // the logging code, or via a Condition function) and might potentially attempt
2546 // to block this thread.  That would be a problem if the thread were already on
2547 // a condition variable waiter queue.  Thus, we use the waitp->cv_word to tell
2548 // the unlock code to call CondVarEnqueue() to queue the thread on the condition
2549 // variable queue just before the mutex is to be unlocked, and (most
2550 // importantly) after any call to an external routine that might re-enter the
2551 // mutex code.
CondVarEnqueue(SynchWaitParams * waitp)2552 static void CondVarEnqueue(SynchWaitParams *waitp) {
2553   // This thread might be transferred to the Mutex queue by Fer() when
2554   // we are woken.  To make sure that is what happens, Enqueue() doesn't
2555   // call CondVarEnqueue() again but instead uses its normal code.  We
2556   // must do this before we queue ourselves so that cv_word will be null
2557   // when seen by the dequeuer, who may wish immediately to requeue
2558   // this thread on another queue.
2559   std::atomic<intptr_t> *cv_word = waitp->cv_word;
2560   waitp->cv_word = nullptr;
2561 
2562   intptr_t v = cv_word->load(std::memory_order_relaxed);
2563   int c = 0;
2564   while ((v & kCvSpin) != 0 ||  // acquire spinlock
2565          !cv_word->compare_exchange_weak(v, v | kCvSpin,
2566                                          std::memory_order_acquire,
2567                                          std::memory_order_relaxed)) {
2568     c = synchronization_internal::MutexDelay(c, GENTLE);
2569     v = cv_word->load(std::memory_order_relaxed);
2570   }
2571   ABSL_RAW_CHECK(waitp->thread->waitp == nullptr, "waiting when shouldn't be");
2572   waitp->thread->waitp = waitp;      // prepare ourselves for waiting
2573   PerThreadSynch *h = reinterpret_cast<PerThreadSynch *>(v & ~kCvLow);
2574   if (h == nullptr) {  // add this thread to waiter list
2575     waitp->thread->next = waitp->thread;
2576   } else {
2577     waitp->thread->next = h->next;
2578     h->next = waitp->thread;
2579   }
2580   waitp->thread->state.store(PerThreadSynch::kQueued,
2581                              std::memory_order_relaxed);
2582   cv_word->store((v & kCvEvent) | reinterpret_cast<intptr_t>(waitp->thread),
2583                  std::memory_order_release);
2584 }
2585 
WaitCommon(Mutex * mutex,KernelTimeout t)2586 bool CondVar::WaitCommon(Mutex *mutex, KernelTimeout t) {
2587   bool rc = false;          // return value; true iff we timed-out
2588 
2589   intptr_t mutex_v = mutex->mu_.load(std::memory_order_relaxed);
2590   Mutex::MuHow mutex_how = ((mutex_v & kMuWriter) != 0) ? kExclusive : kShared;
2591   ABSL_TSAN_MUTEX_PRE_UNLOCK(mutex, TsanFlags(mutex_how));
2592 
2593   // maybe trace this call
2594   intptr_t v = cv_.load(std::memory_order_relaxed);
2595   cond_var_tracer("Wait", this);
2596   if ((v & kCvEvent) != 0) {
2597     PostSynchEvent(this, SYNCH_EV_WAIT);
2598   }
2599 
2600   // Release mu and wait on condition variable.
2601   SynchWaitParams waitp(mutex_how, nullptr, t, mutex,
2602                         Synch_GetPerThreadAnnotated(mutex), &cv_);
2603   // UnlockSlow() will call CondVarEnqueue() just before releasing the
2604   // Mutex, thus queuing this thread on the condition variable.  See
2605   // CondVarEnqueue() for the reasons.
2606   mutex->UnlockSlow(&waitp);
2607 
2608   // wait for signal
2609   while (waitp.thread->state.load(std::memory_order_acquire) ==
2610          PerThreadSynch::kQueued) {
2611     if (!Mutex::DecrementSynchSem(mutex, waitp.thread, t)) {
2612       // DecrementSynchSem returned due to timeout.
2613       // Now we will either (1) remove ourselves from the wait list in Remove
2614       // below, in which case Remove will set thread.state = kAvailable and
2615       // we will not call DecrementSynchSem again; or (2) Signal/SignalAll
2616       // has removed us concurrently and is calling Wakeup, which will set
2617       // thread.state = kAvailable and post to the semaphore.
2618       // It's important to reset the timeout for the case (2) because otherwise
2619       // we can live-lock in this loop since DecrementSynchSem will always
2620       // return immediately due to timeout, but Signal/SignalAll is not
2621       // necessary set thread.state = kAvailable yet (and is not scheduled
2622       // due to thread priorities or other scheduler artifacts).
2623       // Note this could also be resolved if Signal/SignalAll would set
2624       // thread.state = kAvailable while holding the wait list spin lock.
2625       // But this can't be easily done for SignalAll since it grabs the whole
2626       // wait list with a single compare-exchange and does not really grab
2627       // the spin lock.
2628       t = KernelTimeout::Never();
2629       this->Remove(waitp.thread);
2630       rc = true;
2631     }
2632   }
2633 
2634   ABSL_RAW_CHECK(waitp.thread->waitp != nullptr, "not waiting when should be");
2635   waitp.thread->waitp = nullptr;  // cleanup
2636 
2637   // maybe trace this call
2638   cond_var_tracer("Unwait", this);
2639   if ((v & kCvEvent) != 0) {
2640     PostSynchEvent(this, SYNCH_EV_WAIT_RETURNING);
2641   }
2642 
2643   // From synchronization point of view Wait is unlock of the mutex followed
2644   // by lock of the mutex. We've annotated start of unlock in the beginning
2645   // of the function. Now, finish unlock and annotate lock of the mutex.
2646   // (Trans is effectively lock).
2647   ABSL_TSAN_MUTEX_POST_UNLOCK(mutex, TsanFlags(mutex_how));
2648   ABSL_TSAN_MUTEX_PRE_LOCK(mutex, TsanFlags(mutex_how));
2649   mutex->Trans(mutex_how);  // Reacquire mutex
2650   ABSL_TSAN_MUTEX_POST_LOCK(mutex, TsanFlags(mutex_how), 0);
2651   return rc;
2652 }
2653 
WaitWithTimeout(Mutex * mu,absl::Duration timeout)2654 bool CondVar::WaitWithTimeout(Mutex *mu, absl::Duration timeout) {
2655   return WaitWithDeadline(mu, DeadlineFromTimeout(timeout));
2656 }
2657 
WaitWithDeadline(Mutex * mu,absl::Time deadline)2658 bool CondVar::WaitWithDeadline(Mutex *mu, absl::Time deadline) {
2659   return WaitCommon(mu, KernelTimeout(deadline));
2660 }
2661 
Wait(Mutex * mu)2662 void CondVar::Wait(Mutex *mu) {
2663   WaitCommon(mu, KernelTimeout::Never());
2664 }
2665 
2666 // Wake thread w
2667 // If it was a timed wait, w will be waiting on w->cv
2668 // Otherwise, if it was not a Mutex mutex, w will be waiting on w->sem
2669 // Otherwise, w is transferred to the Mutex mutex via Mutex::Fer().
Wakeup(PerThreadSynch * w)2670 void CondVar::Wakeup(PerThreadSynch *w) {
2671   if (w->waitp->timeout.has_timeout() || w->waitp->cvmu == nullptr) {
2672     // The waiting thread only needs to observe "w->state == kAvailable" to be
2673     // released, we must cache "cvmu" before clearing "next".
2674     Mutex *mu = w->waitp->cvmu;
2675     w->next = nullptr;
2676     w->state.store(PerThreadSynch::kAvailable, std::memory_order_release);
2677     Mutex::IncrementSynchSem(mu, w);
2678   } else {
2679     w->waitp->cvmu->Fer(w);
2680   }
2681 }
2682 
Signal()2683 void CondVar::Signal() {
2684   SchedulingGuard::ScopedDisable disable_rescheduling;
2685   ABSL_TSAN_MUTEX_PRE_SIGNAL(nullptr, 0);
2686   intptr_t v;
2687   int c = 0;
2688   for (v = cv_.load(std::memory_order_relaxed); v != 0;
2689        v = cv_.load(std::memory_order_relaxed)) {
2690     if ((v & kCvSpin) == 0 &&  // attempt to acquire spinlock
2691         cv_.compare_exchange_strong(v, v | kCvSpin,
2692                                     std::memory_order_acquire,
2693                                     std::memory_order_relaxed)) {
2694       PerThreadSynch *h = reinterpret_cast<PerThreadSynch *>(v & ~kCvLow);
2695       PerThreadSynch *w = nullptr;
2696       if (h != nullptr) {  // remove first waiter
2697         w = h->next;
2698         if (w == h) {
2699           h = nullptr;
2700         } else {
2701           h->next = w->next;
2702         }
2703       }
2704                                       // release spinlock
2705       cv_.store((v & kCvEvent) | reinterpret_cast<intptr_t>(h),
2706                 std::memory_order_release);
2707       if (w != nullptr) {
2708         CondVar::Wakeup(w);                // wake waiter, if there was one
2709         cond_var_tracer("Signal wakeup", this);
2710       }
2711       if ((v & kCvEvent) != 0) {
2712         PostSynchEvent(this, SYNCH_EV_SIGNAL);
2713       }
2714       ABSL_TSAN_MUTEX_POST_SIGNAL(nullptr, 0);
2715       return;
2716     } else {
2717       c = synchronization_internal::MutexDelay(c, GENTLE);
2718     }
2719   }
2720   ABSL_TSAN_MUTEX_POST_SIGNAL(nullptr, 0);
2721 }
2722 
SignalAll()2723 void CondVar::SignalAll () {
2724   ABSL_TSAN_MUTEX_PRE_SIGNAL(nullptr, 0);
2725   intptr_t v;
2726   int c = 0;
2727   for (v = cv_.load(std::memory_order_relaxed); v != 0;
2728        v = cv_.load(std::memory_order_relaxed)) {
2729     // empty the list if spinlock free
2730     // We do this by simply setting the list to empty using
2731     // compare and swap.   We then have the entire list in our hands,
2732     // which cannot be changing since we grabbed it while no one
2733     // held the lock.
2734     if ((v & kCvSpin) == 0 &&
2735         cv_.compare_exchange_strong(v, v & kCvEvent, std::memory_order_acquire,
2736                                     std::memory_order_relaxed)) {
2737       PerThreadSynch *h = reinterpret_cast<PerThreadSynch *>(v & ~kCvLow);
2738       if (h != nullptr) {
2739         PerThreadSynch *w;
2740         PerThreadSynch *n = h->next;
2741         do {                          // for every thread, wake it up
2742           w = n;
2743           n = n->next;
2744           CondVar::Wakeup(w);
2745         } while (w != h);
2746         cond_var_tracer("SignalAll wakeup", this);
2747       }
2748       if ((v & kCvEvent) != 0) {
2749         PostSynchEvent(this, SYNCH_EV_SIGNALALL);
2750       }
2751       ABSL_TSAN_MUTEX_POST_SIGNAL(nullptr, 0);
2752       return;
2753     } else {
2754       // try again after a delay
2755       c = synchronization_internal::MutexDelay(c, GENTLE);
2756     }
2757   }
2758   ABSL_TSAN_MUTEX_POST_SIGNAL(nullptr, 0);
2759 }
2760 
Release()2761 void ReleasableMutexLock::Release() {
2762   ABSL_RAW_CHECK(this->mu_ != nullptr,
2763                  "ReleasableMutexLock::Release may only be called once");
2764   this->mu_->Unlock();
2765   this->mu_ = nullptr;
2766 }
2767 
2768 #ifdef ABSL_HAVE_THREAD_SANITIZER
2769 extern "C" void __tsan_read1(void *addr);
2770 #else
2771 #define __tsan_read1(addr)  // do nothing if TSan not enabled
2772 #endif
2773 
2774 // A function that just returns its argument, dereferenced
Dereference(void * arg)2775 static bool Dereference(void *arg) {
2776   // ThreadSanitizer does not instrument this file for memory accesses.
2777   // This function dereferences a user variable that can participate
2778   // in a data race, so we need to manually tell TSan about this memory access.
2779   __tsan_read1(arg);
2780   return *(static_cast<bool *>(arg));
2781 }
2782 
Condition()2783 Condition::Condition() {}   // null constructor, used for kTrue only
2784 const Condition Condition::kTrue;
2785 
Condition(bool (* func)(void *),void * arg)2786 Condition::Condition(bool (*func)(void *), void *arg)
2787     : eval_(&CallVoidPtrFunction),
2788       function_(func),
2789       method_(nullptr),
2790       arg_(arg) {}
2791 
CallVoidPtrFunction(const Condition * c)2792 bool Condition::CallVoidPtrFunction(const Condition *c) {
2793   return (*c->function_)(c->arg_);
2794 }
2795 
Condition(const bool * cond)2796 Condition::Condition(const bool *cond)
2797     : eval_(CallVoidPtrFunction),
2798       function_(Dereference),
2799       method_(nullptr),
2800       // const_cast is safe since Dereference does not modify arg
2801       arg_(const_cast<bool *>(cond)) {}
2802 
Eval() const2803 bool Condition::Eval() const {
2804   // eval_ == null for kTrue
2805   return (this->eval_ == nullptr) || (*this->eval_)(this);
2806 }
2807 
GuaranteedEqual(const Condition * a,const Condition * b)2808 bool Condition::GuaranteedEqual(const Condition *a, const Condition *b) {
2809   if (a == nullptr) {
2810     return b == nullptr || b->eval_ == nullptr;
2811   }
2812   if (b == nullptr || b->eval_ == nullptr) {
2813     return a->eval_ == nullptr;
2814   }
2815   return a->eval_ == b->eval_ && a->function_ == b->function_ &&
2816          a->arg_ == b->arg_ && a->method_ == b->method_;
2817 }
2818 
2819 ABSL_NAMESPACE_END
2820 }  // namespace absl
2821