diff options
| author | Garrett D'Amore <garrett@damore.org> | 2020-01-04 10:24:05 -0800 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2020-01-04 10:56:40 -0800 |
| commit | 382b4cff3abd5ccb282ba420ef1f7c7d171ec91a (patch) | |
| tree | 6860e1cceb52a7dab2763001eb27edf95a0e7246 /src/platform | |
| parent | bcc3814b58e9b198344bdaf6e7a916a354841275 (diff) | |
| download | nng-382b4cff3abd5ccb282ba420ef1f7c7d171ec91a.tar.gz nng-382b4cff3abd5ccb282ba420ef1f7c7d171ec91a.tar.bz2 nng-382b4cff3abd5ccb282ba420ef1f7c7d171ec91a.zip | |
fixes #1105 pollable can be inlined, and use atomics
This also introduces an nni_atomic_cas64 to help with lock-free designs.
Some mechanical renaming was done in some of the protocols for spelling.
Diffstat (limited to 'src/platform')
| -rw-r--r-- | src/platform/posix/posix_atomic.c | 27 | ||||
| -rw-r--r-- | src/platform/windows/win_thread.c | 8 |
2 files changed, 33 insertions, 2 deletions
diff --git a/src/platform/posix/posix_atomic.c b/src/platform/posix/posix_atomic.c index b1183865..0d866071 100644 --- a/src/platform/posix/posix_atomic.c +++ b/src/platform/posix/posix_atomic.c @@ -17,6 +17,7 @@ #ifdef NNG_HAVE_STDATOMIC #include <stdatomic.h> + bool nni_atomic_flag_test_and_set(nni_atomic_flag *f) { @@ -45,7 +46,6 @@ bool nni_atomic_swap_bool(nni_atomic_bool *v, bool b) { return (atomic_exchange(&v->v, b)); - } void @@ -108,6 +108,16 @@ nni_atomic_dec64_nv(nni_atomic_u64 *v) return (ov - 1); } +bool +nni_atomic_cas64(nni_atomic_u64 *v, uint64_t comp, uint64_t new) +{ + // It's possible that uint_fast64_t is not the same type underneath + // as uint64_t. (Would be unusual!) + uint_fast64_t cv = (uint_fast64_t) comp; + uint_fast64_t nv = (uint_fast64_t) new; + return (atomic_compare_exchange_strong(&v->v, &cv, nv)); +} + #else #include <pthread.h> @@ -156,7 +166,7 @@ nni_atomic_swap_bool(nni_atomic_bool *b, bool n) { bool v; pthread_mutex_lock(&plat_atomic_lock); - v = b->b; + v = b->b; b->b = n; pthread_mutex_unlock(&plat_atomic_lock); return (v); @@ -238,6 +248,19 @@ nni_atomic_dec64_nv(nni_atomic_u64 *v) return (nv); } +bool +nni_atomic_cas64(nni_atomic_u64 *v, uint64_t comp, uint64_t 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/windows/win_thread.c b/src/platform/windows/win_thread.c index 70ba73cb..0b77e078 100644 --- a/src/platform/windows/win_thread.c +++ b/src/platform/windows/win_thread.c @@ -213,6 +213,14 @@ nni_atomic_dec64_nv(nni_atomic_u64 *v) #endif } +bool +nni_atomic_cas64(nni_atomic_u64 *v, uint64_t comp, uint64_t new) +{ + uint64_t old; + old = InterlockedCompareExchange64(&v->v, (LONG64)new, (LONG64)comp); + return (old == comp); +} + static unsigned int __stdcall nni_plat_thr_main(void *arg) { nni_plat_thr *thr = arg; |
