diff options
| author | Garrett D'Amore <garrett@damore.org> | 2018-08-25 11:41:44 -0700 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2018-08-27 15:33:26 -0700 |
| commit | 84a1e7455c158441dd7b33d2eb296cc33dd5a6df (patch) | |
| tree | 333f17970a90643a40d3392e161e81e9903477f7 /src/platform/posix | |
| parent | 83b7a9afec7b3659974c614cea69fa3abb904d24 (diff) | |
| download | nng-84a1e7455c158441dd7b33d2eb296cc33dd5a6df.tar.gz nng-84a1e7455c158441dd7b33d2eb296cc33dd5a6df.tar.bz2 nng-84a1e7455c158441dd7b33d2eb296cc33dd5a6df.zip | |
fixes #674 want 64-bit atomics (for stats)
Diffstat (limited to 'src/platform/posix')
| -rw-r--r-- | src/platform/posix/posix_atomic.c | 89 | ||||
| -rw-r--r-- | src/platform/posix/posix_impl.h | 10 |
2 files changed, 99 insertions, 0 deletions
diff --git a/src/platform/posix/posix_atomic.c b/src/platform/posix/posix_atomic.c index a8d2579d..7f735ec0 100644 --- a/src/platform/posix/posix_atomic.c +++ b/src/platform/posix/posix_atomic.c @@ -28,6 +28,43 @@ nni_atomic_flag_reset(nni_atomic_flag *f) { atomic_flag_clear(&f->f); } + +void +nni_atomic_inc64(nni_atomic_u64 *v, uint64_t bump) +{ + (void) atomic_fetch_add_explicit(&v->v, bump, memory_order_relaxed); +} + +void +nni_atomic_dec64(nni_atomic_u64 *v, uint64_t bump) +{ + (void) atomic_fetch_sub_explicit(&v->v, bump, memory_order_relaxed); +} + +uint64_t +nni_atomic_get64(nni_atomic_u64 *v) +{ + return (atomic_load(&v->v)); +} + +void +nni_atomic_set64(nni_atomic_u64 *v, uint64_t u) +{ + atomic_store(&v->v, u); +} + +uint64_t +nni_atomic_swap64(nni_atomic_u64 *v, uint64_t u) +{ + return (atomic_exchange(&v->v, u)); +} + +void +nni_atomic_init64(nni_atomic_u64 *v) +{ + atomic_init(&v->v, 0); +} + #else #include <pthread.h> @@ -52,6 +89,58 @@ nni_atomic_flag_reset(nni_atomic_flag *f) f->f = false; pthread_mutex_unlock(&plat_atomic_lock); } + +void +nni_atomic_inc64(nni_atomic_u64 *v, uint64_t bump) +{ + pthread_mutex_lock(&plat_atomic_lock); + v += bump; + pthread_mutex_unlock(&plat_atomic_lock); +} + +void +nni_atomic_dec64(nni_atomic_u64 *v, uint64_t bump) +{ + pthread_mutex_lock(&plat_atomic_lock); + v -= bump; + pthread_mutex_unlock(&plat_atomic_lock); +} + +uint64_t +nni_atomic_get64(nni_atomic_u64 *v) +{ + uint64_t rv; + pthread_mutex_lock(&plat_atomic_lock); + rv = v->v; + pthread_mutex_unlock(&plat_atomic_lock); + return (rv); +} + +void +nni_atomic_set64(nni_atomic_u64 *v, uint64_t u) +{ + pthread_mutex_lock(&plat_atomic_lock); + v->v = u; + pthread_mutex_unlock(&plat_atomic_lock); +} + +uint64_t +nni_atomic_swap64(nni_atomic_u64 *v, uint64_t u) +{ + uint64_t rv; + pthread_mutex_lock(&plat_atomic_lock); + rv = v->v; + v->v = u; + pthread_mutex_unlock(&plat_atomic_lock); + return (rv); +} + +void +nni_atomic_init64(nni_atomic_u64 *v) +{ + v->v = 0; +} + #endif #endif // NNG_PLATFORM_POSIX diff --git a/src/platform/posix/posix_impl.h b/src/platform/posix/posix_impl.h index 70a3615f..9f3b775b 100644 --- a/src/platform/posix/posix_impl.h +++ b/src/platform/posix/posix_impl.h @@ -82,10 +82,20 @@ struct nni_plat_flock { struct nni_atomic_flag { atomic_flag f; }; + +struct nni_atomic_u64 { + _Atomic unsigned long long v; +}; + #else // NNG_HAVE_C11_ATOMIC struct nni_atomic_flag { bool f; }; + +struct nni_atomic_flag { + uint64_t v; +}; + #endif #endif |
