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"
51 # define WIN32_LEAN_AND_MEAN /* prevents from including too many things */
52 # include <windows.h> /* QueryPerformance* stuff */
53 # undef WIN32_LEAN_AND_MEAN
54 #endif /* G_OS_WIN32 */
56 #define GET_ENTRY_STATUS(e) (g_atomic_int_get(&GST_CLOCK_ENTRY_STATUS(e)))
57 #define SET_ENTRY_STATUS(e,val) (g_atomic_int_set(&GST_CLOCK_ENTRY_STATUS(e),(val)))
58 #define CAS_ENTRY_STATUS(e,old,val) (g_atomic_int_compare_and_exchange(\
59 ((volatile gint *)&GST_CLOCK_ENTRY_STATUS(e)), (old), (val)))
61 /* Define this to get some extra debug about jitter from each clock_wait */
64 struct _GstSystemClockPrivate
66 GstClockType clock_type;
68 gint wakeup_count; /* the number of entries with a pending wakeup */
69 gboolean async_wakeup; /* if the wakeup was because of a async list change */
73 LARGE_INTEGER frequency;
74 #endif /* G_OS_WIN32 */
77 #define GST_SYSTEM_CLOCK_GET_PRIVATE(obj) \
78 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_SYSTEM_CLOCK, \
79 GstSystemClockPrivate))
81 #ifdef HAVE_POSIX_TIMERS
82 # ifdef HAVE_MONOTONIC_CLOCK
83 # define DEFAULT_CLOCK_TYPE GST_CLOCK_TYPE_MONOTONIC
85 # define DEFAULT_CLOCK_TYPE GST_CLOCK_TYPE_REALTIME
88 #define DEFAULT_CLOCK_TYPE GST_CLOCK_TYPE_REALTIME
98 /* the one instance of the systemclock */
99 static GstClock *_the_system_clock = NULL;
101 static void gst_system_clock_dispose (GObject * object);
102 static void gst_system_clock_set_property (GObject * object, guint prop_id,
103 const GValue * value, GParamSpec * pspec);
104 static void gst_system_clock_get_property (GObject * object, guint prop_id,
105 GValue * value, GParamSpec * pspec);
107 static GstClockTime gst_system_clock_get_internal_time (GstClock * clock);
108 static guint64 gst_system_clock_get_resolution (GstClock * clock);
109 static GstClockReturn gst_system_clock_id_wait_jitter (GstClock * clock,
110 GstClockEntry * entry, GstClockTimeDiff * jitter);
111 static GstClockReturn gst_system_clock_id_wait_jitter_unlocked
112 (GstClock * clock, GstClockEntry * entry, GstClockTimeDiff * jitter,
114 static GstClockReturn gst_system_clock_id_wait_async (GstClock * clock,
115 GstClockEntry * entry);
116 static void gst_system_clock_id_unschedule (GstClock * clock,
117 GstClockEntry * entry);
118 static void gst_system_clock_async_thread (GstClock * clock);
119 static gboolean gst_system_clock_start_async (GstSystemClock * clock);
120 static void gst_system_clock_add_wakeup (GstSystemClock * sysclock);
122 static GStaticMutex _gst_sysclock_mutex = G_STATIC_MUTEX_INIT;
124 static GstClockClass *parent_class = NULL;
126 /* static guint gst_system_clock_signals[LAST_SIGNAL] = { 0 }; */
128 G_DEFINE_TYPE (GstSystemClock, gst_system_clock, GST_TYPE_CLOCK);
131 gst_system_clock_class_init (GstSystemClockClass * klass)
133 GObjectClass *gobject_class;
134 GstClockClass *gstclock_class;
136 gobject_class = (GObjectClass *) klass;
137 gstclock_class = (GstClockClass *) klass;
139 parent_class = g_type_class_peek_parent (klass);
141 g_type_class_add_private (klass, sizeof (GstSystemClockPrivate));
143 gobject_class->dispose = gst_system_clock_dispose;
144 gobject_class->set_property = gst_system_clock_set_property;
145 gobject_class->get_property = gst_system_clock_get_property;
147 g_object_class_install_property (gobject_class, PROP_CLOCK_TYPE,
148 g_param_spec_enum ("clock-type", "Clock type",
149 "The type of underlying clock implementation used",
150 GST_TYPE_CLOCK_TYPE, DEFAULT_CLOCK_TYPE,
151 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
153 gstclock_class->get_internal_time = gst_system_clock_get_internal_time;
154 gstclock_class->get_resolution = gst_system_clock_get_resolution;
155 gstclock_class->wait = gst_system_clock_id_wait_jitter;
156 gstclock_class->wait_async = gst_system_clock_id_wait_async;
157 gstclock_class->unschedule = gst_system_clock_id_unschedule;
161 gst_system_clock_init (GstSystemClock * clock)
163 GST_OBJECT_FLAG_SET (clock,
164 GST_CLOCK_FLAG_CAN_DO_SINGLE_SYNC |
165 GST_CLOCK_FLAG_CAN_DO_SINGLE_ASYNC |
166 GST_CLOCK_FLAG_CAN_DO_PERIODIC_SYNC |
167 GST_CLOCK_FLAG_CAN_DO_PERIODIC_ASYNC);
169 clock->priv = GST_SYSTEM_CLOCK_GET_PRIVATE (clock);
171 clock->priv->clock_type = DEFAULT_CLOCK_TYPE;
172 clock->priv->timer = gst_poll_new_timer ();
175 QueryPerformanceFrequency (&clock->priv->frequency);
176 /* can be 0 if the hardware does not have hardware support */
177 if (clock->priv->frequency.QuadPart != 0)
178 /* we take a base time so that time starts from 0 to ease debugging */
179 QueryPerformanceCounter (&clock->priv->start);
180 #endif /* G_OS_WIN32 */
183 /* Uncomment this to start the async clock thread straight away */
184 GST_OBJECT_LOCK (clock);
185 gst_system_clock_start_async (clock);
186 GST_OBJECT_UNLOCK (clock);
191 gst_system_clock_dispose (GObject * object)
193 GstClock *clock = (GstClock *) object;
194 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
197 /* else we have to stop the thread */
198 GST_OBJECT_LOCK (clock);
199 sysclock->stopping = TRUE;
200 /* unschedule all entries */
201 for (entries = clock->entries; entries; entries = g_list_next (entries)) {
202 GstClockEntry *entry = (GstClockEntry *) entries->data;
204 GST_CAT_DEBUG (GST_CAT_CLOCK, "unscheduling entry %p", entry);
205 SET_ENTRY_STATUS (entry, GST_CLOCK_UNSCHEDULED);
207 GST_CLOCK_BROADCAST (clock);
208 gst_system_clock_add_wakeup (sysclock);
209 GST_OBJECT_UNLOCK (clock);
211 if (sysclock->thread)
212 g_thread_join (sysclock->thread);
213 sysclock->thread = NULL;
214 GST_CAT_DEBUG (GST_CAT_CLOCK, "joined thread");
216 g_list_foreach (clock->entries, (GFunc) gst_clock_id_unref, NULL);
217 g_list_free (clock->entries);
218 clock->entries = NULL;
220 gst_poll_free (sysclock->priv->timer);
222 G_OBJECT_CLASS (parent_class)->dispose (object);
224 if (_the_system_clock == clock) {
225 _the_system_clock = NULL;
226 GST_CAT_DEBUG (GST_CAT_CLOCK, "disposed system clock");
231 gst_system_clock_set_property (GObject * object, guint prop_id,
232 const GValue * value, GParamSpec * pspec)
234 GstSystemClock *sysclock = GST_SYSTEM_CLOCK (object);
237 case PROP_CLOCK_TYPE:
238 sysclock->priv->clock_type = g_value_get_enum (value);
239 GST_CAT_DEBUG (GST_CAT_CLOCK, "clock-type set to %d",
240 sysclock->priv->clock_type);
243 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
249 gst_system_clock_get_property (GObject * object, guint prop_id, GValue * value,
252 GstSystemClock *sysclock = GST_SYSTEM_CLOCK (object);
255 case PROP_CLOCK_TYPE:
256 g_value_set_enum (value, sysclock->priv->clock_type);
259 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
265 * gst_system_clock_obtain:
267 * Get a handle to the default system clock. The refcount of the
268 * clock will be increased so you need to unref the clock after
271 * Returns: (transfer full): the default clock.
276 gst_system_clock_obtain (void)
280 g_static_mutex_lock (&_gst_sysclock_mutex);
281 clock = _the_system_clock;
284 GST_CAT_DEBUG (GST_CAT_CLOCK, "creating new static system clock");
285 clock = g_object_new (GST_TYPE_SYSTEM_CLOCK,
286 "name", "GstSystemClock", NULL);
288 /* we created the global clock; take ownership so
289 * we can hand out instances later */
290 gst_object_ref_sink (clock);
292 _the_system_clock = clock;
293 g_static_mutex_unlock (&_gst_sysclock_mutex);
295 g_static_mutex_unlock (&_gst_sysclock_mutex);
296 GST_CAT_DEBUG (GST_CAT_CLOCK, "returning static system clock");
299 /* we ref it since we are a clock factory. */
300 gst_object_ref (clock);
305 gst_system_clock_remove_wakeup (GstSystemClock * sysclock)
307 g_return_if_fail (sysclock->priv->wakeup_count > 0);
309 sysclock->priv->wakeup_count--;
310 if (sysclock->priv->wakeup_count == 0) {
311 /* read the control socket byte when we removed the last wakeup count */
312 GST_CAT_DEBUG (GST_CAT_CLOCK, "reading control");
313 while (!gst_poll_read_control (sysclock->priv->timer)) {
314 g_warning ("gstsystemclock: read control failed, trying again\n");
316 GST_CLOCK_BROADCAST (sysclock);
318 GST_CAT_DEBUG (GST_CAT_CLOCK, "wakeup count %d",
319 sysclock->priv->wakeup_count);
323 gst_system_clock_add_wakeup (GstSystemClock * sysclock)
325 /* only write the control socket for the first wakeup */
326 if (sysclock->priv->wakeup_count == 0) {
327 GST_CAT_DEBUG (GST_CAT_CLOCK, "writing control");
328 while (!gst_poll_write_control (sysclock->priv->timer)) {
330 ("gstsystemclock: write control failed in wakeup_async, trying again : %d:%s\n",
331 errno, g_strerror (errno));
334 sysclock->priv->wakeup_count++;
335 GST_CAT_DEBUG (GST_CAT_CLOCK, "wakeup count %d",
336 sysclock->priv->wakeup_count);
340 gst_system_clock_wait_wakeup (GstSystemClock * sysclock)
342 while (sysclock->priv->wakeup_count > 0) {
343 GST_CLOCK_WAIT (sysclock);
347 /* this thread reads the sorted clock entries from the queue.
349 * It waits on each of them and fires the callback when the timeout occurs.
351 * When an entry in the queue was canceled before we wait for it, it is
354 * When waiting for an entry, it can become canceled, in that case we don't
355 * call the callback but move to the next item in the queue.
360 gst_system_clock_async_thread (GstClock * clock)
362 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
364 GST_CAT_DEBUG (GST_CAT_CLOCK, "enter system clock thread");
365 GST_OBJECT_LOCK (clock);
367 GST_CLOCK_BROADCAST (clock);
368 /* now enter our (almost) infinite loop */
369 while (!sysclock->stopping) {
370 GstClockEntry *entry;
371 GstClockTime requested;
374 /* check if something to be done */
375 while (clock->entries == NULL) {
376 GST_CAT_DEBUG (GST_CAT_CLOCK, "no clock entries, waiting..");
377 /* wait for work to do */
378 GST_CLOCK_WAIT (clock);
379 GST_CAT_DEBUG (GST_CAT_CLOCK, "got signal");
380 /* clock was stopping, exit */
381 if (sysclock->stopping)
385 /* see if we have a pending wakeup because the order of the list
387 if (sysclock->priv->async_wakeup) {
388 GST_CAT_DEBUG (GST_CAT_CLOCK, "clear async wakeup");
389 gst_system_clock_remove_wakeup (sysclock);
390 sysclock->priv->async_wakeup = FALSE;
393 /* pick the next entry */
394 entry = clock->entries->data;
395 GST_OBJECT_UNLOCK (clock);
397 requested = entry->time;
399 /* now wait for the entry, we already hold the lock */
401 gst_system_clock_id_wait_jitter_unlocked (clock, (GstClockID) entry,
404 GST_OBJECT_LOCK (clock);
407 case GST_CLOCK_UNSCHEDULED:
408 /* entry was unscheduled, move to the next */
409 GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry %p unscheduled", entry);
412 case GST_CLOCK_EARLY:
414 /* entry timed out normally, fire the callback and move to the next
416 GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry %p timed out", entry);
418 /* unlock before firing the callback */
419 GST_OBJECT_UNLOCK (clock);
420 entry->func (clock, entry->time, (GstClockID) entry,
422 GST_OBJECT_LOCK (clock);
424 if (entry->type == GST_CLOCK_ENTRY_PERIODIC) {
425 GST_CAT_DEBUG (GST_CAT_CLOCK, "updating periodic entry %p", entry);
426 /* adjust time now */
427 entry->time = requested + entry->interval;
428 /* and resort the list now */
430 g_list_sort (clock->entries, gst_clock_id_compare_func);
434 GST_CAT_DEBUG (GST_CAT_CLOCK, "moving to next entry");
439 /* somebody unlocked the entry but is was not canceled, This means that
440 * either a new entry was added in front of the queue or some other entry
441 * was canceled. Whatever it is, pick the head entry of the list and
442 * continue waiting. */
443 GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry %p needs restart", entry);
445 /* we set the entry back to the OK state. This is needed so that the
446 * _unschedule() code can see if an entry is currently being waited
447 * on (when its state is BUSY). */
448 SET_ENTRY_STATUS (entry, GST_CLOCK_OK);
451 GST_CAT_DEBUG (GST_CAT_CLOCK,
452 "strange result %d waiting for %p, skipping", res, entry);
453 g_warning ("%s: strange result %d waiting for %p, skipping",
454 GST_OBJECT_NAME (clock), res, entry);
458 /* we remove the current entry and unref it */
459 clock->entries = g_list_remove (clock->entries, entry);
460 gst_clock_id_unref ((GstClockID) entry);
464 GST_CLOCK_BROADCAST (clock);
465 GST_OBJECT_UNLOCK (clock);
466 GST_CAT_DEBUG (GST_CAT_CLOCK, "exit system clock thread");
469 #ifdef HAVE_POSIX_TIMERS
470 static inline clockid_t
471 clock_type_to_posix_id (GstClockType clock_type)
473 #ifdef HAVE_MONOTONIC_CLOCK
474 if (clock_type == GST_CLOCK_TYPE_MONOTONIC)
475 return CLOCK_MONOTONIC;
478 return CLOCK_REALTIME;
484 gst_system_clock_get_internal_time (GstClock * clock)
487 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
489 if (sysclock->priv->frequency.QuadPart != 0) {
492 /* we prefer the highly accurate performance counters on windows */
493 QueryPerformanceCounter (&now);
495 return gst_util_uint64_scale (now.QuadPart - sysclock->priv->start.QuadPart,
496 GST_SECOND, sysclock->priv->frequency.QuadPart);
498 #endif /* G_OS_WIN32 */
499 #if !defined HAVE_POSIX_TIMERS
503 g_get_current_time (&timeval);
505 return GST_TIMEVAL_TO_TIME (timeval);
509 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
513 ptype = clock_type_to_posix_id (sysclock->priv->clock_type);
515 if (G_UNLIKELY (clock_gettime (ptype, &ts)))
516 return GST_CLOCK_TIME_NONE;
518 return GST_TIMESPEC_TO_TIME (ts);
524 gst_system_clock_get_resolution (GstClock * clock)
527 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
529 if (sysclock->priv->frequency.QuadPart != 0) {
530 return GST_SECOND / sysclock->priv->frequency.QuadPart;
532 #endif /* G_OS_WIN32 */
533 #ifdef HAVE_POSIX_TIMERS
535 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
539 ptype = clock_type_to_posix_id (sysclock->priv->clock_type);
541 if (G_UNLIKELY (clock_getres (ptype, &ts)))
542 return GST_CLOCK_TIME_NONE;
544 return GST_TIMESPEC_TO_TIME (ts);
548 return 1 * GST_USECOND;
553 /* synchronously wait on the given GstClockEntry.
555 * We do this by blocking on the global GstPoll timer with
556 * the requested timeout. This allows us to unblock the
557 * entry by writing on the control fd.
559 * Note that writing the global GstPoll unlocks all waiting entries. So
560 * we need to check if an unlocked entry has changed when it unlocks.
562 * Entries that arrive too late are simply not waited on and a
563 * GST_CLOCK_EARLY result is returned.
567 static GstClockReturn
568 gst_system_clock_id_wait_jitter_unlocked (GstClock * clock,
569 GstClockEntry * entry, GstClockTimeDiff * jitter, gboolean restart)
571 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
572 GstClockTime entryt, now;
573 GstClockTimeDiff diff;
574 GstClockReturn status;
576 if (G_UNLIKELY (GET_ENTRY_STATUS (entry) == GST_CLOCK_UNSCHEDULED))
577 return GST_CLOCK_UNSCHEDULED;
579 /* need to call the overridden method because we want to sync against the time
580 * of the clock, whatever the subclass uses as a clock. */
581 now = gst_clock_get_time (clock);
583 /* get the time of the entry */
584 entryt = GST_CLOCK_ENTRY_TIME (entry);
586 /* the diff of the entry with the clock is the amount of time we have to
588 diff = GST_CLOCK_DIFF (now, entryt);
589 if (G_LIKELY (jitter))
592 GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p"
593 " time %" GST_TIME_FORMAT
594 " now %" GST_TIME_FORMAT
595 " diff (time-now) %" G_GINT64_FORMAT,
596 entry, GST_TIME_ARGS (entryt), GST_TIME_ARGS (now), diff);
598 if (G_LIKELY (diff > 0)) {
599 #ifdef WAIT_DEBUGGING
607 status = GET_ENTRY_STATUS (entry);
609 /* stop when we are unscheduled */
610 if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED))
613 /* mark the entry as busy but watch out for intermediate unscheduled
615 } while (G_UNLIKELY (!CAS_ENTRY_STATUS (entry, status, GST_CLOCK_BUSY)));
617 /* now wait on the entry, it either times out or the fd is written. The
618 * status of the entry is only BUSY around the poll. */
619 pollret = gst_poll_wait (sysclock->priv->timer, diff);
621 /* get the new status, mark as DONE. We do this so that the unschedule
622 * function knows when we left the poll and doesn't need to wakeup the
625 status = GET_ENTRY_STATUS (entry);
626 /* we were unscheduled, exit immediately */
627 if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED))
629 } while (G_UNLIKELY (!CAS_ENTRY_STATUS (entry, status, GST_CLOCK_DONE)));
631 GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p unlocked, status %d, ret %d",
632 entry, status, pollret);
634 if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED)) {
635 /* try to clean up The unschedule function managed to set the status to
636 * unscheduled. We now take the lock and mark the entry as unscheduled.
637 * This makes sure that the unschedule function doesn't perform a
638 * wakeup anymore. If the unschedule function has a change to perform
639 * the wakeup before us, we clean up here */
640 GST_OBJECT_LOCK (sysclock);
641 entry->unscheduled = TRUE;
642 if (entry->woken_up) {
643 gst_system_clock_remove_wakeup (sysclock);
645 GST_OBJECT_UNLOCK (sysclock);
648 if (G_UNLIKELY (pollret != 0)) {
649 /* some other id got unlocked */
651 /* this can happen if the entry got unlocked because of an async
652 * entry was added to the head of the async queue. */
653 GST_CAT_DEBUG (GST_CAT_CLOCK, "wakeup waiting for entry %p", entry);
657 /* wait till all the entries got woken up */
658 GST_OBJECT_LOCK (sysclock);
659 gst_system_clock_wait_wakeup (sysclock);
660 GST_OBJECT_UNLOCK (sysclock);
662 GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p needs to be restarted",
665 GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p unlocked after timeout",
669 /* reschedule if gst_poll_wait returned early or we have to reschedule after
671 now = gst_clock_get_time (clock);
672 diff = GST_CLOCK_DIFF (now, entryt);
675 /* timeout, this is fine, we can report success now */
676 status = GST_CLOCK_OK;
677 SET_ENTRY_STATUS (entry, status);
679 GST_CAT_DEBUG (GST_CAT_CLOCK,
680 "entry %p finished, diff %" G_GINT64_FORMAT, entry, diff);
682 #ifdef WAIT_DEBUGGING
683 final = gst_system_clock_get_internal_time (clock);
684 GST_CAT_DEBUG (GST_CAT_CLOCK, "Waited for %" G_GINT64_FORMAT
685 " got %" G_GINT64_FORMAT " diff %" G_GINT64_FORMAT
686 " %g target-offset %" G_GINT64_FORMAT " %g", entryt, now,
688 (double) (GstClockTimeDiff) (now - entryt) / GST_SECOND,
690 ((double) (GstClockTimeDiff) (final - target)) / GST_SECOND);
694 GST_CAT_DEBUG (GST_CAT_CLOCK,
695 "entry %p restart, diff %" G_GINT64_FORMAT, entry, diff);
700 /* we are right on time or too late */
701 if (G_UNLIKELY (diff == 0))
702 status = GST_CLOCK_OK;
704 status = GST_CLOCK_EARLY;
706 SET_ENTRY_STATUS (entry, status);
712 static GstClockReturn
713 gst_system_clock_id_wait_jitter (GstClock * clock, GstClockEntry * entry,
714 GstClockTimeDiff * jitter)
716 return gst_system_clock_id_wait_jitter_unlocked (clock, entry, jitter, TRUE);
719 /* Start the async clock thread. Must be called with the object lock
722 gst_system_clock_start_async (GstSystemClock * clock)
724 GError *error = NULL;
726 if (G_LIKELY (clock->thread != NULL))
727 return TRUE; /* Thread already running. Nothing to do */
729 clock->thread = g_thread_create ((GThreadFunc) gst_system_clock_async_thread,
730 clock, TRUE, &error);
731 if (G_UNLIKELY (error))
734 /* wait for it to spin up */
735 GST_CLOCK_WAIT (clock);
742 g_warning ("could not create async clock thread: %s", error->message);
743 g_error_free (error);
748 /* Add an entry to the list of pending async waits. The entry is inserted
749 * in sorted order. If we inserted the entry at the head of the list, we
750 * need to signal the thread as it might either be waiting on it or waiting
755 static GstClockReturn
756 gst_system_clock_id_wait_async (GstClock * clock, GstClockEntry * entry)
758 GstSystemClock *sysclock;
761 sysclock = GST_SYSTEM_CLOCK_CAST (clock);
763 GST_CAT_DEBUG (GST_CAT_CLOCK, "adding async entry %p", entry);
765 GST_OBJECT_LOCK (clock);
766 /* Start the clock async thread if needed */
767 if (G_UNLIKELY (!gst_system_clock_start_async (sysclock)))
770 if (G_UNLIKELY (GET_ENTRY_STATUS (entry) == GST_CLOCK_UNSCHEDULED))
771 goto was_unscheduled;
774 head = clock->entries->data;
778 /* need to take a ref */
779 gst_clock_id_ref ((GstClockID) entry);
780 /* insert the entry in sorted order */
781 clock->entries = g_list_insert_sorted (clock->entries, entry,
782 gst_clock_id_compare_func);
784 /* only need to send the signal if the entry was added to the
785 * front, else the thread is just waiting for another entry and
786 * will get to this entry automatically. */
787 if (clock->entries->data == entry) {
788 GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry added to head %p", head);
790 /* the list was empty before, signal the cond so that the async thread can
791 * start taking a look at the queue */
792 GST_CAT_DEBUG (GST_CAT_CLOCK, "first entry, sending signal");
793 GST_CLOCK_BROADCAST (clock);
795 GstClockReturn status;
797 status = GET_ENTRY_STATUS (head);
798 GST_CAT_DEBUG (GST_CAT_CLOCK, "head entry %p status %d", head, status);
800 if (status == GST_CLOCK_BUSY) {
801 GST_CAT_DEBUG (GST_CAT_CLOCK, "head entry is busy");
802 /* the async thread was waiting for an entry, unlock the wait so that it
803 * looks at the new head entry instead, we only need to do this once */
804 if (!sysclock->priv->async_wakeup) {
805 GST_CAT_DEBUG (GST_CAT_CLOCK, "wakeup async thread");
806 sysclock->priv->async_wakeup = TRUE;
807 gst_system_clock_add_wakeup (sysclock);
812 GST_OBJECT_UNLOCK (clock);
819 /* Could not start the async clock thread */
820 GST_OBJECT_UNLOCK (clock);
821 return GST_CLOCK_ERROR;
825 GST_OBJECT_UNLOCK (clock);
826 return GST_CLOCK_UNSCHEDULED;
830 /* unschedule an entry. This will set the state of the entry to GST_CLOCK_UNSCHEDULED
831 * and will signal any thread waiting for entries to recheck their entry.
832 * We cannot really decide if the signal is needed or not because the entry
833 * could be waited on in async or sync mode.
838 gst_system_clock_id_unschedule (GstClock * clock, GstClockEntry * entry)
840 GstSystemClock *sysclock;
841 GstClockReturn status;
843 sysclock = GST_SYSTEM_CLOCK_CAST (clock);
845 GST_CAT_DEBUG (GST_CAT_CLOCK, "unscheduling entry %p", entry);
847 GST_OBJECT_LOCK (clock);
848 /* change the entry status to unscheduled */
850 status = GET_ENTRY_STATUS (entry);
851 } while (G_UNLIKELY (!CAS_ENTRY_STATUS (entry, status,
852 GST_CLOCK_UNSCHEDULED)));
854 if (G_LIKELY (status == GST_CLOCK_BUSY)) {
855 /* the entry was being busy, wake up all entries so that they recheck their
856 * status. We cannot wake up just one entry because allocating such a
857 * datastructure for each entry would be too heavy and unlocking an entry
858 * is usually done when shutting down or some other exceptional case. */
859 GST_CAT_DEBUG (GST_CAT_CLOCK, "entry was BUSY, doing wakeup");
860 if (!entry->unscheduled && !entry->woken_up) {
861 gst_system_clock_add_wakeup (sysclock);
862 entry->woken_up = TRUE;
865 GST_OBJECT_UNLOCK (clock);