From cde7c39de3492f8b701038af6563a6c4e01ff558 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Fri, 26 Nov 2021 11:08:57 +0100 Subject: Bugfix of c_atomic_decrement() asm version in csptr.h. Some cleanup --- examples/random.c | 4 ++-- examples/read.c | 6 +++--- examples/sptr_pthread.c | 8 ++++---- include/stc/csptr.h | 9 +++------ 4 files changed, 12 insertions(+), 15 deletions(-) diff --git a/examples/random.c b/examples/random.c index d4a12a27..81fe71c5 100644 --- a/examples/random.c +++ b/examples/random.c @@ -4,7 +4,7 @@ int main() { - const size_t N = 5000000000; + const size_t N = 1000000000; const uint64_t seed = time(NULL), range = 1000000; stc64_t rng = stc64_init(seed); @@ -39,4 +39,4 @@ int main() diff = clock() - before; printf("biased 0-%zu \t: %f secs, %zu, avg: %f\n", range, (float) diff / CLOCKS_PER_SEC, N, (double) sum / N); -} \ No newline at end of file +} diff --git a/examples/read.c b/examples/read.c index 728bf579..4e670a10 100644 --- a/examples/read.c +++ b/examples/read.c @@ -6,9 +6,9 @@ cvec_str read_file(const char* name) { cvec_str vec = cvec_str_init(); c_autovar (FILE* f = fopen(name, "r"), fclose(f)) - c_auto (cstr, line) - while (cstr_getline(&line, f)) - cvec_str_emplace_back(&vec, line.str); + c_autovar (cstr line = cstr_init(), cstr_del(&line)) + while (cstr_getline(&line, f)) + cvec_str_emplace_back(&vec, line.str); return vec; } diff --git a/examples/sptr_pthread.c b/examples/sptr_pthread.c index 63b9a7a4..cffafd3d 100644 --- a/examples/sptr_pthread.c +++ b/examples/sptr_pthread.c @@ -25,7 +25,7 @@ void* thr(csptr_base* lp) c_autoscope (pthread_mutex_lock(&mtx), pthread_mutex_unlock(&mtx)) { printf("local pointer in a thread:\n" - " p.get() = %p, p.use_count() = %zu\n", (void*)lp->get, *lp->use_count); + " p.get() = %p, p.use_count() = %ld\n", (void*)lp->get, *lp->use_count); } /* atomically decrease ref. */ csptr_base_del(lp); @@ -37,7 +37,7 @@ int main() csptr_base p = csptr_base_make((Base){42}); printf("Created a Base\n" - " p.get() = %p, p.use_count() = %zu\n", (void*)p.get, *p.use_count); + " p.get() = %p, p.use_count() = %ld\n", (void*)p.get, *p.use_count); enum {N = 3}; pthread_t t[N]; csptr_base c[N]; @@ -48,7 +48,7 @@ int main() printf("Shared ownership between %d threads and released\n" "ownership from main:\n" - " p.get() = %p, p.use_count() = %zu\n", N, (void*)p.get, *p.use_count); + " p.get() = %p, p.use_count() = %ld\n", N, (void*)p.get, *p.use_count); csptr_base_reset(&p); c_forrange (i, N) pthread_join(t[i], NULL); @@ -57,4 +57,4 @@ int main() #else int main() {} -#endif \ No newline at end of file +#endif diff --git a/include/stc/csptr.h b/include/stc/csptr.h index 7415c20a..b702f461 100644 --- a/include/stc/csptr.h +++ b/include/stc/csptr.h @@ -65,11 +65,8 @@ typedef long atomic_count_t; #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) { - atomic_count_t r; - __asm__ __volatile__("lock; xadd %0, %1" :"=r"(r) :"m"(*v), "0"(-1)); - return r - 1; - } + STC_INLINE atomic_count_t c_atomic_decrement(atomic_count_t* v) + { __asm__ __volatile__("lock; decq %0" :"=m"(*v) :"m"(*v)); return *v; } #endif #define csptr_null {NULL, NULL} @@ -181,4 +178,4 @@ _cx_memb(_compare)(const _cx_self* x, const _cx_self* y) { #undef cx_increment #undef cx_decrement #undef i_nonatomic -#include "template.h" \ No newline at end of file +#include "template.h" -- cgit v1.2.3