Coverage Report

Created: 2024-02-11 06:23

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