summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2021-12-02 15:20:07 +0100
committerTyge Løvset <[email protected]>2021-12-02 15:20:07 +0100
commitd8588e42dd180b51c6ca8861a426cf4db21c7084 (patch)
tree15a40913cd399066b68867cd65e861082679d3bc
parent323f21d57aaf30ffd43d6b67146bc887ad206298 (diff)
parent8d871133ee563faca037f7d3b13f81153ee340a8 (diff)
downloadSTC-modified-d8588e42dd180b51c6ca8861a426cf4db21c7084.tar.gz
STC-modified-d8588e42dd180b51c6ca8861a426cf4db21c7084.zip
Merge branch 'master' of github.com:tylov/STC into master
-rw-r--r--examples/random.c4
-rw-r--r--examples/read.c6
-rw-r--r--examples/sptr_pthread.c8
-rw-r--r--include/stc/csptr.h39
4 files changed, 29 insertions, 28 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 82d882d9..da52d290 100644
--- a/include/stc/csptr.h
+++ b/include/stc/csptr.h
@@ -56,19 +56,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_inc(v) (void)__atomic_add_fetch(v, 1, __ATOMIC_SEQ_CST)
+ #define c_atomic_dec_and_test(v) !__atomic_sub_fetch(v, 1, __ATOMIC_SEQ_CST)
#elif defined(_MSC_VER)
#include <intrin.h>
- #define c_atomic_increment(v) (void)_InterlockedIncrement(v)
- #define c_atomic_decrement(v) _InterlockedDecrement(v)
+ #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_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 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;
}
#endif
@@ -82,12 +82,13 @@ typedef long atomic_count_t;
#define i_valfrom _not_to_be_used_
#include "template.h"
+
#if !c_option(c_no_atomic)
- #define _i_increment(v) c_atomic_increment(v)
- #define _i_decrement(v) c_atomic_decrement(v)
+ #define _i_atomic_inc(v) c_atomic_inc(v)
+ #define _i_atomic_dec_and_test(v) c_atomic_dec_and_test(v)
#else
- #define _i_increment(v) (++*(v))
- #define _i_decrement(v) (--*(v))
+ #define _i_atomic_inc(v) (void)(++*(v))
+ #define _i_atomic_dec_and_test(v) !(--*(v))
#endif
#if !c_option(c_is_fwd)
_cx_deftypes(_c_csptr_types, _cx_self, i_val);
@@ -117,7 +118,7 @@ _cx_memb(_make)(_cx_value val) {
STC_INLINE _cx_self
_cx_memb(_clone)(_cx_self ptr) {
- if (ptr.use_count) _i_increment(ptr.use_count);
+ if (ptr.use_count) _i_atomic_inc(ptr.use_count);
return ptr;
}
@@ -130,7 +131,7 @@ _cx_memb(_move)(_cx_self* self) {
STC_INLINE void
_cx_memb(_del)(_cx_self* self) {
- if (self->use_count && _i_decrement(self->use_count) == 0) {
+ if (self->use_count && _i_atomic_dec_and_test(self->use_count)) {
i_valdel(self->get);
if (self->get != &((_cx_csptr_rep *)self->use_count)->value)
c_free(self->get);
@@ -158,7 +159,7 @@ _cx_memb(_reset_with)(_cx_self* self, _cx_value val) {
STC_INLINE void
_cx_memb(_copy)(_cx_self* self, _cx_self ptr) {
- if (ptr.use_count) _i_increment(ptr.use_count);
+ if (ptr.use_count) _i_atomic_inc(ptr.use_count);
_cx_memb(_del)(self); *self = ptr;
}
@@ -178,6 +179,6 @@ _cx_memb(_compare)(const _cx_self* x, const _cx_self* y) {
#endif
}
#endif
-#undef _i_increment
-#undef _i_decrement
+#undef _i_atomic_inc
+#undef _i_atomic_dec_and_test
#include "template.h" \ No newline at end of file