From 30f6b34e1d1451f08a652b940d3ff3e1b9adc1e4 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Fri, 26 Nov 2021 11:57:10 +0100 Subject: Fixed atomic_decrement final. --- include/stc/csptr.h | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/include/stc/csptr.h b/include/stc/csptr.h index b702f461..10063bcd 100644 --- a/include/stc/csptr.h +++ b/include/stc/csptr.h @@ -57,16 +57,19 @@ int main() { typedef long atomic_count_t; #if defined(__GNUC__) || defined(__clang__) #define c_atomic_increment(v) (void)__atomic_add_fetch(v, 1, __ATOMIC_SEQ_CST) - #define c_atomic_decrement(v) __atomic_sub_fetch(v, 1, __ATOMIC_SEQ_CST) + #define c_atomic_decrement(v) (bool)__atomic_sub_fetch(v, 1, __ATOMIC_SEQ_CST) #elif defined(_MSC_VER) #include #define c_atomic_increment(v) (void)_InterlockedIncrement(v) - #define c_atomic_decrement(v) _InterlockedDecrement(v) + #define c_atomic_decrement(v) (bool)_InterlockedDecrement(v) #elif defined(__i386__) || defined(__x86_64__) STC_INLINE void c_atomic_increment(atomic_count_t* v) - { __asm__ __volatile__("lock; incq %0" :"=m"(*v) :"m"(*v)); } - STC_INLINE atomic_count_t c_atomic_decrement(atomic_count_t* v) - { __asm__ __volatile__("lock; decq %0" :"=m"(*v) :"m"(*v)); return *v; } + { asm volatile("lock; incq %0" :"=m"(*v) :"m"(*v)); } + STC_INLINE bool c_atomic_decrement(atomic_count_t* v) { + unsigned char c; + asm volatile("lock; decq %0; sete %1" :"=m"(*v), "=qm"(c) :"m"(*v) :"memory"); + return !c; + } #endif #define csptr_null {NULL, NULL} -- cgit v1.2.3