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