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