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., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, 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
39 #include "gst_private.h"
41 #include "gstsystemclock.h"
42 #include "gstenumtypes.h"
45 #include "glib-compat-private.h"
50 # define WIN32_LEAN_AND_MEAN /* prevents from including too many things */
51 # include <windows.h> /* QueryPerformance* stuff */
52 # undef WIN32_LEAN_AND_MEAN
54 # define EWOULDBLOCK EAGAIN /* This is just to placate gcc */
56 #endif /* G_OS_WIN32 */
59 #include <mach/mach_time.h>
62 #define GET_ENTRY_STATUS(e) ((GstClockReturn) g_atomic_int_get(&GST_CLOCK_ENTRY_STATUS(e)))
63 #define SET_ENTRY_STATUS(e,val) (g_atomic_int_set(&GST_CLOCK_ENTRY_STATUS(e),(val)))
64 #define CAS_ENTRY_STATUS(e,old,val) (g_atomic_int_compare_and_exchange(\
65 (&GST_CLOCK_ENTRY_STATUS(e)), (old), (val)))
67 /* Define this to get some extra debug about jitter from each clock_wait */
70 #define GST_SYSTEM_CLOCK_GET_COND(clock) (&GST_SYSTEM_CLOCK_CAST(clock)->priv->entries_changed)
71 #define GST_SYSTEM_CLOCK_WAIT(clock) g_cond_wait(GST_SYSTEM_CLOCK_GET_COND(clock),GST_OBJECT_GET_LOCK(clock))
72 #define GST_SYSTEM_CLOCK_TIMED_WAIT(clock,tv) g_cond_timed_wait(GST_SYSTEM_CLOCK_GET_COND(clock),GST_OBJECT_GET_LOCK(clock),tv)
73 #define GST_SYSTEM_CLOCK_BROADCAST(clock) g_cond_broadcast(GST_SYSTEM_CLOCK_GET_COND(clock))
75 struct _GstSystemClockPrivate
77 GThread *thread; /* thread for async notify */
81 GCond entries_changed;
83 GstClockType clock_type;
85 gint wakeup_count; /* the number of entries with a pending wakeup */
86 gboolean async_wakeup; /* if the wakeup was because of a async list change */
90 LARGE_INTEGER frequency;
91 #endif /* G_OS_WIN32 */
93 struct mach_timebase_info mach_timebase;
97 #define GST_SYSTEM_CLOCK_GET_PRIVATE(obj) \
98 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_SYSTEM_CLOCK, \
99 GstSystemClockPrivate))
101 #ifdef HAVE_POSIX_TIMERS
102 # ifdef HAVE_MONOTONIC_CLOCK
103 # define DEFAULT_CLOCK_TYPE GST_CLOCK_TYPE_MONOTONIC
105 # define DEFAULT_CLOCK_TYPE GST_CLOCK_TYPE_REALTIME
108 #define DEFAULT_CLOCK_TYPE GST_CLOCK_TYPE_REALTIME
118 /* the one instance of the systemclock */
119 static GstClock *_the_system_clock = NULL;
120 static gboolean _external_default_clock = FALSE;
122 static void gst_system_clock_dispose (GObject * object);
123 static void gst_system_clock_set_property (GObject * object, guint prop_id,
124 const GValue * value, GParamSpec * pspec);
125 static void gst_system_clock_get_property (GObject * object, guint prop_id,
126 GValue * value, GParamSpec * pspec);
128 static GstClockTime gst_system_clock_get_internal_time (GstClock * clock);
129 static guint64 gst_system_clock_get_resolution (GstClock * clock);
130 static GstClockReturn gst_system_clock_id_wait_jitter (GstClock * clock,
131 GstClockEntry * entry, GstClockTimeDiff * jitter);
132 static GstClockReturn gst_system_clock_id_wait_jitter_unlocked
133 (GstClock * clock, GstClockEntry * entry, GstClockTimeDiff * jitter,
135 static GstClockReturn gst_system_clock_id_wait_async (GstClock * clock,
136 GstClockEntry * entry);
137 static void gst_system_clock_id_unschedule (GstClock * clock,
138 GstClockEntry * entry);
139 static void gst_system_clock_async_thread (GstClock * clock);
140 static gboolean gst_system_clock_start_async (GstSystemClock * clock);
141 static void gst_system_clock_add_wakeup (GstSystemClock * sysclock);
143 static GMutex _gst_sysclock_mutex;
145 /* static guint gst_system_clock_signals[LAST_SIGNAL] = { 0 }; */
147 #define gst_system_clock_parent_class parent_class
148 G_DEFINE_TYPE (GstSystemClock, gst_system_clock, GST_TYPE_CLOCK);
151 gst_system_clock_class_init (GstSystemClockClass * klass)
153 GObjectClass *gobject_class;
154 GstClockClass *gstclock_class;
156 gobject_class = (GObjectClass *) klass;
157 gstclock_class = (GstClockClass *) klass;
159 g_type_class_add_private (klass, sizeof (GstSystemClockPrivate));
161 gobject_class->dispose = gst_system_clock_dispose;
162 gobject_class->set_property = gst_system_clock_set_property;
163 gobject_class->get_property = gst_system_clock_get_property;
165 g_object_class_install_property (gobject_class, PROP_CLOCK_TYPE,
166 g_param_spec_enum ("clock-type", "Clock type",
167 "The type of underlying clock implementation used",
168 GST_TYPE_CLOCK_TYPE, DEFAULT_CLOCK_TYPE,
169 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
171 gstclock_class->get_internal_time = gst_system_clock_get_internal_time;
172 gstclock_class->get_resolution = gst_system_clock_get_resolution;
173 gstclock_class->wait = gst_system_clock_id_wait_jitter;
174 gstclock_class->wait_async = gst_system_clock_id_wait_async;
175 gstclock_class->unschedule = gst_system_clock_id_unschedule;
179 gst_system_clock_init (GstSystemClock * clock)
181 GstSystemClockPrivate *priv;
183 GST_OBJECT_FLAG_SET (clock,
184 GST_CLOCK_FLAG_CAN_DO_SINGLE_SYNC |
185 GST_CLOCK_FLAG_CAN_DO_SINGLE_ASYNC |
186 GST_CLOCK_FLAG_CAN_DO_PERIODIC_SYNC |
187 GST_CLOCK_FLAG_CAN_DO_PERIODIC_ASYNC);
189 clock->priv = priv = GST_SYSTEM_CLOCK_GET_PRIVATE (clock);
191 priv->clock_type = DEFAULT_CLOCK_TYPE;
192 priv->timer = gst_poll_new_timer ();
194 priv->entries = NULL;
195 g_cond_init (&priv->entries_changed);
198 QueryPerformanceFrequency (&priv->frequency);
199 /* can be 0 if the hardware does not have hardware support */
200 if (priv->frequency.QuadPart != 0)
201 /* we take a base time so that time starts from 0 to ease debugging */
202 QueryPerformanceCounter (&priv->start);
203 #endif /* G_OS_WIN32 */
206 mach_timebase_info (&priv->mach_timebase);
210 /* Uncomment this to start the async clock thread straight away */
211 GST_OBJECT_LOCK (clock);
212 gst_system_clock_start_async (clock);
213 GST_OBJECT_UNLOCK (clock);
218 gst_system_clock_dispose (GObject * object)
220 GstClock *clock = (GstClock *) object;
221 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
222 GstSystemClockPrivate *priv = sysclock->priv;
225 /* else we have to stop the thread */
226 GST_OBJECT_LOCK (clock);
227 priv->stopping = TRUE;
228 /* unschedule all entries */
229 for (entries = priv->entries; entries; entries = g_list_next (entries)) {
230 GstClockEntry *entry = (GstClockEntry *) entries->data;
232 GST_CAT_DEBUG (GST_CAT_CLOCK, "unscheduling entry %p", entry);
233 SET_ENTRY_STATUS (entry, GST_CLOCK_UNSCHEDULED);
235 GST_SYSTEM_CLOCK_BROADCAST (clock);
236 gst_system_clock_add_wakeup (sysclock);
237 GST_OBJECT_UNLOCK (clock);
240 g_thread_join (priv->thread);
242 GST_CAT_DEBUG (GST_CAT_CLOCK, "joined thread");
244 g_list_foreach (priv->entries, (GFunc) gst_clock_id_unref, NULL);
245 g_list_free (priv->entries);
246 priv->entries = NULL;
248 gst_poll_free (priv->timer);
249 g_cond_clear (&priv->entries_changed);
251 G_OBJECT_CLASS (parent_class)->dispose (object);
253 if (_the_system_clock == clock) {
254 _the_system_clock = NULL;
255 GST_CAT_DEBUG (GST_CAT_CLOCK, "disposed system clock");
260 gst_system_clock_set_property (GObject * object, guint prop_id,
261 const GValue * value, GParamSpec * pspec)
263 GstSystemClock *sysclock = GST_SYSTEM_CLOCK (object);
266 case PROP_CLOCK_TYPE:
267 sysclock->priv->clock_type = (GstClockType) g_value_get_enum (value);
268 GST_CAT_DEBUG (GST_CAT_CLOCK, "clock-type set to %d",
269 sysclock->priv->clock_type);
272 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
278 gst_system_clock_get_property (GObject * object, guint prop_id, GValue * value,
281 GstSystemClock *sysclock = GST_SYSTEM_CLOCK (object);
284 case PROP_CLOCK_TYPE:
285 g_value_set_enum (value, sysclock->priv->clock_type);
288 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
294 * gst_system_clock_set_default:
295 * @new_clock: a #GstClock
297 * Sets the default system clock that can be obtained with
298 * gst_system_clock_obtain().
300 * This is mostly used for testing and debugging purposes when you
301 * want to have control over the time reported by the default system
309 gst_system_clock_set_default (GstClock * new_clock)
313 g_mutex_lock (&_gst_sysclock_mutex);
314 clock = _the_system_clock;
317 g_object_unref (clock);
319 if (new_clock == NULL) {
320 GST_CAT_DEBUG (GST_CAT_CLOCK, "resetting default system clock");
321 _external_default_clock = FALSE;
323 GST_CAT_DEBUG (GST_CAT_CLOCK, "setting new default system clock to %p",
325 _external_default_clock = TRUE;
326 g_object_ref (new_clock);
328 _the_system_clock = new_clock;
329 g_mutex_unlock (&_gst_sysclock_mutex);
333 * gst_system_clock_obtain:
335 * Get a handle to the default system clock. The refcount of the
336 * clock will be increased so you need to unref the clock after
339 * Returns: (transfer full): the default clock.
344 gst_system_clock_obtain (void)
348 g_mutex_lock (&_gst_sysclock_mutex);
349 clock = _the_system_clock;
352 GST_CAT_DEBUG (GST_CAT_CLOCK, "creating new static system clock");
353 g_assert (!_external_default_clock);
354 clock = g_object_new (GST_TYPE_SYSTEM_CLOCK,
355 "name", "GstSystemClock", NULL);
357 g_assert (!g_object_is_floating (G_OBJECT (clock)));
359 _the_system_clock = clock;
360 g_mutex_unlock (&_gst_sysclock_mutex);
362 g_mutex_unlock (&_gst_sysclock_mutex);
363 GST_CAT_DEBUG (GST_CAT_CLOCK, "returning static system clock");
366 /* we ref it since we are a clock factory. */
367 gst_object_ref (clock);
372 gst_system_clock_remove_wakeup (GstSystemClock * sysclock)
374 g_return_if_fail (sysclock->priv->wakeup_count > 0);
376 sysclock->priv->wakeup_count--;
377 if (sysclock->priv->wakeup_count == 0) {
378 /* read the control socket byte when we removed the last wakeup count */
379 GST_CAT_DEBUG (GST_CAT_CLOCK, "reading control");
380 while (!gst_poll_read_control (sysclock->priv->timer)) {
381 g_warning ("gstsystemclock: read control failed, trying again\n");
383 GST_SYSTEM_CLOCK_BROADCAST (sysclock);
385 GST_CAT_DEBUG (GST_CAT_CLOCK, "wakeup count %d",
386 sysclock->priv->wakeup_count);
390 gst_system_clock_add_wakeup (GstSystemClock * sysclock)
392 /* only write the control socket for the first wakeup */
393 if (sysclock->priv->wakeup_count == 0) {
394 GST_CAT_DEBUG (GST_CAT_CLOCK, "writing control");
395 while (!gst_poll_write_control (sysclock->priv->timer)) {
396 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {
398 ("gstsystemclock: write control failed in wakeup_async, trying again: %d:%s\n",
399 errno, g_strerror (errno));
402 ("gstsystemclock: write control failed in wakeup_async: %d:%s\n",
403 errno, g_strerror (errno));
408 sysclock->priv->wakeup_count++;
409 GST_CAT_DEBUG (GST_CAT_CLOCK, "wakeup count %d",
410 sysclock->priv->wakeup_count);
414 gst_system_clock_wait_wakeup (GstSystemClock * sysclock)
416 while (sysclock->priv->wakeup_count > 0) {
417 GST_SYSTEM_CLOCK_WAIT (sysclock);
421 /* this thread reads the sorted clock entries from the queue.
423 * It waits on each of them and fires the callback when the timeout occurs.
425 * When an entry in the queue was canceled before we wait for it, it is
428 * When waiting for an entry, it can become canceled, in that case we don't
429 * call the callback but move to the next item in the queue.
434 gst_system_clock_async_thread (GstClock * clock)
436 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
437 GstSystemClockPrivate *priv = sysclock->priv;
438 GstClockReturn status;
440 GST_CAT_DEBUG (GST_CAT_CLOCK, "enter system clock thread");
441 GST_OBJECT_LOCK (clock);
443 GST_SYSTEM_CLOCK_BROADCAST (clock);
444 /* now enter our (almost) infinite loop */
445 while (!priv->stopping) {
446 GstClockEntry *entry;
447 GstClockTime requested;
450 /* check if something to be done */
451 while (priv->entries == NULL) {
452 GST_CAT_DEBUG (GST_CAT_CLOCK, "no clock entries, waiting..");
453 /* wait for work to do */
454 GST_SYSTEM_CLOCK_WAIT (clock);
455 GST_CAT_DEBUG (GST_CAT_CLOCK, "got signal");
456 /* clock was stopping, exit */
461 /* see if we have a pending wakeup because the order of the list
463 if (priv->async_wakeup) {
464 GST_CAT_DEBUG (GST_CAT_CLOCK, "clear async wakeup");
465 gst_system_clock_remove_wakeup (sysclock);
466 priv->async_wakeup = FALSE;
469 /* pick the next entry */
470 entry = priv->entries->data;
472 /* set entry status to busy before we release the clock lock */
474 status = GET_ENTRY_STATUS (entry);
476 /* check for unscheduled */
477 if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED)) {
478 /* entry was unscheduled, move to the next one */
479 GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry %p unscheduled", entry);
483 /* for periodic timers, status can be EARLY from a previous run */
484 if (G_UNLIKELY (status != GST_CLOCK_OK && status != GST_CLOCK_EARLY))
485 GST_CAT_ERROR (GST_CAT_CLOCK, "unexpected status %d for entry %p",
488 /* mark the entry as busy but watch out for intermediate unscheduled
490 } while (G_UNLIKELY (!CAS_ENTRY_STATUS (entry, status, GST_CLOCK_BUSY)));
492 GST_OBJECT_UNLOCK (clock);
494 requested = entry->time;
496 /* now wait for the entry */
498 gst_system_clock_id_wait_jitter_unlocked (clock, (GstClockID) entry,
501 GST_OBJECT_LOCK (clock);
504 case GST_CLOCK_UNSCHEDULED:
505 /* entry was unscheduled, move to the next */
506 GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry %p unscheduled", entry);
509 case GST_CLOCK_EARLY:
511 /* entry timed out normally, fire the callback and move to the next
513 GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry %p timed out", entry);
515 /* unlock before firing the callback */
516 GST_OBJECT_UNLOCK (clock);
517 entry->func (clock, entry->time, (GstClockID) entry,
519 GST_OBJECT_LOCK (clock);
521 if (entry->type == GST_CLOCK_ENTRY_PERIODIC) {
522 GST_CAT_DEBUG (GST_CAT_CLOCK, "updating periodic entry %p", entry);
523 /* adjust time now */
524 entry->time = requested + entry->interval;
525 /* and resort the list now */
527 g_list_sort (priv->entries, gst_clock_id_compare_func);
531 GST_CAT_DEBUG (GST_CAT_CLOCK, "moving to next entry");
536 /* somebody unlocked the entry but is was not canceled, This means that
537 * either a new entry was added in front of the queue or some other entry
538 * was canceled. Whatever it is, pick the head entry of the list and
539 * continue waiting. */
540 GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry %p needs restart", entry);
542 /* we set the entry back to the OK state. This is needed so that the
543 * _unschedule() code can see if an entry is currently being waited
544 * on (when its state is BUSY). */
545 SET_ENTRY_STATUS (entry, GST_CLOCK_OK);
548 GST_CAT_DEBUG (GST_CAT_CLOCK,
549 "strange result %d waiting for %p, skipping", res, entry);
550 g_warning ("%s: strange result %d waiting for %p, skipping",
551 GST_OBJECT_NAME (clock), res, entry);
555 /* we remove the current entry and unref it */
556 priv->entries = g_list_remove (priv->entries, entry);
557 gst_clock_id_unref ((GstClockID) entry);
561 GST_SYSTEM_CLOCK_BROADCAST (clock);
562 GST_OBJECT_UNLOCK (clock);
563 GST_CAT_DEBUG (GST_CAT_CLOCK, "exit system clock thread");
566 #ifdef HAVE_POSIX_TIMERS
567 static inline clockid_t
568 clock_type_to_posix_id (GstClockType clock_type)
570 #ifdef HAVE_MONOTONIC_CLOCK
571 if (clock_type == GST_CLOCK_TYPE_MONOTONIC)
572 return CLOCK_MONOTONIC;
575 return CLOCK_REALTIME;
581 gst_system_clock_get_internal_time (GstClock * clock)
583 #if defined __APPLE__
584 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
585 uint64_t mach_t = mach_absolute_time ();
586 return gst_util_uint64_scale (mach_t, sysclock->priv->mach_timebase.numer,
587 sysclock->priv->mach_timebase.denom);
590 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
592 if (sysclock->priv->frequency.QuadPart != 0) {
595 /* we prefer the highly accurate performance counters on windows */
596 QueryPerformanceCounter (&now);
598 return gst_util_uint64_scale (now.QuadPart - sysclock->priv->start.QuadPart,
599 GST_SECOND, sysclock->priv->frequency.QuadPart);
601 #endif /* G_OS_WIN32 */
602 #if !defined HAVE_POSIX_TIMERS || !defined HAVE_CLOCK_GETTIME
606 g_get_current_time (&timeval);
608 return GST_TIMEVAL_TO_TIME (timeval);
612 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
616 ptype = clock_type_to_posix_id (sysclock->priv->clock_type);
618 if (G_UNLIKELY (clock_gettime (ptype, &ts)))
619 return GST_CLOCK_TIME_NONE;
621 return GST_TIMESPEC_TO_TIME (ts);
624 #endif /* __APPLE__ */
628 gst_system_clock_get_resolution (GstClock * clock)
630 #if defined __APPLE__
631 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
632 return gst_util_uint64_scale (GST_NSECOND,
633 sysclock->priv->mach_timebase.numer, sysclock->priv->mach_timebase.denom);
636 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
638 if (sysclock->priv->frequency.QuadPart != 0) {
639 return GST_SECOND / sysclock->priv->frequency.QuadPart;
641 #endif /* G_OS_WIN32 */
642 #if defined(HAVE_POSIX_TIMERS) && defined(HAVE_CLOCK_GETTIME)
644 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
648 ptype = clock_type_to_posix_id (sysclock->priv->clock_type);
650 if (G_UNLIKELY (clock_getres (ptype, &ts)))
651 return GST_CLOCK_TIME_NONE;
653 return GST_TIMESPEC_TO_TIME (ts);
657 return 1 * GST_USECOND;
660 #endif /* __APPLE__ */
664 gst_system_clock_cleanup_unscheduled (GstSystemClock * sysclock,
665 GstClockEntry * entry)
668 * The unschedule function managed to set the status to
669 * unscheduled. We now take the lock and mark the entry as unscheduled.
670 * This makes sure that the unschedule function doesn't perform a
671 * wakeup anymore. If the unschedule function has a change to perform
672 * the wakeup before us, we clean up here */
673 GST_OBJECT_LOCK (sysclock);
674 entry->unscheduled = TRUE;
675 if (entry->woken_up) {
676 gst_system_clock_remove_wakeup (sysclock);
678 GST_OBJECT_UNLOCK (sysclock);
681 /* synchronously wait on the given GstClockEntry.
683 * We do this by blocking on the global GstPoll timer with
684 * the requested timeout. This allows us to unblock the
685 * entry by writing on the control fd.
687 * Note that writing the global GstPoll unlocks all waiting entries. So
688 * we need to check if an unlocked entry has changed when it unlocks.
690 * Entries that arrive too late are simply not waited on and a
691 * GST_CLOCK_EARLY result is returned.
695 static GstClockReturn
696 gst_system_clock_id_wait_jitter_unlocked (GstClock * clock,
697 GstClockEntry * entry, GstClockTimeDiff * jitter, gboolean restart)
699 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
700 GstClockTime entryt, now;
701 GstClockTimeDiff diff;
702 GstClockReturn status;
704 status = GET_ENTRY_STATUS (entry);
705 if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED)) {
706 gst_system_clock_cleanup_unscheduled (sysclock, entry);
707 return GST_CLOCK_UNSCHEDULED;
710 /* need to call the overridden method because we want to sync against the time
711 * of the clock, whatever the subclass uses as a clock. */
712 now = gst_clock_get_time (clock);
714 /* get the time of the entry */
715 entryt = GST_CLOCK_ENTRY_TIME (entry);
717 /* the diff of the entry with the clock is the amount of time we have to
719 diff = GST_CLOCK_DIFF (now, entryt);
720 if (G_LIKELY (jitter))
723 GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p"
724 " time %" GST_TIME_FORMAT
725 " now %" GST_TIME_FORMAT
726 " diff (time-now) %" G_GINT64_FORMAT,
727 entry, GST_TIME_ARGS (entryt), GST_TIME_ARGS (now), diff);
729 if (G_LIKELY (diff > 0)) {
730 #ifdef WAIT_DEBUGGING
737 /* now wait on the entry, it either times out or the fd is written. The
738 * status of the entry is BUSY only around the poll. */
739 pollret = gst_poll_wait (sysclock->priv->timer, diff);
741 /* get the new status, mark as DONE. We do this so that the unschedule
742 * function knows when we left the poll and doesn't need to wakeup the
745 status = GET_ENTRY_STATUS (entry);
746 /* we were unscheduled, exit immediately */
747 if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED))
749 if (G_UNLIKELY (status != GST_CLOCK_BUSY))
750 GST_CAT_ERROR (GST_CAT_CLOCK, "unexpected status %d for entry %p",
752 } while (G_UNLIKELY (!CAS_ENTRY_STATUS (entry, status, GST_CLOCK_DONE)));
754 GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p unlocked, status %d, ret %d",
755 entry, status, pollret);
757 if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED)) {
758 gst_system_clock_cleanup_unscheduled (sysclock, entry);
761 if (G_UNLIKELY (pollret != 0)) {
762 /* some other id got unlocked */
764 /* this can happen if the entry got unlocked because of an async
765 * entry was added to the head of the async queue. */
766 GST_CAT_DEBUG (GST_CAT_CLOCK, "wakeup waiting for entry %p", entry);
770 /* wait till all the entries got woken up */
771 GST_OBJECT_LOCK (sysclock);
772 gst_system_clock_wait_wakeup (sysclock);
773 GST_OBJECT_UNLOCK (sysclock);
775 GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p needs to be restarted",
778 GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p unlocked after timeout",
782 /* reschedule if gst_poll_wait returned early or we have to reschedule after
784 now = gst_clock_get_time (clock);
785 diff = GST_CLOCK_DIFF (now, entryt);
788 /* timeout, this is fine, we can report success now */
789 if (G_UNLIKELY (!CAS_ENTRY_STATUS (entry, GST_CLOCK_DONE,
791 status = GET_ENTRY_STATUS (entry);
792 if (status != GST_CLOCK_UNSCHEDULED)
793 GST_CAT_ERROR (GST_CAT_CLOCK, "unexpected status %d for entry %p",
797 status = GST_CLOCK_OK;
800 GST_CAT_DEBUG (GST_CAT_CLOCK,
801 "entry %p finished, diff %" G_GINT64_FORMAT, entry, diff);
803 #ifdef WAIT_DEBUGGING
804 final = gst_system_clock_get_internal_time (clock);
805 GST_CAT_DEBUG (GST_CAT_CLOCK, "Waited for %" G_GINT64_FORMAT
806 " got %" G_GINT64_FORMAT " diff %" G_GINT64_FORMAT
807 " %g target-offset %" G_GINT64_FORMAT " %g", entryt, now,
809 (double) (GstClockTimeDiff) (now - entryt) / GST_SECOND,
811 ((double) (GstClockTimeDiff) (final - target)) / GST_SECOND);
815 GST_CAT_DEBUG (GST_CAT_CLOCK,
816 "entry %p restart, diff %" G_GINT64_FORMAT, entry, diff);
817 /* we are going to poll again, set status back to busy */
819 status = GET_ENTRY_STATUS (entry);
820 /* we were unscheduled, exit immediately */
821 if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED))
823 if (G_UNLIKELY (status != GST_CLOCK_DONE))
824 GST_CAT_ERROR (GST_CAT_CLOCK, "unexpected status %d for entry %p",
826 } while (G_UNLIKELY (!CAS_ENTRY_STATUS (entry, status,
832 /* we are right on time or too late */
833 if (G_UNLIKELY (diff == 0)) {
834 if (G_UNLIKELY (!CAS_ENTRY_STATUS (entry, status, GST_CLOCK_OK))) {
835 status = GET_ENTRY_STATUS (entry);
836 if (G_LIKELY (status == GST_CLOCK_UNSCHEDULED))
837 gst_system_clock_cleanup_unscheduled (sysclock, entry);
839 GST_CAT_ERROR (GST_CAT_CLOCK, "unexpected status %d for entry %p",
842 status = GST_CLOCK_OK;
845 if (G_UNLIKELY (!CAS_ENTRY_STATUS (entry, status, GST_CLOCK_EARLY))) {
846 status = GET_ENTRY_STATUS (entry);
847 if (G_LIKELY (status == GST_CLOCK_UNSCHEDULED))
848 gst_system_clock_cleanup_unscheduled (sysclock, entry);
850 GST_CAT_ERROR (GST_CAT_CLOCK, "unexpected status %d for entry %p",
853 status = GST_CLOCK_EARLY;
861 static GstClockReturn
862 gst_system_clock_id_wait_jitter (GstClock * clock, GstClockEntry * entry,
863 GstClockTimeDiff * jitter)
865 GstClockReturn status;
867 status = GET_ENTRY_STATUS (entry);
869 /* stop when we are unscheduled */
870 if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED))
873 if (G_UNLIKELY (status != GST_CLOCK_OK))
874 GST_CAT_ERROR (GST_CAT_CLOCK, "unexpected status %d for entry %p",
877 /* mark the entry as busy but watch out for intermediate unscheduled
879 } while (G_UNLIKELY (!CAS_ENTRY_STATUS (entry, status, GST_CLOCK_BUSY)));
881 return gst_system_clock_id_wait_jitter_unlocked (clock, entry, jitter, TRUE);
884 /* Start the async clock thread. Must be called with the object lock
887 gst_system_clock_start_async (GstSystemClock * clock)
889 GError *error = NULL;
890 GstSystemClockPrivate *priv = clock->priv;
892 if (G_LIKELY (priv->thread != NULL))
893 return TRUE; /* Thread already running. Nothing to do */
895 priv->thread = g_thread_try_new ("GstSystemClock",
896 (GThreadFunc) gst_system_clock_async_thread, clock, &error);
898 if (G_UNLIKELY (error))
901 /* wait for it to spin up */
902 GST_SYSTEM_CLOCK_WAIT (clock);
909 g_warning ("could not create async clock thread: %s", error->message);
910 g_error_free (error);
915 /* Add an entry to the list of pending async waits. The entry is inserted
916 * in sorted order. If we inserted the entry at the head of the list, we
917 * need to signal the thread as it might either be waiting on it or waiting
922 static GstClockReturn
923 gst_system_clock_id_wait_async (GstClock * clock, GstClockEntry * entry)
925 GstSystemClock *sysclock;
926 GstSystemClockPrivate *priv;
929 sysclock = GST_SYSTEM_CLOCK_CAST (clock);
930 priv = sysclock->priv;
932 GST_CAT_DEBUG (GST_CAT_CLOCK, "adding async entry %p", entry);
934 GST_OBJECT_LOCK (clock);
935 /* Start the clock async thread if needed */
936 if (G_UNLIKELY (!gst_system_clock_start_async (sysclock)))
939 if (G_UNLIKELY (GET_ENTRY_STATUS (entry) == GST_CLOCK_UNSCHEDULED))
940 goto was_unscheduled;
943 head = priv->entries->data;
947 /* need to take a ref */
948 gst_clock_id_ref ((GstClockID) entry);
949 /* insert the entry in sorted order */
950 priv->entries = g_list_insert_sorted (priv->entries, entry,
951 gst_clock_id_compare_func);
953 /* only need to send the signal if the entry was added to the
954 * front, else the thread is just waiting for another entry and
955 * will get to this entry automatically. */
956 if (priv->entries->data == entry) {
957 GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry added to head %p", head);
959 /* the list was empty before, signal the cond so that the async thread can
960 * start taking a look at the queue */
961 GST_CAT_DEBUG (GST_CAT_CLOCK, "first entry, sending signal");
962 GST_SYSTEM_CLOCK_BROADCAST (clock);
964 GstClockReturn status;
966 status = GET_ENTRY_STATUS (head);
967 GST_CAT_DEBUG (GST_CAT_CLOCK, "head entry %p status %d", head, status);
969 if (status == GST_CLOCK_BUSY) {
970 GST_CAT_DEBUG (GST_CAT_CLOCK, "head entry is busy");
971 /* the async thread was waiting for an entry, unlock the wait so that it
972 * looks at the new head entry instead, we only need to do this once */
973 if (!priv->async_wakeup) {
974 GST_CAT_DEBUG (GST_CAT_CLOCK, "wakeup async thread");
975 priv->async_wakeup = TRUE;
976 gst_system_clock_add_wakeup (sysclock);
981 GST_OBJECT_UNLOCK (clock);
988 /* Could not start the async clock thread */
989 GST_OBJECT_UNLOCK (clock);
990 return GST_CLOCK_ERROR;
994 GST_OBJECT_UNLOCK (clock);
995 return GST_CLOCK_UNSCHEDULED;
999 /* unschedule an entry. This will set the state of the entry to GST_CLOCK_UNSCHEDULED
1000 * and will signal any thread waiting for entries to recheck their entry.
1001 * We cannot really decide if the signal is needed or not because the entry
1002 * could be waited on in async or sync mode.
1007 gst_system_clock_id_unschedule (GstClock * clock, GstClockEntry * entry)
1009 GstSystemClock *sysclock;
1010 GstClockReturn status;
1012 sysclock = GST_SYSTEM_CLOCK_CAST (clock);
1014 GST_CAT_DEBUG (GST_CAT_CLOCK, "unscheduling entry %p", entry);
1016 GST_OBJECT_LOCK (clock);
1017 /* change the entry status to unscheduled */
1019 status = GET_ENTRY_STATUS (entry);
1020 } while (G_UNLIKELY (!CAS_ENTRY_STATUS (entry, status,
1021 GST_CLOCK_UNSCHEDULED)));
1023 if (G_LIKELY (status == GST_CLOCK_BUSY)) {
1024 /* the entry was being busy, wake up all entries so that they recheck their
1025 * status. We cannot wake up just one entry because allocating such a
1026 * datastructure for each entry would be too heavy and unlocking an entry
1027 * is usually done when shutting down or some other exceptional case. */
1028 GST_CAT_DEBUG (GST_CAT_CLOCK, "entry was BUSY, doing wakeup");
1029 if (!entry->unscheduled && !entry->woken_up) {
1030 gst_system_clock_add_wakeup (sysclock);
1031 entry->woken_up = TRUE;
1034 GST_OBJECT_UNLOCK (clock);