From 9cd20ebfc4f1e10153ff814085499223265ef902 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Thu, 2 Dec 2021 16:40:23 +0100 Subject: shared_ptr atomic count: Using stdatomics.h for other compilers than gcc, clang or msvc. --- examples/sptr_pthread.c | 2 +- include/stc/csptr.h | 12 ++++-------- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/examples/sptr_pthread.c b/examples/sptr_pthread.c index cffafd3d..0a1b7c79 100644 --- a/examples/sptr_pthread.c +++ b/examples/sptr_pthread.c @@ -20,8 +20,8 @@ void Base_del(Base* b) { printf("Base::~Base()\n"); } void* thr(csptr_base* lp) { - static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER; sleep(1); + static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER; c_autoscope (pthread_mutex_lock(&mtx), pthread_mutex_unlock(&mtx)) { printf("local pointer in a thread:\n" diff --git a/include/stc/csptr.h b/include/stc/csptr.h index da52d290..a8496654 100644 --- a/include/stc/csptr.h +++ b/include/stc/csptr.h @@ -62,14 +62,10 @@ typedef long atomic_count_t; #include #define c_atomic_inc(v) (void)_InterlockedIncrement(v) #define c_atomic_dec_and_test(v) !_InterlockedDecrement(v) -#elif defined(__i386__) || defined(__x86_64__) - STC_INLINE void c_atomic_inc(atomic_count_t* v) - { asm volatile("lock; incq %0" :"=m"(*v) :"m"(*v)); } - STC_INLINE bool c_atomic_dec_and_test(atomic_count_t* v) { - unsigned char c; - asm volatile("lock; decq %0; sete %1" :"=m"(*v), "=qm"(c) :"m"(*v) :"memory"); - return !c; - } +#else + #include + #define c_atomic_inc(v) (void)atomic_fetch_add(v, 1) + #define c_atomic_dec_and_test(v) (atomic_fetch_sub(v, 1) == 1) #endif #define csptr_null {NULL, NULL} -- cgit v1.2.3