fdoray | ef19112 | 2016-07-25 14:43:17 | [diff] [blame] | 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" |
gab | d0cc00d | 2016-08-01 19:24:03 | [diff] [blame] | 11 | #include "base/sequence_checker.h" |
fdoray | ef19112 | 2016-07-25 14:43:17 | [diff] [blame] | 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 | |
gab | d0cc00d | 2016-08-01 19:24:03 | [diff] [blame] | 23 | // Set the flag. Must always be called from the same sequence. |
fdoray | ef19112 | 2016-07-25 14:43:17 | [diff] [blame] | 24 | void Set(); |
| 25 | |
gab | d0cc00d | 2016-08-01 19:24:03 | [diff] [blame] | 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. |
fdoray | ef19112 | 2016-07-25 14:43:17 | [diff] [blame] | 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; |
Nicolas Ouellet-payeur | ff1eab6 | 2018-07-19 18:41:50 | [diff] [blame] | 37 | SEQUENCE_CHECKER(set_sequence_checker_); |
fdoray | ef19112 | 2016-07-25 14:43:17 | [diff] [blame] | 38 | |
| 39 | DISALLOW_COPY_AND_ASSIGN(AtomicFlag); |
| 40 | }; |
| 41 | |
| 42 | } // namespace base |
| 43 | |
| 44 | #endif // BASE_SYNCHRONIZATION_ATOMIC_FLAG_H_ |