aboutsummaryrefslogtreecommitdiffstats
path: root/include
diff options
context:
space:
mode:
authorSven Göthel <[email protected]>2024-05-13 19:43:02 +0200
committerSven Göthel <[email protected]>2024-05-13 19:43:02 +0200
commite0ff249054c5b1569c0e1cc65028c05f9d52caa9 (patch)
treefcfd99c951ad0d4ecee32b49084cfd7d7827b5ab /include
parent2366c00f88c20c3bd1facf703f8b7bbb947a801d (diff)
ordered_atomic: Allow prefix ++ and -- via fetch_{add,sub}() {+,-} 1, still no use of builtins
Diffstat (limited to 'include')
-rw-r--r--include/jau/ordered_atomic.hpp26
1 files changed, 23 insertions, 3 deletions
diff --git a/include/jau/ordered_atomic.hpp b/include/jau/ordered_atomic.hpp
index 6a2e6b2..be4db4e 100644
--- a/include/jau/ordered_atomic.hpp
+++ b/include/jau/ordered_atomic.hpp
@@ -115,7 +115,7 @@ template <typename _Tp, std::memory_order _MO> struct ordered_atomic : private s
_Tp operator--(int) volatile noexcept // postfix --
{ return super::fetch_sub(1, _MO); }
-#if 0 /* def _GLIBCXX_ATOMIC_BASE_H */
+#if 0 && __has_builtin(__atomic_add_fetch) && __has_builtin(__atomic_sub_fetch)
// prefix ++, -- impossible w/o using GCC __atomic builtins and access to _M_i .. etc
@@ -126,7 +126,7 @@ template <typename _Tp, std::memory_order _MO> struct ordered_atomic : private s
CXX_ALWAYS_INLINE
_Tp operator++() volatile noexcept // prefix ++
{ return __atomic_add_fetch(&_M_i, 1, int(_MO)); }
-
+
CXX_ALWAYS_INLINE
_Tp operator--() noexcept // prefix --
{ return __atomic_sub_fetch(&_M_i, 1, int(_MO)); }
@@ -135,7 +135,27 @@ template <typename _Tp, std::memory_order _MO> struct ordered_atomic : private s
_Tp operator--() volatile noexcept // prefix --
{ return __atomic_sub_fetch(&_M_i, 1, int(_MO)); }
-#endif /* 0 _GLIBCXX_ATOMIC_BASE_H */
+#else /* __has_builtin(__atomic_add_fetch) && __has_builtin(__atomic_sub_fetch) */
+
+ // prefix ++, -- .. alternative
+
+ CXX_ALWAYS_INLINE
+ _Tp operator++() noexcept // prefix ++
+ { return super::fetch_add(1, _MO) + 1; }
+
+ CXX_ALWAYS_INLINE
+ _Tp operator++() volatile noexcept // prefix ++
+ { return super::fetch_add(1, _MO) + 1; }
+
+ CXX_ALWAYS_INLINE
+ _Tp operator--() noexcept // prefix --
+ { return super::fetch_sub(1, _MO) - 1; }
+
+ CXX_ALWAYS_INLINE
+ _Tp operator--() volatile noexcept // prefix --
+ { return super::fetch_sub(1, _MO) - 1; }
+
+#endif /* __has_builtin(__atomic_add_fetch) && __has_builtin(__atomic_sub_fetch) */
CXX_ALWAYS_INLINE
bool is_lock_free() const noexcept