summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPedro J. Estébanez <pedrojrulez@gmail.com>2017-07-11 17:41:53 +0200
committerPedro J. Estébanez <pedrojrulez@gmail.com>2017-07-17 23:26:53 +0200
commit00d60125df8a853bafe76b970138dfb75083d628 (patch)
tree39653eaa707b55eabc2caccf4a68a7096ffdb62d
parent9a868cd846ac9759b96cd2cbbb99a9a30bb43972 (diff)
Implement add & subtract atomics
-rw-r--r--core/safe_refcount.cpp72
-rw-r--r--core/safe_refcount.h4
2 files changed, 76 insertions, 0 deletions
diff --git a/core/safe_refcount.cpp b/core/safe_refcount.cpp
index 4a57c09f1e..1bd16f9e4f 100644
--- a/core/safe_refcount.cpp
+++ b/core/safe_refcount.cpp
@@ -62,6 +62,22 @@ static _ALWAYS_INLINE_T _atomic_increment_impl(register T *pw) {
return *pw;
}
+template <class T>
+static _ALWAYS_INLINE_ T _atomic_sub_impl(register T *pw, register T val) {
+
+ (*pw) -= val;
+
+ return *pw;
+}
+
+template <class T>
+static _ALWAYS_INLINE_T _atomic_add_impl(register T *pw, register T val) {
+
+ (*pw) += val;
+
+ return *pw;
+}
+
#elif defined(__GNUC__)
/* Implementation for GCC & Clang */
@@ -93,6 +109,18 @@ static _ALWAYS_INLINE_ T _atomic_increment_impl(register T *pw) {
return __sync_add_and_fetch(pw, 1);
}
+template <class T>
+static _ALWAYS_INLINE_ T _atomic_sub_impl(register T *pw, register T val) {
+
+ return __sync_sub_and_fetch(pw, val);
+}
+
+template <class T>
+static _ALWAYS_INLINE_ T _atomic_add_impl(register T *pw, register T val) {
+
+ return __sync_add_and_fetch(pw, val);
+}
+
#elif defined(_MSC_VER)
/* Implementation for MSVC-Windows */
@@ -126,6 +154,20 @@ static _ALWAYS_INLINE_ uint32_t _atomic_increment_impl(register uint32_t *pw) {
return InterlockedIncrement((LONG volatile *)pw);
}
+static _ALWAYS_INLINE_ uint32_t _atomic_sub_impl(register uint32_t *pw, register uint32_t val) {
+
+#if _WIN32_WINNT >= 0x0601 // Windows 7+
+ return InterlockedExchangeSubtract(pw, val) - val;
+#else
+ return InterlockedExchangeAdd((LONG volatile *)pw, -(int32_t)val) - val;
+#endif
+}
+
+static _ALWAYS_INLINE_ uint32_t _atomic_add_impl(register uint32_t *pw, register uint32_t val) {
+
+ return InterlockedAdd((LONG volatile *)pw, val);
+}
+
static _ALWAYS_INLINE_ uint64_t _atomic_conditional_increment_impl(register uint64_t *pw) {
ATOMIC_CONDITIONAL_INCREMENT_BODY(pw, LONGLONG, InterlockedCompareExchange64, uint64_t)
@@ -141,6 +183,20 @@ static _ALWAYS_INLINE_ uint64_t _atomic_increment_impl(register uint64_t *pw) {
return InterlockedIncrement64((LONGLONG volatile *)pw);
}
+static _ALWAYS_INLINE_ uint64_t _atomic_sub_impl(register uint64_t *pw, register uint64_t val) {
+
+#if _WIN32_WINNT >= 0x0601 // Windows 7+
+ return InterlockedExchangeSubtract64(pw, val) - val;
+#else
+ return InterlockedExchangeAdd64((LONGLONG volatile *)pw, -(int64_t)val) - val;
+#endif
+}
+
+static _ALWAYS_INLINE_ uint64_t _atomic_add_impl(register uint64_t *pw, register uint64_t val) {
+
+ return InterlockedAdd64((LONGLONG volatile *)pw, val);
+}
+
#else
//no threads supported?
@@ -162,6 +218,14 @@ uint32_t atomic_increment(register uint32_t *pw) {
return _atomic_increment_impl(pw);
}
+uint32_t atomic_sub(register uint32_t *pw, register uint32_t val) {
+ return _atomic_sub_impl(pw, val);
+}
+
+uint32_t atomic_add(register uint32_t *pw, register uint32_t val) {
+ return _atomic_add_impl(pw, val);
+}
+
uint64_t atomic_conditional_increment(register uint64_t *counter) {
return _atomic_conditional_increment_impl(counter);
}
@@ -173,3 +237,11 @@ uint64_t atomic_decrement(register uint64_t *pw) {
uint64_t atomic_increment(register uint64_t *pw) {
return _atomic_increment_impl(pw);
}
+
+uint64_t atomic_sub(register uint64_t *pw, register uint64_t val) {
+ return _atomic_sub_impl(pw, val);
+}
+
+uint64_t atomic_add(register uint64_t *pw, register uint64_t val) {
+ return _atomic_add_impl(pw, val);
+}
diff --git a/core/safe_refcount.h b/core/safe_refcount.h
index 5c9fd996a0..a2d2b5e127 100644
--- a/core/safe_refcount.h
+++ b/core/safe_refcount.h
@@ -39,10 +39,14 @@
uint32_t atomic_conditional_increment(register uint32_t *counter);
uint32_t atomic_decrement(register uint32_t *pw);
uint32_t atomic_increment(register uint32_t *pw);
+uint32_t atomic_sub(register uint32_t *pw, register uint32_t val);
+uint32_t atomic_add(register uint32_t *pw, register uint32_t val);
uint64_t atomic_conditional_increment(register uint64_t *counter);
uint64_t atomic_decrement(register uint64_t *pw);
uint64_t atomic_increment(register uint64_t *pw);
+uint64_t atomic_sub(register uint64_t *pw, register uint64_t val);
+uint64_t atomic_add(register uint64_t *pw, register uint64_t val);
struct SafeRefCount {