TDME2  1.9.200
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
AtomicOperations.h
Go to the documentation of this file.
1 #pragma once
2 
4 
5 #include <tdme/tdme.h>
6 
7 #if defined(_WIN32) && defined(_MSC_VER)
8  #define NOMINMAX
9  #include <windows.h>
10 #endif
11 
12 /**
13  * Atomic operations
14  * @author Andreas Drewke
15  */
17 public:
18 
19  /**
20  * Increment uint32 value and return its value
21  * @param value reference to value
22  * @param byValue value you like to add
23  * @return incremented value
24  */
25  inline static uint32_t increment(volatile uint32_t& value, uint32_t byValue = 1) {
26  #if defined(_WIN32) && defined(_MSC_VER)
27  return InterlockedAdd((volatile long*)&value, (long)byValue);
28  #else
29  return __sync_add_and_fetch(&value, byValue);
30  #endif
31  }
32 
33  /**
34  * Decrement uint32 value and return its value
35  * @param value reference to value
36  * @param byValue value you like to subtract
37  * @return decremented value
38  */
39  inline static uint32_t decrement(volatile uint32_t& value, uint32_t byValue = 1) {
40  #if defined(_WIN32) && defined(_MSC_VER)
41  return InterlockedAdd((volatile long*)&value, (long)-byValue);
42  #else
43  return __sync_sub_and_fetch(&value, byValue);
44  #endif
45  }
46 
47 };
static uint32_t decrement(volatile uint32_t &value, uint32_t byValue=1)
Decrement uint32 value and return its value.
static uint32_t increment(volatile uint32_t &value, uint32_t byValue=1)
Increment uint32 value and return its value.