DotNetDupe
4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Toggle main menu visibility
Loading...
Searching...
No Matches
Interlocked.h
Go to the documentation of this file.
1
5
6
#pragma once
7
#include "
Common.h
"
8
#include "
System/Object.h
"
9
10
namespace
DotNetDupe
{
11
namespace
System
{
12
namespace
Threading
{
13
14
// Forward declaration for friendship
15
template
<
typename
T>
class
Interlocked
;
16
19
class
InterlockedInternal
{
20
private
:
21
template
<
typename
U>
friend
class
Interlocked
;
22
23
DOTNETDUPE_API
static
long
Increment32(
volatile
long
* location);
24
DOTNETDUPE_API
static
long
Decrement32(
volatile
long
* location);
25
DOTNETDUPE_API
static
long
Add32(
volatile
long
* location,
long
value);
26
DOTNETDUPE_API
static
long
Exchange32(
volatile
long
* location,
long
value);
27
DOTNETDUPE_API
static
long
CompareExchange32(
volatile
long
* location,
long
value,
long
comparand);
28
29
DOTNETDUPE_API
static
long
long
Increment64(
volatile
long
long
* location);
30
DOTNETDUPE_API
static
long
long
Decrement64(
volatile
long
long
* location);
31
DOTNETDUPE_API
static
long
long
Add64(
volatile
long
long
* location,
long
long
value);
32
DOTNETDUPE_API
static
long
long
Exchange64(
volatile
long
long
* location,
long
long
value);
33
DOTNETDUPE_API
static
long
long
CompareExchange64(
volatile
long
long
* location,
long
long
value,
long
long
comparand);
34
};
35
42
template
<
typename
T>
43
class
Interlocked
:
public
Object
{
44
public
:
47
Interlocked
(T initialValue = 0) : _value(initialValue) {}
48
51
T
Increment
() {
52
if
constexpr
(
sizeof
(T) == 4)
return
(T)InterlockedInternal::Increment32((
volatile
long
*)&_value);
53
else
if
constexpr
(
sizeof
(T) == 8)
return
(T)InterlockedInternal::Increment64((
volatile
long
long
*)&_value);
54
else
static_assert
(
sizeof
(T) == 4 ||
sizeof
(T) == 8,
"Unsupported type size for Interlocked"
);
55
}
56
59
T
Decrement
() {
60
if
constexpr
(
sizeof
(T) == 4)
return
(T)InterlockedInternal::Decrement32((
volatile
long
*)&_value);
61
else
if
constexpr
(
sizeof
(T) == 8)
return
(T)InterlockedInternal::Decrement64((
volatile
long
long
*)&_value);
62
else
static_assert
(
sizeof
(T) == 4 ||
sizeof
(T) == 8,
"Unsupported type size for Interlocked"
);
63
}
64
68
T
Add
(T value) {
69
if
constexpr
(
sizeof
(T) == 4)
return
(T)InterlockedInternal::Add32((
volatile
long
*)&_value, (
long
)value);
70
else
if
constexpr
(
sizeof
(T) == 8)
return
(T)InterlockedInternal::Add64((
volatile
long
long
*)&_value, (
long
long
)value);
71
else
static_assert
(
sizeof
(T) == 4 ||
sizeof
(T) == 8,
"Unsupported type size for Interlocked"
);
72
}
73
77
T
Exchange
(T value) {
78
if
constexpr
(
sizeof
(T) == 4)
return
(T)InterlockedInternal::Exchange32((
volatile
long
*)&_value, (
long
)value);
79
else
if
constexpr
(
sizeof
(T) == 8)
return
(T)InterlockedInternal::Exchange64((
volatile
long
long
*)&_value, (
long
long
)value);
80
else
static_assert
(
sizeof
(T) == 4 ||
sizeof
(T) == 8,
"Unsupported type size for Interlocked"
);
81
}
82
87
T
CompareExchange
(T value, T comparand) {
88
if
constexpr
(
sizeof
(T) == 4)
return
(T)InterlockedInternal::CompareExchange32((
volatile
long
*)&_value, (
long
)value, (
long
)comparand);
89
else
if
constexpr
(
sizeof
(T) == 8)
return
(T)InterlockedInternal::CompareExchange64((
volatile
long
long
*)&_value, (
long
long
)value, (
long
long
)comparand);
90
else
static_assert
(
sizeof
(T) == 4 ||
sizeof
(T) == 8,
"Unsupported type size for Interlocked"
);
91
}
92
94
T
operator++
() {
return
Increment
(); }
95
97
T
operator++
(
int
) {
98
T current = (T)_value;
99
Increment
();
100
return
current;
101
}
102
104
T
operator--
() {
return
Decrement
(); }
105
107
T
operator--
(
int
) {
108
T current = (T)_value;
109
Decrement
();
110
return
current;
111
}
112
114
T
operator+=
(T value) {
return
Add
(value); }
115
117
operator
T()
const
{
return
(T)_value; }
118
120
T
operator=
(T value) {
return
(T)
Exchange
(value); }
121
122
private
:
123
volatile
T _value;
124
};
125
}
126
}
127
}
Common.h
Defines common cross-platform macros, export decorators, and fundamental types.
DOTNETDUPE_API
#define DOTNETDUPE_API
Platform-specific linkage decoration for exporting or importing library symbols.
Definition
Common.h:20
Object.h
Base object class for DotNetDupe mirroring .NET System.Object.
DotNetDupe::System::Object
Supports all classes in the DotNetDupe class hierarchy.
Definition
Object.h:18
DotNetDupe::System::Threading::Interlocked
Provides atomic operations for variables that are shared by multiple threads.
Definition
Interlocked.h:43
DotNetDupe::System::Threading::Interlocked::operator--
T operator--()
Atomic pre-decrement operator.
Definition
Interlocked.h:104
DotNetDupe::System::Threading::Interlocked::operator--
T operator--(int)
Atomic post-decrement operator.
Definition
Interlocked.h:107
DotNetDupe::System::Threading::Interlocked::operator+=
T operator+=(T value)
Atomic addition assignment operator.
Definition
Interlocked.h:114
DotNetDupe::System::Threading::Interlocked::Increment
T Increment()
Increments a specified variable and stores the result, as an atomic operation.
Definition
Interlocked.h:51
DotNetDupe::System::Threading::Interlocked::operator++
T operator++(int)
Atomic post-increment operator.
Definition
Interlocked.h:97
DotNetDupe::System::Threading::Interlocked::operator++
T operator++()
Atomic pre-increment operator.
Definition
Interlocked.h:94
DotNetDupe::System::Threading::Interlocked::Interlocked
Interlocked(T initialValue=0)
Initializes a new instance of the Interlocked class with an initial value.
Definition
Interlocked.h:47
DotNetDupe::System::Threading::Interlocked::Decrement
T Decrement()
Decrements a specified variable and stores the result, as an atomic operation.
Definition
Interlocked.h:59
DotNetDupe::System::Threading::Interlocked::CompareExchange
T CompareExchange(T value, T comparand)
Compares two values for equality and, if they are equal, replaces the first value.
Definition
Interlocked.h:87
DotNetDupe::System::Threading::Interlocked::Add
T Add(T value)
Adds two integers and replaces the first integer with the sum, as an atomic operation.
Definition
Interlocked.h:68
DotNetDupe::System::Threading::Interlocked::operator=
T operator=(T value)
Atomic assignment operator.
Definition
Interlocked.h:120
DotNetDupe::System::Threading::Interlocked::Exchange
T Exchange(T value)
Sets a variable to a specified value and returns the original value, as an atomic operation.
Definition
Interlocked.h:77
DotNetDupe::System::Threading::InterlockedInternal
Internal helper exposing platform-specific atomic intrinsic primitives.
Definition
Interlocked.h:19
DotNetDupe::System::Threading::InterlockedInternal::Interlocked
friend class Interlocked
Definition
Interlocked.h:21
DotNetDupe::System::Threading
Definition
AbandonedMutexException.h:11
DotNetDupe::System
Definition
Action.h:11
DotNetDupe
Definition
IServiceCollection.h:7
Include
System
Threading
Interlocked.h
Generated by
1.18.0