gstreamer_core/gst/gstsystemclock.c
changeset 8 4a7fac7dd34a
parent 0 0e761a78d257
child 30 7e817e7e631c
--- a/gstreamer_core/gst/gstsystemclock.c	Fri Mar 19 09:35:09 2010 +0200
+++ b/gstreamer_core/gst/gstsystemclock.c	Fri Apr 16 15:15:52 2010 +0300
@@ -40,8 +40,11 @@
 
 #include "gst_private.h"
 #include "gstinfo.h"
+#include "gstsystemclock.h"
+#include "gstenumtypes.h"
+#include "gstpoll.h"
 
-#include "gstsystemclock.h"
+#include <errno.h>
 
 #ifdef __SYMBIAN32__
 #include <glib_global.h>
@@ -50,12 +53,45 @@
 /* Define this to get some extra debug about jitter from each clock_wait */
 #undef WAIT_DEBUGGING
 
+struct _GstSystemClockPrivate
+{
+  GstClockType clock_type;
+  GstPoll *timer;
+  gint wakeup_count;            /* the number of entries with a pending wakeup */
+  gboolean async_wakeup;        /* if the wakeup was because of a async list change */
+};
+
+#define GST_SYSTEM_CLOCK_GET_PRIVATE(obj)  \
+   (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_SYSTEM_CLOCK, \
+        GstSystemClockPrivate))
+
+#ifdef HAVE_POSIX_TIMERS
+# ifdef HAVE_MONOTONIC_CLOCK
+#  define DEFAULT_CLOCK_TYPE GST_CLOCK_TYPE_MONOTONIC
+# else
+#  define DEFAULT_CLOCK_TYPE GST_CLOCK_TYPE_REALTIME
+# endif
+#else
+#define DEFAULT_CLOCK_TYPE GST_CLOCK_TYPE_REALTIME
+#endif
+
+enum
+{
+  PROP_0,
+  PROP_CLOCK_TYPE,
+  /* FILL ME */
+};
+
 /* the one instance of the systemclock */
 static GstClock *_the_system_clock = NULL;
 
 static void gst_system_clock_class_init (GstSystemClockClass * klass);
 static void gst_system_clock_init (GstSystemClock * clock);
 static void gst_system_clock_dispose (GObject * object);
+static void gst_system_clock_set_property (GObject * object, guint prop_id,
+    const GValue * value, GParamSpec * pspec);
+static void gst_system_clock_get_property (GObject * object, guint prop_id,
+    GValue * value, GParamSpec * pspec);
 
 static GstClockTime gst_system_clock_get_internal_time (GstClock * clock);
 static guint64 gst_system_clock_get_resolution (GstClock * clock);
@@ -70,56 +106,37 @@
     GstClockEntry * entry);
 static void gst_system_clock_async_thread (GstClock * clock);
 static gboolean gst_system_clock_start_async (GstSystemClock * clock);
+static void gst_system_clock_add_wakeup (GstSystemClock * sysclock);
 
 static GStaticMutex _gst_sysclock_mutex = G_STATIC_MUTEX_INIT;
 
 static GstClockClass *parent_class = NULL;
 
 /* static guint gst_system_clock_signals[LAST_SIGNAL] = { 0 }; */
-#ifdef __SYMBIAN32__
-EXPORT_C
-#endif
 
-
-GType
-gst_system_clock_get_type (void)
-{
-  static GType clock_type = 0;
-
-  if (G_UNLIKELY (clock_type == 0)) {
-    static const GTypeInfo clock_info = {
-      sizeof (GstSystemClockClass),
-      NULL,
-      NULL,
-      (GClassInitFunc) gst_system_clock_class_init,
-      NULL,
-      NULL,
-      sizeof (GstSystemClock),
-      0,
-      (GInstanceInitFunc) gst_system_clock_init,
-      NULL
-    };
-
-    clock_type = g_type_register_static (GST_TYPE_CLOCK, "GstSystemClock",
-        &clock_info, 0);
-  }
-  return clock_type;
-}
+G_DEFINE_TYPE (GstSystemClock, gst_system_clock, GST_TYPE_CLOCK);
 
 static void
 gst_system_clock_class_init (GstSystemClockClass * klass)
 {
   GObjectClass *gobject_class;
-  GstObjectClass *gstobject_class;
   GstClockClass *gstclock_class;
 
   gobject_class = (GObjectClass *) klass;
-  gstobject_class = (GstObjectClass *) klass;
   gstclock_class = (GstClockClass *) klass;
 
   parent_class = g_type_class_peek_parent (klass);
 
+  g_type_class_add_private (klass, sizeof (GstSystemClockPrivate));
+
   gobject_class->dispose = gst_system_clock_dispose;
+  gobject_class->set_property = gst_system_clock_set_property;
+  gobject_class->get_property = gst_system_clock_get_property;
+
+  g_object_class_install_property (gobject_class, PROP_CLOCK_TYPE,
+      g_param_spec_enum ("clock-type", "Clock type",
+          "The type of underlying clock implementation used",
+          GST_TYPE_CLOCK_TYPE, DEFAULT_CLOCK_TYPE, G_PARAM_READWRITE));
 
   gstclock_class->get_internal_time = gst_system_clock_get_internal_time;
   gstclock_class->get_resolution = gst_system_clock_get_resolution;
@@ -137,11 +154,16 @@
       GST_CLOCK_FLAG_CAN_DO_PERIODIC_SYNC |
       GST_CLOCK_FLAG_CAN_DO_PERIODIC_ASYNC);
 
+  clock->priv = GST_SYSTEM_CLOCK_GET_PRIVATE (clock);
+
+  clock->priv->clock_type = DEFAULT_CLOCK_TYPE;
+  clock->priv->timer = gst_poll_new_timer ();
+
 #if 0
   /* Uncomment this to start the async clock thread straight away */
-  GST_CLOCK_LOCK (clock);
+  GST_OBJECT_LOCK (clock);
   gst_system_clock_start_async (clock);
-  GST_CLOCK_UNLOCK (clock);
+  GST_OBJECT_UNLOCK (clock);
 #endif
 }
 
@@ -149,8 +171,7 @@
 gst_system_clock_dispose (GObject * object)
 {
   GstClock *clock = (GstClock *) object;
-
-  GstSystemClock *sysclock = GST_SYSTEM_CLOCK (clock);
+  GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
   GList *entries;
 
   /* else we have to stop the thread */
@@ -166,6 +187,7 @@
   g_list_free (clock->entries);
   clock->entries = NULL;
   GST_CLOCK_BROADCAST (clock);
+  gst_system_clock_add_wakeup (sysclock);
   GST_OBJECT_UNLOCK (clock);
 
   if (sysclock->thread)
@@ -173,6 +195,8 @@
   sysclock->thread = NULL;
   GST_CAT_DEBUG (GST_CAT_CLOCK, "joined thread");
 
+  gst_poll_free (sysclock->priv->timer);
+
   G_OBJECT_CLASS (parent_class)->dispose (object);
 
   if (_the_system_clock == clock) {
@@ -181,6 +205,40 @@
   }
 }
 
+static void
+gst_system_clock_set_property (GObject * object, guint prop_id,
+    const GValue * value, GParamSpec * pspec)
+{
+  GstSystemClock *sysclock = GST_SYSTEM_CLOCK (object);
+
+  switch (prop_id) {
+    case PROP_CLOCK_TYPE:
+      sysclock->priv->clock_type = g_value_get_enum (value);
+      GST_CAT_DEBUG (GST_CAT_CLOCK, "clock-type set to %d",
+          sysclock->priv->clock_type);
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+  }
+}
+
+static void
+gst_system_clock_get_property (GObject * object, guint prop_id, GValue * value,
+    GParamSpec * pspec)
+{
+  GstSystemClock *sysclock = GST_SYSTEM_CLOCK (object);
+
+  switch (prop_id) {
+    case PROP_CLOCK_TYPE:
+      g_value_set_enum (value, sysclock->priv->clock_type);
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+  }
+}
+
 /**
  * gst_system_clock_obtain:
  *
@@ -211,8 +269,7 @@
 
     /* we created the global clock; take ownership so
      * we can hand out instances later */
-    gst_object_ref (clock);
-    gst_object_sink (GST_OBJECT (clock));
+    gst_object_ref_sink (clock);
 
     _the_system_clock = clock;
     g_static_mutex_unlock (&_gst_sysclock_mutex);
@@ -226,6 +283,49 @@
   return clock;
 }
 
+static void
+gst_system_clock_remove_wakeup (GstSystemClock * sysclock)
+{
+  g_return_if_fail (sysclock->priv->wakeup_count > 0);
+
+  sysclock->priv->wakeup_count--;
+  if (sysclock->priv->wakeup_count == 0) {
+    /* read the control socket byte when we removed the last wakeup count */
+    GST_CAT_DEBUG (GST_CAT_CLOCK, "reading control");
+    while (!gst_poll_read_control (sysclock->priv->timer)) {
+      g_warning ("gstsystemclock: read control failed, trying again\n");
+    }
+    GST_CLOCK_BROADCAST (sysclock);
+  }
+  GST_CAT_DEBUG (GST_CAT_CLOCK, "wakeup count %d",
+      sysclock->priv->wakeup_count);
+}
+
+static void
+gst_system_clock_add_wakeup (GstSystemClock * sysclock)
+{
+  /* only write the control socket for the first wakeup */
+  if (sysclock->priv->wakeup_count == 0) {
+    GST_CAT_DEBUG (GST_CAT_CLOCK, "writing control");
+    while (!gst_poll_write_control (sysclock->priv->timer)) {
+      g_warning
+          ("gstsystemclock: write control failed in wakeup_async, trying again : %d:%s\n",
+          errno, g_strerror (errno));
+    }
+  }
+  sysclock->priv->wakeup_count++;
+  GST_CAT_DEBUG (GST_CAT_CLOCK, "wakeup count %d",
+      sysclock->priv->wakeup_count);
+}
+
+static void
+gst_system_clock_wait_wakeup (GstSystemClock * sysclock)
+{
+  while (sysclock->priv->wakeup_count > 0) {
+    GST_CLOCK_WAIT (sysclock);
+  }
+}
+
 /* this thread reads the sorted clock entries from the queue.
  *
  * It waits on each of them and fires the callback when the timeout occurs.
@@ -241,7 +341,7 @@
 static void
 gst_system_clock_async_thread (GstClock * clock)
 {
-  GstSystemClock *sysclock = GST_SYSTEM_CLOCK (clock);
+  GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
 
   GST_CAT_DEBUG (GST_CAT_CLOCK, "enter system clock thread");
   GST_OBJECT_LOCK (clock);
@@ -264,6 +364,14 @@
         goto exit;
     }
 
+    /* see if we have a pending wakeup because the order of the list
+     * changed. */
+    if (sysclock->priv->async_wakeup) {
+      GST_CAT_DEBUG (GST_CAT_CLOCK, "clear async wakeup");
+      gst_system_clock_remove_wakeup (sysclock);
+      sysclock->priv->async_wakeup = FALSE;
+    }
+
     /* pick the next entry */
     entry = clock->entries->data;
     /* if it was unscheduled, just move on to the next entry */
@@ -289,7 +397,7 @@
       {
         /* entry timed out normally, fire the callback and move to the next
          * entry */
-        GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry %p unlocked", entry);
+        GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry %p timed out", entry);
         if (entry->func) {
           /* unlock before firing the callback */
           GST_OBJECT_UNLOCK (clock);
@@ -298,6 +406,7 @@
           GST_OBJECT_LOCK (clock);
         }
         if (entry->type == GST_CLOCK_ENTRY_PERIODIC) {
+          GST_CAT_DEBUG (GST_CAT_CLOCK, "updating periodic entry %p", entry);
           /* adjust time now */
           entry->time = requested + entry->interval;
           /* and resort the list now */
@@ -306,6 +415,7 @@
           /* and restart */
           continue;
         } else {
+          GST_CAT_DEBUG (GST_CAT_CLOCK, "moving to next entry");
           goto next_entry;
         }
       }
@@ -315,6 +425,11 @@
          * was canceled. Whatever it is, pick the head entry of the list and
          * continue waiting. */
         GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry %p needs restart", entry);
+
+        /* we set the entry back to the OK state. This is needed so that the
+         * _unschedule() code can see if an entry is currently being waited
+         * on (when its state is BUSY). */
+        entry->status = GST_CLOCK_OK;
         continue;
       default:
         GST_CAT_DEBUG (GST_CAT_CLOCK,
@@ -335,21 +450,60 @@
   GST_CAT_DEBUG (GST_CAT_CLOCK, "exit system clock thread");
 }
 
+#ifdef HAVE_POSIX_TIMERS
+static inline clockid_t
+clock_type_to_posix_id (GstClockType clock_type)
+{
+#ifdef HAVE_MONOTONIC_CLOCK
+  if (clock_type == GST_CLOCK_TYPE_MONOTONIC)
+    return CLOCK_MONOTONIC;
+  else
+#endif
+    return CLOCK_REALTIME;
+}
+#endif
+
 /* MT safe */
 static GstClockTime
 gst_system_clock_get_internal_time (GstClock * clock)
 {
+#ifdef HAVE_POSIX_TIMERS
+  GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
+  clockid_t ptype;
+  struct timespec ts;
+
+  ptype = clock_type_to_posix_id (sysclock->priv->clock_type);
+
+  if (G_UNLIKELY (clock_gettime (ptype, &ts)))
+    return GST_CLOCK_TIME_NONE;
+
+  return GST_TIMESPEC_TO_TIME (ts);
+#else
   GTimeVal timeval;
 
   g_get_current_time (&timeval);
 
   return GST_TIMEVAL_TO_TIME (timeval);
+#endif
 }
 
 static guint64
 gst_system_clock_get_resolution (GstClock * clock)
 {
+#ifdef HAVE_POSIX_TIMERS
+  GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
+  clockid_t ptype;
+  struct timespec ts;
+
+  ptype = clock_type_to_posix_id (sysclock->priv->clock_type);
+
+  if (G_UNLIKELY (clock_getres (ptype, &ts)))
+    return GST_CLOCK_TIME_NONE;
+
+  return GST_TIMESPEC_TO_TIME (ts);
+#else
   return 1 * GST_USECOND;
+#endif
 }
 
 /* synchronously wait on the given GstClockEntry.
@@ -372,81 +526,115 @@
 gst_system_clock_id_wait_jitter_unlocked (GstClock * clock,
     GstClockEntry * entry, GstClockTimeDiff * jitter, gboolean restart)
 {
-  GstClockTime entryt, real, now, target;
+  GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
+  GstClockTime entryt, real, now;
   GstClockTimeDiff diff;
 
   /* need to call the overridden method because we want to sync against the time
    * of the clock, whatever the subclass uses as a clock. */
   real = GST_CLOCK_GET_CLASS (clock)->get_internal_time (clock);
+  now = gst_clock_adjust_unlocked (clock, real);
+
+  /* get the time of the entry */
   entryt = GST_CLOCK_ENTRY_TIME (entry);
 
-  now = gst_clock_adjust_unlocked (clock, real);
   if (jitter) {
     *jitter = GST_CLOCK_DIFF (entryt, now);
   }
   /* the diff of the entry with the clock is the amount of time we have to
    * wait */
   diff = entryt - now;
-  /* Our GCond implementation expects an absolute time against the system clock
-   * as a timeout value. We use our internal time to get the system time and add
-   * the expected timeout to it, this gives us the absolute time of the
-   * timeout. */
-  target = gst_system_clock_get_internal_time (clock) + diff;
 
   GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p"
-      " target %" GST_TIME_FORMAT
-      " entry %" GST_TIME_FORMAT
+      " time %" GST_TIME_FORMAT
       " now %" GST_TIME_FORMAT
       " real %" GST_TIME_FORMAT
-      " diff (entry-now) %" G_GINT64_FORMAT,
+      " diff (time-now) %" G_GINT64_FORMAT,
       entry,
-      GST_TIME_ARGS (target),
       GST_TIME_ARGS (entryt), GST_TIME_ARGS (now), GST_TIME_ARGS (real), diff);
 
   if (diff > 0) {
-    GTimeVal tv;
-
 #ifdef WAIT_DEBUGGING
-    GstClockTime result, final;
+    GstClockTime final;
 #endif
 
-    GST_TIME_TO_TIMEVAL (target, tv);
+    while (entry->status != GST_CLOCK_UNSCHEDULED) {
+      gint pollret;
+
+      /* mark the entry as busy */
+      entry->status = GST_CLOCK_BUSY;
+      GST_OBJECT_UNLOCK (clock);
+
+      /* now wait on the entry, it either times out or the fd is written. */
+      pollret = gst_poll_wait (sysclock->priv->timer, diff);
+
+      /* another thread can read the fd before we get the lock */
+      GST_OBJECT_LOCK (clock);
+      if (entry->status == GST_CLOCK_UNSCHEDULED) {
+        GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p unlocked", entry);
+        gst_system_clock_remove_wakeup (sysclock);
+      } else {
+        if (pollret != 0) {
+          /* some other id got unlocked */
+          if (!restart) {
+            /* this can happen if the entry got unlocked because of an async
+             * entry was added to the head of the async queue. */
+            GST_CAT_DEBUG (GST_CAT_CLOCK, "wakeup waiting for entry %p", entry);
+            break;
+          }
+
+          /* mark ourselves as EARLY, we release the lock and we could be
+           * unscheduled ourselves but we don't want the unscheduling thread
+           * to write on the control socket (it does that when an entry has a
+           * BUSY status). */
+          entry->status = GST_CLOCK_EARLY;
 
-    while (TRUE) {
-      /* now wait on the entry, it either times out or the cond is signaled. */
-      if (!GST_CLOCK_TIMED_WAIT (clock, &tv)) {
-        /* timeout, this is fine, we can report success now */
-        GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p unlocked after timeout", entry);
-        entry->status = GST_CLOCK_OK;
+          /* wait till all the entries got woken up */
+          gst_system_clock_wait_wakeup (sysclock);
+
+          /* we released the lock in the wait, recheck our status, we don't need
+           * to remove the wakeup count because we marked the entry as EARLY
+           * before releasing the object lock. */
+          if (entry->status == GST_CLOCK_UNSCHEDULED) {
+            GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p got unscheduled", entry);
+            break;
+          }
+
+          GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p needs to be restarted",
+              entry);
+        } else {
+          GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p unlocked after timeout",
+              entry);
+        }
+
+        /* reschedule if gst_poll_wait returned early or we have to reschedule after
+         * an unlock*/
+        real = GST_CLOCK_GET_CLASS (clock)->get_internal_time (clock);
+        now = gst_clock_adjust_unlocked (clock, real);
+        diff = entryt - now;
+
+        if (diff <= 0) {
+          /* timeout, this is fine, we can report success now */
+          entry->status = GST_CLOCK_OK;
+
+          GST_CAT_DEBUG (GST_CAT_CLOCK,
+              "entry %p finished, diff %" G_GINT64_FORMAT, entry, diff);
 
 #ifdef WAIT_DEBUGGING
-        real = GST_CLOCK_GET_CLASS (clock)->get_internal_time (clock);
-        result = gst_clock_adjust_unlocked (clock, real);
-        final = gst_system_clock_get_internal_time (clock);
-        GST_CAT_DEBUG (GST_CAT_CLOCK, "Waited for %" G_GINT64_FORMAT
-            " got %" G_GINT64_FORMAT " diff %" G_GINT64_FORMAT
-            " %g target-offset %" G_GINT64_FORMAT " %g", entryt, result,
-            result - entryt,
-            (double) (GstClockTimeDiff) (result - entryt) / GST_SECOND,
-            (final - target),
-            ((double) (GstClockTimeDiff) (final - target)) / GST_SECOND);
+          final = gst_system_clock_get_internal_time (clock);
+          GST_CAT_DEBUG (GST_CAT_CLOCK, "Waited for %" G_GINT64_FORMAT
+              " got %" G_GINT64_FORMAT " diff %" G_GINT64_FORMAT
+              " %g target-offset %" G_GINT64_FORMAT " %g", entryt, now,
+              now - entryt,
+              (double) (GstClockTimeDiff) (now - entryt) / GST_SECOND,
+              (final - target),
+              ((double) (GstClockTimeDiff) (final - target)) / GST_SECOND);
 #endif
-        break;
-      } else {
-        /* the waiting is interrupted because the GCond was signaled. This can
-         * be because this or some other entry was unscheduled. */
-        GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p unlocked with signal", entry);
-        /* if the entry is unscheduled, we can stop waiting for it, else we
-         * continue our while loop. */
-        if (entry->status == GST_CLOCK_UNSCHEDULED)
           break;
-        /* else restart if we must */
-        if (!restart)
-          break;
-
-        /* this can happen if the entry got unlocked because of an async entry
-         * was added to the head of the async queue. */
-        GST_CAT_DEBUG (GST_CAT_CLOCK, "continue waiting for entry %p", entry);
+        } else {
+          GST_CAT_DEBUG (GST_CAT_CLOCK,
+              "entry %p restart, diff %" G_GINT64_FORMAT, entry, diff);
+        }
       }
     }
   } else if (diff == 0) {
@@ -464,10 +652,20 @@
   GstClockReturn ret;
 
   GST_OBJECT_LOCK (clock);
+  if (G_UNLIKELY (entry->status == GST_CLOCK_UNSCHEDULED))
+    goto was_unscheduled;
+
   ret = gst_system_clock_id_wait_jitter_unlocked (clock, entry, jitter, TRUE);
   GST_OBJECT_UNLOCK (clock);
 
   return ret;
+
+  /* ERRORS */
+was_unscheduled:
+  {
+    GST_OBJECT_UNLOCK (clock);
+    return GST_CLOCK_UNSCHEDULED;
+  }
 }
 
 /* Start the async clock thread. Must be called with the object lock
@@ -477,12 +675,12 @@
 {
   GError *error = NULL;
 
-  if (clock->thread != NULL)
+  if (G_LIKELY (clock->thread != NULL))
     return TRUE;                /* Thread already running. Nothing to do */
 
   clock->thread = g_thread_create ((GThreadFunc) gst_system_clock_async_thread,
       clock, TRUE, &error);
-  if (error)
+  if (G_UNLIKELY (error))
     goto no_thread;
 
   /* wait for it to spin up */
@@ -494,6 +692,7 @@
 no_thread:
   {
     g_warning ("could not create async clock thread: %s", error->message);
+    g_error_free (error);
   }
   return FALSE;
 }
@@ -508,13 +707,25 @@
 static GstClockReturn
 gst_system_clock_id_wait_async (GstClock * clock, GstClockEntry * entry)
 {
+  GstSystemClock *sysclock;
+  GstClockEntry *head;
+
+  sysclock = GST_SYSTEM_CLOCK_CAST (clock);
+
   GST_CAT_DEBUG (GST_CAT_CLOCK, "adding async entry %p", entry);
 
   GST_OBJECT_LOCK (clock);
+  /* Start the clock async thread if needed */
+  if (G_UNLIKELY (!gst_system_clock_start_async (sysclock)))
+    goto thread_error;
 
-  /* Start the clock async thread if needed */
-  if (!gst_system_clock_start_async (GST_SYSTEM_CLOCK (clock)))
-    goto thread_error;
+  if (G_UNLIKELY (entry->status == GST_CLOCK_UNSCHEDULED))
+    goto was_unscheduled;
+
+  if (clock->entries)
+    head = clock->entries->data;
+  else
+    head = NULL;
 
   /* need to take a ref */
   gst_clock_id_ref ((GstClockID) entry);
@@ -526,21 +737,40 @@
    * front, else the thread is just waiting for another entry and
    * will get to this entry automatically. */
   if (clock->entries->data == entry) {
-    GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry added to head, sending signal");
-    /* this will wake up _all_ entries waiting for the clock because we have
-     * only one cond for all entries (makes allocation faster). Entries that
-     * have not timed out will have their status set to BUSY and should continue
-     * to wait. In the case of the async ones, the new head entry should be
-     * taken and waited for. */
-    GST_CLOCK_BROADCAST (clock);
+    GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry added to head");
+    if (head == NULL) {
+      /* the list was empty before, signal the cond so that the async thread can
+       * start taking a look at the queue */
+      GST_CAT_DEBUG (GST_CAT_CLOCK, "first entry, sending signal");
+      GST_CLOCK_BROADCAST (clock);
+    } else {
+      if (head->status == GST_CLOCK_BUSY) {
+        /* the async thread was waiting for an entry, unlock the wait so that it
+         * looks at the new head entry instead, we only need to do this once */
+        if (!sysclock->priv->async_wakeup) {
+          GST_CAT_DEBUG (GST_CAT_CLOCK, "wakeup async thread");
+          sysclock->priv->async_wakeup = TRUE;
+          gst_system_clock_add_wakeup (sysclock);
+        }
+      }
+    }
   }
   GST_OBJECT_UNLOCK (clock);
 
   return GST_CLOCK_OK;
 
+  /* ERRORS */
 thread_error:
-  /* Could not start the async clock thread */
-  return GST_CLOCK_ERROR;
+  {
+    /* Could not start the async clock thread */
+    GST_OBJECT_UNLOCK (clock);
+    return GST_CLOCK_ERROR;
+  }
+was_unscheduled:
+  {
+    GST_OBJECT_UNLOCK (clock);
+    return GST_CLOCK_UNSCHEDULED;
+  }
 }
 
 /* unschedule an entry. This will set the state of the entry to GST_CLOCK_UNSCHEDULED
@@ -553,14 +783,22 @@
 static void
 gst_system_clock_id_unschedule (GstClock * clock, GstClockEntry * entry)
 {
+  GstSystemClock *sysclock;
+
+  sysclock = GST_SYSTEM_CLOCK_CAST (clock);
+
   GST_CAT_DEBUG (GST_CAT_CLOCK, "unscheduling entry %p", entry);
 
   GST_OBJECT_LOCK (clock);
-  /* mark entry as unscheduled, then wake up all entries. The entries that did
-   * not timeout will be woken up but immediatly go to sleep again because their
-   * status would still be busy. */
+  if (entry->status == GST_CLOCK_BUSY) {
+    /* the entry was being busy, wake up all entries so that they recheck their
+     * status. We cannot wake up just one entry because allocating such a
+     * datastructure for each entry would be too heavy and unlocking an entry
+     * is usually done when shutting down or some other exceptional case. */
+    GST_CAT_DEBUG (GST_CAT_CLOCK, "entry was BUSY, doing wakeup");
+    gst_system_clock_add_wakeup (sysclock);
+  }
+  /* when it leaves the poll, it'll detect the unscheduled */
   entry->status = GST_CLOCK_UNSCHEDULED;
-  GST_CAT_DEBUG (GST_CAT_CLOCK, "sending signal");
-  GST_CLOCK_BROADCAST (clock);
   GST_OBJECT_UNLOCK (clock);
 }