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/windows | |
| 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/windows')
| -rw-r--r-- | src/platform/windows/win_impl.h | 4 | ||||
| -rw-r--r-- | src/platform/windows/win_thread.c | 66 |
2 files changed, 67 insertions, 3 deletions
diff --git a/src/platform/windows/win_impl.h b/src/platform/windows/win_impl.h index befcd185..17260676 100644 --- a/src/platform/windows/win_impl.h +++ b/src/platform/windows/win_impl.h @@ -54,6 +54,10 @@ struct nni_atomic_bool { LONG v; }; +struct nni_atomic_int { + 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 0b77e078..5d98ddde 100644 --- a/src/platform/windows/win_thread.c +++ b/src/platform/windows/win_thread.c @@ -1,5 +1,5 @@ // -// Copyright 2019 Staysail Systems, Inc. <info@staysail.tech> +// Copyright 2020 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 @@ -8,13 +8,13 @@ // found online at https://opensource.org/licenses/MIT. // -// POSIX threads. +// Windows threads. #include "core/nng_impl.h" #ifdef NNG_PLATFORM_WINDOWS -// mingw does not define InterlockedAddNoFence64, use the mingw equivelent +// mingw does not define InterlockedAddNoFence64, use the mingw equivalent #if defined(__MINGW32__) || defined(__MINGW64__) #define InterlockedAddNoFence64(a, b) \ __atomic_add_fetch(a, b, __ATOMIC_RELAXED) @@ -221,6 +221,66 @@ nni_atomic_cas64(nni_atomic_u64 *v, uint64_t comp, uint64_t new) return (old == comp); } + +void +nni_atomic_add(nni_atomic_int *v, int bump) +{ + InterlockedAddNoFence(&v->v, (LONG) bump); +} + +void +nni_atomic_sub(nni_atomic_int *v, int bump) +{ + // Windows lacks a sub, so we add the negative. + InterlockedAddNoFence(&v->v, (LONG) -bump); +} + +int +nni_atomic_get(nni_atomic_int *v) +{ + + return (InterlockedExchangeAdd(&v->v, 0)); +} + +void +nni_atomic_set(nni_atomic_int *v, int i) +{ + (void) InterlockedExchange(&v->v, (LONG) i); +} + +int +nni_atomic_swap(nni_atomic_int *v, int i) +{ + return (InterlockedExchange(&v->v, (LONG) i)); +} + +void +nni_atomic_init(nni_atomic_int *v) +{ + InterlockedExchange(&v->v, 0); +} + +void +nni_atomic_inc(nni_atomic_int *v) +{ + (void) InterlockedIncrementAcquire(&v->v); +} + +int +nni_atomic_dec_nv(nni_atomic_int *v) +{ + return (InterlockedDecrementRelease(&v->v)); +} + +bool +nni_atomic_cas(nni_atomic_int *v, int comp, int new) +{ + int old; + old = InterlockedCompareExchange(&v->v, (LONG)new, (LONG)comp); + return (old == comp); +} + + static unsigned int __stdcall nni_plat_thr_main(void *arg) { nni_plat_thr *thr = arg; |
