diff options
| author | Garrett D'Amore <garrett@damore.org> | 2021-12-05 23:39:22 -0500 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2021-12-05 23:39:22 -0500 |
| commit | 21528dfe0998d056222191a4abe53d8d9f1286e3 (patch) | |
| tree | 3231c744883aa89af32e75f2d775e1cc8b63c1f8 | |
| parent | c9bbe8eb574fe10ff16cc71a23fcc9b31fb8ed04 (diff) | |
| download | nng-21528dfe0998d056222191a4abe53d8d9f1286e3.tar.gz nng-21528dfe0998d056222191a4abe53d8d9f1286e3.tar.bz2 nng-21528dfe0998d056222191a4abe53d8d9f1286e3.zip | |
Static condvar initialization.
| -rw-r--r-- | src/core/reap.c | 18 | ||||
| -rw-r--r-- | src/platform/posix/posix_impl.h | 5 | ||||
| -rw-r--r-- | src/platform/posix/posix_resolv_gai.c | 33 | ||||
| -rw-r--r-- | src/platform/windows/win_impl.h | 2 | ||||
| -rw-r--r-- | src/platform/windows/win_resolv.c | 14 |
5 files changed, 31 insertions, 41 deletions
diff --git a/src/core/reap.c b/src/core/reap.c index 8be5ee12..3f182205 100644 --- a/src/core/reap.c +++ b/src/core/reap.c @@ -1,5 +1,5 @@ // -// Copyright 2020 Staysail Systems, Inc. <info@staysail.tech> +// Copyright 2021 Staysail Systems, Inc. <info@staysail.tech> // Copyright 2017 Capitar IT Group BV <info@capitar.com> // // This software is supplied under the terms of the MIT License, a @@ -17,11 +17,11 @@ // New stuff. static nni_reap_list *reap_list = NULL; static nni_thr reap_thr; -static bool reap_exit; -static nni_mtx reap_mtx; +static bool reap_exit = false; +static nni_mtx reap_mtx = NNI_MTX_INITIALIZER; static bool reap_empty; -static nni_cv reap_work_cv; -static nni_cv reap_empty_cv; +static nni_cv reap_work_cv = NNI_CV_INITIALIZER(&reap_mtx); +static nni_cv reap_empty_cv = NNI_CV_INITIALIZER(&reap_mtx); static void reap_worker(void *unused) @@ -105,17 +105,9 @@ nni_reap_sys_init(void) { int rv; - reap_exit = false; - nni_mtx_init(&reap_mtx); - nni_cv_init(&reap_work_cv, &reap_mtx); - nni_cv_init(&reap_empty_cv, &reap_mtx); - // If this fails, we don't fail init, instead we will try to // start up at reap time. if ((rv = nni_thr_init(&reap_thr, reap_worker, NULL)) != 0) { - nni_cv_fini(&reap_work_cv); - nni_cv_fini(&reap_empty_cv); - nni_mtx_fini(&reap_mtx); return (rv); } nni_thr_run(&reap_thr); diff --git a/src/platform/posix/posix_impl.h b/src/platform/posix/posix_impl.h index 97924a45..0ee7cb38 100644 --- a/src/platform/posix/posix_impl.h +++ b/src/platform/posix/posix_impl.h @@ -73,6 +73,11 @@ struct nni_plat_cv { nni_plat_mtx *mtx; }; +// NOTE: condition variables initialized with this should *NOT* +// be used with nni_cv_until -- the clock attributes are not passed +// and the wake-up times will not be correct. +#define NNI_CV_INITIALIZER(mxp) { .mtx = mxp, .cv = PTHREAD_COND_INITIALIZER } + struct nni_plat_thr { pthread_t tid; void (*func)(void *); diff --git a/src/platform/posix/posix_resolv_gai.c b/src/platform/posix/posix_resolv_gai.c index 83974b3d..aa6547a5 100644 --- a/src/platform/posix/posix_resolv_gai.c +++ b/src/platform/posix/posix_resolv_gai.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 @@ -33,9 +33,9 @@ #define NNG_RESOLV_CONCURRENCY 4 #endif -static nni_mtx resolv_mtx; -static nni_cv resolv_cv; -static bool resolv_fini; +static nni_mtx resolv_mtx = NNI_MTX_INITIALIZER; +static nni_cv resolv_cv = NNI_CV_INITIALIZER(&resolv_mtx); +static bool resolv_fini = false; static nni_list resolv_aios; static nni_thr resolv_thrs[NNG_RESOLV_CONCURRENCY]; @@ -43,9 +43,9 @@ typedef struct resolv_item resolv_item; struct resolv_item { int family; bool passive; - char * host; - char * serv; - nni_aio * aio; + char *host; + char *serv; + nni_aio *aio; nng_sockaddr *sa; }; @@ -189,9 +189,9 @@ resolv_task(resolv_item *item) nni_mtx_lock(&resolv_mtx); if ((probe != NULL) && (item->aio != NULL)) { - struct sockaddr_in * sin; + struct sockaddr_in *sin; struct sockaddr_in6 *sin6; - nng_sockaddr * sa = item->sa; + nng_sockaddr *sa = item->sa; switch (probe->ai_addr->sa_family) { case AF_INET: @@ -301,7 +301,7 @@ resolv_worker(void *unused) nni_mtx_lock(&resolv_mtx); for (;;) { - nni_aio * aio; + nni_aio *aio; resolv_item *item; int rv; @@ -343,9 +343,9 @@ parse_ip(const char *addr, nng_sockaddr *sa, bool want_port) int rv; bool v6 = false; bool wrapped = false; - char * port; - char * host; - char * buf; + char *port; + char *host; + char *buf; size_t buf_len; if (addr == NULL) { @@ -443,11 +443,8 @@ nni_parse_ip_port(const char *addr, nni_sockaddr *sa) int nni_posix_resolv_sysinit(void) { - nni_mtx_init(&resolv_mtx); - nni_cv_init(&resolv_cv, &resolv_mtx); - nni_aio_list_init(&resolv_aios); - resolv_fini = false; + nni_aio_list_init(&resolv_aios); for (int i = 0; i < NNG_RESOLV_CONCURRENCY; i++) { int rv = nni_thr_init(&resolv_thrs[i], resolv_worker, NULL); @@ -474,8 +471,6 @@ nni_posix_resolv_sysfini(void) for (int i = 0; i < NNG_RESOLV_CONCURRENCY; i++) { nni_thr_fini(&resolv_thrs[i]); } - nni_cv_fini(&resolv_cv); - nni_mtx_fini(&resolv_mtx); } #endif // NNG_USE_POSIX_RESOLV_GAI diff --git a/src/platform/windows/win_impl.h b/src/platform/windows/win_impl.h index b3c08edf..bbd007d0 100644 --- a/src/platform/windows/win_impl.h +++ b/src/platform/windows/win_impl.h @@ -53,6 +53,8 @@ struct nni_plat_cv { PSRWLOCK srl; }; +#define NNI_CV_INITIALIZER(mxp) { .srl = mxp, .cv = CONDITION_VARIABLE_INIT } + struct nni_atomic_flag { unsigned f; }; diff --git a/src/platform/windows/win_resolv.c b/src/platform/windows/win_resolv.c index 8628719f..f855e255 100644 --- a/src/platform/windows/win_resolv.c +++ b/src/platform/windows/win_resolv.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 @@ -26,9 +26,9 @@ #define NNG_RESOLV_CONCURRENCY 4 #endif -static nni_mtx resolv_mtx; -static nni_cv resolv_cv; -static bool resolv_fini; +static nni_mtx resolv_mtx = NNI_MTX_INITIALIZER; +static nni_cv resolv_cv = NNI_CV_INITIALIZER(&resolv_mtx); +static bool resolv_fini = false; static nni_list resolv_aios; static nni_thr resolv_thrs[NNG_RESOLV_CONCURRENCY]; @@ -408,11 +408,9 @@ nni_parse_ip_port(const char *addr, nni_sockaddr *sa) int nni_win_resolv_sysinit(void) { - nni_mtx_init(&resolv_mtx); - nni_cv_init(&resolv_cv, &resolv_mtx); nni_aio_list_init(&resolv_aios); - resolv_fini = false; + for (int i = 0; i < NNG_RESOLV_CONCURRENCY; i++) { int rv = nni_thr_init(&resolv_thrs[i], resolv_worker, NULL); if (rv != 0) { @@ -437,8 +435,6 @@ nni_win_resolv_sysfini(void) for (int i = 0; i < NNG_RESOLV_CONCURRENCY; i++) { nni_thr_fini(&resolv_thrs[i]); } - nni_cv_fini(&resolv_cv); - nni_mtx_fini(&resolv_mtx); } #endif // NNG_PLATFORM_WINDOWS |
