summaryrefslogtreecommitdiffstats
path: root/base/atomic_flag.h
blob: ee33bf20586fcb52e30803a48a6d9a728985f08a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef BASE_ATOMIC_FLAG_H_
#define BASE_ATOMIC_FLAG_H_

#include "base/atomicops.h"

namespace base {

// AtomicFlag allows threads to notify each other for a single occurrence
// of a single event. It maintains an abstract boolean "flag" that transitions
// to true at most once. It provides calls to query the boolean.
//
// Memory ordering: For any threads X and Y, if X calls Set(), then any
// action taken by X before it calls Set() is visible to thread Y after
// Y receives a true return value from IsSet().
class AtomicFlag {
 public:
  // Sets "flag_" to "initial_value".
  explicit AtomicFlag(bool initial_value = false)
      : flag_(initial_value ? 1 : 0) { }
  ~AtomicFlag() {}

  void Set();  // Set "flag_" to true. May be called only once.
  bool IsSet() const;  // Return "flag_".

 private:
  base::subtle::Atomic32 flag_;

  DISALLOW_COPY_AND_ASSIGN(AtomicFlag);
};

}  // namespace base

#endif  // BASE_ATOMIC_FLAG_H_