feat(atomic): add SInterlockedIncrement and SInterlockedDecrement

This commit is contained in:
fallenoak 2020-12-04 00:00:03 -06:00
parent 4307fb2a1d
commit 653fa81853
No known key found for this signature in database
GPG key ID: 7628F8E61AEA070D
3 changed files with 55 additions and 0 deletions

25
storm/Atomic.cpp Normal file
View file

@ -0,0 +1,25 @@
#include "storm/Atomic.hpp"
#if defined(WHOA_SYSTEM_WIN)
#include <windows.h>
#endif
int32_t SInterlockedDecrement(int32_t* ptr) {
#if defined(WHOA_SYSTEM_WIN)
return InterlockedDecrement(ptr);
#endif
#if defined(WHOA_SYSTEM_MAC) || defined(WHOA_SYSTEM_LINUX)
return __sync_fetch_and_sub(ptr, 1) - 1;
#endif
}
int32_t SInterlockedIncrement(int32_t* ptr) {
#if defined(WHOA_SYSTEM_WIN)
return InterlockedIncrement(ptr);
#endif
#if defined(WHOA_SYSTEM_MAC) || defined(WHOA_SYSTEM_LINUX)
return __sync_fetch_and_add(ptr, 1) + 1;
#endif
}

10
storm/Atomic.hpp Normal file
View file

@ -0,0 +1,10 @@
#ifndef STORM_ATOMIC_HPP
#define STORM_ATOMIC_HPP
#include <cstdint>
int32_t SInterlockedDecrement(int32_t* ptr);
int32_t SInterlockedIncrement(int32_t* ptr);
#endif

20
test/Atomic.cpp Normal file
View file

@ -0,0 +1,20 @@
#include "storm/Atomic.hpp"
#include "test/Test.hpp"
TEST_CASE("SInterlockedDecrement", "[atomic]") {
SECTION("decrements value") {
int32_t value = 1;
auto decremented = SInterlockedDecrement(&value);
CHECK(value == 0);
CHECK(decremented == 0);
}
}
TEST_CASE("SInterlockedIncrement", "[atomic]") {
SECTION("increments value") {
int32_t value = { 1 };
auto incremented = SInterlockedIncrement(&value);
CHECK(value == 2);
CHECK(incremented == 2);
}
}