gst-validate-runner: Add locking for the reports list.
authorMathieu Duponchelle <mathieu.duponchelle@collabora.com>
Tue, 30 Sep 2014 07:11:58 +0000 (09:11 +0200)
committerMathieu Duponchelle <mathieu.duponchelle@opencreed.com>
Tue, 30 Sep 2014 09:42:42 +0000 (11:42 +0200)
validate/gst/validate/gst-validate-runner.c
validate/gst/validate/gst-validate-runner.h

index 88bda2b..8eec688 100644 (file)
@@ -77,6 +77,8 @@ gst_validate_runner_dispose (GObject * object)
   g_slist_free_full (runner->reports,
       (GDestroyNotify) gst_validate_report_unref);
 
+  g_mutex_clear (&runner->mutex);
+
   G_OBJECT_CLASS (parent_class)->dispose (object);
 }
 
@@ -100,6 +102,7 @@ gst_validate_runner_init (GstValidateRunner * runner)
 {
   runner->setup = FALSE;
   runner->max_printed_level = GST_VALIDATE_REPORT_LEVEL_NUM_ENTRIES;
+  g_mutex_init (&runner->mutex);
 }
 
 /**
@@ -119,7 +122,9 @@ void
 gst_validate_runner_add_report (GstValidateRunner * runner,
     GstValidateReport * report)
 {
+  GST_VALIDATE_RUNNER_LOCK (runner);
   runner->reports = g_slist_prepend (runner->reports, report);
+  GST_VALIDATE_RUNNER_UNLOCK (runner);
 
   g_signal_emit (runner, _signals[REPORT_ADDED_SIGNAL], 0, report);
 }
@@ -135,16 +140,27 @@ gst_validate_runner_add_report (GstValidateRunner * runner,
 guint
 gst_validate_runner_get_reports_count (GstValidateRunner * runner)
 {
+  guint l;
+
   g_return_val_if_fail (runner != NULL, 0);
-  return g_slist_length (runner->reports);
+
+  GST_VALIDATE_RUNNER_LOCK (runner);
+  l = g_slist_length (runner->reports);
+  GST_VALIDATE_RUNNER_UNLOCK (runner);
+
+  return l;
 }
 
 GSList *
 gst_validate_runner_get_reports (GstValidateRunner * runner)
 {
-  /* TODO should we need locking or put in the docs to always call this
-   * after pipeline ends? */
-  return g_slist_reverse (runner->reports);
+  GSList *ret;
+
+  GST_VALIDATE_RUNNER_LOCK (runner);
+  ret = g_slist_reverse (runner->reports);
+  GST_VALIDATE_RUNNER_UNLOCK (runner);
+
+  return ret;
 }
 
 /**
index 76da09b..f544a95 100644 (file)
@@ -41,6 +41,20 @@ G_BEGIN_DECLS
 #define GST_VALIDATE_RUNNER_CAST(obj)                 ((GstValidateRunner*)(obj))
 #define GST_VALIDATE_RUNNER_CLASS_CAST(klass)         ((GstValidateRunnerClass*)(klass))
 
+#define GST_VALIDATE_RUNNER_LOCK(r)                    \
+  G_STMT_START {                                       \
+  GST_LOG_OBJECT (r, "About to lock %p", &GST_VALIDATE_RUNNER_CAST(r)->mutex); \
+  (g_mutex_lock (&GST_VALIDATE_RUNNER_CAST(r)->mutex));                \
+  GST_LOG_OBJECT (r, "Acquired lock %p", &GST_VALIDATE_RUNNER_CAST(r)->mutex); \
+  } G_STMT_END
+
+#define GST_VALIDATE_RUNNER_UNLOCK(r)                  \
+  G_STMT_START {                                       \
+  GST_LOG_OBJECT (r, "About to unlock %p", &GST_VALIDATE_RUNNER_CAST(r)->mutex); \
+  (g_mutex_unlock (&GST_VALIDATE_RUNNER_CAST(r)->mutex));              \
+  GST_LOG_OBJECT (r, "Released lock %p", &GST_VALIDATE_RUNNER_CAST(r)->mutex); \
+  } G_STMT_END
+
 /* TODO hide this to be opaque? */
 /**
  * GstValidateRunner:
@@ -54,6 +68,7 @@ struct _GstValidateRunner {
 
   gboolean       setup;
   guint max_printed_level;
+  GMutex         mutex;
 
   /*< private >*/
   GSList *reports;