Plasma Engine  2.0
Loading...
Searching...
No Matches
AtomicInteger.h
1#pragma once
2
3#include <Foundation/Types/TypeTraits.h>
4
5#include <Foundation/Threading/AtomicUtils.h>
6
7template <int T>
9{
10};
11
12template <>
14{
15 using Type = plInt32;
16};
17
18template <>
20{
21 using Type = plInt32;
22};
23
24template <>
26{
27 using Type = plInt32;
28};
29
30template <>
32{
33 using Type = plInt64;
34};
35
37template <typename T>
39{
40 using UnderlyingType = typename plAtomicStorageType<sizeof(T)>::Type;
41
42public:
43 PL_DECLARE_POD_TYPE();
44
46 plAtomicInteger(); // [tested]
47
49 plAtomicInteger(const T value); // [tested]
50
52 plAtomicInteger(const plAtomicInteger<T>& value); // [tested]
53
55 plAtomicInteger& operator=(T value); // [tested]
56
58 plAtomicInteger& operator=(const plAtomicInteger& value); // [tested]
59
61 T Increment(); // [tested]
62
64 T Decrement(); // [tested]
65
67 T PostIncrement(); // [tested]
68
70 T PostDecrement(); // [tested]
71
72 void Add(T x); // [tested]
73 void Subtract(T x); // [tested]
74
75 void And(T x); // [tested]
76 void Or(T x); // [tested]
77 void Xor(T x); // [tested]
78
79 void Min(T x); // [tested]
80 void Max(T x); // [tested]
81
83 T Set(T x); // [tested]
84
86 bool TestAndSet(T expected, T x); // [tested]
87
90 T CompareAndSwap(T expected, T x); // [tested]
91
92 operator T() const; // [tested]
93
94private:
95 UnderlyingType m_Value;
96};
97
100{
101public:
103 plAtomicBool(); // [tested]
105
107 plAtomicBool(bool value); // [tested]
108
110 plAtomicBool(const plAtomicBool& rhs);
111
113 bool Set(bool value); // [tested]
114
116 void operator=(bool value); // [tested]
117
119 void operator=(const plAtomicBool& rhs);
120
122 operator bool() const; // [tested]
123
126 bool TestAndSet(bool bExpected, bool bNewValue);
127
128private:
129 plAtomicInteger<plInt32> m_iAtomicInt;
130};
131
132// Include inline file
133#include <Foundation/Threading/Implementation/AtomicInteger_inl.h>
134
137static_assert(sizeof(plAtomicInteger32) == sizeof(plInt32));
138static_assert(sizeof(plAtomicInteger64) == sizeof(plInt64));
An atomic boolean variable. This is just a wrapper around an atomic int32 for convenience.
Definition AtomicInteger.h:100
bool Set(bool value)
Sets the bool to the given value and returns its previous value.
Definition AtomicInteger_inl.h:139
plAtomicBool()
Initializes the bool to 'false'.
void operator=(bool value)
Sets the bool to the given value.
Definition AtomicInteger_inl.h:144
bool TestAndSet(bool bExpected, bool bNewValue)
Sets the internal value to newValue if the internal value is equal to expected and returns true,...
Definition AtomicInteger_inl.h:159
Integer class that can be manipulated in an atomic (i.e. thread-safe) fashion.
Definition AtomicInteger.h:39
bool TestAndSet(T expected, T x)
Sets the internal value to x if the internal value is equal to expected and returns true,...
Definition AtomicInteger_inl.h:107
T Increment()
Increments the internal value and returns the incremented value.
Definition AtomicInteger_inl.h:35
plAtomicInteger & operator=(T value)
Assigns a new integer value to this object.
Definition AtomicInteger_inl.h:21
T PostDecrement()
Decrements the internal value and returns the value immediately before the decrement.
Definition AtomicInteger_inl.h:53
T Decrement()
Decrements the internal value and returns the decremented value.
Definition AtomicInteger_inl.h:41
T Set(T x)
Sets the internal value to x and returns the original internal value.
Definition AtomicInteger_inl.h:101
T PostIncrement()
Increments the internal value and returns the value immediately before the increment.
Definition AtomicInteger_inl.h:47
T CompareAndSwap(T expected, T x)
If this is equal to expected, it is set to value. Otherwise it won't be modified. Always returns the ...
Definition AtomicInteger_inl.h:113
plAtomicInteger()
Initializes the value to zero.
Definition AtomicInteger_inl.h:3
Definition AtomicInteger.h:9