From 713b80f440cb414cd0b856bde0ea1b31f939777f Mon Sep 17 00:00:00 2001 From: Garrett D'Amore Date: Sat, 9 Nov 2024 23:45:21 -0800 Subject: refactor initialization/finalization Applications must now call nng_init(), but they can supply a set of parameters optionally. The code is now safe for multiple libraries to do this concurrently, meaning nng_fini no longer can race against another instance starting up. The nni_init checks on all public APIs are removed now. --- src/platform/windows/win_thread.c | 39 +++++++++++++-------------------------- 1 file changed, 13 insertions(+), 26 deletions(-) (limited to 'src/platform/windows/win_thread.c') diff --git a/src/platform/windows/win_thread.c b/src/platform/windows/win_thread.c index 67ff60b2..a6416cd8 100644 --- a/src/platform/windows/win_thread.c +++ b/src/platform/windows/win_thread.c @@ -416,8 +416,6 @@ nni_plat_thr_set_name(nni_plat_thr *thr, const char *name) } } -static LONG plat_inited = 0; - int nni_plat_ncpu(void) { @@ -428,36 +426,26 @@ nni_plat_ncpu(void) } int -nni_plat_init(int (*helper)(void)) +nni_plat_init(nng_init_params *params) { int rv = 0; static SRWLOCK lock = SRWLOCK_INIT; - if (plat_inited) { - return (0); // fast path - } - AcquireSRWLockExclusive(&lock); - if (!plat_inited) { - // Let's look up the function to set thread descriptions. - hKernel32 = LoadLibrary(TEXT("kernel32.dll")); - if (hKernel32 != NULL) { - set_thread_desc = - (pfnSetThreadDescription) GetProcAddress( - hKernel32, "SetThreadDescription"); - } - - if (((rv = nni_win_io_sysinit()) != 0) || - ((rv = nni_win_ipc_sysinit()) != 0) || - ((rv = nni_win_tcp_sysinit()) != 0) || - ((rv = nni_win_udp_sysinit()) != 0) || - ((rv = nni_win_resolv_sysinit()) != 0)) { - goto out; - } + // Let's look up the function to set thread descriptions. + hKernel32 = LoadLibrary(TEXT("kernel32.dll")); + if (hKernel32 != NULL) { + set_thread_desc = (pfnSetThreadDescription) GetProcAddress( + hKernel32, "SetThreadDescription"); + } - helper(); - plat_inited = 1; + if (((rv = nni_win_io_sysinit(params)) != 0) || + ((rv = nni_win_ipc_sysinit()) != 0) || + ((rv = nni_win_tcp_sysinit()) != 0) || + ((rv = nni_win_udp_sysinit()) != 0) || + ((rv = nni_win_resolv_sysinit(params)) != 0)) { + goto out; } out: @@ -478,7 +466,6 @@ nni_plat_fini(void) if (hKernel32 != NULL) { FreeLibrary(hKernel32); } - plat_inited = 0; } #endif -- cgit v1.2.3-70-g09d2