Forráskód Böngészése

Fix issue #24: move initialization of thread synchronization objects befor the first call of free_context

bel 11 éve
szülő
commit
864aaf037c
1 módosított fájl, 14 hozzáadás és 8 törlés
  1. 14 8
      src/civetweb.c

+ 14 - 8
src/civetweb.c

@@ -6864,7 +6864,7 @@ struct mg_context *mg_start(const struct mg_callbacks *callbacks,
 {
     struct mg_context *ctx;
     const char *name, *value, *default_value;
-    int i;
+    int i, ok;
     int workerthreadcount;
 
 #if defined(_WIN32) && !defined(__SYMBIAN32__)
@@ -6886,6 +6886,7 @@ struct mg_context *mg_start(const struct mg_callbacks *callbacks,
 
     if (sTlsInit==0) {
         if (0 != pthread_key_create(&sTlsKey, NULL)) {
+            /* Fatal error - abort start. However, this situation should never occur in practice. */
             mg_cry(fc(ctx), "Cannot initialize thread local storage");
             mg_free(ctx);
             return NULL;
@@ -6893,6 +6894,18 @@ struct mg_context *mg_start(const struct mg_callbacks *callbacks,
         sTlsInit++;
     }
 
+    ok =  0==pthread_mutex_init(&ctx->thread_mutex, NULL);
+    ok &= 0==pthread_cond_init(&ctx->thread_cond, NULL);
+    ok &= 0==pthread_cond_init(&ctx->sq_empty, NULL);
+    ok &= 0==pthread_cond_init(&ctx->sq_full, NULL);
+    ok &= 0==pthread_mutex_init(&ctx->nonce_mutex, NULL);
+    if (!ok) {
+        /* Fatal error - abort start. However, this situation should never occur in practice. */
+        mg_cry(fc(ctx), "Cannot initialize thread synchronization objects");
+        mg_free(ctx);
+        return NULL;
+    }
+
     if (callbacks) {
         ctx->callbacks = *callbacks;
     }
@@ -6952,13 +6965,6 @@ struct mg_context *mg_start(const struct mg_callbacks *callbacks,
     (void) signal(SIGPIPE, SIG_IGN);
 #endif /* !_WIN32 && !__SYMBIAN32__ */
 
-    (void) pthread_mutex_init(&ctx->thread_mutex, NULL);
-    (void) pthread_cond_init(&ctx->thread_cond, NULL);
-    (void) pthread_cond_init(&ctx->sq_empty, NULL);
-    (void) pthread_cond_init(&ctx->sq_full, NULL);
-
-    (void) pthread_mutex_init(&ctx->nonce_mutex, NULL);
-
     workerthreadcount = atoi(ctx->config[NUM_THREADS]);
 
     if (workerthreadcount > MAX_WORKER_THREADS) {