diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/core/platform.h | 5 | ||||
| -rw-r--r-- | src/platform/posix/posix_atomic.c | 34 | ||||
| -rw-r--r-- | src/platform/posix/posix_impl.h | 10 | ||||
| -rw-r--r-- | src/platform/windows/win_impl.h | 6 | ||||
| -rw-r--r-- | src/platform/windows/win_thread.c | 12 | ||||
| -rw-r--r-- | src/supplemental/tls/tls_common.c | 31 |
6 files changed, 73 insertions, 25 deletions
diff --git a/src/core/platform.h b/src/core/platform.h index f1127c5b..89759921 100644 --- a/src/core/platform.h +++ b/src/core/platform.h @@ -232,6 +232,11 @@ extern void nni_atomic_inc(nni_atomic_int *); // true if the value was set. extern bool nni_atomic_cas(nni_atomic_int *, int, int); +// atomic pointers. We only support a few operations. +typedef struct nni_atomic_ptr nni_atomic_ptr; +extern void nni_atomic_set_ptr(nni_atomic_ptr *, void *); +extern void *nni_atomic_get_ptr(nni_atomic_ptr *); + // // Clock Support // diff --git a/src/platform/posix/posix_atomic.c b/src/platform/posix/posix_atomic.c index 57ef709a..e4da9e77 100644 --- a/src/platform/posix/posix_atomic.c +++ b/src/platform/posix/posix_atomic.c @@ -1,5 +1,5 @@ // -// Copyright 2020 Staysail Systems, Inc. <info@staysail.tech> +// Copyright 2021 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 @@ -81,7 +81,19 @@ nni_atomic_get(nni_atomic_int *v) void nni_atomic_set(nni_atomic_int *v, int i) { - return (atomic_store(&v->v, i)); + atomic_store(&v->v, i); +} + +void * +nni_atomic_get_ptr(nni_atomic_ptr *v) +{ + return ((void *) atomic_load(&v->v)); +} + +void +nni_atomic_set_ptr(nni_atomic_ptr *v, void *p) +{ + atomic_store(&v->v, (uintptr_t) p); } int @@ -363,6 +375,24 @@ nni_atomic_set(nni_atomic_int *v, int i) pthread_mutex_unlock(&plat_atomic_lock); } +void * +nni_atomic_get_ptr(nni_atomic_atomic *v) +{ + void *p; + pthread_mutex_lock(&plat_atomic_lock); + p = v->v; + pthread_mutex_unlock(&plat_atomic_lock); + return (p); +} + +void +nni_atomic_set_ptr(nni_atomic_atomic *v, void *p) +{ + pthread_mutex_lock(&plat_atomic_lock); + v->v = p; + pthread_mutex_unlock(&plat_atomic_lock); +} + int nni_atomic_swap(nni_atomic_int *v, int i) { diff --git a/src/platform/posix/posix_impl.h b/src/platform/posix/posix_impl.h index 851c80dc..234d1501 100644 --- a/src/platform/posix/posix_impl.h +++ b/src/platform/posix/posix_impl.h @@ -64,7 +64,7 @@ struct nni_rwlock { struct nni_plat_cv { pthread_cond_t cv; - nni_plat_mtx * mtx; + nni_plat_mtx *mtx; }; struct nni_plat_thr { @@ -99,6 +99,10 @@ struct nni_atomic_bool { atomic_bool v; }; +struct nni_atomic_ptr { + atomic_uintptr_t v; +}; + #else // NNG_HAVE_C11_ATOMIC struct nni_atomic_flag { bool f; @@ -116,6 +120,10 @@ struct nni_atomic_u64 { uint64_t v; }; +struct nni_atomic_ptr { + void *v; +}; + #endif #endif diff --git a/src/platform/windows/win_impl.h b/src/platform/windows/win_impl.h index de4f8e5b..608a065e 100644 --- a/src/platform/windows/win_impl.h +++ b/src/platform/windows/win_impl.h @@ -1,5 +1,5 @@ // -// Copyright 2018 Staysail Systems, Inc. <info@staysail.tech> +// Copyright 2021 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 @@ -67,6 +67,10 @@ struct nni_atomic_u64 { LONGLONG v; }; +struct nni_atomic_ptr { + LONGLONG v; +}; + // nni_win_io is used with io completion ports. This allows us to get // to a specific completion callback without requiring the poller (in the // completion port) to know anything about the event itself. diff --git a/src/platform/windows/win_thread.c b/src/platform/windows/win_thread.c index 9c7c09d3..31e783a3 100644 --- a/src/platform/windows/win_thread.c +++ b/src/platform/windows/win_thread.c @@ -223,6 +223,18 @@ nni_atomic_set64(nni_atomic_u64 *v, uint64_t u) (void) InterlockedExchange64(&v->v, (LONGLONG) u); } +void * +nni_atomic_get_ptr(nni_atomic_ptr *v) +{ + return ((void *) (InterlockedExchangeAdd64(&v->v, 0))); +} + +void +nni_atomic_set_ptr(nni_atomic_ptr *v) +{ + (void) InterlockedExchange64(&v->v, (LONGLONG) (uintptr_t) v); +} + uint64_t nni_atomic_swap64(nni_atomic_u64 *v, uint64_t u) { diff --git a/src/supplemental/tls/tls_common.c b/src/supplemental/tls/tls_common.c index 404a8cf7..3e497b0c 100644 --- a/src/supplemental/tls/tls_common.c +++ b/src/supplemental/tls/tls_common.c @@ -1,5 +1,5 @@ // -// Copyright 2020 Staysail Systems, Inc. <info@staysail.tech> +// Copyright 2021 Staysail Systems, Inc. <info@staysail.tech> // Copyright 2018 Capitar IT Group BV <info@capitar.com> // Copyright 2019 Devolutions <info@devolutions.net> // @@ -39,8 +39,7 @@ #ifdef NNG_SUPP_TLS -static const nng_tls_engine *tls_engine; -static nni_mtx tls_engine_lock; +static nni_atomic_ptr tls_engine; struct nng_tls_config { nng_tls_engine_config_ops ops; @@ -1366,9 +1365,7 @@ nng_tls_config_alloc(nng_tls_config **cfg_p, nng_tls_mode mode) return (rv); } - nni_mtx_lock(&tls_engine_lock); - eng = tls_engine; - nni_mtx_unlock(&tls_engine_lock); + eng = nni_atomic_get_ptr(&tls_engine); if (eng == NULL) { return (NNG_ENOTSUP); @@ -1424,9 +1421,8 @@ nng_tls_engine_name(void) const nng_tls_engine *eng; nni_init(); - nni_mtx_lock(&tls_engine_lock); - eng = tls_engine; - nni_mtx_unlock(&tls_engine_lock); + + eng = nni_atomic_get_ptr(&tls_engine); return (eng == NULL ? "none" : eng->name); } @@ -1437,9 +1433,8 @@ nng_tls_engine_description(void) const nng_tls_engine *eng; nni_init(); - nni_mtx_lock(&tls_engine_lock); - eng = tls_engine; - nni_mtx_unlock(&tls_engine_lock); + + eng = nni_atomic_get_ptr(&tls_engine); return (eng == NULL ? "" : eng->description); } @@ -1450,9 +1445,8 @@ nng_tls_engine_fips_mode(void) const nng_tls_engine *eng; nni_init(); - nni_mtx_lock(&tls_engine_lock); - eng = tls_engine; - nni_mtx_unlock(&tls_engine_lock); + + eng = nni_atomic_get_ptr(&tls_engine); return (eng == NULL ? false : eng->fips_mode); } @@ -1463,9 +1457,7 @@ nng_tls_engine_register(const nng_tls_engine *engine) if (engine->version != NNG_TLS_ENGINE_VERSION) { return (NNG_ENOTSUP); } - nni_mtx_lock(&tls_engine_lock); - tls_engine = engine; - nni_mtx_unlock(&tls_engine_lock); + nni_atomic_set_ptr(&tls_engine, (void *)engine); return (0); } @@ -1492,12 +1484,9 @@ int nni_tls_sys_init(void) { int rv; - nni_mtx_init(&tls_engine_lock); - tls_engine = NULL; rv = NNG_TLS_ENGINE_INIT(); if (rv != 0) { - nni_mtx_fini(&tls_engine_lock); return (rv); } return (0); |
