aboutsummaryrefslogtreecommitdiff
path: root/src/platform/posix
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2024-01-01 15:07:00 -0800
committerGarrett D'Amore <garrett@damore.org>2024-01-01 16:39:43 -0800
commita9e98e546c4cf40251435b3d0e84b9ac980a9623 (patch)
treeaaed3361bf0718d5cedb932ac29ab72c0bbd35ed /src/platform/posix
parent07ad78c04594ffce668892bea7b8f0f7e0ecccd2 (diff)
downloadnng-a9e98e546c4cf40251435b3d0e84b9ac980a9623.tar.gz
nng-a9e98e546c4cf40251435b3d0e84b9ac980a9623.tar.bz2
nng-a9e98e546c4cf40251435b3d0e84b9ac980a9623.zip
fixes #1572 nng creates too many threads
This further limits some of the thread counts, but principally it offers a new runtime facility, nng_init_set_parameter(), which can be used to set certain runtime parameters on the number of threads, provided it is called before the rest of application start up. This facility is quite intentionally "undocumented", at least for now, as we want to limit our commitment to it. Still this should be helpful for applications that need to reduce the number of threads that are created.
Diffstat (limited to 'src/platform/posix')
-rw-r--r--src/platform/posix/posix_resolv_gai.c38
1 files changed, 27 insertions, 11 deletions
diff --git a/src/platform/posix/posix_resolv_gai.c b/src/platform/posix/posix_resolv_gai.c
index c6abee5f..8eaa29f2 100644
--- a/src/platform/posix/posix_resolv_gai.c
+++ b/src/platform/posix/posix_resolv_gai.c
@@ -1,5 +1,5 @@
//
-// Copyright 2021 Staysail Systems, Inc. <info@staysail.tech>
+// Copyright 2024 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
@@ -25,14 +25,10 @@
// for it to ensure that names can be looked up concurrently. This isn't
// as elegant or scalable as a true asynchronous resolver would be, but
// it has the advantage of being fairly portable, and concurrent enough for
-// the vast, vast majority of use cases. The total thread count can be
+// the vast majority of use cases. The total thread count can be
// changed with this define. Note that some platforms may not have a
// thread-safe getaddrinfo(). In that case they should set this to 1.
-#ifndef NNG_RESOLV_CONCURRENCY
-#define NNG_RESOLV_CONCURRENCY 4
-#endif
-
#ifndef AI_NUMERICSERV
#define AI_NUMERICSERV 0
#endif
@@ -41,7 +37,8 @@ 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];
+static nni_thr *resolv_thrs;
+static int resolv_num_thr;
typedef struct resolv_item resolv_item;
struct resolv_item {
@@ -450,14 +447,30 @@ nni_posix_resolv_sysinit(void)
resolv_fini = false;
nni_aio_list_init(&resolv_aios);
- for (int i = 0; i < NNG_RESOLV_CONCURRENCY; i++) {
+#ifndef NNG_RESOLV_CONCURRENCY
+#define NNG_RESOLV_CONCURRENCY 4
+#endif
+
+ resolv_num_thr = (int) nni_init_get_param(
+ NNG_INIT_NUM_RESOLVER_THREADS, NNG_RESOLV_CONCURRENCY);
+ if (resolv_num_thr < 1) {
+ resolv_num_thr = 1;
+ }
+ // no limit on the maximum for now
+ nni_init_set_effective(NNG_INIT_NUM_RESOLVER_THREADS, resolv_num_thr);
+ resolv_thrs = NNI_ALLOC_STRUCTS(resolv_thrs, resolv_num_thr);
+ if (resolv_thrs == NULL) {
+ return (NNG_ENOMEM);
+ }
+
+ for (int i = 0; i < resolv_num_thr; i++) {
int rv = nni_thr_init(&resolv_thrs[i], resolv_worker, NULL);
if (rv != 0) {
nni_posix_resolv_sysfini();
return (rv);
}
}
- for (int i = 0; i < NNG_RESOLV_CONCURRENCY; i++) {
+ for (int i = 0; i < resolv_num_thr; i++) {
nni_thr_run(&resolv_thrs[i]);
}
@@ -472,8 +485,11 @@ nni_posix_resolv_sysfini(void)
nni_cv_wake(&resolv_cv);
nni_mtx_unlock(&resolv_mtx);
- for (int i = 0; i < NNG_RESOLV_CONCURRENCY; i++) {
- nni_thr_fini(&resolv_thrs[i]);
+ if (resolv_thrs != NULL) {
+ for (int i = 0; i < resolv_num_thr; i++) {
+ nni_thr_fini(&resolv_thrs[i]);
+ }
+ NNI_FREE_STRUCTS(resolv_thrs, resolv_num_thr);
}
}