1//
2// AtomicFlag.h
3//
4// Library: Foundation
5// Package: Core
6// Module: AtomicFlag
7//
8// Definition of the AtomicFlag class.
9//
10// Copyright (c) 2009, Applied Informatics Software Engineering GmbH.
11// and Contributors.
12//
13// SPDX-License-Identifier: BSL-1.0
14//
15
16
17#ifndef Foundation_AtomicFlag_INCLUDED
18#define Foundation_AtomicFlag_INCLUDED
19
20
21#include "Poco/Foundation.h"
22#include <atomic>
23
24
25namespace Poco {
26
27
28class AtomicFlag
29 /// This class implements an atomic boolean flag by wrapping
30 /// the std::atomic_flag. It is guaranteed to be thread-safe
31 /// and lock-free.
32 ///
33 /// Only default-construction is allowed, objects of this
34 /// class are not copyable, assignable or movable.
35 ///
36 /// Note that this class is not a replacement for (atomic)
37 /// bool type because its value can not be read without also
38 /// being set to true. However, after being set (either
39 /// explicitly, through the set() call or implicitly, via
40 /// operator bool()), it can be reset and reused.
41 ///
42 /// The class is useful in scenarios such as busy-wait or
43 /// one-off code blocks, e.g.:
44 ///
45 /// class MyClass
46 /// {
47 /// public:
48 /// void myFunc()
49 /// {
50 /// if (!_beenHere) doOnce();
51 /// doMany();
52 /// }
53 ///
54 /// void doOnce() { /* executed once */ }
55 ///
56 /// void doMany() { /* executed multiple times */ }
57 ///
58 /// private:
59 /// AtomicFlag _beenHere;
60 /// }
61 ///
62 /// MyClass myClass;
63 /// int i = 0;
64 /// while (++i < 10) myClass.myFunc();
65{
66public:
67 AtomicFlag() {}
68
69 ~AtomicFlag() {}
70
71 bool set();
72 /// Sets the flag to true and returns previously
73 /// held value.
74
75 void reset();
76 /// Resets the flag to false.
77
78 operator bool ();
79 /// Sets the flag to true and returns previously
80 /// held value.
81
82private:
83 AtomicFlag(const AtomicFlag&) = delete;
84 AtomicFlag& operator = (const AtomicFlag&) = delete;
85 AtomicFlag(AtomicFlag&&) = delete;
86 AtomicFlag& operator = (AtomicFlag&&) = delete;
87
88 std::atomic_flag _flag = ATOMIC_FLAG_INIT;
89};
90
91
92//
93// inlines
94//
95
96inline bool AtomicFlag::set()
97{
98 return _flag.test_and_set(std::memory_order_acquire);
99}
100
101
102inline void AtomicFlag::reset()
103{
104 _flag.clear(std::memory_order_release);
105}
106
107
108inline AtomicFlag::operator bool ()
109{
110 return set();
111}
112
113
114} // namespace Poco
115
116
117#endif // Foundation_AtomicFlag_INCLUDED
118