1 /* GStreamer
2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2004 Wim Taymans <wim@fluendo.com>
4 *
5 * gstsystemclock.c: Default clock, uses the system clock
6 *
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.
11 *
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.
16 *
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.
21 */
23 /**
24 * SECTION:gstsystemclock
25 * @short_description: Default clock that uses the current system time
26 * @see_also: #GstClock
27 *
28 * The GStreamer core provides a GstSystemClock based on the system time.
29 * Asynchronous callbacks are scheduled from an internal thread.
30 *
31 * Clock implementors are encouraged to subclass this systemclock as it
32 * implements the async notification.
33 *
34 * Subclasses can however override all of the important methods for sync and
35 * async notifications to implement their own callback methods or blocking
36 * wait operations.
37 *
38 * Last reviewed on 2006-03-08 (0.10.4)
39 */
41 #include "gst_private.h"
42 #include "gstinfo.h"
43 #include "gstsystemclock.h"
44 #include "gstenumtypes.h"
45 #include "gstpoll.h"
46 #include "gstutils.h"
48 #include <errno.h>
50 #ifdef G_OS_WIN32
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 this to get some extra debug about jitter from each clock_wait */
57 #undef WAIT_DEBUGGING
59 struct _GstSystemClockPrivate
60 {
61 GstClockType clock_type;
62 GstPoll *timer;
63 gint wakeup_count; /* the number of entries with a pending wakeup */
64 gboolean async_wakeup; /* if the wakeup was because of a async list change */
66 #ifdef G_OS_WIN32
67 LARGE_INTEGER start;
68 LARGE_INTEGER frequency;
69 #endif /* G_OS_WIN32 */
70 };
72 #define GST_SYSTEM_CLOCK_GET_PRIVATE(obj) \
73 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_SYSTEM_CLOCK, \
74 GstSystemClockPrivate))
76 #ifdef HAVE_POSIX_TIMERS
77 # ifdef HAVE_MONOTONIC_CLOCK
78 # define DEFAULT_CLOCK_TYPE GST_CLOCK_TYPE_MONOTONIC
79 # else
80 # define DEFAULT_CLOCK_TYPE GST_CLOCK_TYPE_REALTIME
81 # endif
82 #else
83 #define DEFAULT_CLOCK_TYPE GST_CLOCK_TYPE_REALTIME
84 #endif
86 enum
87 {
88 PROP_0,
89 PROP_CLOCK_TYPE,
90 /* FILL ME */
91 };
93 /* the one instance of the systemclock */
94 static GstClock *_the_system_clock = NULL;
96 static void gst_system_clock_dispose (GObject * object);
97 static void gst_system_clock_set_property (GObject * object, guint prop_id,
98 const GValue * value, GParamSpec * pspec);
99 static void gst_system_clock_get_property (GObject * object, guint prop_id,
100 GValue * value, GParamSpec * pspec);
102 static GstClockTime gst_system_clock_get_internal_time (GstClock * clock);
103 static guint64 gst_system_clock_get_resolution (GstClock * clock);
104 static GstClockReturn gst_system_clock_id_wait_jitter (GstClock * clock,
105 GstClockEntry * entry, GstClockTimeDiff * jitter);
106 static GstClockReturn gst_system_clock_id_wait_jitter_unlocked
107 (GstClock * clock, GstClockEntry * entry, GstClockTimeDiff * jitter,
108 gboolean restart);
109 static GstClockReturn gst_system_clock_id_wait_async (GstClock * clock,
110 GstClockEntry * entry);
111 static void gst_system_clock_id_unschedule (GstClock * clock,
112 GstClockEntry * entry);
113 static void gst_system_clock_async_thread (GstClock * clock);
114 static gboolean gst_system_clock_start_async (GstSystemClock * clock);
115 static void gst_system_clock_add_wakeup (GstSystemClock * sysclock);
117 static GStaticMutex _gst_sysclock_mutex = G_STATIC_MUTEX_INIT;
119 static GstClockClass *parent_class = NULL;
121 /* static guint gst_system_clock_signals[LAST_SIGNAL] = { 0 }; */
123 G_DEFINE_TYPE (GstSystemClock, gst_system_clock, GST_TYPE_CLOCK);
125 static void
126 gst_system_clock_class_init (GstSystemClockClass * klass)
127 {
128 GObjectClass *gobject_class;
129 GstClockClass *gstclock_class;
131 gobject_class = (GObjectClass *) klass;
132 gstclock_class = (GstClockClass *) klass;
134 parent_class = g_type_class_peek_parent (klass);
136 g_type_class_add_private (klass, sizeof (GstSystemClockPrivate));
138 gobject_class->dispose = gst_system_clock_dispose;
139 gobject_class->set_property = gst_system_clock_set_property;
140 gobject_class->get_property = gst_system_clock_get_property;
142 g_object_class_install_property (gobject_class, PROP_CLOCK_TYPE,
143 g_param_spec_enum ("clock-type", "Clock type",
144 "The type of underlying clock implementation used",
145 GST_TYPE_CLOCK_TYPE, DEFAULT_CLOCK_TYPE, G_PARAM_READWRITE));
147 gstclock_class->get_internal_time = gst_system_clock_get_internal_time;
148 gstclock_class->get_resolution = gst_system_clock_get_resolution;
149 gstclock_class->wait_jitter = gst_system_clock_id_wait_jitter;
150 gstclock_class->wait_async = gst_system_clock_id_wait_async;
151 gstclock_class->unschedule = gst_system_clock_id_unschedule;
152 }
154 static void
155 gst_system_clock_init (GstSystemClock * clock)
156 {
157 GST_OBJECT_FLAG_SET (clock,
158 GST_CLOCK_FLAG_CAN_DO_SINGLE_SYNC |
159 GST_CLOCK_FLAG_CAN_DO_SINGLE_ASYNC |
160 GST_CLOCK_FLAG_CAN_DO_PERIODIC_SYNC |
161 GST_CLOCK_FLAG_CAN_DO_PERIODIC_ASYNC);
163 clock->priv = GST_SYSTEM_CLOCK_GET_PRIVATE (clock);
165 clock->priv->clock_type = DEFAULT_CLOCK_TYPE;
166 clock->priv->timer = gst_poll_new_timer ();
168 #ifdef G_OS_WIN32
169 QueryPerformanceFrequency (&clock->priv->frequency);
170 /* can be 0 if the hardware does not have hardware support */
171 if (clock->priv->frequency.QuadPart != 0)
172 /* we take a base time so that time starts from 0 to ease debugging */
173 QueryPerformanceCounter (&clock->priv->start);
174 #endif /* G_OS_WIN32 */
176 #if 0
177 /* Uncomment this to start the async clock thread straight away */
178 GST_OBJECT_LOCK (clock);
179 gst_system_clock_start_async (clock);
180 GST_OBJECT_UNLOCK (clock);
181 #endif
182 }
184 static void
185 gst_system_clock_dispose (GObject * object)
186 {
187 GstClock *clock = (GstClock *) object;
188 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
189 GList *entries;
191 /* else we have to stop the thread */
192 GST_OBJECT_LOCK (clock);
193 sysclock->stopping = TRUE;
194 /* unschedule all entries */
195 for (entries = clock->entries; entries; entries = g_list_next (entries)) {
196 GstClockEntry *entry = (GstClockEntry *) entries->data;
198 GST_CAT_DEBUG (GST_CAT_CLOCK, "unscheduling entry %p", entry);
199 entry->status = GST_CLOCK_UNSCHEDULED;
200 }
201 g_list_free (clock->entries);
202 clock->entries = NULL;
203 GST_CLOCK_BROADCAST (clock);
204 gst_system_clock_add_wakeup (sysclock);
205 GST_OBJECT_UNLOCK (clock);
207 if (sysclock->thread)
208 g_thread_join (sysclock->thread);
209 sysclock->thread = NULL;
210 GST_CAT_DEBUG (GST_CAT_CLOCK, "joined thread");
212 gst_poll_free (sysclock->priv->timer);
214 G_OBJECT_CLASS (parent_class)->dispose (object);
216 if (_the_system_clock == clock) {
217 _the_system_clock = NULL;
218 GST_CAT_DEBUG (GST_CAT_CLOCK, "disposed system clock");
219 }
220 }
222 static void
223 gst_system_clock_set_property (GObject * object, guint prop_id,
224 const GValue * value, GParamSpec * pspec)
225 {
226 GstSystemClock *sysclock = GST_SYSTEM_CLOCK (object);
228 switch (prop_id) {
229 case PROP_CLOCK_TYPE:
230 sysclock->priv->clock_type = g_value_get_enum (value);
231 GST_CAT_DEBUG (GST_CAT_CLOCK, "clock-type set to %d",
232 sysclock->priv->clock_type);
233 break;
234 default:
235 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
236 break;
237 }
238 }
240 static void
241 gst_system_clock_get_property (GObject * object, guint prop_id, GValue * value,
242 GParamSpec * pspec)
243 {
244 GstSystemClock *sysclock = GST_SYSTEM_CLOCK (object);
246 switch (prop_id) {
247 case PROP_CLOCK_TYPE:
248 g_value_set_enum (value, sysclock->priv->clock_type);
249 break;
250 default:
251 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
252 break;
253 }
254 }
256 /**
257 * gst_system_clock_obtain:
258 *
259 * Get a handle to the default system clock. The refcount of the
260 * clock will be increased so you need to unref the clock after
261 * usage.
262 *
263 * Returns: the default clock.
264 *
265 * MT safe.
266 */
267 GstClock *
268 gst_system_clock_obtain (void)
269 {
270 GstClock *clock;
272 g_static_mutex_lock (&_gst_sysclock_mutex);
273 clock = _the_system_clock;
275 if (clock == NULL) {
276 GST_CAT_DEBUG (GST_CAT_CLOCK, "creating new static system clock");
277 clock = g_object_new (GST_TYPE_SYSTEM_CLOCK,
278 "name", "GstSystemClock", NULL);
280 /* we created the global clock; take ownership so
281 * we can hand out instances later */
282 gst_object_ref_sink (clock);
284 _the_system_clock = clock;
285 g_static_mutex_unlock (&_gst_sysclock_mutex);
286 } else {
287 g_static_mutex_unlock (&_gst_sysclock_mutex);
288 GST_CAT_DEBUG (GST_CAT_CLOCK, "returning static system clock");
289 }
291 /* we ref it since we are a clock factory. */
292 gst_object_ref (clock);
293 return clock;
294 }
296 static void
297 gst_system_clock_remove_wakeup (GstSystemClock * sysclock)
298 {
299 g_return_if_fail (sysclock->priv->wakeup_count > 0);
301 sysclock->priv->wakeup_count--;
302 if (sysclock->priv->wakeup_count == 0) {
303 /* read the control socket byte when we removed the last wakeup count */
304 GST_CAT_DEBUG (GST_CAT_CLOCK, "reading control");
305 while (!gst_poll_read_control (sysclock->priv->timer)) {
306 g_warning ("gstsystemclock: read control failed, trying again\n");
307 }
308 GST_CLOCK_BROADCAST (sysclock);
309 }
310 GST_CAT_DEBUG (GST_CAT_CLOCK, "wakeup count %d",
311 sysclock->priv->wakeup_count);
312 }
314 static void
315 gst_system_clock_add_wakeup (GstSystemClock * sysclock)
316 {
317 /* only write the control socket for the first wakeup */
318 if (sysclock->priv->wakeup_count == 0) {
319 GST_CAT_DEBUG (GST_CAT_CLOCK, "writing control");
320 while (!gst_poll_write_control (sysclock->priv->timer)) {
321 g_warning
322 ("gstsystemclock: write control failed in wakeup_async, trying again : %d:%s\n",
323 errno, g_strerror (errno));
324 }
325 }
326 sysclock->priv->wakeup_count++;
327 GST_CAT_DEBUG (GST_CAT_CLOCK, "wakeup count %d",
328 sysclock->priv->wakeup_count);
329 }
331 static void
332 gst_system_clock_wait_wakeup (GstSystemClock * sysclock)
333 {
334 while (sysclock->priv->wakeup_count > 0) {
335 GST_CLOCK_WAIT (sysclock);
336 }
337 }
339 /* this thread reads the sorted clock entries from the queue.
340 *
341 * It waits on each of them and fires the callback when the timeout occurs.
342 *
343 * When an entry in the queue was canceled before we wait for it, it is
344 * simply skipped.
345 *
346 * When waiting for an entry, it can become canceled, in that case we don't
347 * call the callback but move to the next item in the queue.
348 *
349 * MT safe.
350 */
351 static void
352 gst_system_clock_async_thread (GstClock * clock)
353 {
354 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
356 GST_CAT_DEBUG (GST_CAT_CLOCK, "enter system clock thread");
357 GST_OBJECT_LOCK (clock);
358 /* signal spinup */
359 GST_CLOCK_BROADCAST (clock);
360 /* now enter our (almost) infinite loop */
361 while (!sysclock->stopping) {
362 GstClockEntry *entry;
363 GstClockTime requested;
364 GstClockReturn res;
366 /* check if something to be done */
367 while (clock->entries == NULL) {
368 GST_CAT_DEBUG (GST_CAT_CLOCK, "no clock entries, waiting..");
369 /* wait for work to do */
370 GST_CLOCK_WAIT (clock);
371 GST_CAT_DEBUG (GST_CAT_CLOCK, "got signal");
372 /* clock was stopping, exit */
373 if (sysclock->stopping)
374 goto exit;
375 }
377 /* see if we have a pending wakeup because the order of the list
378 * changed. */
379 if (sysclock->priv->async_wakeup) {
380 GST_CAT_DEBUG (GST_CAT_CLOCK, "clear async wakeup");
381 gst_system_clock_remove_wakeup (sysclock);
382 sysclock->priv->async_wakeup = FALSE;
383 }
385 /* pick the next entry */
386 entry = clock->entries->data;
387 /* if it was unscheduled, just move on to the next entry */
388 if (entry->status == GST_CLOCK_UNSCHEDULED) {
389 GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p was unscheduled", entry);
390 goto next_entry;
391 }
393 requested = entry->time;
395 /* now wait for the entry, we already hold the lock */
396 res =
397 gst_system_clock_id_wait_jitter_unlocked (clock, (GstClockID) entry,
398 NULL, FALSE);
400 switch (res) {
401 case GST_CLOCK_UNSCHEDULED:
402 /* entry was unscheduled, move to the next */
403 GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry %p unscheduled", entry);
404 goto next_entry;
405 case GST_CLOCK_OK:
406 case GST_CLOCK_EARLY:
407 {
408 /* entry timed out normally, fire the callback and move to the next
409 * entry */
410 GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry %p timed out", entry);
411 if (entry->func) {
412 /* unlock before firing the callback */
413 GST_OBJECT_UNLOCK (clock);
414 entry->func (clock, entry->time, (GstClockID) entry,
415 entry->user_data);
416 GST_OBJECT_LOCK (clock);
417 }
418 if (entry->type == GST_CLOCK_ENTRY_PERIODIC) {
419 GST_CAT_DEBUG (GST_CAT_CLOCK, "updating periodic entry %p", entry);
420 /* adjust time now */
421 entry->time = requested + entry->interval;
422 /* and resort the list now */
423 clock->entries =
424 g_list_sort (clock->entries, gst_clock_id_compare_func);
425 /* and restart */
426 continue;
427 } else {
428 GST_CAT_DEBUG (GST_CAT_CLOCK, "moving to next entry");
429 goto next_entry;
430 }
431 }
432 case GST_CLOCK_BUSY:
433 /* somebody unlocked the entry but is was not canceled, This means that
434 * either a new entry was added in front of the queue or some other entry
435 * was canceled. Whatever it is, pick the head entry of the list and
436 * continue waiting. */
437 GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry %p needs restart", entry);
439 /* we set the entry back to the OK state. This is needed so that the
440 * _unschedule() code can see if an entry is currently being waited
441 * on (when its state is BUSY). */
442 entry->status = GST_CLOCK_OK;
443 continue;
444 default:
445 GST_CAT_DEBUG (GST_CAT_CLOCK,
446 "strange result %d waiting for %p, skipping", res, entry);
447 g_warning ("%s: strange result %d waiting for %p, skipping",
448 GST_OBJECT_NAME (clock), res, entry);
449 goto next_entry;
450 }
451 next_entry:
452 /* we remove the current entry and unref it */
453 clock->entries = g_list_remove (clock->entries, entry);
454 gst_clock_id_unref ((GstClockID) entry);
455 }
456 exit:
457 /* signal exit */
458 GST_CLOCK_BROADCAST (clock);
459 GST_OBJECT_UNLOCK (clock);
460 GST_CAT_DEBUG (GST_CAT_CLOCK, "exit system clock thread");
461 }
463 #ifdef HAVE_POSIX_TIMERS
464 static inline clockid_t
465 clock_type_to_posix_id (GstClockType clock_type)
466 {
467 #ifdef HAVE_MONOTONIC_CLOCK
468 if (clock_type == GST_CLOCK_TYPE_MONOTONIC)
469 return CLOCK_MONOTONIC;
470 else
471 #endif
472 return CLOCK_REALTIME;
473 }
474 #endif
476 /* MT safe */
477 static GstClockTime
478 gst_system_clock_get_internal_time (GstClock * clock)
479 {
480 #ifdef G_OS_WIN32
481 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
483 if (sysclock->priv->frequency.QuadPart != 0) {
484 LARGE_INTEGER now;
486 /* we prefer the highly accurate performance counters on windows */
487 QueryPerformanceCounter (&now);
489 return gst_util_uint64_scale (now.QuadPart - sysclock->priv->start.QuadPart,
490 GST_SECOND, sysclock->priv->frequency.QuadPart);
491 } else
492 #endif /* G_OS_WIN32 */
493 #if !defined HAVE_POSIX_TIMERS
494 {
495 GTimeVal timeval;
497 g_get_current_time (&timeval);
499 return GST_TIMEVAL_TO_TIME (timeval);
500 }
501 #else
502 {
503 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
504 clockid_t ptype;
505 struct timespec ts;
507 ptype = clock_type_to_posix_id (sysclock->priv->clock_type);
509 if (G_UNLIKELY (clock_gettime (ptype, &ts)))
510 return GST_CLOCK_TIME_NONE;
512 return GST_TIMESPEC_TO_TIME (ts);
513 }
514 #endif
515 }
517 static guint64
518 gst_system_clock_get_resolution (GstClock * clock)
519 {
520 #ifdef G_OS_WIN32
521 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
523 if (sysclock->priv->frequency.QuadPart != 0) {
524 return GST_SECOND / sysclock->priv->frequency.QuadPart;
525 } else
526 #endif /* G_OS_WIN32 */
527 #ifdef HAVE_POSIX_TIMERS
528 {
529 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
530 clockid_t ptype;
531 struct timespec ts;
533 ptype = clock_type_to_posix_id (sysclock->priv->clock_type);
535 if (G_UNLIKELY (clock_getres (ptype, &ts)))
536 return GST_CLOCK_TIME_NONE;
538 return GST_TIMESPEC_TO_TIME (ts);
539 }
540 #else
541 {
542 return 1 * GST_USECOND;
543 }
544 #endif
545 }
547 /* synchronously wait on the given GstClockEntry.
548 *
549 * We do this by blocking on the global clock GCond variable with
550 * the requested time as a timeout. This allows us to unblock the
551 * entry by signaling the GCond variable.
552 *
553 * Note that signaling the global GCond unlocks all waiting entries. So
554 * we need to check if an unlocked entry has changed when it unlocks.
555 *
556 * Entries that arrive too late are simply not waited on and a
557 * GST_CLOCK_EARLY result is returned.
558 *
559 * should be called with LOCK held.
560 *
561 * MT safe.
562 */
563 static GstClockReturn
564 gst_system_clock_id_wait_jitter_unlocked (GstClock * clock,
565 GstClockEntry * entry, GstClockTimeDiff * jitter, gboolean restart)
566 {
567 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
568 GstClockTime entryt, real, now;
569 GstClockTimeDiff diff;
571 /* need to call the overridden method because we want to sync against the time
572 * of the clock, whatever the subclass uses as a clock. */
573 real = GST_CLOCK_GET_CLASS (clock)->get_internal_time (clock);
574 now = gst_clock_adjust_unlocked (clock, real);
576 /* get the time of the entry */
577 entryt = GST_CLOCK_ENTRY_TIME (entry);
579 if (jitter) {
580 *jitter = GST_CLOCK_DIFF (entryt, now);
581 }
582 /* the diff of the entry with the clock is the amount of time we have to
583 * wait */
584 diff = entryt - now;
586 GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p"
587 " time %" GST_TIME_FORMAT
588 " now %" GST_TIME_FORMAT
589 " real %" GST_TIME_FORMAT
590 " diff (time-now) %" G_GINT64_FORMAT,
591 entry,
592 GST_TIME_ARGS (entryt), GST_TIME_ARGS (now), GST_TIME_ARGS (real), diff);
594 if (diff > 0) {
595 #ifdef WAIT_DEBUGGING
596 GstClockTime final;
597 #endif
599 while (entry->status != GST_CLOCK_UNSCHEDULED) {
600 gint pollret;
602 /* mark the entry as busy */
603 entry->status = GST_CLOCK_BUSY;
604 GST_OBJECT_UNLOCK (clock);
606 /* now wait on the entry, it either times out or the fd is written. */
607 pollret = gst_poll_wait (sysclock->priv->timer, diff);
609 /* another thread can read the fd before we get the lock */
610 GST_OBJECT_LOCK (clock);
611 if (entry->status == GST_CLOCK_UNSCHEDULED) {
612 GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p unlocked", entry);
613 gst_system_clock_remove_wakeup (sysclock);
614 } else {
615 if (pollret != 0) {
616 /* some other id got unlocked */
617 if (!restart) {
618 /* this can happen if the entry got unlocked because of an async
619 * entry was added to the head of the async queue. */
620 GST_CAT_DEBUG (GST_CAT_CLOCK, "wakeup waiting for entry %p", entry);
621 break;
622 }
624 /* mark ourselves as EARLY, we release the lock and we could be
625 * unscheduled ourselves but we don't want the unscheduling thread
626 * to write on the control socket (it does that when an entry has a
627 * BUSY status). */
628 entry->status = GST_CLOCK_EARLY;
630 /* wait till all the entries got woken up */
631 gst_system_clock_wait_wakeup (sysclock);
633 /* we released the lock in the wait, recheck our status, we don't need
634 * to remove the wakeup count because we marked the entry as EARLY
635 * before releasing the object lock. */
636 if (entry->status == GST_CLOCK_UNSCHEDULED) {
637 GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p got unscheduled", entry);
638 break;
639 }
641 GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p needs to be restarted",
642 entry);
643 } else {
644 GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p unlocked after timeout",
645 entry);
646 }
648 /* reschedule if gst_poll_wait returned early or we have to reschedule after
649 * an unlock*/
650 real = GST_CLOCK_GET_CLASS (clock)->get_internal_time (clock);
651 now = gst_clock_adjust_unlocked (clock, real);
652 diff = entryt - now;
654 if (diff <= 0) {
655 /* timeout, this is fine, we can report success now */
656 entry->status = GST_CLOCK_OK;
658 GST_CAT_DEBUG (GST_CAT_CLOCK,
659 "entry %p finished, diff %" G_GINT64_FORMAT, entry, diff);
661 #ifdef WAIT_DEBUGGING
662 final = gst_system_clock_get_internal_time (clock);
663 GST_CAT_DEBUG (GST_CAT_CLOCK, "Waited for %" G_GINT64_FORMAT
664 " got %" G_GINT64_FORMAT " diff %" G_GINT64_FORMAT
665 " %g target-offset %" G_GINT64_FORMAT " %g", entryt, now,
666 now - entryt,
667 (double) (GstClockTimeDiff) (now - entryt) / GST_SECOND,
668 (final - target),
669 ((double) (GstClockTimeDiff) (final - target)) / GST_SECOND);
670 #endif
671 break;
672 } else {
673 GST_CAT_DEBUG (GST_CAT_CLOCK,
674 "entry %p restart, diff %" G_GINT64_FORMAT, entry, diff);
675 }
676 }
677 }
678 } else if (diff == 0) {
679 entry->status = GST_CLOCK_OK;
680 } else {
681 entry->status = GST_CLOCK_EARLY;
682 }
683 return entry->status;
684 }
686 static GstClockReturn
687 gst_system_clock_id_wait_jitter (GstClock * clock, GstClockEntry * entry,
688 GstClockTimeDiff * jitter)
689 {
690 GstClockReturn ret;
692 GST_OBJECT_LOCK (clock);
693 if (G_UNLIKELY (entry->status == GST_CLOCK_UNSCHEDULED))
694 goto was_unscheduled;
696 ret = gst_system_clock_id_wait_jitter_unlocked (clock, entry, jitter, TRUE);
697 GST_OBJECT_UNLOCK (clock);
699 return ret;
701 /* ERRORS */
702 was_unscheduled:
703 {
704 GST_OBJECT_UNLOCK (clock);
705 return GST_CLOCK_UNSCHEDULED;
706 }
707 }
709 /* Start the async clock thread. Must be called with the object lock
710 * held */
711 static gboolean
712 gst_system_clock_start_async (GstSystemClock * clock)
713 {
714 GError *error = NULL;
716 if (G_LIKELY (clock->thread != NULL))
717 return TRUE; /* Thread already running. Nothing to do */
719 clock->thread = g_thread_create ((GThreadFunc) gst_system_clock_async_thread,
720 clock, TRUE, &error);
721 if (G_UNLIKELY (error))
722 goto no_thread;
724 /* wait for it to spin up */
725 GST_CLOCK_WAIT (clock);
727 return TRUE;
729 /* ERRORS */
730 no_thread:
731 {
732 g_warning ("could not create async clock thread: %s", error->message);
733 g_error_free (error);
734 }
735 return FALSE;
736 }
738 /* Add an entry to the list of pending async waits. The entry is inserted
739 * in sorted order. If we inserted the entry at the head of the list, we
740 * need to signal the thread as it might either be waiting on it or waiting
741 * for a new entry.
742 *
743 * MT safe.
744 */
745 static GstClockReturn
746 gst_system_clock_id_wait_async (GstClock * clock, GstClockEntry * entry)
747 {
748 GstSystemClock *sysclock;
749 GstClockEntry *head;
751 sysclock = GST_SYSTEM_CLOCK_CAST (clock);
753 GST_CAT_DEBUG (GST_CAT_CLOCK, "adding async entry %p", entry);
755 GST_OBJECT_LOCK (clock);
756 /* Start the clock async thread if needed */
757 if (G_UNLIKELY (!gst_system_clock_start_async (sysclock)))
758 goto thread_error;
760 if (G_UNLIKELY (entry->status == GST_CLOCK_UNSCHEDULED))
761 goto was_unscheduled;
763 if (clock->entries)
764 head = clock->entries->data;
765 else
766 head = NULL;
768 /* need to take a ref */
769 gst_clock_id_ref ((GstClockID) entry);
770 /* insert the entry in sorted order */
771 clock->entries = g_list_insert_sorted (clock->entries, entry,
772 gst_clock_id_compare_func);
774 /* only need to send the signal if the entry was added to the
775 * front, else the thread is just waiting for another entry and
776 * will get to this entry automatically. */
777 if (clock->entries->data == entry) {
778 GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry added to head");
779 if (head == NULL) {
780 /* the list was empty before, signal the cond so that the async thread can
781 * start taking a look at the queue */
782 GST_CAT_DEBUG (GST_CAT_CLOCK, "first entry, sending signal");
783 GST_CLOCK_BROADCAST (clock);
784 } else {
785 if (head->status == GST_CLOCK_BUSY) {
786 /* the async thread was waiting for an entry, unlock the wait so that it
787 * looks at the new head entry instead, we only need to do this once */
788 if (!sysclock->priv->async_wakeup) {
789 GST_CAT_DEBUG (GST_CAT_CLOCK, "wakeup async thread");
790 sysclock->priv->async_wakeup = TRUE;
791 gst_system_clock_add_wakeup (sysclock);
792 }
793 }
794 }
795 }
796 GST_OBJECT_UNLOCK (clock);
798 return GST_CLOCK_OK;
800 /* ERRORS */
801 thread_error:
802 {
803 /* Could not start the async clock thread */
804 GST_OBJECT_UNLOCK (clock);
805 return GST_CLOCK_ERROR;
806 }
807 was_unscheduled:
808 {
809 GST_OBJECT_UNLOCK (clock);
810 return GST_CLOCK_UNSCHEDULED;
811 }
812 }
814 /* unschedule an entry. This will set the state of the entry to GST_CLOCK_UNSCHEDULED
815 * and will signal any thread waiting for entries to recheck their entry.
816 * We cannot really decide if the signal is needed or not because the entry
817 * could be waited on in async or sync mode.
818 *
819 * MT safe.
820 */
821 static void
822 gst_system_clock_id_unschedule (GstClock * clock, GstClockEntry * entry)
823 {
824 GstSystemClock *sysclock;
826 sysclock = GST_SYSTEM_CLOCK_CAST (clock);
828 GST_CAT_DEBUG (GST_CAT_CLOCK, "unscheduling entry %p", entry);
830 GST_OBJECT_LOCK (clock);
831 if (entry->status == GST_CLOCK_BUSY) {
832 /* the entry was being busy, wake up all entries so that they recheck their
833 * status. We cannot wake up just one entry because allocating such a
834 * datastructure for each entry would be too heavy and unlocking an entry
835 * is usually done when shutting down or some other exceptional case. */
836 GST_CAT_DEBUG (GST_CAT_CLOCK, "entry was BUSY, doing wakeup");
837 gst_system_clock_add_wakeup (sysclock);
838 }
839 /* when it leaves the poll, it'll detect the unscheduled */
840 entry->status = GST_CLOCK_UNSCHEDULED;
841 GST_OBJECT_UNLOCK (clock);
842 }