diff options
Diffstat (limited to 'src/platform')
| -rw-r--r-- | src/platform/posix/posix_atomic.c | 76 | ||||
| -rw-r--r-- | src/platform/posix/posix_impl.h | 12 | ||||
| -rw-r--r-- | src/platform/windows/win_impl.h | 4 | ||||
| -rw-r--r-- | src/platform/windows/win_thread.c | 24 |
4 files changed, 107 insertions, 9 deletions
diff --git a/src/platform/posix/posix_atomic.c b/src/platform/posix/posix_atomic.c index 56e2c370..b1183865 100644 --- a/src/platform/posix/posix_atomic.c +++ b/src/platform/posix/posix_atomic.c @@ -1,5 +1,5 @@ // -// Copyright 2018 Staysail Systems, Inc. <info@staysail.tech> +// Copyright 2019 Staysail Systems, Inc. <info@staysail.tech> // Copyright 2018 Capitar IT Group BV <info@capitar.com> // // This software is supplied under the terms of the MIT License, a @@ -30,33 +30,60 @@ nni_atomic_flag_reset(nni_atomic_flag *f) } void +nni_atomic_set_bool(nni_atomic_bool *v, bool b) +{ + atomic_store(&v->v, b); +} + +bool +nni_atomic_get_bool(nni_atomic_bool *v) +{ + return (atomic_load(&v->v)); +} + +bool +nni_atomic_swap_bool(nni_atomic_bool *v, bool b) +{ + return (atomic_exchange(&v->v, b)); + +} + +void +nni_atomic_init_bool(nni_atomic_bool *v) +{ + atomic_init(&v->v, false); +} + +void nni_atomic_add64(nni_atomic_u64 *v, uint64_t bump) { - (void) atomic_fetch_add_explicit(&v->v, bump, memory_order_relaxed); + (void) atomic_fetch_add_explicit( + &v->v, (uint_fast64_t) bump, memory_order_relaxed); } void nni_atomic_sub64(nni_atomic_u64 *v, uint64_t bump) { - (void) atomic_fetch_sub_explicit(&v->v, bump, memory_order_relaxed); + (void) atomic_fetch_sub_explicit( + &v->v, (uint_fast64_t) bump, memory_order_relaxed); } uint64_t nni_atomic_get64(nni_atomic_u64 *v) { - return (atomic_load(&v->v)); + return ((uint64_t) atomic_load(&v->v)); } void nni_atomic_set64(nni_atomic_u64 *v, uint64_t u) { - atomic_store(&v->v, u); + atomic_store(&v->v, (uint_fast64_t) u); } uint64_t nni_atomic_swap64(nni_atomic_u64 *v, uint64_t u) { - return (atomic_exchange(&v->v, u)); + return ((uint64_t) atomic_exchange(&v->v, (uint_fast64_t) u)); } void @@ -77,7 +104,7 @@ nni_atomic_dec64_nv(nni_atomic_u64 *v) uint64_t ov; // C11 atomics give the old rather than new value. - ov = atomic_fetch_sub(&v->v, 1); + ov = (uint64_t) atomic_fetch_sub(&v->v, 1); return (ov - 1); } @@ -107,6 +134,41 @@ nni_atomic_flag_reset(nni_atomic_flag *f) } void +nni_atomic_set_bool(nni_atomic_bool *b) +{ + pthread_mutex_lock(&plat_atomic_lock); + b->b = false; + pthread_mutex_unlock(&plat_atomic_lock); +} + +void +nni_atomic_get_bool(nni_atomic_bool *b) +{ + bool v; + pthread_mutex_lock(&plat_atomic_lock); + v = b->b; + pthread_mutex_unlock(&plat_atomic_lock); + return (v); +} + +void +nni_atomic_swap_bool(nni_atomic_bool *b, bool n) +{ + bool v; + pthread_mutex_lock(&plat_atomic_lock); + v = b->b; + b->b = n; + pthread_mutex_unlock(&plat_atomic_lock); + return (v); +} + +void +nni_atomic_initbool(nni_atomic_bool *b) +{ + b->b = false; +} + +void nni_atomic_add64(nni_atomic_u64 *v, uint64_t bump) { pthread_mutex_lock(&plat_atomic_lock); diff --git a/src/platform/posix/posix_impl.h b/src/platform/posix/posix_impl.h index b07d268b..a1cb62c5 100644 --- a/src/platform/posix/posix_impl.h +++ b/src/platform/posix/posix_impl.h @@ -1,5 +1,5 @@ // -// Copyright 2018 Staysail Systems, Inc. <info@staysail.tech> +// Copyright 2019 Staysail Systems, Inc. <info@staysail.tech> // Copyright 2018 Capitar IT Group BV <info@capitar.com> // // This software is supplied under the terms of the MIT License, a @@ -84,7 +84,11 @@ struct nni_atomic_flag { }; struct nni_atomic_u64 { - _Atomic unsigned long long v; + atomic_uint_fast64_t v; +}; + +struct nni_atomic_bool { + atomic_bool v; }; #else // NNG_HAVE_C11_ATOMIC @@ -92,6 +96,10 @@ struct nni_atomic_flag { bool f; }; +struct nni_atomic_bol { + bool b; +}; + struct nni_atomic_u64 { uint64_t v; }; diff --git a/src/platform/windows/win_impl.h b/src/platform/windows/win_impl.h index cd15804a..befcd185 100644 --- a/src/platform/windows/win_impl.h +++ b/src/platform/windows/win_impl.h @@ -50,6 +50,10 @@ struct nni_atomic_flag { unsigned f; }; +struct nni_atomic_bool { + LONG v; +}; + struct nni_atomic_u64 { LONGLONG v; }; diff --git a/src/platform/windows/win_thread.c b/src/platform/windows/win_thread.c index 076da9e4..763dcf7f 100644 --- a/src/platform/windows/win_thread.c +++ b/src/platform/windows/win_thread.c @@ -132,6 +132,30 @@ nni_atomic_flag_reset(nni_atomic_flag *f) } void +nni_atomic_set_bool(nni_atomic_bool *v, bool b) +{ + InterlockedExchange(&v->v, (LONG) b); +} + +bool +nni_atomic_get_bool(nni_atomic_bool *v) +{ + return ((bool) InterlockedAdd(&v->v, 0)); +} + +bool +nni_atomic_swap_bool(nni_atomic_bool *v, bool b) +{ + return ((bool) InterlockedExchange(&v->v, (LONG) b)); +} + +void +nni_atomic_init_bool(nni_atomic_bool *v) +{ + InterlockedExchange(&v->v, 0); +} + +void nni_atomic_add64(nni_atomic_u64 *v, uint64_t bump) { InterlockedAddNoFence64(&v->v, (LONGLONG) bump); |
