diff options
| author | Garrett D'Amore <garrett@damore.org> | 2021-12-05 17:28:40 -0500 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2021-12-05 17:28:40 -0500 |
| commit | eee06d1e8365ea1b1aa9363a3c6445745b002324 (patch) | |
| tree | 101363a56893db15b7aeaf90d074f8c4b438f138 /src/supplemental | |
| parent | 111b241473ceeecee1f1c232d3c9879fb850361d (diff) | |
| download | nng-eee06d1e8365ea1b1aa9363a3c6445745b002324.tar.gz nng-eee06d1e8365ea1b1aa9363a3c6445745b002324.tar.bz2 nng-eee06d1e8365ea1b1aa9363a3c6445745b002324.zip | |
Provide atomic pointer support.
This is initially used for TLS to make loading the engine pointer
faster, eliminating a much more expensive lock operation.
Diffstat (limited to 'src/supplemental')
| -rw-r--r-- | src/supplemental/tls/tls_common.c | 31 |
1 files changed, 10 insertions, 21 deletions
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); |
