blob: a98a35c250e1beeb1806df69cddd8aacb5575def [file] [log] [blame]
fdorayef191122016-07-25 14:43:171// 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"
gabd0cc00d2016-08-01 19:24:0311#include "base/sequence_checker.h"
fdorayef191122016-07-25 14:43:1712
13namespace 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.
18class BASE_EXPORT AtomicFlag {
19 public:
20 AtomicFlag();
21 ~AtomicFlag() = default;
22
gabd0cc00d2016-08-01 19:24:0323 // Set the flag. Must always be called from the same sequence.
fdorayef191122016-07-25 14:43:1724 void Set();
25
gabd0cc00d2016-08-01 19:24:0326 // 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.
fdorayef191122016-07-25 14:43:1729 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-payeurff1eab62018-07-19 18:41:5037 SEQUENCE_CHECKER(set_sequence_checker_);
fdorayef191122016-07-25 14:43:1738
39 DISALLOW_COPY_AND_ASSIGN(AtomicFlag);
40};
41
42} // namespace base
43
44#endif // BASE_SYNCHRONIZATION_ATOMIC_FLAG_H_