xref: /aosp_15_r20/external/libchrome/base/synchronization/atomic_flag.h (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker 
5*635a8641SAndroid Build Coastguard Worker #ifndef BASE_SYNCHRONIZATION_ATOMIC_FLAG_H_
6*635a8641SAndroid Build Coastguard Worker #define BASE_SYNCHRONIZATION_ATOMIC_FLAG_H_
7*635a8641SAndroid Build Coastguard Worker 
8*635a8641SAndroid Build Coastguard Worker #include "base/atomicops.h"
9*635a8641SAndroid Build Coastguard Worker #include "base/base_export.h"
10*635a8641SAndroid Build Coastguard Worker #include "base/macros.h"
11*635a8641SAndroid Build Coastguard Worker #include "base/sequence_checker.h"
12*635a8641SAndroid Build Coastguard Worker 
13*635a8641SAndroid Build Coastguard Worker namespace base {
14*635a8641SAndroid Build Coastguard Worker 
15*635a8641SAndroid Build Coastguard Worker // A flag that can safely be set from one thread and read from other threads.
16*635a8641SAndroid Build Coastguard Worker //
17*635a8641SAndroid Build Coastguard Worker // This class IS NOT intended for synchronization between threads.
18*635a8641SAndroid Build Coastguard Worker class BASE_EXPORT AtomicFlag {
19*635a8641SAndroid Build Coastguard Worker  public:
20*635a8641SAndroid Build Coastguard Worker   AtomicFlag();
21*635a8641SAndroid Build Coastguard Worker   ~AtomicFlag() = default;
22*635a8641SAndroid Build Coastguard Worker 
23*635a8641SAndroid Build Coastguard Worker   // Set the flag. Must always be called from the same sequence.
24*635a8641SAndroid Build Coastguard Worker   void Set();
25*635a8641SAndroid Build Coastguard Worker 
26*635a8641SAndroid Build Coastguard Worker   // Returns true iff the flag was set. If this returns true, the current thread
27*635a8641SAndroid Build Coastguard Worker   // is guaranteed to be synchronized with all memory operations on the sequence
28*635a8641SAndroid Build Coastguard Worker   // which invoked Set() up until at least the first call to Set() on it.
29*635a8641SAndroid Build Coastguard Worker   bool IsSet() const;
30*635a8641SAndroid Build Coastguard Worker 
31*635a8641SAndroid Build Coastguard Worker   // Resets the flag. Be careful when using this: callers might not expect
32*635a8641SAndroid Build Coastguard Worker   // IsSet() to return false after returning true once.
33*635a8641SAndroid Build Coastguard Worker   void UnsafeResetForTesting();
34*635a8641SAndroid Build Coastguard Worker 
35*635a8641SAndroid Build Coastguard Worker  private:
36*635a8641SAndroid Build Coastguard Worker   base::subtle::Atomic32 flag_ = 0;
37*635a8641SAndroid Build Coastguard Worker   SequenceChecker set_sequence_checker_;
38*635a8641SAndroid Build Coastguard Worker 
39*635a8641SAndroid Build Coastguard Worker   DISALLOW_COPY_AND_ASSIGN(AtomicFlag);
40*635a8641SAndroid Build Coastguard Worker };
41*635a8641SAndroid Build Coastguard Worker 
42*635a8641SAndroid Build Coastguard Worker }  // namespace base
43*635a8641SAndroid Build Coastguard Worker 
44*635a8641SAndroid Build Coastguard Worker #endif  // BASE_SYNCHRONIZATION_ATOMIC_FLAG_H_
45