src/rrdd.c: Let the connection thread add itself to the list of threads.
authorFlorian Forster <octo@leeloo.home.verplant.org>
Sat, 21 Jun 2008 16:22:44 +0000 (18:22 +0200)
committerFlorian Forster <octo@leeloo.home.verplant.org>
Sat, 21 Jun 2008 16:22:44 +0000 (18:22 +0200)
This avoids an ugly race condition.

src/rrdd.c

index 6625da3..71674bc 100644 (file)
@@ -345,6 +345,28 @@ static void *connection_thread_main (void *args) /* {{{ */
   
   fd = *((int *) args);
 
+  RRDD_LOG (LOG_DEBUG, "connection_thread_main: Adding myself to "
+      "connetion_threads[]..");
+  pthread_mutex_lock (&connetion_threads_lock);
+  {
+    pthread_t *temp;
+
+    temp = (pthread_t *) realloc (connetion_threads,
+        sizeof (pthread_t) * (connetion_threads_num + 1));
+    if (temp == NULL)
+    {
+      RRDD_LOG (LOG_ERR, "connection_thread_main: realloc failed.");
+    }
+    else
+    {
+      connetion_threads = temp;
+      connetion_threads[connetion_threads_num] = pthread_self ();
+      connetion_threads_num++;
+    }
+  }
+  pthread_mutex_unlock (&connetion_threads_lock);
+  RRDD_LOG (LOG_DEBUG, "connection_thread_main: done");
+
   while (do_shutdown == 0)
   {
     struct pollfd pollfd;
@@ -533,27 +555,9 @@ static void *listen_thread_main (void *args) /* {{{ */
       continue;
     }
 
-    /* FIXME: bug: if the new thread is run first, it may run into the
-     *   assert (i < connetion_threads_num);
-     * assertion. */
-    pthread_mutex_lock (&connetion_threads_lock);
-    {
-      pthread_t *temp;
-
-      temp = (pthread_t *) realloc (connetion_threads,
-          sizeof (pthread_t) * (connetion_threads_num + 1));
-      if (temp == NULL)
-      {
-        RRDD_LOG (LOG_ERR, "listen_thread_main: realloc failed.");
-      }
-      else
-      {
-        connetion_threads = temp;
-        connetion_threads[connetion_threads_num] = tid;
-        connetion_threads_num++;
-      }
-    }
-    pthread_mutex_unlock (&connetion_threads_lock);
+    RRDD_LOG (LOG_DEBUG, "listen_thread_main: pthread_create succeeded: "
+        "tid = %lu",
+        *((unsigned long *) &tid));
   } /* while (do_shutdown == 0) */
 
   close_listen_sockets ();