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;
92 #endif /* G_OS_WIN32 */
94 struct mach_timebase_info mach_timebase;
98 #ifdef HAVE_POSIX_TIMERS
99 # ifdef HAVE_MONOTONIC_CLOCK
100 # define DEFAULT_CLOCK_TYPE GST_CLOCK_TYPE_MONOTONIC
102 # define DEFAULT_CLOCK_TYPE GST_CLOCK_TYPE_REALTIME
105 #define DEFAULT_CLOCK_TYPE GST_CLOCK_TYPE_REALTIME
115 /* the one instance of the systemclock */
116 static GstClock *_the_system_clock = NULL;
117 static gboolean _external_default_clock = FALSE;
119 static void gst_system_clock_dispose (GObject * object);
120 static void gst_system_clock_set_property (GObject * object, guint prop_id,
121 const GValue * value, GParamSpec * pspec);
122 static void gst_system_clock_get_property (GObject * object, guint prop_id,
123 GValue * value, GParamSpec * pspec);
125 static GstClockTime gst_system_clock_get_internal_time (GstClock * clock);
126 static guint64 gst_system_clock_get_resolution (GstClock * clock);
127 static GstClockReturn gst_system_clock_id_wait_jitter (GstClock * clock,
128 GstClockEntry * entry, GstClockTimeDiff * jitter);
129 static GstClockReturn gst_system_clock_id_wait_jitter_unlocked
130 (GstClock * clock, GstClockEntry * entry, GstClockTimeDiff * jitter,
132 static GstClockReturn gst_system_clock_id_wait_async (GstClock * clock,
133 GstClockEntry * entry);
134 static void gst_system_clock_id_unschedule (GstClock * clock,
135 GstClockEntry * entry);
136 static void gst_system_clock_async_thread (GstClock * clock);
137 static gboolean gst_system_clock_start_async (GstSystemClock * clock);
138 static void gst_system_clock_add_wakeup (GstSystemClock * sysclock);
140 static GMutex _gst_sysclock_mutex;
142 /* static guint gst_system_clock_signals[LAST_SIGNAL] = { 0 }; */
144 #define gst_system_clock_parent_class parent_class
145 G_DEFINE_TYPE_WITH_PRIVATE (GstSystemClock, gst_system_clock, GST_TYPE_CLOCK);
148 gst_system_clock_class_init (GstSystemClockClass * klass)
150 GObjectClass *gobject_class;
151 GstClockClass *gstclock_class;
153 gobject_class = (GObjectClass *) klass;
154 gstclock_class = (GstClockClass *) klass;
156 gobject_class->dispose = gst_system_clock_dispose;
157 gobject_class->set_property = gst_system_clock_set_property;
158 gobject_class->get_property = gst_system_clock_get_property;
160 g_object_class_install_property (gobject_class, PROP_CLOCK_TYPE,
161 g_param_spec_enum ("clock-type", "Clock type",
162 "The type of underlying clock implementation used",
163 GST_TYPE_CLOCK_TYPE, DEFAULT_CLOCK_TYPE,
164 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
166 gstclock_class->get_internal_time = gst_system_clock_get_internal_time;
167 gstclock_class->get_resolution = gst_system_clock_get_resolution;
168 gstclock_class->wait = gst_system_clock_id_wait_jitter;
169 gstclock_class->wait_async = gst_system_clock_id_wait_async;
170 gstclock_class->unschedule = gst_system_clock_id_unschedule;
174 gst_system_clock_init (GstSystemClock * clock)
176 GstSystemClockPrivate *priv;
178 GST_OBJECT_FLAG_SET (clock,
179 GST_CLOCK_FLAG_CAN_DO_SINGLE_SYNC |
180 GST_CLOCK_FLAG_CAN_DO_SINGLE_ASYNC |
181 GST_CLOCK_FLAG_CAN_DO_PERIODIC_SYNC |
182 GST_CLOCK_FLAG_CAN_DO_PERIODIC_ASYNC);
184 clock->priv = priv = gst_system_clock_get_instance_private (clock);
186 priv->clock_type = DEFAULT_CLOCK_TYPE;
187 priv->timer = gst_poll_new_timer ();
189 priv->entries = NULL;
190 g_cond_init (&priv->entries_changed);
193 QueryPerformanceFrequency (&priv->frequency);
194 /* can be 0 if the hardware does not have hardware support */
195 if (priv->frequency.QuadPart != 0)
196 /* we take a base time so that time starts from 0 to ease debugging */
197 QueryPerformanceCounter (&priv->start);
198 #endif /* G_OS_WIN32 */
201 mach_timebase_info (&priv->mach_timebase);
205 /* Uncomment this to start the async clock thread straight away */
206 GST_OBJECT_LOCK (clock);
207 gst_system_clock_start_async (clock);
208 GST_OBJECT_UNLOCK (clock);
213 gst_system_clock_dispose (GObject * object)
215 GstClock *clock = (GstClock *) object;
216 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
217 GstSystemClockPrivate *priv = sysclock->priv;
220 /* else we have to stop the thread */
221 GST_OBJECT_LOCK (clock);
222 priv->stopping = TRUE;
223 /* unschedule all entries */
224 for (entries = priv->entries; entries; entries = g_list_next (entries)) {
225 GstClockEntry *entry = (GstClockEntry *) entries->data;
227 GST_CAT_DEBUG (GST_CAT_CLOCK, "unscheduling entry %p", entry);
228 SET_ENTRY_STATUS (entry, GST_CLOCK_UNSCHEDULED);
230 GST_SYSTEM_CLOCK_BROADCAST (clock);
231 gst_system_clock_add_wakeup (sysclock);
232 GST_OBJECT_UNLOCK (clock);
235 g_thread_join (priv->thread);
237 GST_CAT_DEBUG (GST_CAT_CLOCK, "joined thread");
239 g_list_foreach (priv->entries, (GFunc) gst_clock_id_unref, NULL);
240 g_list_free (priv->entries);
241 priv->entries = NULL;
243 gst_poll_free (priv->timer);
244 g_cond_clear (&priv->entries_changed);
246 G_OBJECT_CLASS (parent_class)->dispose (object);
248 if (_the_system_clock == clock) {
249 _the_system_clock = NULL;
250 GST_CAT_DEBUG (GST_CAT_CLOCK, "disposed system clock");
255 gst_system_clock_set_property (GObject * object, guint prop_id,
256 const GValue * value, GParamSpec * pspec)
258 GstSystemClock *sysclock = GST_SYSTEM_CLOCK (object);
261 case PROP_CLOCK_TYPE:
262 sysclock->priv->clock_type = (GstClockType) g_value_get_enum (value);
263 GST_CAT_DEBUG (GST_CAT_CLOCK, "clock-type set to %d",
264 sysclock->priv->clock_type);
267 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
273 gst_system_clock_get_property (GObject * object, guint prop_id, GValue * value,
276 GstSystemClock *sysclock = GST_SYSTEM_CLOCK (object);
279 case PROP_CLOCK_TYPE:
280 g_value_set_enum (value, sysclock->priv->clock_type);
283 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
289 * gst_system_clock_set_default:
290 * @new_clock: (allow-none): a #GstClock
292 * Sets the default system clock that can be obtained with
293 * gst_system_clock_obtain().
295 * This is mostly used for testing and debugging purposes when you
296 * want to have control over the time reported by the default system
304 gst_system_clock_set_default (GstClock * new_clock)
308 g_mutex_lock (&_gst_sysclock_mutex);
309 clock = _the_system_clock;
312 g_object_unref (clock);
314 if (new_clock == NULL) {
315 GST_CAT_DEBUG (GST_CAT_CLOCK, "resetting default system clock");
316 _external_default_clock = FALSE;
318 GST_CAT_DEBUG (GST_CAT_CLOCK, "setting new default system clock to %p",
320 _external_default_clock = TRUE;
321 g_object_ref (new_clock);
323 _the_system_clock = new_clock;
324 g_mutex_unlock (&_gst_sysclock_mutex);
328 * gst_system_clock_obtain:
330 * Get a handle to the default system clock. The refcount of the
331 * clock will be increased so you need to unref the clock after
334 * Returns: (transfer full): the default clock.
339 gst_system_clock_obtain (void)
343 g_mutex_lock (&_gst_sysclock_mutex);
344 clock = _the_system_clock;
347 GST_CAT_DEBUG (GST_CAT_CLOCK, "creating new static system clock");
348 g_assert (!_external_default_clock);
349 clock = g_object_new (GST_TYPE_SYSTEM_CLOCK,
350 "name", "GstSystemClock", NULL);
352 /* Clear floating flag */
353 gst_object_ref_sink (clock);
354 _the_system_clock = clock;
355 g_mutex_unlock (&_gst_sysclock_mutex);
357 g_mutex_unlock (&_gst_sysclock_mutex);
358 GST_CAT_DEBUG (GST_CAT_CLOCK, "returning static system clock");
361 /* we ref it since we are a clock factory. */
362 gst_object_ref (clock);
367 gst_system_clock_remove_wakeup (GstSystemClock * sysclock)
369 g_return_if_fail (sysclock->priv->wakeup_count > 0);
371 sysclock->priv->wakeup_count--;
372 GST_CAT_DEBUG (GST_CAT_CLOCK, "reading control");
373 while (!gst_poll_read_control (sysclock->priv->timer)) {
374 if (errno == EWOULDBLOCK) {
375 /* Try again and give other threads the chance to do something */
379 /* Critical error, GstPoll will have printed a critical warning already */
383 GST_SYSTEM_CLOCK_BROADCAST (sysclock);
384 GST_CAT_DEBUG (GST_CAT_CLOCK, "wakeup count %d",
385 sysclock->priv->wakeup_count);
389 gst_system_clock_add_wakeup (GstSystemClock * sysclock)
391 GST_CAT_DEBUG (GST_CAT_CLOCK, "writing control");
392 gst_poll_write_control (sysclock->priv->timer);
393 sysclock->priv->wakeup_count++;
394 GST_CAT_DEBUG (GST_CAT_CLOCK, "wakeup count %d",
395 sysclock->priv->wakeup_count);
399 gst_system_clock_wait_wakeup (GstSystemClock * sysclock)
401 while (sysclock->priv->wakeup_count > 0) {
402 GST_SYSTEM_CLOCK_WAIT (sysclock);
406 /* this thread reads the sorted clock entries from the queue.
408 * It waits on each of them and fires the callback when the timeout occurs.
410 * When an entry in the queue was canceled before we wait for it, it is
413 * When waiting for an entry, it can become canceled, in that case we don't
414 * call the callback but move to the next item in the queue.
419 gst_system_clock_async_thread (GstClock * clock)
421 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
422 GstSystemClockPrivate *priv = sysclock->priv;
423 GstClockReturn status;
425 GST_CAT_DEBUG (GST_CAT_CLOCK, "enter system clock thread");
426 GST_OBJECT_LOCK (clock);
428 GST_SYSTEM_CLOCK_BROADCAST (clock);
429 /* now enter our (almost) infinite loop */
430 while (!priv->stopping) {
431 GstClockEntry *entry;
432 GstClockTime requested;
435 /* check if something to be done */
436 while (priv->entries == NULL) {
437 GST_CAT_DEBUG (GST_CAT_CLOCK, "no clock entries, waiting..");
438 /* wait for work to do */
439 GST_SYSTEM_CLOCK_WAIT (clock);
440 GST_CAT_DEBUG (GST_CAT_CLOCK, "got signal");
441 /* clock was stopping, exit */
446 /* see if we have a pending wakeup because the order of the list
448 if (priv->async_wakeup) {
449 GST_CAT_DEBUG (GST_CAT_CLOCK, "clear async wakeup");
450 gst_system_clock_remove_wakeup (sysclock);
451 priv->async_wakeup = FALSE;
454 /* pick the next entry */
455 entry = priv->entries->data;
457 /* set entry status to busy before we release the clock lock */
459 status = GET_ENTRY_STATUS (entry);
461 /* check for unscheduled */
462 if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED)) {
463 /* entry was unscheduled, move to the next one */
464 GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry %p unscheduled", entry);
468 /* for periodic timers, status can be EARLY from a previous run */
469 if (G_UNLIKELY (status != GST_CLOCK_OK && status != GST_CLOCK_EARLY))
470 GST_CAT_ERROR (GST_CAT_CLOCK, "unexpected status %d for entry %p",
473 /* mark the entry as busy but watch out for intermediate unscheduled
475 } while (G_UNLIKELY (!CAS_ENTRY_STATUS (entry, status, GST_CLOCK_BUSY)));
477 GST_OBJECT_UNLOCK (clock);
479 requested = entry->time;
481 /* now wait for the entry */
483 gst_system_clock_id_wait_jitter_unlocked (clock, (GstClockID) entry,
486 GST_OBJECT_LOCK (clock);
489 case GST_CLOCK_UNSCHEDULED:
490 /* entry was unscheduled, move to the next */
491 GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry %p unscheduled", entry);
494 case GST_CLOCK_EARLY:
496 /* entry timed out normally, fire the callback and move to the next
498 GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry %p timed out", entry);
500 /* unlock before firing the callback */
501 GST_OBJECT_UNLOCK (clock);
502 entry->func (clock, entry->time, (GstClockID) entry,
504 GST_OBJECT_LOCK (clock);
506 if (entry->type == GST_CLOCK_ENTRY_PERIODIC) {
507 GST_CAT_DEBUG (GST_CAT_CLOCK, "updating periodic entry %p", entry);
508 /* adjust time now */
509 entry->time = requested + entry->interval;
510 /* and resort the list now */
512 g_list_sort (priv->entries, gst_clock_id_compare_func);
516 GST_CAT_DEBUG (GST_CAT_CLOCK, "moving to next entry");
521 /* somebody unlocked the entry but is was not canceled, This means that
522 * either a new entry was added in front of the queue or some other entry
523 * was canceled. Whatever it is, pick the head entry of the list and
524 * continue waiting. */
525 GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry %p needs restart", entry);
527 /* we set the entry back to the OK state. This is needed so that the
528 * _unschedule() code can see if an entry is currently being waited
529 * on (when its state is BUSY). */
530 SET_ENTRY_STATUS (entry, GST_CLOCK_OK);
533 GST_CAT_DEBUG (GST_CAT_CLOCK,
534 "strange result %d waiting for %p, skipping", res, entry);
535 g_warning ("%s: strange result %d waiting for %p, skipping",
536 GST_OBJECT_NAME (clock), res, entry);
540 /* we remove the current entry and unref it */
541 priv->entries = g_list_remove (priv->entries, entry);
542 gst_clock_id_unref ((GstClockID) entry);
546 GST_SYSTEM_CLOCK_BROADCAST (clock);
547 GST_OBJECT_UNLOCK (clock);
548 GST_CAT_DEBUG (GST_CAT_CLOCK, "exit system clock thread");
551 #ifdef HAVE_POSIX_TIMERS
552 static inline clockid_t
553 clock_type_to_posix_id (GstClockType clock_type)
555 #ifdef HAVE_MONOTONIC_CLOCK
556 if (clock_type == GST_CLOCK_TYPE_MONOTONIC)
557 return CLOCK_MONOTONIC;
560 return CLOCK_REALTIME;
566 gst_system_clock_get_internal_time (GstClock * clock)
568 #if defined __APPLE__
569 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
570 uint64_t mach_t = mach_absolute_time ();
571 return gst_util_uint64_scale (mach_t, sysclock->priv->mach_timebase.numer,
572 sysclock->priv->mach_timebase.denom);
575 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
577 if (sysclock->priv->frequency.QuadPart != 0) {
580 /* we prefer the highly accurate performance counters on windows */
581 QueryPerformanceCounter (&now);
583 return gst_util_uint64_scale (now.QuadPart - sysclock->priv->start.QuadPart,
584 GST_SECOND, sysclock->priv->frequency.QuadPart);
586 #endif /* G_OS_WIN32 */
587 #if !defined HAVE_POSIX_TIMERS || !defined HAVE_CLOCK_GETTIME
591 g_get_current_time (&timeval);
593 return GST_TIMEVAL_TO_TIME (timeval);
597 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
601 ptype = clock_type_to_posix_id (sysclock->priv->clock_type);
603 if (G_UNLIKELY (clock_gettime (ptype, &ts)))
604 return GST_CLOCK_TIME_NONE;
606 return GST_TIMESPEC_TO_TIME (ts);
609 #endif /* __APPLE__ */
613 gst_system_clock_get_resolution (GstClock * clock)
615 #if defined __APPLE__
616 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
617 return gst_util_uint64_scale (GST_NSECOND,
618 sysclock->priv->mach_timebase.numer, sysclock->priv->mach_timebase.denom);
621 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
623 if (sysclock->priv->frequency.QuadPart != 0) {
624 return GST_SECOND / sysclock->priv->frequency.QuadPart;
626 #endif /* G_OS_WIN32 */
627 #if defined(HAVE_POSIX_TIMERS) && defined(HAVE_CLOCK_GETTIME)
629 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
633 ptype = clock_type_to_posix_id (sysclock->priv->clock_type);
635 if (G_UNLIKELY (clock_getres (ptype, &ts)))
636 return GST_CLOCK_TIME_NONE;
638 return GST_TIMESPEC_TO_TIME (ts);
642 return 1 * GST_USECOND;
645 #endif /* __APPLE__ */
649 gst_system_clock_cleanup_unscheduled (GstSystemClock * sysclock,
650 GstClockEntry * entry)
653 * The unschedule function managed to set the status to
654 * unscheduled. We now take the lock and mark the entry as unscheduled.
655 * This makes sure that the unschedule function doesn't perform a
656 * wakeup anymore. If the unschedule function has a change to perform
657 * the wakeup before us, we clean up here */
658 GST_OBJECT_LOCK (sysclock);
659 entry->unscheduled = TRUE;
660 if (entry->woken_up) {
661 gst_system_clock_remove_wakeup (sysclock);
663 GST_OBJECT_UNLOCK (sysclock);
666 /* synchronously wait on the given GstClockEntry.
668 * We do this by blocking on the global GstPoll timer with
669 * the requested timeout. This allows us to unblock the
670 * entry by writing on the control fd.
672 * Note that writing the global GstPoll unlocks all waiting entries. So
673 * we need to check if an unlocked entry has changed when it unlocks.
675 * Entries that arrive too late are simply not waited on and a
676 * GST_CLOCK_EARLY result is returned.
680 static GstClockReturn
681 gst_system_clock_id_wait_jitter_unlocked (GstClock * clock,
682 GstClockEntry * entry, GstClockTimeDiff * jitter, gboolean restart)
684 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
685 GstClockTime entryt, now;
686 GstClockTimeDiff diff;
687 GstClockReturn status;
689 status = GET_ENTRY_STATUS (entry);
690 if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED)) {
691 gst_system_clock_cleanup_unscheduled (sysclock, entry);
692 return GST_CLOCK_UNSCHEDULED;
695 /* need to call the overridden method because we want to sync against the time
696 * of the clock, whatever the subclass uses as a clock. */
697 now = gst_clock_get_time (clock);
699 /* get the time of the entry */
700 entryt = GST_CLOCK_ENTRY_TIME (entry);
702 /* the diff of the entry with the clock is the amount of time we have to
704 diff = GST_CLOCK_DIFF (now, entryt);
705 if (G_LIKELY (jitter))
708 GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p"
709 " time %" GST_TIME_FORMAT
710 " now %" GST_TIME_FORMAT
711 " diff (time-now) %" G_GINT64_FORMAT,
712 entry, GST_TIME_ARGS (entryt), GST_TIME_ARGS (now), diff);
714 if (G_LIKELY (diff > 0)) {
715 #ifdef WAIT_DEBUGGING
722 /* now wait on the entry, it either times out or the fd is written. The
723 * status of the entry is BUSY only around the poll. */
724 pollret = gst_poll_wait (sysclock->priv->timer, diff);
726 /* get the new status, mark as DONE. We do this so that the unschedule
727 * function knows when we left the poll and doesn't need to wakeup the
730 status = GET_ENTRY_STATUS (entry);
731 /* we were unscheduled, exit immediately */
732 if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED))
734 if (G_UNLIKELY (status != GST_CLOCK_BUSY))
735 GST_CAT_ERROR (GST_CAT_CLOCK, "unexpected status %d for entry %p",
737 } while (G_UNLIKELY (!CAS_ENTRY_STATUS (entry, status, GST_CLOCK_DONE)));
739 GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p unlocked, status %d, ret %d",
740 entry, status, pollret);
742 if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED)) {
743 gst_system_clock_cleanup_unscheduled (sysclock, entry);
746 if (G_UNLIKELY (pollret != 0)) {
747 /* some other id got unlocked */
749 /* this can happen if the entry got unlocked because of an async
750 * entry was added to the head of the async queue. */
751 GST_CAT_DEBUG (GST_CAT_CLOCK, "wakeup waiting for entry %p", entry);
755 /* wait till all the entries got woken up */
756 GST_OBJECT_LOCK (sysclock);
757 gst_system_clock_wait_wakeup (sysclock);
758 GST_OBJECT_UNLOCK (sysclock);
760 GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p needs to be restarted",
763 GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p unlocked after timeout",
767 /* reschedule if gst_poll_wait returned early or we have to reschedule after
769 now = gst_clock_get_time (clock);
770 diff = GST_CLOCK_DIFF (now, entryt);
773 /* timeout, this is fine, we can report success now */
774 if (G_UNLIKELY (!CAS_ENTRY_STATUS (entry, GST_CLOCK_DONE,
776 status = GET_ENTRY_STATUS (entry);
777 if (status != GST_CLOCK_UNSCHEDULED)
778 GST_CAT_ERROR (GST_CAT_CLOCK, "unexpected status %d for entry %p",
782 status = GST_CLOCK_OK;
785 GST_CAT_DEBUG (GST_CAT_CLOCK,
786 "entry %p finished, diff %" G_GINT64_FORMAT, entry, diff);
788 #ifdef WAIT_DEBUGGING
789 final = gst_system_clock_get_internal_time (clock);
790 GST_CAT_DEBUG (GST_CAT_CLOCK, "Waited for %" G_GINT64_FORMAT
791 " got %" G_GINT64_FORMAT " diff %" G_GINT64_FORMAT
792 " %g target-offset %" G_GINT64_FORMAT " %g", entryt, now,
794 (double) (GstClockTimeDiff) (now - entryt) / GST_SECOND,
796 ((double) (GstClockTimeDiff) (final - target)) / GST_SECOND);
800 GST_CAT_DEBUG (GST_CAT_CLOCK,
801 "entry %p restart, diff %" G_GINT64_FORMAT, entry, diff);
802 /* we are going to poll again, set status back to busy */
804 status = GET_ENTRY_STATUS (entry);
805 /* we were unscheduled, exit immediately */
806 if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED))
808 if (G_UNLIKELY (status != GST_CLOCK_DONE))
809 GST_CAT_ERROR (GST_CAT_CLOCK, "unexpected status %d for entry %p",
811 } while (G_UNLIKELY (!CAS_ENTRY_STATUS (entry, status,
817 /* we are right on time or too late */
818 if (G_UNLIKELY (diff == 0)) {
819 if (G_UNLIKELY (!CAS_ENTRY_STATUS (entry, status, GST_CLOCK_OK))) {
820 status = GET_ENTRY_STATUS (entry);
821 if (G_LIKELY (status == GST_CLOCK_UNSCHEDULED))
822 gst_system_clock_cleanup_unscheduled (sysclock, entry);
824 GST_CAT_ERROR (GST_CAT_CLOCK, "unexpected status %d for entry %p",
827 status = GST_CLOCK_OK;
830 if (G_UNLIKELY (!CAS_ENTRY_STATUS (entry, status, GST_CLOCK_EARLY))) {
831 status = GET_ENTRY_STATUS (entry);
832 if (G_LIKELY (status == GST_CLOCK_UNSCHEDULED))
833 gst_system_clock_cleanup_unscheduled (sysclock, entry);
835 GST_CAT_ERROR (GST_CAT_CLOCK, "unexpected status %d for entry %p",
838 status = GST_CLOCK_EARLY;
846 static GstClockReturn
847 gst_system_clock_id_wait_jitter (GstClock * clock, GstClockEntry * entry,
848 GstClockTimeDiff * jitter)
850 GstClockReturn status;
852 status = GET_ENTRY_STATUS (entry);
854 /* stop when we are unscheduled */
855 if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED))
858 if (G_UNLIKELY (status != GST_CLOCK_OK))
859 GST_CAT_ERROR (GST_CAT_CLOCK, "unexpected status %d for entry %p",
862 /* mark the entry as busy but watch out for intermediate unscheduled
864 } while (G_UNLIKELY (!CAS_ENTRY_STATUS (entry, status, GST_CLOCK_BUSY)));
866 return gst_system_clock_id_wait_jitter_unlocked (clock, entry, jitter, TRUE);
869 /* Start the async clock thread. Must be called with the object lock
872 gst_system_clock_start_async (GstSystemClock * clock)
874 GError *error = NULL;
875 GstSystemClockPrivate *priv = clock->priv;
877 if (G_LIKELY (priv->thread != NULL))
878 return TRUE; /* Thread already running. Nothing to do */
880 priv->thread = g_thread_try_new ("GstSystemClock",
881 (GThreadFunc) gst_system_clock_async_thread, clock, &error);
883 if (G_UNLIKELY (error))
886 /* wait for it to spin up */
887 GST_SYSTEM_CLOCK_WAIT (clock);
894 g_warning ("could not create async clock thread: %s", error->message);
895 g_error_free (error);
900 /* Add an entry to the list of pending async waits. The entry is inserted
901 * in sorted order. If we inserted the entry at the head of the list, we
902 * need to signal the thread as it might either be waiting on it or waiting
907 static GstClockReturn
908 gst_system_clock_id_wait_async (GstClock * clock, GstClockEntry * entry)
910 GstSystemClock *sysclock;
911 GstSystemClockPrivate *priv;
914 sysclock = GST_SYSTEM_CLOCK_CAST (clock);
915 priv = sysclock->priv;
917 GST_CAT_DEBUG (GST_CAT_CLOCK, "adding async entry %p", entry);
919 GST_OBJECT_LOCK (clock);
920 /* Start the clock async thread if needed */
921 if (G_UNLIKELY (!gst_system_clock_start_async (sysclock)))
924 if (G_UNLIKELY (GET_ENTRY_STATUS (entry) == GST_CLOCK_UNSCHEDULED))
925 goto was_unscheduled;
928 head = priv->entries->data;
932 /* need to take a ref */
933 gst_clock_id_ref ((GstClockID) entry);
934 /* insert the entry in sorted order */
935 priv->entries = g_list_insert_sorted (priv->entries, entry,
936 gst_clock_id_compare_func);
938 /* only need to send the signal if the entry was added to the
939 * front, else the thread is just waiting for another entry and
940 * will get to this entry automatically. */
941 if (priv->entries->data == entry) {
942 GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry added to head %p", head);
944 /* the list was empty before, signal the cond so that the async thread can
945 * start taking a look at the queue */
946 GST_CAT_DEBUG (GST_CAT_CLOCK, "first entry, sending signal");
947 GST_SYSTEM_CLOCK_BROADCAST (clock);
949 GstClockReturn status;
951 status = GET_ENTRY_STATUS (head);
952 GST_CAT_DEBUG (GST_CAT_CLOCK, "head entry %p status %d", head, status);
954 if (status == GST_CLOCK_BUSY) {
955 GST_CAT_DEBUG (GST_CAT_CLOCK, "head entry is busy");
956 /* the async thread was waiting for an entry, unlock the wait so that it
957 * looks at the new head entry instead, we only need to do this once */
958 if (!priv->async_wakeup) {
959 GST_CAT_DEBUG (GST_CAT_CLOCK, "wakeup async thread");
960 priv->async_wakeup = TRUE;
961 gst_system_clock_add_wakeup (sysclock);
966 GST_OBJECT_UNLOCK (clock);
973 /* Could not start the async clock thread */
974 GST_OBJECT_UNLOCK (clock);
975 return GST_CLOCK_ERROR;
979 GST_OBJECT_UNLOCK (clock);
980 return GST_CLOCK_UNSCHEDULED;
984 /* unschedule an entry. This will set the state of the entry to GST_CLOCK_UNSCHEDULED
985 * and will signal any thread waiting for entries to recheck their entry.
986 * We cannot really decide if the signal is needed or not because the entry
987 * could be waited on in async or sync mode.
992 gst_system_clock_id_unschedule (GstClock * clock, GstClockEntry * entry)
994 GstSystemClock *sysclock;
995 GstClockReturn status;
997 sysclock = GST_SYSTEM_CLOCK_CAST (clock);
999 GST_CAT_DEBUG (GST_CAT_CLOCK, "unscheduling entry %p", entry);
1001 GST_OBJECT_LOCK (clock);
1002 /* change the entry status to unscheduled */
1004 status = GET_ENTRY_STATUS (entry);
1005 } while (G_UNLIKELY (!CAS_ENTRY_STATUS (entry, status,
1006 GST_CLOCK_UNSCHEDULED)));
1008 if (G_LIKELY (status == GST_CLOCK_BUSY)) {
1009 /* the entry was being busy, wake up all entries so that they recheck their
1010 * status. We cannot wake up just one entry because allocating such a
1011 * datastructure for each entry would be too heavy and unlocking an entry
1012 * is usually done when shutting down or some other exceptional case. */
1013 GST_CAT_DEBUG (GST_CAT_CLOCK, "entry was BUSY, doing wakeup");
1014 if (!entry->unscheduled && !entry->woken_up) {
1015 gst_system_clock_add_wakeup (sysclock);
1016 entry->woken_up = TRUE;
1019 GST_OBJECT_UNLOCK (clock);