aboutsummaryrefslogtreecommitdiff
path: root/src/platform
diff options
context:
space:
mode:
Diffstat (limited to 'src/platform')
-rw-r--r--src/platform/posix/posix_atomic.c27
-rw-r--r--src/platform/windows/win_thread.c8
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;