diff options
| author | Tyge Løvset <[email protected]> | 2021-12-02 16:40:23 +0100 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2021-12-02 16:40:23 +0100 |
| commit | 9cd20ebfc4f1e10153ff814085499223265ef902 (patch) | |
| tree | 13abd2d95ab748f5c06a9c61dc1a8f200a341a2e | |
| parent | d8588e42dd180b51c6ca8861a426cf4db21c7084 (diff) | |
| download | STC-modified-9cd20ebfc4f1e10153ff814085499223265ef902.tar.gz STC-modified-9cd20ebfc4f1e10153ff814085499223265ef902.zip | |
shared_ptr atomic count: Using stdatomics.h for other compilers than gcc, clang or msvc.
| -rw-r--r-- | examples/sptr_pthread.c | 2 | ||||
| -rw-r--r-- | 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 <intrin.h>
#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 <stdatomic.h>
+ #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}
|
