harness: improve _wait_for_clock_id_waits performance
authorHavard Graff <havard.graff@gmail.com>
Tue, 6 Nov 2018 10:45:45 +0000 (11:45 +0100)
committerHavard Graff <havard.graff@gmail.com>
Tue, 6 Nov 2018 10:46:06 +0000 (11:46 +0100)
By moving the functionality down to the testclock, the implementation
no longer needs to poll the waits, but rather wait properly for
them to be added.

The performance-hit here would be that by polling the test-clock
regularly, you would create contention on the testclock-lock, making code
using the testclock (gst_clock_id_wait) fighting for the lock.

libs/gst/check/gstharness.c
libs/gst/check/gsttestclock.c
libs/gst/check/gsttestclock.h

index 893d772..c0f4467 100644 (file)
@@ -1364,30 +1364,15 @@ gst_harness_set_time (GstHarness * h, GstClockTime time)
  * MT safe.
  *
  * Returns: a @gboolean %TRUE if the waits have been registered, %FALSE if not.
- * (Could be that it timed out waiting or that more waits then waits was found)
+ * (Could be that it timed out waiting or that more waits than waits was found)
  *
  * Since: 1.6
  */
 gboolean
 gst_harness_wait_for_clock_id_waits (GstHarness * h, guint waits, guint timeout)
 {
-  GstTestClock *testclock = h->priv->testclock;
-  gint64 start_time;
-  gboolean ret;
-
-  start_time = g_get_monotonic_time ();
-  while (gst_test_clock_peek_id_count (testclock) < waits) {
-    gint64 time_spent;
-
-    g_usleep (G_USEC_PER_SEC / 1000);
-    time_spent = g_get_monotonic_time () - start_time;
-    if ((time_spent / G_USEC_PER_SEC) > timeout)
-      break;
-  }
-
-  ret = (waits == gst_test_clock_peek_id_count (testclock));
-
-  return ret;
+  return gst_test_clock_timed_wait_for_multiple_pending_ids (h->priv->testclock,
+      waits, timeout * 1000, NULL);
 }
 
 /**
index 13258c3..e25893d 100644 (file)
@@ -1035,6 +1035,55 @@ gst_test_clock_wait_for_multiple_pending_ids (GstTestClock * test_clock,
 }
 
 /**
+ * gst_test_clock_timed_wait_for_multiple_pending_ids:
+ * @test_clock: #GstTestClock for which to await having enough pending clock
+ * @count: the number of pending clock notifications to wait for
+ * @timeout_ms: the timeout in milliseconds
+ * @pending_list: (out) (element-type Gst.ClockID) (transfer full) (allow-none): Address
+ *     of a #GList pointer variable to store the list of pending #GstClockIDs
+ *     that expired, or %NULL
+ *
+ * Blocks until at least @count clock notifications have been requested from
+ * @test_clock, or the timeout expires.
+ *
+ * MT safe.
+ *
+ * Returns: a @gboolean %TRUE if the waits have been registered, %FALSE if not.
+ * (Could be that it timed out waiting or that more waits than waits was found)
+ *
+ * Since: 1.16
+ */
+gboolean
+gst_test_clock_timed_wait_for_multiple_pending_ids (GstTestClock * test_clock,
+    guint count, guint timeout_ms, GList ** pending_list)
+{
+  GstTestClockPrivate *priv;
+  gint64 timeout = g_get_monotonic_time () +
+      timeout_ms * (G_USEC_PER_SEC / 1000);
+  gboolean ret;
+
+  g_return_val_if_fail (GST_IS_TEST_CLOCK (test_clock), FALSE);
+  priv = GST_TEST_CLOCK_GET_PRIVATE (test_clock);
+
+  GST_OBJECT_LOCK (test_clock);
+
+  while (g_list_length (priv->entry_contexts) < count &&
+      g_get_monotonic_time () < timeout) {
+    g_cond_wait_until (&priv->entry_added_cond,
+        GST_OBJECT_GET_LOCK (test_clock), timeout);
+  }
+
+  if (pending_list)
+    *pending_list = gst_test_clock_get_pending_id_list_unlocked (test_clock);
+
+  ret = (g_list_length (priv->entry_contexts) == count);
+
+  GST_OBJECT_UNLOCK (test_clock);
+
+  return ret;
+}
+
+/**
  * gst_test_clock_process_id_list:
  * @test_clock: #GstTestClock for which to process the pending IDs
  * @pending_list: (element-type Gst.ClockID) (transfer none) (allow-none): List
index ee1afd0..3016aea 100644 (file)
@@ -122,6 +122,12 @@ void          gst_test_clock_wait_for_multiple_pending_ids (GstTestClock * test_
                                                             GList       ** pending_list);
 
 GST_CHECK_API
+gboolean      gst_test_clock_timed_wait_for_multiple_pending_ids (GstTestClock * test_clock,
+                                                                  guint          count,
+                                                                  guint          timeout_ms,
+                                                                  GList       ** pending_list);
+
+GST_CHECK_API
 guint         gst_test_clock_process_id_list (GstTestClock * test_clock,
                                               const GList  * pending_list);