Cleanup: Use own mutex within timer setup/teardown
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Fri, 26 Apr 2013 15:02:54 +0000 (11:02 -0400)
committerDavid Goulet <dgoulet@efficios.com>
Fri, 26 Apr 2013 15:26:50 +0000 (11:26 -0400)
More robust than relying on single-caller-thread assumptions.

Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Signed-off-by: David Goulet <dgoulet@efficios.com>
src/common/consumer-timer.c
src/common/consumer-timer.h

index 641478697904e516df1feb7e5c214b814fd38fb0..f9c41c0eb0f8f9f32def26028f743ce7df45d322 100644 (file)
 #include "consumer-timer.h"
 #include "ust-consumer/ust-consumer.h"
 
-static struct timer_signal_data timer_signal;
+static struct timer_signal_data timer_signal = {
+       .tid = 0,
+       .setup_done = 0,
+       .qs_done = 0,
+       .lock = PTHREAD_MUTEX_INITIALIZER,
+};
 
 /*
  * Set custom signal mask to current thread.
@@ -81,10 +86,59 @@ static void metadata_switch_timer(struct lttng_consumer_local_data *ctx,
        }
 }
 
+static
+void consumer_timer_signal_thread_qs(unsigned int signr)
+{
+       sigset_t pending_set;
+       int ret;
+
+       /*
+        * We need to be the only thread interacting with the thread
+        * that manages signals for teardown synchronization.
+        */
+       pthread_mutex_lock(&timer_signal.lock);
+
+       /* Ensure we don't have any signal queued for this channel. */
+       for (;;) {
+               ret = sigemptyset(&pending_set);
+               if (ret == -1) {
+                       PERROR("sigemptyset");
+               }
+               ret = sigpending(&pending_set);
+               if (ret == -1) {
+                       PERROR("sigpending");
+               }
+               if (!sigismember(&pending_set, LTTNG_CONSUMER_SIG_SWITCH)) {
+                       break;
+               }
+               caa_cpu_relax();
+       }
+
+       /*
+        * From this point, no new signal handler will be fired that would try to
+        * access "chan". However, we still need to wait for any currently
+        * executing handler to complete.
+        */
+       cmm_smp_mb();
+       CMM_STORE_SHARED(timer_signal.qs_done, 0);
+       cmm_smp_mb();
+
+       /*
+        * Kill with LTTNG_CONSUMER_SIG_TEARDOWN, so signal management thread wakes
+        * up.
+        */
+       kill(getpid(), LTTNG_CONSUMER_SIG_TEARDOWN);
+
+       while (!CMM_LOAD_SHARED(timer_signal.qs_done)) {
+               caa_cpu_relax();
+       }
+       cmm_smp_mb();
+
+       pthread_mutex_unlock(&timer_signal.lock);
+}
+
 /*
  * Set the timer for periodical metadata flush.
- * Should be called only from the recv cmd thread (single thread ensures
- * mutual exclusion).
  */
 void consumer_timer_switch_start(struct lttng_consumer_channel *channel,
                unsigned int switch_timer_interval)
@@ -122,13 +176,10 @@ void consumer_timer_switch_start(struct lttng_consumer_channel *channel,
 
 /*
  * Stop and delete timer.
- * Should be called only from the recv cmd thread (single thread ensures
- * mutual exclusion).
  */
 void consumer_timer_switch_stop(struct lttng_consumer_channel *channel)
 {
        int ret;
-       sigset_t pending_set;
 
        assert(channel);
 
@@ -137,41 +188,10 @@ void consumer_timer_switch_stop(struct lttng_consumer_channel *channel)
                PERROR("timer_delete");
        }
 
-       /* Ensure we don't have any signal queued for this channel. */
-       for (;;) {
-               ret = sigemptyset(&pending_set);
-               if (ret == -1) {
-                       PERROR("sigemptyset");
-               }
-               ret = sigpending(&pending_set);
-               if (ret == -1) {
-                       PERROR("sigpending");
-               }
-               if (!sigismember(&pending_set, LTTNG_CONSUMER_SIG_SWITCH)) {
-                       break;
-               }
-               caa_cpu_relax();
-       }
-
-       /*
-        * From this point, no new signal handler will be fired that would try to
-        * access "chan". However, we still need to wait for any currently
-        * executing handler to complete.
-        */
-       cmm_smp_mb();
-       CMM_STORE_SHARED(timer_signal.qs_done, 0);
-       cmm_smp_mb();
-
-       /*
-        * Kill with LTTNG_CONSUMER_SIG_TEARDOWN, so signal management thread wakes
-        * up.
-        */
-       kill(getpid(), LTTNG_CONSUMER_SIG_TEARDOWN);
+       consumer_timer_signal_thread_qs(LTTNG_CONSUMER_SIG_SWITCH);
 
-       while (!CMM_LOAD_SHARED(timer_signal.qs_done)) {
-               caa_cpu_relax();
-       }
-       cmm_smp_mb();
+       channel->switch_timer = 0;
+       channel->switch_timer_enabled = 0;
 }
 
 /*
index 84061587f04ddd4a06f6234860bd0147a3b62ac4..04743abf83a6d9625774953a08db931911df503e 100644 (file)
 /*
  * Handle timer teardown race wrt memory free of private data by consumer
  * signals are handled by a single thread, which permits a synchronization
- * point between handling of each signal.
+ * point between handling of each signal. Internal lock ensures mutual
+ * exclusion.
  */
 struct timer_signal_data {
        pthread_t tid;  /* thread id managing signals */
        int setup_done;
        int qs_done;
+       pthread_mutex_t lock;
 };
 
 void consumer_timer_switch_start(struct lttng_consumer_channel *channel,
This page took 0.027636 seconds and 4 git commands to generate.