DotNetDupe 4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
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
10namespace DotNetDupe {
11 namespace System {
12 namespace Threading {
13
14 // Forward declaration for friendship
15 template <typename T> class Interlocked;
16
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
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
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}
Defines common cross-platform macros, export decorators, and fundamental types.
#define DOTNETDUPE_API
Platform-specific linkage decoration for exporting or importing library symbols.
Definition Common.h:20
Base object class for DotNetDupe mirroring .NET System.Object.
Supports all classes in the DotNetDupe class hierarchy.
Definition Object.h:18
Provides atomic operations for variables that are shared by multiple threads.
Definition Interlocked.h:43
T operator--()
Atomic pre-decrement operator.
T operator--(int)
Atomic post-decrement operator.
T operator+=(T value)
Atomic addition assignment operator.
T Increment()
Increments a specified variable and stores the result, as an atomic operation.
Definition Interlocked.h:51
T operator++(int)
Atomic post-increment operator.
Definition Interlocked.h:97
T operator++()
Atomic pre-increment operator.
Definition Interlocked.h:94
Interlocked(T initialValue=0)
Initializes a new instance of the Interlocked class with an initial value.
Definition Interlocked.h:47
T Decrement()
Decrements a specified variable and stores the result, as an atomic operation.
Definition Interlocked.h:59
T CompareExchange(T value, T comparand)
Compares two values for equality and, if they are equal, replaces the first value.
Definition Interlocked.h:87
T Add(T value)
Adds two integers and replaces the first integer with the sum, as an atomic operation.
Definition Interlocked.h:68
T operator=(T value)
Atomic assignment operator.
T Exchange(T value)
Sets a variable to a specified value and returns the original value, as an atomic operation.
Definition Interlocked.h:77
Internal helper exposing platform-specific atomic intrinsic primitives.
Definition Interlocked.h:19