François Doray | efe18b9 | 2024-05-31 21:44:59 | [diff] [blame] | 1 | // Copyright 2024 The Chromium Authors |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef BASE_SYNCHRONIZATION_LOCK_SUBTLE_H_ |
| 6 | #define BASE_SYNCHRONIZATION_LOCK_SUBTLE_H_ |
| 7 | |
François Doray | efe18b9 | 2024-05-31 21:44:59 | [diff] [blame] | 8 | #include "base/base_export.h" |
| 9 | #include "base/containers/span.h" |
| 10 | #include "base/dcheck_is_on.h" |
| 11 | |
| 12 | namespace base::subtle { |
| 13 | |
| 14 | #if DCHECK_IS_ON() |
François Doray | 6671c40 | 2024-06-21 17:47:39 | [diff] [blame] | 15 | // Returns addresses of locks acquired by the current thread with |
| 16 | // `subtle::LockTracking::kEnabled`. `uintptr_t` is used because addresses are |
| 17 | // meant to be used as unique identifiers but not to be dereferenced. |
| 18 | BASE_EXPORT span<const uintptr_t> GetTrackedLocksHeldByCurrentThread(); |
François Doray | efe18b9 | 2024-05-31 21:44:59 | [diff] [blame] | 19 | #endif |
| 20 | |
François Doray | 6671c40 | 2024-06-21 17:47:39 | [diff] [blame] | 21 | // Whether to add a lock to the list returned by |
| 22 | // `subtle::GetLocksHeldByCurrentThread()` upon acquisition. This has no effect |
| 23 | // in non-DCHECK builds because tracking is always disabled. This is disabled by |
| 24 | // default to avoid exceeding the fixed-size storage backing |
| 25 | // `GetTrackedLocksHeldByCurrentThread()` and to avoid reentrancy, e.g.: |
| 26 | // |
| 27 | // thread_local implementation |
| 28 | // Add lock to the thread_local array of locks held by current thread |
| 29 | // base::Lock::Acquire from allocator shim |
| 30 | // ... Allocator shim ... |
| 31 | // thread_local implementation |
| 32 | // Access to a thread_local variable |
| 33 | // |
| 34 | // A lock acquired with `subtle::LockTracking::kEnabled` can be used to provide |
| 35 | // a mutual exclusion guarantee for SEQUENCE_CHECKER. |
| 36 | enum class LockTracking { |
| 37 | kDisabled, |
| 38 | kEnabled, |
François Doray | efe18b9 | 2024-05-31 21:44:59 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | } // namespace base::subtle |
| 42 | |
| 43 | #endif // BASE_SYNCHRONIZATION_LOCK_SUBTLE_H_ |