From 6d92c73e5cdf93fe70b0646e78a250e01a8d2f65 Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Sat, 11 Jan 2020 13:15:40 -0800 Subject: 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. --- src/platform/windows/win_thread.c | 66 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 63 insertions(+), 3 deletions(-) (limited to 'src/platform/windows/win_thread.c') 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. +// Copyright 2020 Staysail Systems, Inc. // Copyright 2018 Capitar IT Group BV // // 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; -- cgit v1.2.3-70-g09d2