diff options
| author | Garrett D'Amore <garrett@damore.org> | 2024-01-01 15:07:00 -0800 |
|---|---|---|
| committer | Garrett D'Amore <garrett@damore.org> | 2024-01-01 16:39:43 -0800 |
| commit | a9e98e546c4cf40251435b3d0e84b9ac980a9623 (patch) | |
| tree | aaed3361bf0718d5cedb932ac29ab72c0bbd35ed /src/platform/windows/win_io.c | |
| parent | 07ad78c04594ffce668892bea7b8f0f7e0ecccd2 (diff) | |
| download | nng-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/windows/win_io.c')
| -rw-r--r-- | src/platform/windows/win_io.c | 36 |
1 files changed, 22 insertions, 14 deletions
diff --git a/src/platform/windows/win_io.c b/src/platform/windows/win_io.c index b08f2e4d..1e985130 100644 --- a/src/platform/windows/win_io.c +++ b/src/platform/windows/win_io.c @@ -1,5 +1,5 @@ // -// Copyright 2023 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 @@ -89,26 +89,34 @@ nni_win_io_sysinit(void) HANDLE h; int i; int rv; - int nthr = nni_plat_ncpu() * 2; + int num_thr; + int max_thr; - // Limits on the thread count. This is fairly arbitrary. - if (nthr < 2) { - nthr = 2; - } #ifndef NNG_MAX_POLLER_THREADS #define NNG_MAX_POLLER_THREADS 8 #endif -#if NNG_MAX_POLLER_THREADS > 0 - if (nthr > NNG_MAX_POLLER_THREADS) { - nthr = NNG_MAX_POLLER_THREADS; - } +#ifndef NNG_NUM_POLLER_THREADS +#define NNG_NUM_POLLER_THREADS (nni_plat_ncpu()) #endif - if ((win_io_thrs = NNI_ALLOC_STRUCTS(win_io_thrs, nthr)) == NULL) { + max_thr = (int) nni_init_get_param( + NNG_INIT_MAX_POLLER_THREADS, NNG_MAX_POLLER_THREADS); + + num_thr = (int) nni_init_get_param( + NNG_INIT_NUM_POLLER_THREADS, NNG_NUM_POLLER_THREADS); + + if ((max_thr > 0) && (num_thr > max_thr)) { + num_thr = max_thr; + } + if (num_thr < 1) { + num_thr = 1; + } + nni_init_set_effective(NNG_INIT_NUM_POLLER_THREADS, num_thr); + if ((win_io_thrs = NNI_ALLOC_STRUCTS(win_io_thrs, num_thr)) == NULL) { return (NNG_ENOMEM); } - win_io_nthr = nthr; + win_io_nthr = num_thr; - h = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, nthr); + h = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, num_thr); if (h == NULL) { return (nni_win_error(GetLastError())); } @@ -145,7 +153,7 @@ nni_win_io_sysfini(void) nni_thr_fini(&win_io_thrs[i]); } - NNI_FREE_STRUCTS(win_io_thrs, win_io_nthr); + NNI_FREE_STRUCTS(win_io_thrs, win_io_nthr); } #endif // NNG_PLATFORM_WINDOWS |
