2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2004 Wim Taymans <wim@fluendo.com>
5 * gstsystemclock.c: Default clock, uses the system clock
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
24 * SECTION:gstsystemclock
25 * @short_description: Default clock that uses the current system time
26 * @see_also: #GstClock
28 * The GStreamer core provides a GstSystemClock based on the system time.
29 * Asynchronous callbacks are scheduled from an internal thread.
31 * Clock implementors are encouraged to subclass this systemclock as it
32 * implements the async notification.
34 * Subclasses can however override all of the important methods for sync and
35 * async notifications to implement their own callback methods or blocking
38 * Last reviewed on 2006-03-08 (0.10.4)
41 #include "gst_private.h"
43 #include "gstsystemclock.h"
44 #include "gstenumtypes.h"
47 #include "glib-compat-private.h"
52 # define WIN32_LEAN_AND_MEAN /* prevents from including too many things */
53 # include <windows.h> /* QueryPerformance* stuff */
54 # undef WIN32_LEAN_AND_MEAN
55 # define EWOULDBLOCK EAGAIN /* This is just to placate gcc */
56 #endif /* G_OS_WIN32 */
58 #define GET_ENTRY_STATUS(e) ((GstClockReturn) g_atomic_int_get(&GST_CLOCK_ENTRY_STATUS(e)))
59 #define SET_ENTRY_STATUS(e,val) (g_atomic_int_set(&GST_CLOCK_ENTRY_STATUS(e),(val)))
60 #define CAS_ENTRY_STATUS(e,old,val) (g_atomic_int_compare_and_exchange(\
61 (&GST_CLOCK_ENTRY_STATUS(e)), (old), (val)))
63 /* Define this to get some extra debug about jitter from each clock_wait */
66 #define GST_SYSTEM_CLOCK_GET_COND(clock) (&GST_SYSTEM_CLOCK_CAST(clock)->priv->entries_changed)
67 #define GST_SYSTEM_CLOCK_WAIT(clock) g_cond_wait(GST_SYSTEM_CLOCK_GET_COND(clock),GST_OBJECT_GET_LOCK(clock))
68 #define GST_SYSTEM_CLOCK_TIMED_WAIT(clock,tv) g_cond_timed_wait(GST_SYSTEM_CLOCK_GET_COND(clock),GST_OBJECT_GET_LOCK(clock),tv)
69 #define GST_SYSTEM_CLOCK_BROADCAST(clock) g_cond_broadcast(GST_SYSTEM_CLOCK_GET_COND(clock))
71 struct _GstSystemClockPrivate
73 GThread *thread; /* thread for async notify */
77 GCond entries_changed;
79 GstClockType clock_type;
81 gint wakeup_count; /* the number of entries with a pending wakeup */
82 gboolean async_wakeup; /* if the wakeup was because of a async list change */
86 LARGE_INTEGER frequency;
87 #endif /* G_OS_WIN32 */
90 #define GST_SYSTEM_CLOCK_GET_PRIVATE(obj) \
91 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_SYSTEM_CLOCK, \
92 GstSystemClockPrivate))
94 #ifdef HAVE_POSIX_TIMERS
95 # ifdef HAVE_MONOTONIC_CLOCK
96 # define DEFAULT_CLOCK_TYPE GST_CLOCK_TYPE_MONOTONIC
98 # define DEFAULT_CLOCK_TYPE GST_CLOCK_TYPE_REALTIME
101 #define DEFAULT_CLOCK_TYPE GST_CLOCK_TYPE_REALTIME
111 /* the one instance of the systemclock */
112 static GstClock *_the_system_clock = NULL;
114 static void gst_system_clock_dispose (GObject * object);
115 static void gst_system_clock_set_property (GObject * object, guint prop_id,
116 const GValue * value, GParamSpec * pspec);
117 static void gst_system_clock_get_property (GObject * object, guint prop_id,
118 GValue * value, GParamSpec * pspec);
120 static GstClockTime gst_system_clock_get_internal_time (GstClock * clock);
121 static guint64 gst_system_clock_get_resolution (GstClock * clock);
122 static GstClockReturn gst_system_clock_id_wait_jitter (GstClock * clock,
123 GstClockEntry * entry, GstClockTimeDiff * jitter);
124 static GstClockReturn gst_system_clock_id_wait_jitter_unlocked
125 (GstClock * clock, GstClockEntry * entry, GstClockTimeDiff * jitter,
127 static GstClockReturn gst_system_clock_id_wait_async (GstClock * clock,
128 GstClockEntry * entry);
129 static void gst_system_clock_id_unschedule (GstClock * clock,
130 GstClockEntry * entry);
131 static void gst_system_clock_async_thread (GstClock * clock);
132 static gboolean gst_system_clock_start_async (GstSystemClock * clock);
133 static void gst_system_clock_add_wakeup (GstSystemClock * sysclock);
135 static GMutex _gst_sysclock_mutex;
137 /* static guint gst_system_clock_signals[LAST_SIGNAL] = { 0 }; */
139 #define gst_system_clock_parent_class parent_class
140 G_DEFINE_TYPE (GstSystemClock, gst_system_clock, GST_TYPE_CLOCK);
143 gst_system_clock_class_init (GstSystemClockClass * klass)
145 GObjectClass *gobject_class;
146 GstClockClass *gstclock_class;
148 gobject_class = (GObjectClass *) klass;
149 gstclock_class = (GstClockClass *) klass;
151 g_type_class_add_private (klass, sizeof (GstSystemClockPrivate));
153 gobject_class->dispose = gst_system_clock_dispose;
154 gobject_class->set_property = gst_system_clock_set_property;
155 gobject_class->get_property = gst_system_clock_get_property;
157 g_object_class_install_property (gobject_class, PROP_CLOCK_TYPE,
158 g_param_spec_enum ("clock-type", "Clock type",
159 "The type of underlying clock implementation used",
160 GST_TYPE_CLOCK_TYPE, DEFAULT_CLOCK_TYPE,
161 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
163 gstclock_class->get_internal_time = gst_system_clock_get_internal_time;
164 gstclock_class->get_resolution = gst_system_clock_get_resolution;
165 gstclock_class->wait = gst_system_clock_id_wait_jitter;
166 gstclock_class->wait_async = gst_system_clock_id_wait_async;
167 gstclock_class->unschedule = gst_system_clock_id_unschedule;
171 gst_system_clock_init (GstSystemClock * clock)
173 GstSystemClockPrivate *priv;
175 GST_OBJECT_FLAG_SET (clock,
176 GST_CLOCK_FLAG_CAN_DO_SINGLE_SYNC |
177 GST_CLOCK_FLAG_CAN_DO_SINGLE_ASYNC |
178 GST_CLOCK_FLAG_CAN_DO_PERIODIC_SYNC |
179 GST_CLOCK_FLAG_CAN_DO_PERIODIC_ASYNC);
181 clock->priv = priv = GST_SYSTEM_CLOCK_GET_PRIVATE (clock);
183 priv->clock_type = DEFAULT_CLOCK_TYPE;
184 priv->timer = gst_poll_new_timer ();
186 priv->entries = NULL;
187 g_cond_init (&priv->entries_changed);
190 QueryPerformanceFrequency (&priv->frequency);
191 /* can be 0 if the hardware does not have hardware support */
192 if (priv->frequency.QuadPart != 0)
193 /* we take a base time so that time starts from 0 to ease debugging */
194 QueryPerformanceCounter (&priv->start);
195 #endif /* G_OS_WIN32 */
198 /* Uncomment this to start the async clock thread straight away */
199 GST_OBJECT_LOCK (clock);
200 gst_system_clock_start_async (clock);
201 GST_OBJECT_UNLOCK (clock);
206 gst_system_clock_dispose (GObject * object)
208 GstClock *clock = (GstClock *) object;
209 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
210 GstSystemClockPrivate *priv = sysclock->priv;
213 /* else we have to stop the thread */
214 GST_OBJECT_LOCK (clock);
215 priv->stopping = TRUE;
216 /* unschedule all entries */
217 for (entries = priv->entries; entries; entries = g_list_next (entries)) {
218 GstClockEntry *entry = (GstClockEntry *) entries->data;
220 GST_CAT_DEBUG (GST_CAT_CLOCK, "unscheduling entry %p", entry);
221 SET_ENTRY_STATUS (entry, GST_CLOCK_UNSCHEDULED);
223 GST_SYSTEM_CLOCK_BROADCAST (clock);
224 gst_system_clock_add_wakeup (sysclock);
225 GST_OBJECT_UNLOCK (clock);
228 g_thread_join (priv->thread);
230 GST_CAT_DEBUG (GST_CAT_CLOCK, "joined thread");
232 g_list_foreach (priv->entries, (GFunc) gst_clock_id_unref, NULL);
233 g_list_free (priv->entries);
234 priv->entries = NULL;
236 gst_poll_free (priv->timer);
237 g_cond_clear (&priv->entries_changed);
239 G_OBJECT_CLASS (parent_class)->dispose (object);
241 if (_the_system_clock == clock) {
242 _the_system_clock = NULL;
243 GST_CAT_DEBUG (GST_CAT_CLOCK, "disposed system clock");
248 gst_system_clock_set_property (GObject * object, guint prop_id,
249 const GValue * value, GParamSpec * pspec)
251 GstSystemClock *sysclock = GST_SYSTEM_CLOCK (object);
254 case PROP_CLOCK_TYPE:
255 sysclock->priv->clock_type = (GstClockType) g_value_get_enum (value);
256 GST_CAT_DEBUG (GST_CAT_CLOCK, "clock-type set to %d",
257 sysclock->priv->clock_type);
260 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
266 gst_system_clock_get_property (GObject * object, guint prop_id, GValue * value,
269 GstSystemClock *sysclock = GST_SYSTEM_CLOCK (object);
272 case PROP_CLOCK_TYPE:
273 g_value_set_enum (value, sysclock->priv->clock_type);
276 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
282 * gst_system_clock_obtain:
284 * Get a handle to the default system clock. The refcount of the
285 * clock will be increased so you need to unref the clock after
288 * Returns: (transfer full): the default clock.
293 gst_system_clock_obtain (void)
297 g_mutex_lock (&_gst_sysclock_mutex);
298 clock = _the_system_clock;
301 GST_CAT_DEBUG (GST_CAT_CLOCK, "creating new static system clock");
302 clock = g_object_new (GST_TYPE_SYSTEM_CLOCK,
303 "name", "GstSystemClock", NULL);
305 /* we created the global clock; take ownership so
306 * we can hand out instances later */
307 gst_object_ref_sink (clock);
309 _the_system_clock = clock;
310 g_mutex_unlock (&_gst_sysclock_mutex);
312 g_mutex_unlock (&_gst_sysclock_mutex);
313 GST_CAT_DEBUG (GST_CAT_CLOCK, "returning static system clock");
316 /* we ref it since we are a clock factory. */
317 gst_object_ref (clock);
322 gst_system_clock_remove_wakeup (GstSystemClock * sysclock)
324 g_return_if_fail (sysclock->priv->wakeup_count > 0);
326 sysclock->priv->wakeup_count--;
327 if (sysclock->priv->wakeup_count == 0) {
328 /* read the control socket byte when we removed the last wakeup count */
329 GST_CAT_DEBUG (GST_CAT_CLOCK, "reading control");
330 while (!gst_poll_read_control (sysclock->priv->timer)) {
331 g_warning ("gstsystemclock: read control failed, trying again\n");
333 GST_SYSTEM_CLOCK_BROADCAST (sysclock);
335 GST_CAT_DEBUG (GST_CAT_CLOCK, "wakeup count %d",
336 sysclock->priv->wakeup_count);
340 gst_system_clock_add_wakeup (GstSystemClock * sysclock)
342 /* only write the control socket for the first wakeup */
343 if (sysclock->priv->wakeup_count == 0) {
344 GST_CAT_DEBUG (GST_CAT_CLOCK, "writing control");
345 while (!gst_poll_write_control (sysclock->priv->timer)) {
346 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {
348 ("gstsystemclock: write control failed in wakeup_async, trying again: %d:%s\n",
349 errno, g_strerror (errno));
352 ("gstsystemclock: write control failed in wakeup_async: %d:%s\n",
353 errno, g_strerror (errno));
358 sysclock->priv->wakeup_count++;
359 GST_CAT_DEBUG (GST_CAT_CLOCK, "wakeup count %d",
360 sysclock->priv->wakeup_count);
364 gst_system_clock_wait_wakeup (GstSystemClock * sysclock)
366 while (sysclock->priv->wakeup_count > 0) {
367 GST_SYSTEM_CLOCK_WAIT (sysclock);
371 /* this thread reads the sorted clock entries from the queue.
373 * It waits on each of them and fires the callback when the timeout occurs.
375 * When an entry in the queue was canceled before we wait for it, it is
378 * When waiting for an entry, it can become canceled, in that case we don't
379 * call the callback but move to the next item in the queue.
384 gst_system_clock_async_thread (GstClock * clock)
386 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
387 GstSystemClockPrivate *priv = sysclock->priv;
389 GST_CAT_DEBUG (GST_CAT_CLOCK, "enter system clock thread");
390 GST_OBJECT_LOCK (clock);
392 GST_SYSTEM_CLOCK_BROADCAST (clock);
393 /* now enter our (almost) infinite loop */
394 while (!priv->stopping) {
395 GstClockEntry *entry;
396 GstClockTime requested;
399 /* check if something to be done */
400 while (priv->entries == NULL) {
401 GST_CAT_DEBUG (GST_CAT_CLOCK, "no clock entries, waiting..");
402 /* wait for work to do */
403 GST_SYSTEM_CLOCK_WAIT (clock);
404 GST_CAT_DEBUG (GST_CAT_CLOCK, "got signal");
405 /* clock was stopping, exit */
410 /* see if we have a pending wakeup because the order of the list
412 if (priv->async_wakeup) {
413 GST_CAT_DEBUG (GST_CAT_CLOCK, "clear async wakeup");
414 gst_system_clock_remove_wakeup (sysclock);
415 priv->async_wakeup = FALSE;
418 /* pick the next entry */
419 entry = priv->entries->data;
420 GST_OBJECT_UNLOCK (clock);
422 requested = entry->time;
424 /* now wait for the entry, we already hold the lock */
426 gst_system_clock_id_wait_jitter_unlocked (clock, (GstClockID) entry,
429 GST_OBJECT_LOCK (clock);
432 case GST_CLOCK_UNSCHEDULED:
433 /* entry was unscheduled, move to the next */
434 GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry %p unscheduled", entry);
437 case GST_CLOCK_EARLY:
439 /* entry timed out normally, fire the callback and move to the next
441 GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry %p timed out", entry);
443 /* unlock before firing the callback */
444 GST_OBJECT_UNLOCK (clock);
445 entry->func (clock, entry->time, (GstClockID) entry,
447 GST_OBJECT_LOCK (clock);
449 if (entry->type == GST_CLOCK_ENTRY_PERIODIC) {
450 GST_CAT_DEBUG (GST_CAT_CLOCK, "updating periodic entry %p", entry);
451 /* adjust time now */
452 entry->time = requested + entry->interval;
453 /* and resort the list now */
455 g_list_sort (priv->entries, gst_clock_id_compare_func);
459 GST_CAT_DEBUG (GST_CAT_CLOCK, "moving to next entry");
464 /* somebody unlocked the entry but is was not canceled, This means that
465 * either a new entry was added in front of the queue or some other entry
466 * was canceled. Whatever it is, pick the head entry of the list and
467 * continue waiting. */
468 GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry %p needs restart", entry);
470 /* we set the entry back to the OK state. This is needed so that the
471 * _unschedule() code can see if an entry is currently being waited
472 * on (when its state is BUSY). */
473 SET_ENTRY_STATUS (entry, GST_CLOCK_OK);
476 GST_CAT_DEBUG (GST_CAT_CLOCK,
477 "strange result %d waiting for %p, skipping", res, entry);
478 g_warning ("%s: strange result %d waiting for %p, skipping",
479 GST_OBJECT_NAME (clock), res, entry);
483 /* we remove the current entry and unref it */
484 priv->entries = g_list_remove (priv->entries, entry);
485 gst_clock_id_unref ((GstClockID) entry);
489 GST_SYSTEM_CLOCK_BROADCAST (clock);
490 GST_OBJECT_UNLOCK (clock);
491 GST_CAT_DEBUG (GST_CAT_CLOCK, "exit system clock thread");
494 #ifdef HAVE_POSIX_TIMERS
495 static inline clockid_t
496 clock_type_to_posix_id (GstClockType clock_type)
498 #ifdef HAVE_MONOTONIC_CLOCK
499 if (clock_type == GST_CLOCK_TYPE_MONOTONIC)
500 return CLOCK_MONOTONIC;
503 return CLOCK_REALTIME;
509 gst_system_clock_get_internal_time (GstClock * clock)
512 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
514 if (sysclock->priv->frequency.QuadPart != 0) {
517 /* we prefer the highly accurate performance counters on windows */
518 QueryPerformanceCounter (&now);
520 return gst_util_uint64_scale (now.QuadPart - sysclock->priv->start.QuadPart,
521 GST_SECOND, sysclock->priv->frequency.QuadPart);
523 #endif /* G_OS_WIN32 */
524 #if !defined HAVE_POSIX_TIMERS
528 g_get_current_time (&timeval);
530 return GST_TIMEVAL_TO_TIME (timeval);
534 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
538 ptype = clock_type_to_posix_id (sysclock->priv->clock_type);
540 if (G_UNLIKELY (clock_gettime (ptype, &ts)))
541 return GST_CLOCK_TIME_NONE;
543 return GST_TIMESPEC_TO_TIME (ts);
549 gst_system_clock_get_resolution (GstClock * clock)
552 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
554 if (sysclock->priv->frequency.QuadPart != 0) {
555 return GST_SECOND / sysclock->priv->frequency.QuadPart;
557 #endif /* G_OS_WIN32 */
558 #ifdef HAVE_POSIX_TIMERS
560 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
564 ptype = clock_type_to_posix_id (sysclock->priv->clock_type);
566 if (G_UNLIKELY (clock_getres (ptype, &ts)))
567 return GST_CLOCK_TIME_NONE;
569 return GST_TIMESPEC_TO_TIME (ts);
573 return 1 * GST_USECOND;
578 /* synchronously wait on the given GstClockEntry.
580 * We do this by blocking on the global GstPoll timer with
581 * the requested timeout. This allows us to unblock the
582 * entry by writing on the control fd.
584 * Note that writing the global GstPoll unlocks all waiting entries. So
585 * we need to check if an unlocked entry has changed when it unlocks.
587 * Entries that arrive too late are simply not waited on and a
588 * GST_CLOCK_EARLY result is returned.
592 static GstClockReturn
593 gst_system_clock_id_wait_jitter_unlocked (GstClock * clock,
594 GstClockEntry * entry, GstClockTimeDiff * jitter, gboolean restart)
596 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
597 GstClockTime entryt, now;
598 GstClockTimeDiff diff;
599 GstClockReturn status;
601 if (G_UNLIKELY (GET_ENTRY_STATUS (entry) == GST_CLOCK_UNSCHEDULED))
602 return GST_CLOCK_UNSCHEDULED;
604 /* need to call the overridden method because we want to sync against the time
605 * of the clock, whatever the subclass uses as a clock. */
606 now = gst_clock_get_time (clock);
608 /* get the time of the entry */
609 entryt = GST_CLOCK_ENTRY_TIME (entry);
611 /* the diff of the entry with the clock is the amount of time we have to
613 diff = GST_CLOCK_DIFF (now, entryt);
614 if (G_LIKELY (jitter))
617 GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p"
618 " time %" GST_TIME_FORMAT
619 " now %" GST_TIME_FORMAT
620 " diff (time-now) %" G_GINT64_FORMAT,
621 entry, GST_TIME_ARGS (entryt), GST_TIME_ARGS (now), diff);
623 if (G_LIKELY (diff > 0)) {
624 #ifdef WAIT_DEBUGGING
632 status = GET_ENTRY_STATUS (entry);
634 /* stop when we are unscheduled */
635 if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED))
638 /* mark the entry as busy but watch out for intermediate unscheduled
640 } while (G_UNLIKELY (!CAS_ENTRY_STATUS (entry, status, GST_CLOCK_BUSY)));
642 /* now wait on the entry, it either times out or the fd is written. The
643 * status of the entry is only BUSY around the poll. */
644 pollret = gst_poll_wait (sysclock->priv->timer, diff);
646 /* get the new status, mark as DONE. We do this so that the unschedule
647 * function knows when we left the poll and doesn't need to wakeup the
650 status = GET_ENTRY_STATUS (entry);
651 /* we were unscheduled, exit immediately */
652 if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED))
654 } while (G_UNLIKELY (!CAS_ENTRY_STATUS (entry, status, GST_CLOCK_DONE)));
656 GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p unlocked, status %d, ret %d",
657 entry, status, pollret);
659 if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED)) {
660 /* try to clean up The unschedule function managed to set the status to
661 * unscheduled. We now take the lock and mark the entry as unscheduled.
662 * This makes sure that the unschedule function doesn't perform a
663 * wakeup anymore. If the unschedule function has a change to perform
664 * the wakeup before us, we clean up here */
665 GST_OBJECT_LOCK (sysclock);
666 entry->unscheduled = TRUE;
667 if (entry->woken_up) {
668 gst_system_clock_remove_wakeup (sysclock);
670 GST_OBJECT_UNLOCK (sysclock);
673 if (G_UNLIKELY (pollret != 0)) {
674 /* some other id got unlocked */
676 /* this can happen if the entry got unlocked because of an async
677 * entry was added to the head of the async queue. */
678 GST_CAT_DEBUG (GST_CAT_CLOCK, "wakeup waiting for entry %p", entry);
682 /* wait till all the entries got woken up */
683 GST_OBJECT_LOCK (sysclock);
684 gst_system_clock_wait_wakeup (sysclock);
685 GST_OBJECT_UNLOCK (sysclock);
687 GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p needs to be restarted",
690 GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p unlocked after timeout",
694 /* reschedule if gst_poll_wait returned early or we have to reschedule after
696 now = gst_clock_get_time (clock);
697 diff = GST_CLOCK_DIFF (now, entryt);
700 /* timeout, this is fine, we can report success now */
701 status = GST_CLOCK_OK;
702 SET_ENTRY_STATUS (entry, status);
704 GST_CAT_DEBUG (GST_CAT_CLOCK,
705 "entry %p finished, diff %" G_GINT64_FORMAT, entry, diff);
707 #ifdef WAIT_DEBUGGING
708 final = gst_system_clock_get_internal_time (clock);
709 GST_CAT_DEBUG (GST_CAT_CLOCK, "Waited for %" G_GINT64_FORMAT
710 " got %" G_GINT64_FORMAT " diff %" G_GINT64_FORMAT
711 " %g target-offset %" G_GINT64_FORMAT " %g", entryt, now,
713 (double) (GstClockTimeDiff) (now - entryt) / GST_SECOND,
715 ((double) (GstClockTimeDiff) (final - target)) / GST_SECOND);
719 GST_CAT_DEBUG (GST_CAT_CLOCK,
720 "entry %p restart, diff %" G_GINT64_FORMAT, entry, diff);
725 /* we are right on time or too late */
726 if (G_UNLIKELY (diff == 0))
727 status = GST_CLOCK_OK;
729 status = GST_CLOCK_EARLY;
731 SET_ENTRY_STATUS (entry, status);
737 static GstClockReturn
738 gst_system_clock_id_wait_jitter (GstClock * clock, GstClockEntry * entry,
739 GstClockTimeDiff * jitter)
741 return gst_system_clock_id_wait_jitter_unlocked (clock, entry, jitter, TRUE);
744 /* Start the async clock thread. Must be called with the object lock
747 gst_system_clock_start_async (GstSystemClock * clock)
749 GError *error = NULL;
750 GstSystemClockPrivate *priv = clock->priv;
752 if (G_LIKELY (priv->thread != NULL))
753 return TRUE; /* Thread already running. Nothing to do */
755 priv->thread = g_thread_try_new ("GstSystemClock",
756 (GThreadFunc) gst_system_clock_async_thread, clock, &error);
758 if (G_UNLIKELY (error))
761 /* wait for it to spin up */
762 GST_SYSTEM_CLOCK_WAIT (clock);
769 g_warning ("could not create async clock thread: %s", error->message);
770 g_error_free (error);
775 /* Add an entry to the list of pending async waits. The entry is inserted
776 * in sorted order. If we inserted the entry at the head of the list, we
777 * need to signal the thread as it might either be waiting on it or waiting
782 static GstClockReturn
783 gst_system_clock_id_wait_async (GstClock * clock, GstClockEntry * entry)
785 GstSystemClock *sysclock;
786 GstSystemClockPrivate *priv;
789 sysclock = GST_SYSTEM_CLOCK_CAST (clock);
790 priv = sysclock->priv;
792 GST_CAT_DEBUG (GST_CAT_CLOCK, "adding async entry %p", entry);
794 GST_OBJECT_LOCK (clock);
795 /* Start the clock async thread if needed */
796 if (G_UNLIKELY (!gst_system_clock_start_async (sysclock)))
799 if (G_UNLIKELY (GET_ENTRY_STATUS (entry) == GST_CLOCK_UNSCHEDULED))
800 goto was_unscheduled;
803 head = priv->entries->data;
807 /* need to take a ref */
808 gst_clock_id_ref ((GstClockID) entry);
809 /* insert the entry in sorted order */
810 priv->entries = g_list_insert_sorted (priv->entries, entry,
811 gst_clock_id_compare_func);
813 /* only need to send the signal if the entry was added to the
814 * front, else the thread is just waiting for another entry and
815 * will get to this entry automatically. */
816 if (priv->entries->data == entry) {
817 GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry added to head %p", head);
819 /* the list was empty before, signal the cond so that the async thread can
820 * start taking a look at the queue */
821 GST_CAT_DEBUG (GST_CAT_CLOCK, "first entry, sending signal");
822 GST_SYSTEM_CLOCK_BROADCAST (clock);
824 GstClockReturn status;
826 status = GET_ENTRY_STATUS (head);
827 GST_CAT_DEBUG (GST_CAT_CLOCK, "head entry %p status %d", head, status);
829 if (status == GST_CLOCK_BUSY) {
830 GST_CAT_DEBUG (GST_CAT_CLOCK, "head entry is busy");
831 /* the async thread was waiting for an entry, unlock the wait so that it
832 * looks at the new head entry instead, we only need to do this once */
833 if (!priv->async_wakeup) {
834 GST_CAT_DEBUG (GST_CAT_CLOCK, "wakeup async thread");
835 priv->async_wakeup = TRUE;
836 gst_system_clock_add_wakeup (sysclock);
841 GST_OBJECT_UNLOCK (clock);
848 /* Could not start the async clock thread */
849 GST_OBJECT_UNLOCK (clock);
850 return GST_CLOCK_ERROR;
854 GST_OBJECT_UNLOCK (clock);
855 return GST_CLOCK_UNSCHEDULED;
859 /* unschedule an entry. This will set the state of the entry to GST_CLOCK_UNSCHEDULED
860 * and will signal any thread waiting for entries to recheck their entry.
861 * We cannot really decide if the signal is needed or not because the entry
862 * could be waited on in async or sync mode.
867 gst_system_clock_id_unschedule (GstClock * clock, GstClockEntry * entry)
869 GstSystemClock *sysclock;
870 GstClockReturn status;
872 sysclock = GST_SYSTEM_CLOCK_CAST (clock);
874 GST_CAT_DEBUG (GST_CAT_CLOCK, "unscheduling entry %p", entry);
876 GST_OBJECT_LOCK (clock);
877 /* change the entry status to unscheduled */
879 status = GET_ENTRY_STATUS (entry);
880 } while (G_UNLIKELY (!CAS_ENTRY_STATUS (entry, status,
881 GST_CLOCK_UNSCHEDULED)));
883 if (G_LIKELY (status == GST_CLOCK_BUSY)) {
884 /* the entry was being busy, wake up all entries so that they recheck their
885 * status. We cannot wake up just one entry because allocating such a
886 * datastructure for each entry would be too heavy and unlocking an entry
887 * is usually done when shutting down or some other exceptional case. */
888 GST_CAT_DEBUG (GST_CAT_CLOCK, "entry was BUSY, doing wakeup");
889 if (!entry->unscheduled && !entry->woken_up) {
890 gst_system_clock_add_wakeup (sysclock);
891 entry->woken_up = TRUE;
894 GST_OBJECT_UNLOCK (clock);