// Copyright 2011 The Chromium Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. // This file is used for debugging assertion support. The Lock class // is functionally a wrapper around the LockImpl class, so the only // real intelligence in the class is in the debugging logic. #ifdef UNSAFE_BUFFERS_BUILD // TODO(crbug.com/40284755): Remove this and spanify to fix the errors. #pragma allow_unsafe_buffers #endif #include "base/synchronization/lock.h" #include <cstdint> #if DCHECK_IS_ON() #include <array> #include "base/synchronization/lock_subtle.h" #include "base/threading/platform_thread.h" namespace base { namespace { // List of locks held by a thread. // // As of May 2024, no more than 5 locks were held simultaneously by a thread in // a test browsing session or while running the CQ (% locks acquired in unit // tests "WaitSetTest.NoStarvation" and // "MessagePipeTest.DataPipeConsumerHandlePingPong"). An array of size 10 is // therefore considered sufficient to track all locks held by a thread. A // dynamic-size array (e.g. owned by a `ThreadLocalOwnedPointer`) would require // handling reentrancy issues with allocator shims that use `base::Lock`. constexpr int kHeldLocksCapacity = …; thread_local std::array<uintptr_t, kHeldLocksCapacity> g_tracked_locks_held_by_thread; // Number of non-nullptr elements in `g_tracked_locks_held_by_thread`. thread_local size_t g_num_tracked_locks_held_by_thread = …; } // namespace Lock::~Lock() { … } void Lock::Acquire(subtle::LockTracking tracking) { … } void Lock::Release() { … } bool Lock::Try(subtle::LockTracking tracking) { … } void Lock::AssertAcquired() const { … } void Lock::AssertNotHeld() const { … } void Lock::CheckHeldAndUnmark() { … } void Lock::CheckUnheldAndMark() { … } void Lock::AddToLocksHeldOnCurrentThread() { … } void Lock::RemoveFromLocksHeldOnCurrentThread() { … } namespace subtle { span<const uintptr_t> GetTrackedLocksHeldByCurrentThread() { … } } // namespace subtle } // namespace base #endif // DCHECK_IS_ON()