xtd 0.2.0
interlocked.h
Go to the documentation of this file.
1 #pragma once
5 #include "../core_export.h"
6 #include "../lock.h"
7 #include "../object.h"
8 #include "../static.h"
9 #include "../types.h"
10 
12 namespace xtd {
14  namespace threading {
38  class core_export_ interlocked static_ {
39  public:
44  static int32 add(int32& location, int32 value) noexcept;
49  static int64 add(int64& location, int64 value) noexcept;
50 
56  static double compare_exchange(double& location, double value, double comparand) noexcept;
62  static int32 compare_exchange(int32& location, int32 value, int32 comparand) noexcept;
68  static int64 compare_exchange(int64& location, int64 value, int64 comparand) noexcept;
70  static slong compare_exchange(slong& location, slong value, slong comparand) noexcept;
77  static void* compare_exchange(void*& location, void* value, void* comparand) noexcept;
83  template<typename type_t>
84  static type_t compare_exchange(object& location, const object& value, const object& comparand) noexcept {
85  type_t retValue = location;
86  lock_(location) {
87  if (location.equals(comparand)) location = value;
88  }
89  return retValue;
90  }
96  template<typename type_t>
97  static type_t compare_exchange(type_t& location, type_t value, type_t comparand) noexcept {
98  type_t retValue = location;
99  lock_(location) {
100  if (location == comparand) location = value;
101  }
102  return retValue;
103  }
109  static float compare_exchange(float& location, float value, float comparand) noexcept;
110 
117  static int32 decrement(int32& location) noexcept;
121  static int64 decrement(int64& location) noexcept;
122 
127  template<typename type_t>
128  static type_t exchange(type_t& location, type_t value) {
129  type_t original = location;
130  lock_(location)
131  location = value;
132  return original;
133  }
138  static double exchange(double& location, double value) noexcept;
146  static int32 exchange(int32& location, int32 value) noexcept;
151  static int64 exchange(int64& location, int64 value) noexcept;
153  static slong exchange(slong& location, slong value) noexcept;
159  static void* exchange(void*& location, void* value) noexcept;
164  template<typename type_t>
165  static type_t exchange(object& location, const object& value) noexcept {
166  type_t original = location;
167  lock_(location)
168  location = value;
169  return original;
170  }
175  static float exchange(float& location, float value) noexcept;
176 
183  static int32 increment(int32& location) noexcept;
187  static int64 increment(int64& location) noexcept;
188 
193  static void memory_barrier() noexcept;
194 
198  static int64 read(int64& location) noexcept;
199 
200  private:
201  };
202  }
203 }
static type_t exchange(type_t &location, type_t value)
Sets a variable of the specified type type_t to a specified value and returns the original value...
Definition: interlocked.h:128
#define static_
This keyword is use to represent a static object. A static object can&#39;t be instantiated (constructors...
Definition: static.h:37
The xtd namespace contains all fundamental classes to access Hardware, Os, System, and more.
Definition: system_report.h:17
static type_t compare_exchange(type_t &location, type_t value, type_t comparand) noexcept
Compares two instances of the specified reference type type_t for equality and, if they are equal...
Definition: interlocked.h:97
#define lock_(object)
The lock_ keyword marks a statement block as a critical section by obtaining the mutual-exclusion loc...
Definition: lock.h:85
__slong__ slong
Represents a 32-bit or 64-bit signed integer.
Definition: types.h:206
static type_t compare_exchange(object &location, const object &value, const object &comparand) noexcept
Compares two objects for equality and, if they are equal, replaces one of them.
Definition: interlocked.h:84
Provides atomic operations for variables that are shared by multiple threads.
Definition: interlocked.h:38
int_least32_t int32
Represents a 32-bit signed integer.
Definition: types.h:129
static type_t exchange(object &location, const object &value) noexcept
Sets an object to a specified value and returns the original value, as an atomic operation.
Definition: interlocked.h:165
int_least64_t int64
Represents a 64-bit signed integer.
Definition: types.h:140