summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2018-05-22 20:36:31 -0700
committerGarrett D'Amore <garrett@damore.org>2018-05-22 20:36:31 -0700
commit84030980aa8a307872ac8e28f82846ce5ef40506 (patch)
tree7933a131d4e4bb72193a09b365af42e4e3e65f24
parent9b67c965d55a543b7fd916df9d4f1a5b249226b3 (diff)
downloadnanomsg-84030980aa8a307872ac8e28f82846ce5ef40506.tar.gz
fixes #935 nn_term will crash if no nn_socket is ever createdrel113
-rw-r--r--src/core/global.c22
1 files changed, 15 insertions, 7 deletions
diff --git a/src/core/global.c b/src/core/global.c
index 9fc55ea..6fbf003 100644
--- a/src/core/global.c
+++ b/src/core/global.c
@@ -168,6 +168,7 @@ struct nn_global {
int print_errors;
+ int inited;
nn_mutex_t lock;
nn_condvar_t cond;
};
@@ -306,6 +307,10 @@ void nn_term (void)
{
int i;
+ if (!self.inited) {
+ return;
+ }
+
nn_mutex_lock (&self.lock);
self.flags |= NN_CTX_FLAG_TERMING;
nn_mutex_unlock (&self.lock);
@@ -323,8 +328,18 @@ void nn_term (void)
nn_mutex_unlock (&self.lock);
}
+static void nn_lib_init(void)
+{
+ /* This function is executed once to initialize global locks. */
+ nn_mutex_init (&self.lock);
+ nn_condvar_init (&self.cond);
+ self.inited = 1;
+}
+
void nn_init (void)
{
+ nn_do_once (&once, nn_lib_init);
+
nn_mutex_lock (&self.lock);
/* Wait for any in progress term to complete. */
while (self.flags & NN_CTX_FLAG_TERMING) {
@@ -454,13 +469,6 @@ int nn_global_create_socket (int domain, int protocol)
return -EINVAL;
}
-static void nn_lib_init(void)
-{
- /* This function is executed once to initialize global locks. */
- nn_mutex_init (&self.lock);
- nn_condvar_init (&self.cond);
-}
-
int nn_socket (int domain, int protocol)
{
int rc;