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