diff options
| author | Garrett D'Amore <garrett@damore.org> | 2020-01-11 13:15:40 -0800 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2020-01-11 14:15:37 -0800 |
| commit | 6d92c73e5cdf93fe70b0646e78a250e01a8d2f65 (patch) | |
| tree | cb14648b92b02e17bcb48f41a623009cdd65e0ab /src/platform/posix | |
| parent | 516b99946aad5da15020de9d7175d44c12fd14c6 (diff) | |
| download | nng-6d92c73e5cdf93fe70b0646e78a250e01a8d2f65.tar.gz nng-6d92c73e5cdf93fe70b0646e78a250e01a8d2f65.tar.bz2 nng-6d92c73e5cdf93fe70b0646e78a250e01a8d2f65.zip | |
XREQ and others race on TTL.
The TTL in these cases should have been atomic. To facilitate
things we actually introduce an atomic int for convenience. We
also introduce a convenience nni_msg_must_append_u32() and
nni_msg_header_must_append_u32(), so that we can eliminate some
failure tests that cannot ever happen. Combined with a new test
for xreq, we have 100% coverage for xreq and more coverage for
the other REQ/REP protocols.
Diffstat (limited to 'src/platform/posix')
| -rw-r--r-- | src/platform/posix/posix_atomic.c | 151 | ||||
| -rw-r--r-- | src/platform/posix/posix_impl.h | 8 |
2 files changed, 159 insertions, 0 deletions
diff --git a/src/platform/posix/posix_atomic.c b/src/platform/posix/posix_atomic.c index 0d866071..6b110712 100644 --- a/src/platform/posix/posix_atomic.c +++ b/src/platform/posix/posix_atomic.c @@ -55,6 +55,66 @@ nni_atomic_init_bool(nni_atomic_bool *v) } void +nni_atomic_init(nni_atomic_int *v) +{ + atomic_init(&v->v, 0); +} + +void +nni_atomic_add(nni_atomic_int *v, int bump) +{ + (void) atomic_fetch_add_explicit(&v->v, bump, memory_order_relaxed); +} + +void +nni_atomic_sub(nni_atomic_int *v, int bump) +{ + (void) atomic_fetch_sub_explicit(&v->v, bump, memory_order_relaxed); +} + +int +nni_atomic_get(nni_atomic_int *v) +{ + return (atomic_load(&v->v)); +} + +void +nni_atomic_set(nni_atomic_int *v, int i) +{ + return (atomic_store(&v->v, i)); +} + +int +nni_atomic_swap(nni_atomic_int *v, int i) +{ + return (atomic_exchange(&v->v, i)); +} + +void +nni_atomic_inc(nni_atomic_int *v) +{ + atomic_fetch_add(&v->v, 1); +} + +void +nni_atomic_dec(nni_atomic_int *v) +{ + atomic_fetch_sub(&v->v, 1); +} + +int +nni_atomic_dec_nv(nni_atomic_int *v) +{ + return (atomic_fetch_sub(&v->v, 1) - 1); +} + +bool +nni_atomic_cas(nni_atomic_int *v, int comp, int new) +{ + return (atomic_compare_exchange_strong(&v->v, &comp, new)); +} + +void nni_atomic_add64(nni_atomic_u64 *v, uint64_t bump) { (void) atomic_fetch_add_explicit( @@ -261,6 +321,97 @@ nni_atomic_cas64(nni_atomic_u64 *v, uint64_t comp, uint64_t new) return (result); } +void +nni_atomic_init(nni_atomic_int *v) +{ + atomic_init(&v->v, 0); +} + +void +nni_atomic_add(nni_atomic_int *v, int bump) +{ + pthread_mutex_lock(&plat_atomic_lock); + v->v += bump; + pthread_mutex_unlock(&plat_atomic_lock); +} + +void +nni_atomic_sub(nni_atomic_int *v, int bump) +{ + pthread_mutex_lock(&plat_atomic_lock); + v->v -= bump; + pthread_mutex_unlock(&plat_atomic_lock); +} + +int +nni_atomic_get(nni_atomic_int *v) +{ + int rv; + pthread_mutex_lock(&plat_atomic_lock); + rv = v->v; + pthread_mutex_unlock(&plat_atomic_lock); + return (rv); +} + +void +nni_atomic_set(nni_atomic_int *v, int i) +{ + pthread_mutex_lock(&plat_atomic_lock); + v->v = i; + pthread_mutex_unlock(&plat_atomic_lock); +} + +int +nni_atomic_swap(nni_atomic_int *v, int i) +{ + int rv; + pthread_mutex_lock(&plat_atomic_lock); + rv = v->v; + v->v = i; + pthread_mutex_unlock(&plat_atomic_lock); + return (rv); +} + +void +nni_atomic_inc(nni_atomic_int *v) +{ + pthread_mutex_lock(&plat_atomic_lock); + v->v++; + pthread_mutex_unlock(&plat_atomic_lock); +} + +void +nni_atomic_dec(nni_atomic_int *v) +{ + pthread_mutex_lock(&plat_atomic_lock); + v->v--; + pthread_mutex_unlock(&plat_atomic_lock); +} + +int +nni_atomic_dec_nv(nni_atomic_int *v) +{ + int nv; + pthread_mutex_lock(&plat_atomic_lock); + v->v--; + nv = v->v; + pthread_mutex_unlock(&plat_atomic_lock); + return (nv); +} + +bool +nni_atomic_cas(nni_atomic_int *v, int comp, int new) +{ + bool result = false; + pthread_mutex_lock(&plat_atomic_lock); + if (v->v == comp) { + v->v = new; + result = true; + } + pthread_mutex_unlock(&plat_atomic_lock); + return (result); +} + #endif #endif // NNG_PLATFORM_POSIX diff --git a/src/platform/posix/posix_impl.h b/src/platform/posix/posix_impl.h index a1cb62c5..3ec66b6b 100644 --- a/src/platform/posix/posix_impl.h +++ b/src/platform/posix/posix_impl.h @@ -83,6 +83,10 @@ struct nni_atomic_flag { atomic_flag f; }; +struct nni_atomic_int { + atomic_int v; +}; + struct nni_atomic_u64 { atomic_uint_fast64_t v; }; @@ -100,6 +104,10 @@ struct nni_atomic_bol { bool b; }; +struct nni_atomic_int { + int v; +}; + struct nni_atomic_u64 { uint64_t v; }; |
