CodePorting.Translator Cs2Cpp
CodePorting.Translator.Cs2Cpp.Framework
interlocked.h
1
2#pragma once
3#include <atomic>
4#include <type_traits>
5#include "system/threading/thread.h"
6
7namespace System { namespace Threading {
8
13{
15 template <typename T>
16 static constexpr bool IsSupportedInt = std::is_same<T, int32_t>::value || std::is_same<T, uint32_t>::value ||
17 std::is_same<T, int64_t>::value || std::is_same<T, uint64_t>::value;
18
19public:
23 static ASPOSECPP_SHARED_API int32_t Increment(int32_t& location);
27 static ASPOSECPP_SHARED_API int64_t Increment(int64_t& location);
31 static ASPOSECPP_SHARED_API int32_t Decrement(int32_t& location);
35 static ASPOSECPP_SHARED_API int64_t Decrement(int64_t& location);
36
41 static ASPOSECPP_SHARED_API int32_t Add(int32_t& location1, int32_t value);
46 static ASPOSECPP_SHARED_API int64_t Add(int64_t& location1, int64_t value);
47
51 static ASPOSECPP_SHARED_API int64_t Read(int64_t& location1);
52
57 static ASPOSECPP_SHARED_API int32_t ExchangeAdd(int32_t& location1, int32_t value);
62 static ASPOSECPP_SHARED_API int64_t ExchangeAdd(int64_t& location1, int64_t value);
63
69 template <typename T>
70 static typename std::enable_if<IsSupportedInt<T>, T>::type Exchange(T& location1, T value)
71 {
72 return std::atomic_exchange((std::atomic<T>*)&location1, value);
73 }
80 template <typename T>
81 static typename std::enable_if<!IsSupportedInt<T>, T>::type Exchange(T& location1, T value)
82 {
83 throw NotImplementedException(ASPOSE_CURRENT_FUNCTION);
84 }
85
93 template <typename T>
94 static typename std::enable_if<IsSupportedInt<T>, T>::type CompareExchange(T& location1, T value, T comparand)
95 {
96 auto orig = location1;
97 std::atomic_compare_exchange_strong((std::atomic<T>*)&location1, &comparand, value);
98 return orig;
99 }
107 template <typename T>
108 static typename std::enable_if<!IsSupportedInt<T>, T>::type CompareExchange(T& location1, T value, T comparand)
109 {
110 throw NotImplementedException(ASPOSE_CURRENT_FUNCTION);
111 }
112
120 static ASPOSECPP_SHARED_API int32_t CompareExchange(int32_t& location1, int32_t value, int32_t comparand,
121 bool& succeeded);
122};
123
124}} // namespace System::Threading
Provides API for thread-safe operations. This is a static type with no instance services....
Definition: interlocked.h:13
static int32_t Increment(int32_t &location)
Increments value atomically.
static int32_t Add(int32_t &location1, int32_t value)
Increases value atomically.
static std::enable_if<!IsSupportedInt< T >, T >::type Exchange(T &location1, T value)
Exchanges value on variable: stores new value and returns the value variable had immediately before s...
Definition: interlocked.h:81
static std::enable_if< IsSupportedInt< T >, T >::type Exchange(T &location1, T value)
Exchanges value on variable: stores new value and returns the value variable had immediately before s...
Definition: interlocked.h:70
static int64_t Decrement(int64_t &location)
Decrements value atomically.
static int64_t Read(int64_t &location1)
Returns a 64-bit value, loaded as an atomic operation.
static int64_t Add(int64_t &location1, int64_t value)
Increases value atomically.
static std::enable_if<!IsSupportedInt< T >, T >::type CompareExchange(T &location1, T value, T comparand)
Compare-exchanges value on variable: checks if variable is equal to specific value and stores the new...
Definition: interlocked.h:108
static std::enable_if< IsSupportedInt< T >, T >::type CompareExchange(T &location1, T value, T comparand)
Compare-exchanges value on variable: checks if variable is equal to specific value and stores the new...
Definition: interlocked.h:94
static int32_t ExchangeAdd(int32_t &location1, int32_t value)
Increases value atomically via exchange-add procedure.
static int64_t ExchangeAdd(int64_t &location1, int64_t value)
Increases value atomically via exchange-add procedure.
static int32_t CompareExchange(int32_t &location1, int32_t value, int32_t comparand, bool &succeeded)
Compare-exchanges value on variable: checks if variable is equal to specific value and stores the new...
static int64_t Increment(int64_t &location)
Increments value atomically.
static int32_t Decrement(int32_t &location)
Decrements value atomically.
Definition: db_command.h:9