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 * @title: GstSystemClock
26 * @short_description: Default clock that uses the current system time
27 * @see_also: #GstClock
29 * The GStreamer core provides a GstSystemClock based on the system time.
30 * Asynchronous callbacks are scheduled from an internal thread.
32 * Clock implementors are encouraged to subclass this systemclock as it
33 * implements the async notification.
35 * Subclasses can however override all of the important methods for sync and
36 * async notifications to implement their own callback methods or blocking
40 #include "gst_private.h"
42 #include "gstsystemclock.h"
43 #include "gstenumtypes.h"
46 #include "glib-compat-private.h"
51 # define WIN32_LEAN_AND_MEAN /* prevents from including too many things */
52 # include <windows.h> /* QueryPerformance* stuff */
53 # undef WIN32_LEAN_AND_MEAN
55 # define EWOULDBLOCK EAGAIN /* This is just to placate gcc */
57 #endif /* G_OS_WIN32 */
60 #include <mach/mach_time.h>
63 #define GET_ENTRY_STATUS(e) ((GstClockReturn) g_atomic_int_get(&GST_CLOCK_ENTRY_STATUS(e)))
64 #define SET_ENTRY_STATUS(e,val) (g_atomic_int_set(&GST_CLOCK_ENTRY_STATUS(e),(val)))
65 #define CAS_ENTRY_STATUS(e,old,val) (g_atomic_int_compare_and_exchange(\
66 (&GST_CLOCK_ENTRY_STATUS(e)), (old), (val)))
68 /* Define this to get some extra debug about jitter from each clock_wait */
71 #define GST_SYSTEM_CLOCK_GET_COND(clock) (&GST_SYSTEM_CLOCK_CAST(clock)->priv->entries_changed)
72 #define GST_SYSTEM_CLOCK_WAIT(clock) g_cond_wait(GST_SYSTEM_CLOCK_GET_COND(clock),GST_OBJECT_GET_LOCK(clock))
73 #define GST_SYSTEM_CLOCK_TIMED_WAIT(clock,tv) g_cond_timed_wait(GST_SYSTEM_CLOCK_GET_COND(clock),GST_OBJECT_GET_LOCK(clock),tv)
74 #define GST_SYSTEM_CLOCK_BROADCAST(clock) g_cond_broadcast(GST_SYSTEM_CLOCK_GET_COND(clock))
76 struct _GstSystemClockPrivate
78 GThread *thread; /* thread for async notify */
82 GCond entries_changed;
84 GstClockType clock_type;
86 gint wakeup_count; /* the number of entries with a pending wakeup */
87 gboolean async_wakeup; /* if the wakeup was because of a async list change */
91 LARGE_INTEGER frequency;
93 #endif /* G_OS_WIN32 */
95 struct mach_timebase_info mach_timebase;
99 #ifdef HAVE_POSIX_TIMERS
100 # ifdef HAVE_MONOTONIC_CLOCK
101 # define DEFAULT_CLOCK_TYPE GST_CLOCK_TYPE_MONOTONIC
103 # define DEFAULT_CLOCK_TYPE GST_CLOCK_TYPE_REALTIME
106 #define DEFAULT_CLOCK_TYPE GST_CLOCK_TYPE_REALTIME
116 /* the one instance of the systemclock */
117 static GstClock *_the_system_clock = NULL;
118 static gboolean _external_default_clock = FALSE;
120 static void gst_system_clock_dispose (GObject * object);
121 static void gst_system_clock_set_property (GObject * object, guint prop_id,
122 const GValue * value, GParamSpec * pspec);
123 static void gst_system_clock_get_property (GObject * object, guint prop_id,
124 GValue * value, GParamSpec * pspec);
126 static GstClockTime gst_system_clock_get_internal_time (GstClock * clock);
127 static guint64 gst_system_clock_get_resolution (GstClock * clock);
128 static GstClockReturn gst_system_clock_id_wait_jitter (GstClock * clock,
129 GstClockEntry * entry, GstClockTimeDiff * jitter);
130 static GstClockReturn gst_system_clock_id_wait_jitter_unlocked
131 (GstClock * clock, GstClockEntry * entry, GstClockTimeDiff * jitter,
133 static GstClockReturn gst_system_clock_id_wait_async (GstClock * clock,
134 GstClockEntry * entry);
135 static void gst_system_clock_id_unschedule (GstClock * clock,
136 GstClockEntry * entry);
137 static void gst_system_clock_async_thread (GstClock * clock);
138 static gboolean gst_system_clock_start_async (GstSystemClock * clock);
139 static void gst_system_clock_add_wakeup (GstSystemClock * sysclock);
141 static GMutex _gst_sysclock_mutex;
143 /* static guint gst_system_clock_signals[LAST_SIGNAL] = { 0 }; */
145 #define gst_system_clock_parent_class parent_class
146 G_DEFINE_TYPE_WITH_PRIVATE (GstSystemClock, gst_system_clock, GST_TYPE_CLOCK);
149 gst_system_clock_class_init (GstSystemClockClass * klass)
151 GObjectClass *gobject_class;
152 GstClockClass *gstclock_class;
154 gobject_class = (GObjectClass *) klass;
155 gstclock_class = (GstClockClass *) klass;
157 gobject_class->dispose = gst_system_clock_dispose;
158 gobject_class->set_property = gst_system_clock_set_property;
159 gobject_class->get_property = gst_system_clock_get_property;
161 g_object_class_install_property (gobject_class, PROP_CLOCK_TYPE,
162 g_param_spec_enum ("clock-type", "Clock type",
163 "The type of underlying clock implementation used",
164 GST_TYPE_CLOCK_TYPE, DEFAULT_CLOCK_TYPE,
165 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
167 gstclock_class->get_internal_time = gst_system_clock_get_internal_time;
168 gstclock_class->get_resolution = gst_system_clock_get_resolution;
169 gstclock_class->wait = gst_system_clock_id_wait_jitter;
170 gstclock_class->wait_async = gst_system_clock_id_wait_async;
171 gstclock_class->unschedule = gst_system_clock_id_unschedule;
175 gst_system_clock_init (GstSystemClock * clock)
177 GstSystemClockPrivate *priv;
179 GST_OBJECT_FLAG_SET (clock,
180 GST_CLOCK_FLAG_CAN_DO_SINGLE_SYNC |
181 GST_CLOCK_FLAG_CAN_DO_SINGLE_ASYNC |
182 GST_CLOCK_FLAG_CAN_DO_PERIODIC_SYNC |
183 GST_CLOCK_FLAG_CAN_DO_PERIODIC_ASYNC);
185 clock->priv = priv = gst_system_clock_get_instance_private (clock);
187 priv->clock_type = DEFAULT_CLOCK_TYPE;
188 priv->timer = gst_poll_new_timer ();
190 priv->entries = NULL;
191 g_cond_init (&priv->entries_changed);
194 QueryPerformanceFrequency (&priv->frequency);
195 /* can be 0 if the hardware does not have hardware support */
196 if (priv->frequency.QuadPart != 0)
197 /* we take a base time so that time starts from 0 to ease debugging */
198 QueryPerformanceCounter (&priv->start);
199 priv->ratio = GST_SECOND / priv->frequency.QuadPart;
200 #endif /* G_OS_WIN32 */
203 mach_timebase_info (&priv->mach_timebase);
207 /* Uncomment this to start the async clock thread straight away */
208 GST_OBJECT_LOCK (clock);
209 gst_system_clock_start_async (clock);
210 GST_OBJECT_UNLOCK (clock);
215 gst_system_clock_dispose (GObject * object)
217 GstClock *clock = (GstClock *) object;
218 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
219 GstSystemClockPrivate *priv = sysclock->priv;
222 /* else we have to stop the thread */
223 GST_OBJECT_LOCK (clock);
224 priv->stopping = TRUE;
225 /* unschedule all entries */
226 for (entries = priv->entries; entries; entries = g_list_next (entries)) {
227 GstClockEntry *entry = (GstClockEntry *) entries->data;
229 GST_CAT_DEBUG (GST_CAT_CLOCK, "unscheduling entry %p", entry);
230 SET_ENTRY_STATUS (entry, GST_CLOCK_UNSCHEDULED);
232 GST_SYSTEM_CLOCK_BROADCAST (clock);
233 gst_system_clock_add_wakeup (sysclock);
234 GST_OBJECT_UNLOCK (clock);
237 g_thread_join (priv->thread);
239 GST_CAT_DEBUG (GST_CAT_CLOCK, "joined thread");
241 g_list_foreach (priv->entries, (GFunc) gst_clock_id_unref, NULL);
242 g_list_free (priv->entries);
243 priv->entries = NULL;
245 gst_poll_free (priv->timer);
246 g_cond_clear (&priv->entries_changed);
248 G_OBJECT_CLASS (parent_class)->dispose (object);
250 if (_the_system_clock == clock) {
251 _the_system_clock = NULL;
252 GST_CAT_DEBUG (GST_CAT_CLOCK, "disposed system clock");
257 gst_system_clock_set_property (GObject * object, guint prop_id,
258 const GValue * value, GParamSpec * pspec)
260 GstSystemClock *sysclock = GST_SYSTEM_CLOCK (object);
263 case PROP_CLOCK_TYPE:
264 sysclock->priv->clock_type = (GstClockType) g_value_get_enum (value);
265 GST_CAT_DEBUG (GST_CAT_CLOCK, "clock-type set to %d",
266 sysclock->priv->clock_type);
269 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
275 gst_system_clock_get_property (GObject * object, guint prop_id, GValue * value,
278 GstSystemClock *sysclock = GST_SYSTEM_CLOCK (object);
281 case PROP_CLOCK_TYPE:
282 g_value_set_enum (value, sysclock->priv->clock_type);
285 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
291 * gst_system_clock_set_default:
292 * @new_clock: (allow-none): a #GstClock
294 * Sets the default system clock that can be obtained with
295 * gst_system_clock_obtain().
297 * This is mostly used for testing and debugging purposes when you
298 * want to have control over the time reported by the default system
306 gst_system_clock_set_default (GstClock * new_clock)
310 g_mutex_lock (&_gst_sysclock_mutex);
311 clock = _the_system_clock;
314 g_object_unref (clock);
316 if (new_clock == NULL) {
317 GST_CAT_DEBUG (GST_CAT_CLOCK, "resetting default system clock");
318 _external_default_clock = FALSE;
320 GST_CAT_DEBUG (GST_CAT_CLOCK, "setting new default system clock to %p",
322 _external_default_clock = TRUE;
323 g_object_ref (new_clock);
325 _the_system_clock = new_clock;
326 g_mutex_unlock (&_gst_sysclock_mutex);
330 * gst_system_clock_obtain:
332 * Get a handle to the default system clock. The refcount of the
333 * clock will be increased so you need to unref the clock after
336 * Returns: (transfer full): the default clock.
341 gst_system_clock_obtain (void)
345 g_mutex_lock (&_gst_sysclock_mutex);
346 clock = _the_system_clock;
349 GST_CAT_DEBUG (GST_CAT_CLOCK, "creating new static system clock");
350 g_assert (!_external_default_clock);
351 clock = g_object_new (GST_TYPE_SYSTEM_CLOCK,
352 "name", "GstSystemClock", NULL);
354 /* Clear floating flag */
355 gst_object_ref_sink (clock);
356 _the_system_clock = clock;
357 g_mutex_unlock (&_gst_sysclock_mutex);
359 g_mutex_unlock (&_gst_sysclock_mutex);
360 GST_CAT_DEBUG (GST_CAT_CLOCK, "returning static system clock");
363 /* we ref it since we are a clock factory. */
364 gst_object_ref (clock);
369 gst_system_clock_remove_wakeup (GstSystemClock * sysclock)
371 g_return_if_fail (sysclock->priv->wakeup_count > 0);
373 sysclock->priv->wakeup_count--;
374 GST_CAT_DEBUG (GST_CAT_CLOCK, "reading control");
375 while (!gst_poll_read_control (sysclock->priv->timer)) {
376 if (errno == EWOULDBLOCK) {
377 /* Try again and give other threads the chance to do something */
381 /* Critical error, GstPoll will have printed a critical warning already */
385 GST_SYSTEM_CLOCK_BROADCAST (sysclock);
386 GST_CAT_DEBUG (GST_CAT_CLOCK, "wakeup count %d",
387 sysclock->priv->wakeup_count);
391 gst_system_clock_add_wakeup (GstSystemClock * sysclock)
393 GST_CAT_DEBUG (GST_CAT_CLOCK, "writing control");
394 gst_poll_write_control (sysclock->priv->timer);
395 sysclock->priv->wakeup_count++;
396 GST_CAT_DEBUG (GST_CAT_CLOCK, "wakeup count %d",
397 sysclock->priv->wakeup_count);
401 gst_system_clock_wait_wakeup (GstSystemClock * sysclock)
403 while (sysclock->priv->wakeup_count > 0) {
404 GST_SYSTEM_CLOCK_WAIT (sysclock);
408 /* this thread reads the sorted clock entries from the queue.
410 * It waits on each of them and fires the callback when the timeout occurs.
412 * When an entry in the queue was canceled before we wait for it, it is
415 * When waiting for an entry, it can become canceled, in that case we don't
416 * call the callback but move to the next item in the queue.
421 gst_system_clock_async_thread (GstClock * clock)
423 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
424 GstSystemClockPrivate *priv = sysclock->priv;
425 GstClockReturn status;
427 GST_CAT_DEBUG (GST_CAT_CLOCK, "enter system clock thread");
428 GST_OBJECT_LOCK (clock);
430 GST_SYSTEM_CLOCK_BROADCAST (clock);
431 /* now enter our (almost) infinite loop */
432 while (!priv->stopping) {
433 GstClockEntry *entry;
434 GstClockTime requested;
437 /* check if something to be done */
438 while (priv->entries == NULL) {
439 GST_CAT_DEBUG (GST_CAT_CLOCK, "no clock entries, waiting..");
440 /* wait for work to do */
441 GST_SYSTEM_CLOCK_WAIT (clock);
442 GST_CAT_DEBUG (GST_CAT_CLOCK, "got signal");
443 /* clock was stopping, exit */
448 /* see if we have a pending wakeup because the order of the list
450 if (priv->async_wakeup) {
451 GST_CAT_DEBUG (GST_CAT_CLOCK, "clear async wakeup");
452 gst_system_clock_remove_wakeup (sysclock);
453 priv->async_wakeup = FALSE;
456 /* pick the next entry */
457 entry = priv->entries->data;
459 /* set entry status to busy before we release the clock lock */
461 status = GET_ENTRY_STATUS (entry);
463 /* check for unscheduled */
464 if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED)) {
465 /* entry was unscheduled, move to the next one */
466 GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry %p unscheduled", entry);
470 /* for periodic timers, status can be EARLY from a previous run */
471 if (G_UNLIKELY (status != GST_CLOCK_OK && status != GST_CLOCK_EARLY))
472 GST_CAT_ERROR (GST_CAT_CLOCK, "unexpected status %d for entry %p",
475 /* mark the entry as busy but watch out for intermediate unscheduled
477 } while (G_UNLIKELY (!CAS_ENTRY_STATUS (entry, status, GST_CLOCK_BUSY)));
479 GST_OBJECT_UNLOCK (clock);
481 requested = entry->time;
483 /* now wait for the entry */
485 gst_system_clock_id_wait_jitter_unlocked (clock, (GstClockID) entry,
488 GST_OBJECT_LOCK (clock);
491 case GST_CLOCK_UNSCHEDULED:
492 /* entry was unscheduled, move to the next */
493 GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry %p unscheduled", entry);
496 case GST_CLOCK_EARLY:
498 /* entry timed out normally, fire the callback and move to the next
500 GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry %p timed out", entry);
502 /* unlock before firing the callback */
503 GST_OBJECT_UNLOCK (clock);
504 entry->func (clock, entry->time, (GstClockID) entry,
506 GST_OBJECT_LOCK (clock);
508 if (entry->type == GST_CLOCK_ENTRY_PERIODIC) {
509 GST_CAT_DEBUG (GST_CAT_CLOCK, "updating periodic entry %p", entry);
510 /* adjust time now */
511 entry->time = requested + entry->interval;
512 /* and resort the list now */
514 g_list_sort (priv->entries, gst_clock_id_compare_func);
518 GST_CAT_DEBUG (GST_CAT_CLOCK, "moving to next entry");
523 /* somebody unlocked the entry but is was not canceled, This means that
524 * either a new entry was added in front of the queue or some other entry
525 * was canceled. Whatever it is, pick the head entry of the list and
526 * continue waiting. */
527 GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry %p needs restart", entry);
529 /* we set the entry back to the OK state. This is needed so that the
530 * _unschedule() code can see if an entry is currently being waited
531 * on (when its state is BUSY). */
532 SET_ENTRY_STATUS (entry, GST_CLOCK_OK);
535 GST_CAT_DEBUG (GST_CAT_CLOCK,
536 "strange result %d waiting for %p, skipping", res, entry);
537 g_warning ("%s: strange result %d waiting for %p, skipping",
538 GST_OBJECT_NAME (clock), res, entry);
542 /* we remove the current entry and unref it */
543 priv->entries = g_list_remove (priv->entries, entry);
544 gst_clock_id_unref ((GstClockID) entry);
548 GST_SYSTEM_CLOCK_BROADCAST (clock);
549 GST_OBJECT_UNLOCK (clock);
550 GST_CAT_DEBUG (GST_CAT_CLOCK, "exit system clock thread");
553 #ifdef HAVE_POSIX_TIMERS
554 static inline clockid_t
555 clock_type_to_posix_id (GstClockType clock_type)
557 #ifdef HAVE_MONOTONIC_CLOCK
558 if (clock_type == GST_CLOCK_TYPE_MONOTONIC)
559 return CLOCK_MONOTONIC;
562 return CLOCK_REALTIME;
568 gst_system_clock_get_internal_time (GstClock * clock)
570 #if defined __APPLE__
571 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
572 uint64_t mach_t = mach_absolute_time ();
573 return gst_util_uint64_scale (mach_t, sysclock->priv->mach_timebase.numer,
574 sysclock->priv->mach_timebase.denom);
577 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
579 if (sysclock->priv->frequency.QuadPart != 0) {
582 /* we prefer the highly accurate performance counters on windows */
583 QueryPerformanceCounter (&now);
585 return ((now.QuadPart -
586 sysclock->priv->start.QuadPart) * sysclock->priv->ratio);
588 #endif /* G_OS_WIN32 */
589 #if !defined HAVE_POSIX_TIMERS || !defined HAVE_CLOCK_GETTIME
593 g_get_current_time (&timeval);
595 return GST_TIMEVAL_TO_TIME (timeval);
599 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
603 ptype = clock_type_to_posix_id (sysclock->priv->clock_type);
605 if (G_UNLIKELY (clock_gettime (ptype, &ts)))
606 return GST_CLOCK_TIME_NONE;
608 return GST_TIMESPEC_TO_TIME (ts);
611 #endif /* __APPLE__ */
615 gst_system_clock_get_resolution (GstClock * clock)
617 #if defined __APPLE__
618 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
619 return gst_util_uint64_scale (GST_NSECOND,
620 sysclock->priv->mach_timebase.numer, sysclock->priv->mach_timebase.denom);
623 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
625 if (sysclock->priv->frequency.QuadPart != 0) {
626 return GST_SECOND / sysclock->priv->frequency.QuadPart;
628 #endif /* G_OS_WIN32 */
629 #if defined(HAVE_POSIX_TIMERS) && defined(HAVE_CLOCK_GETTIME)
631 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
635 ptype = clock_type_to_posix_id (sysclock->priv->clock_type);
637 if (G_UNLIKELY (clock_getres (ptype, &ts)))
638 return GST_CLOCK_TIME_NONE;
640 return GST_TIMESPEC_TO_TIME (ts);
644 return 1 * GST_USECOND;
647 #endif /* __APPLE__ */
651 gst_system_clock_cleanup_unscheduled (GstSystemClock * sysclock,
652 GstClockEntry * entry)
655 * The unschedule function managed to set the status to
656 * unscheduled. We now take the lock and mark the entry as unscheduled.
657 * This makes sure that the unschedule function doesn't perform a
658 * wakeup anymore. If the unschedule function has a change to perform
659 * the wakeup before us, we clean up here */
660 GST_OBJECT_LOCK (sysclock);
661 entry->unscheduled = TRUE;
662 if (entry->woken_up) {
663 gst_system_clock_remove_wakeup (sysclock);
665 GST_OBJECT_UNLOCK (sysclock);
668 /* synchronously wait on the given GstClockEntry.
670 * We do this by blocking on the global GstPoll timer with
671 * the requested timeout. This allows us to unblock the
672 * entry by writing on the control fd.
674 * Note that writing the global GstPoll unlocks all waiting entries. So
675 * we need to check if an unlocked entry has changed when it unlocks.
677 * Entries that arrive too late are simply not waited on and a
678 * GST_CLOCK_EARLY result is returned.
682 static GstClockReturn
683 gst_system_clock_id_wait_jitter_unlocked (GstClock * clock,
684 GstClockEntry * entry, GstClockTimeDiff * jitter, gboolean restart)
686 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
687 GstClockTime entryt, now;
688 GstClockTimeDiff diff;
689 GstClockReturn status;
691 status = GET_ENTRY_STATUS (entry);
692 if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED)) {
693 gst_system_clock_cleanup_unscheduled (sysclock, entry);
694 return GST_CLOCK_UNSCHEDULED;
697 /* need to call the overridden method because we want to sync against the time
698 * of the clock, whatever the subclass uses as a clock. */
699 now = gst_clock_get_time (clock);
701 /* get the time of the entry */
702 entryt = GST_CLOCK_ENTRY_TIME (entry);
704 /* the diff of the entry with the clock is the amount of time we have to
706 diff = GST_CLOCK_DIFF (now, entryt);
707 if (G_LIKELY (jitter))
710 GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p"
711 " time %" GST_TIME_FORMAT
712 " now %" GST_TIME_FORMAT
713 " diff (time-now) %" G_GINT64_FORMAT,
714 entry, GST_TIME_ARGS (entryt), GST_TIME_ARGS (now), diff);
716 if (G_LIKELY (diff > 0)) {
717 #ifdef WAIT_DEBUGGING
724 /* now wait on the entry, it either times out or the fd is written. The
725 * status of the entry is BUSY only around the poll. */
726 pollret = gst_poll_wait (sysclock->priv->timer, diff);
728 /* get the new status, mark as DONE. We do this so that the unschedule
729 * function knows when we left the poll and doesn't need to wakeup the
732 status = GET_ENTRY_STATUS (entry);
733 /* we were unscheduled, exit immediately */
734 if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED))
736 if (G_UNLIKELY (status != GST_CLOCK_BUSY))
737 GST_CAT_ERROR (GST_CAT_CLOCK, "unexpected status %d for entry %p",
739 } while (G_UNLIKELY (!CAS_ENTRY_STATUS (entry, status, GST_CLOCK_DONE)));
741 GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p unlocked, status %d, ret %d",
742 entry, status, pollret);
744 if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED)) {
745 gst_system_clock_cleanup_unscheduled (sysclock, entry);
748 if (G_UNLIKELY (pollret != 0)) {
749 /* some other id got unlocked */
751 /* this can happen if the entry got unlocked because of an async
752 * entry was added to the head of the async queue. */
753 GST_CAT_DEBUG (GST_CAT_CLOCK, "wakeup waiting for entry %p", entry);
757 /* wait till all the entries got woken up */
758 GST_OBJECT_LOCK (sysclock);
759 gst_system_clock_wait_wakeup (sysclock);
760 GST_OBJECT_UNLOCK (sysclock);
762 GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p needs to be restarted",
765 GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p unlocked after timeout",
769 /* reschedule if gst_poll_wait returned early or we have to reschedule after
771 now = gst_clock_get_time (clock);
772 diff = GST_CLOCK_DIFF (now, entryt);
775 /* timeout, this is fine, we can report success now */
776 if (G_UNLIKELY (!CAS_ENTRY_STATUS (entry, GST_CLOCK_DONE,
778 status = GET_ENTRY_STATUS (entry);
779 if (status != GST_CLOCK_UNSCHEDULED)
780 GST_CAT_ERROR (GST_CAT_CLOCK, "unexpected status %d for entry %p",
784 status = GST_CLOCK_OK;
787 GST_CAT_DEBUG (GST_CAT_CLOCK,
788 "entry %p finished, diff %" G_GINT64_FORMAT, entry, diff);
790 #ifdef WAIT_DEBUGGING
791 final = gst_system_clock_get_internal_time (clock);
792 GST_CAT_DEBUG (GST_CAT_CLOCK, "Waited for %" G_GINT64_FORMAT
793 " got %" G_GINT64_FORMAT " diff %" G_GINT64_FORMAT
794 " %g target-offset %" G_GINT64_FORMAT " %g", entryt, now,
796 (double) (GstClockTimeDiff) (now - entryt) / GST_SECOND,
798 ((double) (GstClockTimeDiff) (final - target)) / GST_SECOND);
802 GST_CAT_DEBUG (GST_CAT_CLOCK,
803 "entry %p restart, diff %" G_GINT64_FORMAT, entry, diff);
804 /* we are going to poll again, set status back to busy */
806 status = GET_ENTRY_STATUS (entry);
807 /* we were unscheduled, exit immediately */
808 if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED))
810 if (G_UNLIKELY (status != GST_CLOCK_DONE))
811 GST_CAT_ERROR (GST_CAT_CLOCK, "unexpected status %d for entry %p",
813 } while (G_UNLIKELY (!CAS_ENTRY_STATUS (entry, status,
819 /* we are right on time or too late */
820 if (G_UNLIKELY (diff == 0)) {
821 if (G_UNLIKELY (!CAS_ENTRY_STATUS (entry, status, GST_CLOCK_OK))) {
822 status = GET_ENTRY_STATUS (entry);
823 if (G_LIKELY (status == GST_CLOCK_UNSCHEDULED))
824 gst_system_clock_cleanup_unscheduled (sysclock, entry);
826 GST_CAT_ERROR (GST_CAT_CLOCK, "unexpected status %d for entry %p",
829 status = GST_CLOCK_OK;
832 if (G_UNLIKELY (!CAS_ENTRY_STATUS (entry, status, GST_CLOCK_EARLY))) {
833 status = GET_ENTRY_STATUS (entry);
834 if (G_LIKELY (status == GST_CLOCK_UNSCHEDULED))
835 gst_system_clock_cleanup_unscheduled (sysclock, entry);
837 GST_CAT_ERROR (GST_CAT_CLOCK, "unexpected status %d for entry %p",
840 status = GST_CLOCK_EARLY;
848 static GstClockReturn
849 gst_system_clock_id_wait_jitter (GstClock * clock, GstClockEntry * entry,
850 GstClockTimeDiff * jitter)
852 GstClockReturn status;
854 status = GET_ENTRY_STATUS (entry);
856 /* stop when we are unscheduled */
857 if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED))
860 if (G_UNLIKELY (status != GST_CLOCK_OK))
861 GST_CAT_ERROR (GST_CAT_CLOCK, "unexpected status %d for entry %p",
864 /* mark the entry as busy but watch out for intermediate unscheduled
866 } while (G_UNLIKELY (!CAS_ENTRY_STATUS (entry, status, GST_CLOCK_BUSY)));
868 return gst_system_clock_id_wait_jitter_unlocked (clock, entry, jitter, TRUE);
871 /* Start the async clock thread. Must be called with the object lock
874 gst_system_clock_start_async (GstSystemClock * clock)
876 GError *error = NULL;
877 GstSystemClockPrivate *priv = clock->priv;
879 if (G_LIKELY (priv->thread != NULL))
880 return TRUE; /* Thread already running. Nothing to do */
882 priv->thread = g_thread_try_new ("GstSystemClock",
883 (GThreadFunc) gst_system_clock_async_thread, clock, &error);
885 if (G_UNLIKELY (error))
888 /* wait for it to spin up */
889 GST_SYSTEM_CLOCK_WAIT (clock);
896 g_warning ("could not create async clock thread: %s", error->message);
897 g_error_free (error);
902 /* Add an entry to the list of pending async waits. The entry is inserted
903 * in sorted order. If we inserted the entry at the head of the list, we
904 * need to signal the thread as it might either be waiting on it or waiting
909 static GstClockReturn
910 gst_system_clock_id_wait_async (GstClock * clock, GstClockEntry * entry)
912 GstSystemClock *sysclock;
913 GstSystemClockPrivate *priv;
916 sysclock = GST_SYSTEM_CLOCK_CAST (clock);
917 priv = sysclock->priv;
919 GST_CAT_DEBUG (GST_CAT_CLOCK, "adding async entry %p", entry);
921 GST_OBJECT_LOCK (clock);
922 /* Start the clock async thread if needed */
923 if (G_UNLIKELY (!gst_system_clock_start_async (sysclock)))
926 if (G_UNLIKELY (GET_ENTRY_STATUS (entry) == GST_CLOCK_UNSCHEDULED))
927 goto was_unscheduled;
930 head = priv->entries->data;
934 /* need to take a ref */
935 gst_clock_id_ref ((GstClockID) entry);
936 /* insert the entry in sorted order */
937 priv->entries = g_list_insert_sorted (priv->entries, entry,
938 gst_clock_id_compare_func);
940 /* only need to send the signal if the entry was added to the
941 * front, else the thread is just waiting for another entry and
942 * will get to this entry automatically. */
943 if (priv->entries->data == entry) {
944 GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry added to head %p", head);
946 /* the list was empty before, signal the cond so that the async thread can
947 * start taking a look at the queue */
948 GST_CAT_DEBUG (GST_CAT_CLOCK, "first entry, sending signal");
949 GST_SYSTEM_CLOCK_BROADCAST (clock);
951 GstClockReturn status;
953 status = GET_ENTRY_STATUS (head);
954 GST_CAT_DEBUG (GST_CAT_CLOCK, "head entry %p status %d", head, status);
956 if (status == GST_CLOCK_BUSY) {
957 GST_CAT_DEBUG (GST_CAT_CLOCK, "head entry is busy");
958 /* the async thread was waiting for an entry, unlock the wait so that it
959 * looks at the new head entry instead, we only need to do this once */
960 if (!priv->async_wakeup) {
961 GST_CAT_DEBUG (GST_CAT_CLOCK, "wakeup async thread");
962 priv->async_wakeup = TRUE;
963 gst_system_clock_add_wakeup (sysclock);
968 GST_OBJECT_UNLOCK (clock);
975 /* Could not start the async clock thread */
976 GST_OBJECT_UNLOCK (clock);
977 return GST_CLOCK_ERROR;
981 GST_OBJECT_UNLOCK (clock);
982 return GST_CLOCK_UNSCHEDULED;
986 /* unschedule an entry. This will set the state of the entry to GST_CLOCK_UNSCHEDULED
987 * and will signal any thread waiting for entries to recheck their entry.
988 * We cannot really decide if the signal is needed or not because the entry
989 * could be waited on in async or sync mode.
994 gst_system_clock_id_unschedule (GstClock * clock, GstClockEntry * entry)
996 GstSystemClock *sysclock;
997 GstClockReturn status;
999 sysclock = GST_SYSTEM_CLOCK_CAST (clock);
1001 GST_CAT_DEBUG (GST_CAT_CLOCK, "unscheduling entry %p", entry);
1003 GST_OBJECT_LOCK (clock);
1004 /* change the entry status to unscheduled */
1006 status = GET_ENTRY_STATUS (entry);
1007 } while (G_UNLIKELY (!CAS_ENTRY_STATUS (entry, status,
1008 GST_CLOCK_UNSCHEDULED)));
1010 if (G_LIKELY (status == GST_CLOCK_BUSY)) {
1011 /* the entry was being busy, wake up all entries so that they recheck their
1012 * status. We cannot wake up just one entry because allocating such a
1013 * datastructure for each entry would be too heavy and unlocking an entry
1014 * is usually done when shutting down or some other exceptional case. */
1015 GST_CAT_DEBUG (GST_CAT_CLOCK, "entry was BUSY, doing wakeup");
1016 if (!entry->unscheduled && !entry->woken_up) {
1017 gst_system_clock_add_wakeup (sysclock);
1018 entry->woken_up = TRUE;
1021 GST_OBJECT_UNLOCK (clock);