![]() |
|
|||
File indexing completed on 2025-02-23 10:08:49
0001 // Copyright 2020 the V8 project authors. All rights reserved. 0002 // Use of this source code is governed by a BSD-style license that can be 0003 // found in the LICENSE file. 0004 0005 #ifndef INCLUDE_CPPGC_INTERNAL_ATOMIC_ENTRY_FLAG_H_ 0006 #define INCLUDE_CPPGC_INTERNAL_ATOMIC_ENTRY_FLAG_H_ 0007 0008 #include <atomic> 0009 0010 namespace cppgc { 0011 namespace internal { 0012 0013 // A flag which provides a fast check whether a scope may be entered on the 0014 // current thread, without needing to access thread-local storage or mutex. Can 0015 // have false positives (i.e., spuriously report that it might be entered), so 0016 // it is expected that this will be used in tandem with a precise check that the 0017 // scope is in fact entered on that thread. 0018 // 0019 // Example: 0020 // g_frobnicating_flag.MightBeEntered() && 0021 // ThreadLocalFrobnicator().IsFrobnicating() 0022 // 0023 // Relaxed atomic operations are sufficient, since: 0024 // - all accesses remain atomic 0025 // - each thread must observe its own operations in order 0026 // - no thread ever exits the flag more times than it enters (if used correctly) 0027 // And so if a thread observes zero, it must be because it has observed an equal 0028 // number of exits as entries. 0029 class AtomicEntryFlag final { 0030 public: 0031 void Enter() { entries_.fetch_add(1, std::memory_order_relaxed); } 0032 void Exit() { entries_.fetch_sub(1, std::memory_order_relaxed); } 0033 0034 // Returns false only if the current thread is not between a call to Enter 0035 // and a call to Exit. Returns true if this thread or another thread may 0036 // currently be in the scope guarded by this flag. 0037 bool MightBeEntered() const { 0038 return entries_.load(std::memory_order_relaxed) != 0; 0039 } 0040 0041 private: 0042 std::atomic_int entries_{0}; 0043 }; 0044 0045 } // namespace internal 0046 } // namespace cppgc 0047 0048 #endif // INCLUDE_CPPGC_INTERNAL_ATOMIC_ENTRY_FLAG_H_
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
![]() ![]() |