aboutsummaryrefslogtreecommitdiff
path: root/src/platform/posix
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2024-11-11 14:20:09 -0800
committerGarrett D'Amore <garrett@damore.org>2024-11-11 14:20:09 -0800
commit41a06cdc7e2159bb2e117f52a948ce9d2eb780ef (patch)
tree3621c9301b7df67dfbceabc1cad551dd2009fa59 /src/platform/posix
parent95fc24120509110553e03573028c34c76a53eb7f (diff)
downloadnng-41a06cdc7e2159bb2e117f52a948ce9d2eb780ef.tar.gz
nng-41a06cdc7e2159bb2e117f52a948ce9d2eb780ef.tar.bz2
nng-41a06cdc7e2159bb2e117f52a948ce9d2eb780ef.zip
Cleanup POSIX platform initialization.
This also avoids a potential leak of thread attributes. although no current platform actually seems to do so.
Diffstat (limited to 'src/platform/posix')
-rw-r--r--src/platform/posix/posix_thread.c44
1 files changed, 18 insertions, 26 deletions
diff --git a/src/platform/posix/posix_thread.c b/src/platform/posix/posix_thread.c
index 343a6ba5..5238d624 100644
--- a/src/platform/posix/posix_thread.c
+++ b/src/platform/posix/posix_thread.c
@@ -34,8 +34,8 @@
#include <sys/resource.h>
#endif
-static bool nni_plat_inited = 0;
-static bool nni_plat_forked = 0;
+static bool nni_plat_inited = false;
+static bool nni_plat_forked = false;
pthread_condattr_t nni_cvattr;
pthread_mutexattr_t nni_mxattr;
@@ -356,9 +356,7 @@ nni_plat_init(nng_init_params *params)
#if !defined(NNG_USE_GETTIMEOFDAY) && NNG_USE_CLOCKID != CLOCK_REALTIME
if (pthread_condattr_setclock(&nni_cvattr, NNG_USE_CLOCKID) != 0) {
- pthread_mutexattr_destroy(&nni_mxattr);
- pthread_condattr_destroy(&nni_cvattr);
- pthread_attr_destroy(&nni_thrattr);
+ nni_plat_fini();
return (NNG_ENOMEM);
}
#endif
@@ -369,9 +367,7 @@ nni_plat_init(nng_init_params *params)
(rl.rlim_cur != RLIM_INFINITY) &&
(rl.rlim_cur >= PTHREAD_STACK_MIN) &&
(pthread_attr_setstacksize(&nni_thrattr, rl.rlim_cur) != 0)) {
- pthread_mutexattr_destroy(&nni_mxattr);
- pthread_condattr_destroy(&nni_cvattr);
- pthread_attr_destroy(&nni_thrattr);
+ nni_plat_fini();
return (NNG_ENOMEM);
}
#endif
@@ -381,29 +377,24 @@ nni_plat_init(nng_init_params *params)
&nni_mxattr, PTHREAD_MUTEX_ERRORCHECK);
if ((rv = nni_posix_pollq_sysinit(params)) != 0) {
- pthread_mutexattr_destroy(&nni_mxattr);
- pthread_condattr_destroy(&nni_cvattr);
- pthread_attr_destroy(&nni_thrattr);
+ nni_plat_fini();
return (rv);
}
if ((rv = nni_posix_resolv_sysinit(params)) != 0) {
nni_posix_pollq_sysfini();
- pthread_mutexattr_destroy(&nni_mxattr);
- pthread_condattr_destroy(&nni_cvattr);
- pthread_attr_destroy(&nni_thrattr);
+ nni_plat_fini();
return (rv);
}
- if (pthread_atfork(NULL, NULL, nni_atfork_child) != 0) {
- nni_posix_resolv_sysfini();
- nni_posix_pollq_sysfini();
- pthread_mutexattr_destroy(&nni_mxattr);
- pthread_condattr_destroy(&nni_cvattr);
- pthread_attr_destroy(&nni_thrattr);
- return (NNG_ENOMEM);
- }
- nni_plat_inited = 1;
+ nni_plat_inited = true;
+
+ // if this fails, its not critical, but it means the user won't get the
+ // benefit of checking for incorrect use-after-fork-in-child behavior.
+ // Since that only can occur if the application developer has made a
+ // gross mistake, and the consequence of that will be a panic anyway,
+ // we just ignore this error.
+ (void) pthread_atfork(NULL, NULL, nni_atfork_child);
return (rv);
}
@@ -414,10 +405,11 @@ nni_plat_fini(void)
if (nni_plat_inited) {
nni_posix_resolv_sysfini();
nni_posix_pollq_sysfini();
- pthread_mutexattr_destroy(&nni_mxattr);
- pthread_condattr_destroy(&nni_cvattr);
- nni_plat_inited = 0;
}
+ pthread_mutexattr_destroy(&nni_mxattr);
+ pthread_condattr_destroy(&nni_cvattr);
+ pthread_attr_destroy(&nni_thrattr);
+ nni_plat_inited = false;
}
int