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) (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 struct _GstSystemClockPrivate
68 GstClockType clock_type;
70 gint wakeup_count; /* the number of entries with a pending wakeup */
71 gboolean async_wakeup; /* if the wakeup was because of a async list change */
75 LARGE_INTEGER frequency;
76 #endif /* G_OS_WIN32 */
79 #define GST_SYSTEM_CLOCK_GET_PRIVATE(obj) \
80 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_SYSTEM_CLOCK, \
81 GstSystemClockPrivate))
83 #ifdef HAVE_POSIX_TIMERS
84 # ifdef HAVE_MONOTONIC_CLOCK
85 # define DEFAULT_CLOCK_TYPE GST_CLOCK_TYPE_MONOTONIC
87 # define DEFAULT_CLOCK_TYPE GST_CLOCK_TYPE_REALTIME
90 #define DEFAULT_CLOCK_TYPE GST_CLOCK_TYPE_REALTIME
100 /* the one instance of the systemclock */
101 static GstClock *_the_system_clock = NULL;
103 static void gst_system_clock_dispose (GObject * object);
104 static void gst_system_clock_set_property (GObject * object, guint prop_id,
105 const GValue * value, GParamSpec * pspec);
106 static void gst_system_clock_get_property (GObject * object, guint prop_id,
107 GValue * value, GParamSpec * pspec);
109 static GstClockTime gst_system_clock_get_internal_time (GstClock * clock);
110 static guint64 gst_system_clock_get_resolution (GstClock * clock);
111 static GstClockReturn gst_system_clock_id_wait_jitter (GstClock * clock,
112 GstClockEntry * entry, GstClockTimeDiff * jitter);
113 static GstClockReturn gst_system_clock_id_wait_jitter_unlocked
114 (GstClock * clock, GstClockEntry * entry, GstClockTimeDiff * jitter,
116 static GstClockReturn gst_system_clock_id_wait_async (GstClock * clock,
117 GstClockEntry * entry);
118 static void gst_system_clock_id_unschedule (GstClock * clock,
119 GstClockEntry * entry);
120 static void gst_system_clock_async_thread (GstClock * clock);
121 static gboolean gst_system_clock_start_async (GstSystemClock * clock);
122 static void gst_system_clock_add_wakeup (GstSystemClock * sysclock);
124 static GStaticMutex _gst_sysclock_mutex = G_STATIC_MUTEX_INIT;
126 static GstClockClass *parent_class = NULL;
128 /* static guint gst_system_clock_signals[LAST_SIGNAL] = { 0 }; */
130 G_DEFINE_TYPE (GstSystemClock, gst_system_clock, GST_TYPE_CLOCK);
133 gst_system_clock_class_init (GstSystemClockClass * klass)
135 GObjectClass *gobject_class;
136 GstClockClass *gstclock_class;
138 gobject_class = (GObjectClass *) klass;
139 gstclock_class = (GstClockClass *) klass;
141 parent_class = g_type_class_peek_parent (klass);
143 g_type_class_add_private (klass, sizeof (GstSystemClockPrivate));
145 gobject_class->dispose = gst_system_clock_dispose;
146 gobject_class->set_property = gst_system_clock_set_property;
147 gobject_class->get_property = gst_system_clock_get_property;
149 g_object_class_install_property (gobject_class, PROP_CLOCK_TYPE,
150 g_param_spec_enum ("clock-type", "Clock type",
151 "The type of underlying clock implementation used",
152 GST_TYPE_CLOCK_TYPE, DEFAULT_CLOCK_TYPE,
153 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
155 gstclock_class->get_internal_time = gst_system_clock_get_internal_time;
156 gstclock_class->get_resolution = gst_system_clock_get_resolution;
157 gstclock_class->wait_jitter = gst_system_clock_id_wait_jitter;
158 gstclock_class->wait_async = gst_system_clock_id_wait_async;
159 gstclock_class->unschedule = gst_system_clock_id_unschedule;
163 gst_system_clock_init (GstSystemClock * clock)
165 GST_OBJECT_FLAG_SET (clock,
166 GST_CLOCK_FLAG_CAN_DO_SINGLE_SYNC |
167 GST_CLOCK_FLAG_CAN_DO_SINGLE_ASYNC |
168 GST_CLOCK_FLAG_CAN_DO_PERIODIC_SYNC |
169 GST_CLOCK_FLAG_CAN_DO_PERIODIC_ASYNC);
171 clock->priv = GST_SYSTEM_CLOCK_GET_PRIVATE (clock);
173 clock->priv->clock_type = DEFAULT_CLOCK_TYPE;
174 clock->priv->timer = gst_poll_new_timer ();
177 QueryPerformanceFrequency (&clock->priv->frequency);
178 /* can be 0 if the hardware does not have hardware support */
179 if (clock->priv->frequency.QuadPart != 0)
180 /* we take a base time so that time starts from 0 to ease debugging */
181 QueryPerformanceCounter (&clock->priv->start);
182 #endif /* G_OS_WIN32 */
185 /* Uncomment this to start the async clock thread straight away */
186 GST_OBJECT_LOCK (clock);
187 gst_system_clock_start_async (clock);
188 GST_OBJECT_UNLOCK (clock);
193 gst_system_clock_dispose (GObject * object)
195 GstClock *clock = (GstClock *) object;
196 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
199 /* else we have to stop the thread */
200 GST_OBJECT_LOCK (clock);
201 sysclock->stopping = TRUE;
202 /* unschedule all entries */
203 for (entries = clock->entries; entries; entries = g_list_next (entries)) {
204 GstClockEntry *entry = (GstClockEntry *) entries->data;
206 GST_CAT_DEBUG (GST_CAT_CLOCK, "unscheduling entry %p", entry);
207 SET_ENTRY_STATUS (entry, GST_CLOCK_UNSCHEDULED);
209 GST_CLOCK_BROADCAST (clock);
210 gst_system_clock_add_wakeup (sysclock);
211 GST_OBJECT_UNLOCK (clock);
213 if (sysclock->thread)
214 g_thread_join (sysclock->thread);
215 sysclock->thread = NULL;
216 GST_CAT_DEBUG (GST_CAT_CLOCK, "joined thread");
218 g_list_foreach (clock->entries, (GFunc) gst_clock_id_unref, NULL);
219 g_list_free (clock->entries);
220 clock->entries = NULL;
222 gst_poll_free (sysclock->priv->timer);
224 G_OBJECT_CLASS (parent_class)->dispose (object);
226 if (_the_system_clock == clock) {
227 _the_system_clock = NULL;
228 GST_CAT_DEBUG (GST_CAT_CLOCK, "disposed system clock");
233 gst_system_clock_set_property (GObject * object, guint prop_id,
234 const GValue * value, GParamSpec * pspec)
236 GstSystemClock *sysclock = GST_SYSTEM_CLOCK (object);
239 case PROP_CLOCK_TYPE:
240 sysclock->priv->clock_type = g_value_get_enum (value);
241 GST_CAT_DEBUG (GST_CAT_CLOCK, "clock-type set to %d",
242 sysclock->priv->clock_type);
245 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
251 gst_system_clock_get_property (GObject * object, guint prop_id, GValue * value,
254 GstSystemClock *sysclock = GST_SYSTEM_CLOCK (object);
257 case PROP_CLOCK_TYPE:
258 g_value_set_enum (value, sysclock->priv->clock_type);
261 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
267 * gst_system_clock_obtain:
269 * Get a handle to the default system clock. The refcount of the
270 * clock will be increased so you need to unref the clock after
273 * Returns: (transfer full): the default clock.
278 gst_system_clock_obtain (void)
282 g_static_mutex_lock (&_gst_sysclock_mutex);
283 clock = _the_system_clock;
286 GST_CAT_DEBUG (GST_CAT_CLOCK, "creating new static system clock");
287 clock = g_object_new (GST_TYPE_SYSTEM_CLOCK,
288 "name", "GstSystemClock", NULL);
290 /* we created the global clock; take ownership so
291 * we can hand out instances later */
292 gst_object_ref_sink (clock);
294 _the_system_clock = clock;
295 g_static_mutex_unlock (&_gst_sysclock_mutex);
297 g_static_mutex_unlock (&_gst_sysclock_mutex);
298 GST_CAT_DEBUG (GST_CAT_CLOCK, "returning static system clock");
301 /* we ref it since we are a clock factory. */
302 gst_object_ref (clock);
307 gst_system_clock_remove_wakeup (GstSystemClock * sysclock)
309 g_return_if_fail (sysclock->priv->wakeup_count > 0);
311 sysclock->priv->wakeup_count--;
312 if (sysclock->priv->wakeup_count == 0) {
313 /* read the control socket byte when we removed the last wakeup count */
314 GST_CAT_DEBUG (GST_CAT_CLOCK, "reading control");
315 while (!gst_poll_read_control (sysclock->priv->timer)) {
316 g_warning ("gstsystemclock: read control failed, trying again\n");
318 GST_CLOCK_BROADCAST (sysclock);
320 GST_CAT_DEBUG (GST_CAT_CLOCK, "wakeup count %d",
321 sysclock->priv->wakeup_count);
325 gst_system_clock_add_wakeup (GstSystemClock * sysclock)
327 /* only write the control socket for the first wakeup */
328 if (sysclock->priv->wakeup_count == 0) {
329 GST_CAT_DEBUG (GST_CAT_CLOCK, "writing control");
330 while (!gst_poll_write_control (sysclock->priv->timer)) {
332 ("gstsystemclock: write control failed in wakeup_async, trying again : %d:%s\n",
333 errno, g_strerror (errno));
336 sysclock->priv->wakeup_count++;
337 GST_CAT_DEBUG (GST_CAT_CLOCK, "wakeup count %d",
338 sysclock->priv->wakeup_count);
342 gst_system_clock_wait_wakeup (GstSystemClock * sysclock)
344 while (sysclock->priv->wakeup_count > 0) {
345 GST_CLOCK_WAIT (sysclock);
349 /* this thread reads the sorted clock entries from the queue.
351 * It waits on each of them and fires the callback when the timeout occurs.
353 * When an entry in the queue was canceled before we wait for it, it is
356 * When waiting for an entry, it can become canceled, in that case we don't
357 * call the callback but move to the next item in the queue.
362 gst_system_clock_async_thread (GstClock * clock)
364 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
366 GST_CAT_DEBUG (GST_CAT_CLOCK, "enter system clock thread");
367 GST_OBJECT_LOCK (clock);
369 GST_CLOCK_BROADCAST (clock);
370 /* now enter our (almost) infinite loop */
371 while (!sysclock->stopping) {
372 GstClockEntry *entry;
373 GstClockTime requested;
376 /* check if something to be done */
377 while (clock->entries == NULL) {
378 GST_CAT_DEBUG (GST_CAT_CLOCK, "no clock entries, waiting..");
379 /* wait for work to do */
380 GST_CLOCK_WAIT (clock);
381 GST_CAT_DEBUG (GST_CAT_CLOCK, "got signal");
382 /* clock was stopping, exit */
383 if (sysclock->stopping)
387 /* see if we have a pending wakeup because the order of the list
389 if (sysclock->priv->async_wakeup) {
390 GST_CAT_DEBUG (GST_CAT_CLOCK, "clear async wakeup");
391 gst_system_clock_remove_wakeup (sysclock);
392 sysclock->priv->async_wakeup = FALSE;
395 /* pick the next entry */
396 entry = clock->entries->data;
397 GST_OBJECT_UNLOCK (clock);
399 requested = entry->time;
401 /* now wait for the entry, we already hold the lock */
403 gst_system_clock_id_wait_jitter_unlocked (clock, (GstClockID) entry,
406 GST_OBJECT_LOCK (clock);
409 case GST_CLOCK_UNSCHEDULED:
410 /* entry was unscheduled, move to the next */
411 GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry %p unscheduled", entry);
414 case GST_CLOCK_EARLY:
416 /* entry timed out normally, fire the callback and move to the next
418 GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry %p timed out", entry);
420 /* unlock before firing the callback */
421 GST_OBJECT_UNLOCK (clock);
422 entry->func (clock, entry->time, (GstClockID) entry,
424 GST_OBJECT_LOCK (clock);
426 if (entry->type == GST_CLOCK_ENTRY_PERIODIC) {
427 GST_CAT_DEBUG (GST_CAT_CLOCK, "updating periodic entry %p", entry);
428 /* adjust time now */
429 entry->time = requested + entry->interval;
430 /* and resort the list now */
432 g_list_sort (clock->entries, gst_clock_id_compare_func);
436 GST_CAT_DEBUG (GST_CAT_CLOCK, "moving to next entry");
441 /* somebody unlocked the entry but is was not canceled, This means that
442 * either a new entry was added in front of the queue or some other entry
443 * was canceled. Whatever it is, pick the head entry of the list and
444 * continue waiting. */
445 GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry %p needs restart", entry);
447 /* we set the entry back to the OK state. This is needed so that the
448 * _unschedule() code can see if an entry is currently being waited
449 * on (when its state is BUSY). */
450 SET_ENTRY_STATUS (entry, GST_CLOCK_OK);
453 GST_CAT_DEBUG (GST_CAT_CLOCK,
454 "strange result %d waiting for %p, skipping", res, entry);
455 g_warning ("%s: strange result %d waiting for %p, skipping",
456 GST_OBJECT_NAME (clock), res, entry);
460 /* we remove the current entry and unref it */
461 clock->entries = g_list_remove (clock->entries, entry);
462 gst_clock_id_unref ((GstClockID) entry);
466 GST_CLOCK_BROADCAST (clock);
467 GST_OBJECT_UNLOCK (clock);
468 GST_CAT_DEBUG (GST_CAT_CLOCK, "exit system clock thread");
471 #ifdef HAVE_POSIX_TIMERS
472 static inline clockid_t
473 clock_type_to_posix_id (GstClockType clock_type)
475 #ifdef HAVE_MONOTONIC_CLOCK
476 if (clock_type == GST_CLOCK_TYPE_MONOTONIC)
477 return CLOCK_MONOTONIC;
480 return CLOCK_REALTIME;
486 gst_system_clock_get_internal_time (GstClock * clock)
489 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
491 if (sysclock->priv->frequency.QuadPart != 0) {
494 /* we prefer the highly accurate performance counters on windows */
495 QueryPerformanceCounter (&now);
497 return gst_util_uint64_scale (now.QuadPart - sysclock->priv->start.QuadPart,
498 GST_SECOND, sysclock->priv->frequency.QuadPart);
500 #endif /* G_OS_WIN32 */
501 #if !defined HAVE_POSIX_TIMERS
505 g_get_current_time (&timeval);
507 return GST_TIMEVAL_TO_TIME (timeval);
511 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
515 ptype = clock_type_to_posix_id (sysclock->priv->clock_type);
517 if (G_UNLIKELY (clock_gettime (ptype, &ts)))
518 return GST_CLOCK_TIME_NONE;
520 return GST_TIMESPEC_TO_TIME (ts);
526 gst_system_clock_get_resolution (GstClock * clock)
529 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
531 if (sysclock->priv->frequency.QuadPart != 0) {
532 return GST_SECOND / sysclock->priv->frequency.QuadPart;
534 #endif /* G_OS_WIN32 */
535 #ifdef HAVE_POSIX_TIMERS
537 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
541 ptype = clock_type_to_posix_id (sysclock->priv->clock_type);
543 if (G_UNLIKELY (clock_getres (ptype, &ts)))
544 return GST_CLOCK_TIME_NONE;
546 return GST_TIMESPEC_TO_TIME (ts);
550 return 1 * GST_USECOND;
555 /* synchronously wait on the given GstClockEntry.
557 * We do this by blocking on the global GstPoll timer with
558 * the requested timeout. This allows us to unblock the
559 * entry by writing on the control fd.
561 * Note that writing the global GstPoll unlocks all waiting entries. So
562 * we need to check if an unlocked entry has changed when it unlocks.
564 * Entries that arrive too late are simply not waited on and a
565 * GST_CLOCK_EARLY result is returned.
569 static GstClockReturn
570 gst_system_clock_id_wait_jitter_unlocked (GstClock * clock,
571 GstClockEntry * entry, GstClockTimeDiff * jitter, gboolean restart)
573 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
574 GstClockTime entryt, now;
575 GstClockTimeDiff diff;
576 GstClockReturn status;
578 if (G_UNLIKELY (GET_ENTRY_STATUS (entry) == GST_CLOCK_UNSCHEDULED))
579 return GST_CLOCK_UNSCHEDULED;
581 /* need to call the overridden method because we want to sync against the time
582 * of the clock, whatever the subclass uses as a clock. */
583 now = gst_clock_get_time (clock);
585 /* get the time of the entry */
586 entryt = GST_CLOCK_ENTRY_TIME (entry);
588 /* the diff of the entry with the clock is the amount of time we have to
590 diff = GST_CLOCK_DIFF (now, entryt);
591 if (G_LIKELY (jitter))
594 GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p"
595 " time %" GST_TIME_FORMAT
596 " now %" GST_TIME_FORMAT
597 " diff (time-now) %" G_GINT64_FORMAT,
598 entry, GST_TIME_ARGS (entryt), GST_TIME_ARGS (now), diff);
600 if (G_LIKELY (diff > 0)) {
601 #ifdef WAIT_DEBUGGING
609 status = GET_ENTRY_STATUS (entry);
611 /* stop when we are unscheduled */
612 if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED))
615 /* mark the entry as busy but watch out for intermediate unscheduled
617 } while (G_UNLIKELY (!CAS_ENTRY_STATUS (entry, status, GST_CLOCK_BUSY)));
619 /* now wait on the entry, it either times out or the fd is written. The
620 * status of the entry is only BUSY around the poll. */
621 pollret = gst_poll_wait (sysclock->priv->timer, diff);
623 /* get the new status, mark as DONE. We do this so that the unschedule
624 * function knows when we left the poll and doesn't need to wakeup the
627 status = GET_ENTRY_STATUS (entry);
628 /* we were unscheduled, exit immediately */
629 if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED))
631 } while (G_UNLIKELY (!CAS_ENTRY_STATUS (entry, status, GST_CLOCK_DONE)));
633 GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p unlocked, status %d, ret %d",
634 entry, status, pollret);
636 if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED)) {
637 /* try to clean up The unschedule function managed to set the status to
638 * unscheduled. We now take the lock and mark the entry as unscheduled.
639 * This makes sure that the unschedule function doesn't perform a
640 * wakeup anymore. If the unschedule function has a change to perform
641 * the wakeup before us, we clean up here */
642 GST_OBJECT_LOCK (sysclock);
643 entry->unscheduled = TRUE;
644 if (entry->woken_up) {
645 gst_system_clock_remove_wakeup (sysclock);
647 GST_OBJECT_UNLOCK (sysclock);
650 if (G_UNLIKELY (pollret != 0)) {
651 /* some other id got unlocked */
653 /* this can happen if the entry got unlocked because of an async
654 * entry was added to the head of the async queue. */
655 GST_CAT_DEBUG (GST_CAT_CLOCK, "wakeup waiting for entry %p", entry);
659 /* wait till all the entries got woken up */
660 GST_OBJECT_LOCK (sysclock);
661 gst_system_clock_wait_wakeup (sysclock);
662 GST_OBJECT_UNLOCK (sysclock);
664 GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p needs to be restarted",
667 GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p unlocked after timeout",
671 /* reschedule if gst_poll_wait returned early or we have to reschedule after
673 now = gst_clock_get_time (clock);
674 diff = GST_CLOCK_DIFF (now, entryt);
677 /* timeout, this is fine, we can report success now */
678 status = GST_CLOCK_OK;
679 SET_ENTRY_STATUS (entry, status);
681 GST_CAT_DEBUG (GST_CAT_CLOCK,
682 "entry %p finished, diff %" G_GINT64_FORMAT, entry, diff);
684 #ifdef WAIT_DEBUGGING
685 final = gst_system_clock_get_internal_time (clock);
686 GST_CAT_DEBUG (GST_CAT_CLOCK, "Waited for %" G_GINT64_FORMAT
687 " got %" G_GINT64_FORMAT " diff %" G_GINT64_FORMAT
688 " %g target-offset %" G_GINT64_FORMAT " %g", entryt, now,
690 (double) (GstClockTimeDiff) (now - entryt) / GST_SECOND,
692 ((double) (GstClockTimeDiff) (final - target)) / GST_SECOND);
696 GST_CAT_DEBUG (GST_CAT_CLOCK,
697 "entry %p restart, diff %" G_GINT64_FORMAT, entry, diff);
702 /* we are right on time or too late */
703 if (G_UNLIKELY (diff == 0))
704 status = GST_CLOCK_OK;
706 status = GST_CLOCK_EARLY;
708 SET_ENTRY_STATUS (entry, status);
714 static GstClockReturn
715 gst_system_clock_id_wait_jitter (GstClock * clock, GstClockEntry * entry,
716 GstClockTimeDiff * jitter)
718 return gst_system_clock_id_wait_jitter_unlocked (clock, entry, jitter, TRUE);
721 /* Start the async clock thread. Must be called with the object lock
724 gst_system_clock_start_async (GstSystemClock * clock)
726 GError *error = NULL;
728 if (G_LIKELY (clock->thread != NULL))
729 return TRUE; /* Thread already running. Nothing to do */
731 clock->thread = g_thread_create ((GThreadFunc) gst_system_clock_async_thread,
732 clock, TRUE, &error);
733 if (G_UNLIKELY (error))
736 /* wait for it to spin up */
737 GST_CLOCK_WAIT (clock);
744 g_warning ("could not create async clock thread: %s", error->message);
745 g_error_free (error);
750 /* Add an entry to the list of pending async waits. The entry is inserted
751 * in sorted order. If we inserted the entry at the head of the list, we
752 * need to signal the thread as it might either be waiting on it or waiting
757 static GstClockReturn
758 gst_system_clock_id_wait_async (GstClock * clock, GstClockEntry * entry)
760 GstSystemClock *sysclock;
763 sysclock = GST_SYSTEM_CLOCK_CAST (clock);
765 GST_CAT_DEBUG (GST_CAT_CLOCK, "adding async entry %p", entry);
767 GST_OBJECT_LOCK (clock);
768 /* Start the clock async thread if needed */
769 if (G_UNLIKELY (!gst_system_clock_start_async (sysclock)))
772 if (G_UNLIKELY (GET_ENTRY_STATUS (entry) == GST_CLOCK_UNSCHEDULED))
773 goto was_unscheduled;
776 head = clock->entries->data;
780 /* need to take a ref */
781 gst_clock_id_ref ((GstClockID) entry);
782 /* insert the entry in sorted order */
783 clock->entries = g_list_insert_sorted (clock->entries, entry,
784 gst_clock_id_compare_func);
786 /* only need to send the signal if the entry was added to the
787 * front, else the thread is just waiting for another entry and
788 * will get to this entry automatically. */
789 if (clock->entries->data == entry) {
790 GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry added to head %p", head);
792 /* the list was empty before, signal the cond so that the async thread can
793 * start taking a look at the queue */
794 GST_CAT_DEBUG (GST_CAT_CLOCK, "first entry, sending signal");
795 GST_CLOCK_BROADCAST (clock);
797 GstClockReturn status;
799 status = GET_ENTRY_STATUS (head);
800 GST_CAT_DEBUG (GST_CAT_CLOCK, "head entry %p status %d", head, status);
802 if (status == GST_CLOCK_BUSY) {
803 GST_CAT_DEBUG (GST_CAT_CLOCK, "head entry is busy");
804 /* the async thread was waiting for an entry, unlock the wait so that it
805 * looks at the new head entry instead, we only need to do this once */
806 if (!sysclock->priv->async_wakeup) {
807 GST_CAT_DEBUG (GST_CAT_CLOCK, "wakeup async thread");
808 sysclock->priv->async_wakeup = TRUE;
809 gst_system_clock_add_wakeup (sysclock);
814 GST_OBJECT_UNLOCK (clock);
821 /* Could not start the async clock thread */
822 GST_OBJECT_UNLOCK (clock);
823 return GST_CLOCK_ERROR;
827 GST_OBJECT_UNLOCK (clock);
828 return GST_CLOCK_UNSCHEDULED;
832 /* unschedule an entry. This will set the state of the entry to GST_CLOCK_UNSCHEDULED
833 * and will signal any thread waiting for entries to recheck their entry.
834 * We cannot really decide if the signal is needed or not because the entry
835 * could be waited on in async or sync mode.
840 gst_system_clock_id_unschedule (GstClock * clock, GstClockEntry * entry)
842 GstSystemClock *sysclock;
843 GstClockReturn status;
845 sysclock = GST_SYSTEM_CLOCK_CAST (clock);
847 GST_CAT_DEBUG (GST_CAT_CLOCK, "unscheduling entry %p", entry);
849 GST_OBJECT_LOCK (clock);
850 /* change the entry status to unscheduled */
852 status = GET_ENTRY_STATUS (entry);
853 } while (G_UNLIKELY (!CAS_ENTRY_STATUS (entry, status,
854 GST_CLOCK_UNSCHEDULED)));
856 if (G_LIKELY (status == GST_CLOCK_BUSY)) {
857 /* the entry was being busy, wake up all entries so that they recheck their
858 * status. We cannot wake up just one entry because allocating such a
859 * datastructure for each entry would be too heavy and unlocking an entry
860 * is usually done when shutting down or some other exceptional case. */
861 GST_CAT_DEBUG (GST_CAT_CLOCK, "entry was BUSY, doing wakeup");
862 if (!entry->unscheduled && !entry->woken_up) {
863 gst_system_clock_add_wakeup (sysclock);
864 entry->woken_up = TRUE;
867 GST_OBJECT_UNLOCK (clock);