[base] Add DCHECK-only function to get locks held by calling thread.
This will be used by SequenceCheckerImpl to support guaranteeing
mutual exclusion with a lock (see crrev.com/c/5546923).
Bug: 40944462
Change-Id: I007f3730d304f86538d6dc77142791742c063043
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5544753
Reviewed-by: Daniel Cheng <[email protected]>
Commit-Queue: Francois Pierre Doray <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1308910}
diff --git a/base/synchronization/lock_subtle.h b/base/synchronization/lock_subtle.h
new file mode 100644
index 0000000..b2968d4
--- /dev/null
+++ b/base/synchronization/lock_subtle.h
@@ -0,0 +1,45 @@
+// Copyright 2024 The Chromium Authors
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef BASE_SYNCHRONIZATION_LOCK_SUBTLE_H_
+#define BASE_SYNCHRONIZATION_LOCK_SUBTLE_H_
+
+#include "base/auto_reset.h"
+#include "base/base_export.h"
+#include "base/containers/span.h"
+#include "base/dcheck_is_on.h"
+
+namespace base::subtle {
+
+#if DCHECK_IS_ON()
+// Returns addresses of locks held by the current thread. `uintptr_t` is used
+// because addresses are meant to be used as unique identifiers but not to be
+// dereferenced.
+BASE_EXPORT span<const uintptr_t> GetLocksHeldByCurrentThread();
+#endif
+
+// Creates a scope in which acquired locks aren't tracked by
+// `GetLocksHeldByCurrentThread()`. This is required in rare circumstances where
+// the number of locks held simultaneously by a thread may exceed the
+// fixed-sized thread-local storage. A lock which isn't tracked by
+// `GetLocksHeldByCurrentThread()` cannot be used to satisfy a
+// `SequenceChecker`.
+class BASE_EXPORT [[maybe_unused]] DoNotTrackLocks {
+#if DCHECK_IS_ON()
+ public:
+ DoNotTrackLocks();
+ ~DoNotTrackLocks();
+
+ private:
+ AutoReset<bool> auto_reset_;
+#else
+ public:
+ DoNotTrackLocks() = default;
+ ~DoNotTrackLocks() = default;
+#endif // DCHECK_IS_ON()
+};
+
+} // namespace base::subtle
+
+#endif // BASE_SYNCHRONIZATION_LOCK_SUBTLE_H_