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"
47 #include "glib-compat-private.h"
49 #include <errno.h>
51 #ifdef G_OS_WIN32
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) ((GstClockReturn) 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 */
64 #undef WAIT_DEBUGGING
66 struct _GstSystemClockPrivate
67 {
68 GstClockType clock_type;
69 GstPoll *timer;
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 */
73 #ifdef G_OS_WIN32
74 LARGE_INTEGER start;
75 LARGE_INTEGER frequency;
76 #endif /* G_OS_WIN32 */
77 };
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
86 # else
87 # define DEFAULT_CLOCK_TYPE GST_CLOCK_TYPE_REALTIME
88 # endif
89 #else
90 #define DEFAULT_CLOCK_TYPE GST_CLOCK_TYPE_REALTIME
91 #endif
93 enum
94 {
95 PROP_0,
96 PROP_CLOCK_TYPE,
97 /* FILL ME */
98 };
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,
115 gboolean restart);
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);
132 static void
133 gst_system_clock_class_init (GstSystemClockClass * klass)
134 {
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;
160 }
162 static void
163 gst_system_clock_init (GstSystemClock * clock)
164 {
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 ();
176 #ifdef G_OS_WIN32
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 */
184 #if 0
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);
189 #endif
190 }
192 static void
193 gst_system_clock_dispose (GObject * object)
194 {
195 GstClock *clock = (GstClock *) object;
196 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
197 GList *entries;
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);
208 }
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");
229 }
230 }
232 static void
233 gst_system_clock_set_property (GObject * object, guint prop_id,
234 const GValue * value, GParamSpec * pspec)
235 {
236 GstSystemClock *sysclock = GST_SYSTEM_CLOCK (object);
238 switch (prop_id) {
239 case PROP_CLOCK_TYPE:
240 sysclock->priv->clock_type = (GstClockType) g_value_get_enum (value);
241 GST_CAT_DEBUG (GST_CAT_CLOCK, "clock-type set to %d",
242 sysclock->priv->clock_type);
243 break;
244 default:
245 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
246 break;
247 }
248 }
250 static void
251 gst_system_clock_get_property (GObject * object, guint prop_id, GValue * value,
252 GParamSpec * pspec)
253 {
254 GstSystemClock *sysclock = GST_SYSTEM_CLOCK (object);
256 switch (prop_id) {
257 case PROP_CLOCK_TYPE:
258 g_value_set_enum (value, sysclock->priv->clock_type);
259 break;
260 default:
261 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
262 break;
263 }
264 }
266 /**
267 * gst_system_clock_obtain:
268 *
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
271 * usage.
272 *
273 * Returns: (transfer full): the default clock.
274 *
275 * MT safe.
276 */
277 GstClock *
278 gst_system_clock_obtain (void)
279 {
280 GstClock *clock;
282 g_static_mutex_lock (&_gst_sysclock_mutex);
283 clock = _the_system_clock;
285 if (clock == NULL) {
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);
296 } else {
297 g_static_mutex_unlock (&_gst_sysclock_mutex);
298 GST_CAT_DEBUG (GST_CAT_CLOCK, "returning static system clock");
299 }
301 /* we ref it since we are a clock factory. */
302 gst_object_ref (clock);
303 return clock;
304 }
306 static void
307 gst_system_clock_remove_wakeup (GstSystemClock * sysclock)
308 {
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");
317 }
318 GST_CLOCK_BROADCAST (sysclock);
319 }
320 GST_CAT_DEBUG (GST_CAT_CLOCK, "wakeup count %d",
321 sysclock->priv->wakeup_count);
322 }
324 static void
325 gst_system_clock_add_wakeup (GstSystemClock * sysclock)
326 {
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)) {
331 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {
332 g_warning
333 ("gstsystemclock: write control failed in wakeup_async, trying again: %d:%s\n",
334 errno, g_strerror (errno));
335 } else {
336 g_critical
337 ("gstsystemclock: write control failed in wakeup_async: %d:%s\n",
338 errno, g_strerror (errno));
339 return;
340 }
341 }
342 }
343 sysclock->priv->wakeup_count++;
344 GST_CAT_DEBUG (GST_CAT_CLOCK, "wakeup count %d",
345 sysclock->priv->wakeup_count);
346 }
348 static void
349 gst_system_clock_wait_wakeup (GstSystemClock * sysclock)
350 {
351 while (sysclock->priv->wakeup_count > 0) {
352 GST_CLOCK_WAIT (sysclock);
353 }
354 }
356 /* this thread reads the sorted clock entries from the queue.
357 *
358 * It waits on each of them and fires the callback when the timeout occurs.
359 *
360 * When an entry in the queue was canceled before we wait for it, it is
361 * simply skipped.
362 *
363 * When waiting for an entry, it can become canceled, in that case we don't
364 * call the callback but move to the next item in the queue.
365 *
366 * MT safe.
367 */
368 static void
369 gst_system_clock_async_thread (GstClock * clock)
370 {
371 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
373 GST_CAT_DEBUG (GST_CAT_CLOCK, "enter system clock thread");
374 GST_OBJECT_LOCK (clock);
375 /* signal spinup */
376 GST_CLOCK_BROADCAST (clock);
377 /* now enter our (almost) infinite loop */
378 while (!sysclock->stopping) {
379 GstClockEntry *entry;
380 GstClockTime requested;
381 GstClockReturn res;
383 /* check if something to be done */
384 while (clock->entries == NULL) {
385 GST_CAT_DEBUG (GST_CAT_CLOCK, "no clock entries, waiting..");
386 /* wait for work to do */
387 GST_CLOCK_WAIT (clock);
388 GST_CAT_DEBUG (GST_CAT_CLOCK, "got signal");
389 /* clock was stopping, exit */
390 if (sysclock->stopping)
391 goto exit;
392 }
394 /* see if we have a pending wakeup because the order of the list
395 * changed. */
396 if (sysclock->priv->async_wakeup) {
397 GST_CAT_DEBUG (GST_CAT_CLOCK, "clear async wakeup");
398 gst_system_clock_remove_wakeup (sysclock);
399 sysclock->priv->async_wakeup = FALSE;
400 }
402 /* pick the next entry */
403 entry = clock->entries->data;
404 GST_OBJECT_UNLOCK (clock);
406 requested = entry->time;
408 /* now wait for the entry, we already hold the lock */
409 res =
410 gst_system_clock_id_wait_jitter_unlocked (clock, (GstClockID) entry,
411 NULL, FALSE);
413 GST_OBJECT_LOCK (clock);
415 switch (res) {
416 case GST_CLOCK_UNSCHEDULED:
417 /* entry was unscheduled, move to the next */
418 GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry %p unscheduled", entry);
419 goto next_entry;
420 case GST_CLOCK_OK:
421 case GST_CLOCK_EARLY:
422 {
423 /* entry timed out normally, fire the callback and move to the next
424 * entry */
425 GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry %p timed out", entry);
426 if (entry->func) {
427 /* unlock before firing the callback */
428 GST_OBJECT_UNLOCK (clock);
429 entry->func (clock, entry->time, (GstClockID) entry,
430 entry->user_data);
431 GST_OBJECT_LOCK (clock);
432 }
433 if (entry->type == GST_CLOCK_ENTRY_PERIODIC) {
434 GST_CAT_DEBUG (GST_CAT_CLOCK, "updating periodic entry %p", entry);
435 /* adjust time now */
436 entry->time = requested + entry->interval;
437 /* and resort the list now */
438 clock->entries =
439 g_list_sort (clock->entries, gst_clock_id_compare_func);
440 /* and restart */
441 continue;
442 } else {
443 GST_CAT_DEBUG (GST_CAT_CLOCK, "moving to next entry");
444 goto next_entry;
445 }
446 }
447 case GST_CLOCK_BUSY:
448 /* somebody unlocked the entry but is was not canceled, This means that
449 * either a new entry was added in front of the queue or some other entry
450 * was canceled. Whatever it is, pick the head entry of the list and
451 * continue waiting. */
452 GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry %p needs restart", entry);
454 /* we set the entry back to the OK state. This is needed so that the
455 * _unschedule() code can see if an entry is currently being waited
456 * on (when its state is BUSY). */
457 SET_ENTRY_STATUS (entry, GST_CLOCK_OK);
458 continue;
459 default:
460 GST_CAT_DEBUG (GST_CAT_CLOCK,
461 "strange result %d waiting for %p, skipping", res, entry);
462 g_warning ("%s: strange result %d waiting for %p, skipping",
463 GST_OBJECT_NAME (clock), res, entry);
464 goto next_entry;
465 }
466 next_entry:
467 /* we remove the current entry and unref it */
468 clock->entries = g_list_remove (clock->entries, entry);
469 gst_clock_id_unref ((GstClockID) entry);
470 }
471 exit:
472 /* signal exit */
473 GST_CLOCK_BROADCAST (clock);
474 GST_OBJECT_UNLOCK (clock);
475 GST_CAT_DEBUG (GST_CAT_CLOCK, "exit system clock thread");
476 }
478 #ifdef HAVE_POSIX_TIMERS
479 static inline clockid_t
480 clock_type_to_posix_id (GstClockType clock_type)
481 {
482 #ifdef HAVE_MONOTONIC_CLOCK
483 if (clock_type == GST_CLOCK_TYPE_MONOTONIC)
484 return CLOCK_MONOTONIC;
485 else
486 #endif
487 return CLOCK_REALTIME;
488 }
489 #endif
491 /* MT safe */
492 static GstClockTime
493 gst_system_clock_get_internal_time (GstClock * clock)
494 {
495 #ifdef G_OS_WIN32
496 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
498 if (sysclock->priv->frequency.QuadPart != 0) {
499 LARGE_INTEGER now;
501 /* we prefer the highly accurate performance counters on windows */
502 QueryPerformanceCounter (&now);
504 return gst_util_uint64_scale (now.QuadPart - sysclock->priv->start.QuadPart,
505 GST_SECOND, sysclock->priv->frequency.QuadPart);
506 } else
507 #endif /* G_OS_WIN32 */
508 #if !defined HAVE_POSIX_TIMERS
509 {
510 GTimeVal timeval;
512 g_get_current_time (&timeval);
514 return GST_TIMEVAL_TO_TIME (timeval);
515 }
516 #else
517 {
518 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
519 clockid_t ptype;
520 struct timespec ts;
522 ptype = clock_type_to_posix_id (sysclock->priv->clock_type);
524 if (G_UNLIKELY (clock_gettime (ptype, &ts)))
525 return GST_CLOCK_TIME_NONE;
527 return GST_TIMESPEC_TO_TIME (ts);
528 }
529 #endif
530 }
532 static guint64
533 gst_system_clock_get_resolution (GstClock * clock)
534 {
535 #ifdef G_OS_WIN32
536 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
538 if (sysclock->priv->frequency.QuadPart != 0) {
539 return GST_SECOND / sysclock->priv->frequency.QuadPart;
540 } else
541 #endif /* G_OS_WIN32 */
542 #ifdef HAVE_POSIX_TIMERS
543 {
544 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
545 clockid_t ptype;
546 struct timespec ts;
548 ptype = clock_type_to_posix_id (sysclock->priv->clock_type);
550 if (G_UNLIKELY (clock_getres (ptype, &ts)))
551 return GST_CLOCK_TIME_NONE;
553 return GST_TIMESPEC_TO_TIME (ts);
554 }
555 #else
556 {
557 return 1 * GST_USECOND;
558 }
559 #endif
560 }
562 /* synchronously wait on the given GstClockEntry.
563 *
564 * We do this by blocking on the global GstPoll timer with
565 * the requested timeout. This allows us to unblock the
566 * entry by writing on the control fd.
567 *
568 * Note that writing the global GstPoll unlocks all waiting entries. So
569 * we need to check if an unlocked entry has changed when it unlocks.
570 *
571 * Entries that arrive too late are simply not waited on and a
572 * GST_CLOCK_EARLY result is returned.
573 *
574 * MT safe.
575 */
576 static GstClockReturn
577 gst_system_clock_id_wait_jitter_unlocked (GstClock * clock,
578 GstClockEntry * entry, GstClockTimeDiff * jitter, gboolean restart)
579 {
580 GstSystemClock *sysclock = GST_SYSTEM_CLOCK_CAST (clock);
581 GstClockTime entryt, now;
582 GstClockTimeDiff diff;
583 GstClockReturn status;
585 if (G_UNLIKELY (GET_ENTRY_STATUS (entry) == GST_CLOCK_UNSCHEDULED))
586 return GST_CLOCK_UNSCHEDULED;
588 /* need to call the overridden method because we want to sync against the time
589 * of the clock, whatever the subclass uses as a clock. */
590 now = gst_clock_get_time (clock);
592 /* get the time of the entry */
593 entryt = GST_CLOCK_ENTRY_TIME (entry);
595 /* the diff of the entry with the clock is the amount of time we have to
596 * wait */
597 diff = GST_CLOCK_DIFF (now, entryt);
598 if (G_LIKELY (jitter))
599 *jitter = -diff;
601 GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p"
602 " time %" GST_TIME_FORMAT
603 " now %" GST_TIME_FORMAT
604 " diff (time-now) %" G_GINT64_FORMAT,
605 entry, GST_TIME_ARGS (entryt), GST_TIME_ARGS (now), diff);
607 if (G_LIKELY (diff > 0)) {
608 #ifdef WAIT_DEBUGGING
609 GstClockTime final;
610 #endif
612 while (TRUE) {
613 gint pollret;
615 do {
616 status = GET_ENTRY_STATUS (entry);
618 /* stop when we are unscheduled */
619 if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED))
620 goto done;
622 /* mark the entry as busy but watch out for intermediate unscheduled
623 * statuses */
624 } while (G_UNLIKELY (!CAS_ENTRY_STATUS (entry, status, GST_CLOCK_BUSY)));
626 /* now wait on the entry, it either times out or the fd is written. The
627 * status of the entry is only BUSY around the poll. */
628 pollret = gst_poll_wait (sysclock->priv->timer, diff);
630 /* get the new status, mark as DONE. We do this so that the unschedule
631 * function knows when we left the poll and doesn't need to wakeup the
632 * poll anymore. */
633 do {
634 status = GET_ENTRY_STATUS (entry);
635 /* we were unscheduled, exit immediately */
636 if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED))
637 break;
638 } while (G_UNLIKELY (!CAS_ENTRY_STATUS (entry, status, GST_CLOCK_DONE)));
640 GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p unlocked, status %d, ret %d",
641 entry, status, pollret);
643 if (G_UNLIKELY (status == GST_CLOCK_UNSCHEDULED)) {
644 /* try to clean up The unschedule function managed to set the status to
645 * unscheduled. We now take the lock and mark the entry as unscheduled.
646 * This makes sure that the unschedule function doesn't perform a
647 * wakeup anymore. If the unschedule function has a change to perform
648 * the wakeup before us, we clean up here */
649 GST_OBJECT_LOCK (sysclock);
650 entry->unscheduled = TRUE;
651 if (entry->woken_up) {
652 gst_system_clock_remove_wakeup (sysclock);
653 }
654 GST_OBJECT_UNLOCK (sysclock);
655 goto done;
656 } else {
657 if (G_UNLIKELY (pollret != 0)) {
658 /* some other id got unlocked */
659 if (!restart) {
660 /* this can happen if the entry got unlocked because of an async
661 * entry was added to the head of the async queue. */
662 GST_CAT_DEBUG (GST_CAT_CLOCK, "wakeup waiting for entry %p", entry);
663 goto done;
664 }
666 /* wait till all the entries got woken up */
667 GST_OBJECT_LOCK (sysclock);
668 gst_system_clock_wait_wakeup (sysclock);
669 GST_OBJECT_UNLOCK (sysclock);
671 GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p needs to be restarted",
672 entry);
673 } else {
674 GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p unlocked after timeout",
675 entry);
676 }
678 /* reschedule if gst_poll_wait returned early or we have to reschedule after
679 * an unlock*/
680 now = gst_clock_get_time (clock);
681 diff = GST_CLOCK_DIFF (now, entryt);
683 if (diff <= 0) {
684 /* timeout, this is fine, we can report success now */
685 status = GST_CLOCK_OK;
686 SET_ENTRY_STATUS (entry, status);
688 GST_CAT_DEBUG (GST_CAT_CLOCK,
689 "entry %p finished, diff %" G_GINT64_FORMAT, entry, diff);
691 #ifdef WAIT_DEBUGGING
692 final = gst_system_clock_get_internal_time (clock);
693 GST_CAT_DEBUG (GST_CAT_CLOCK, "Waited for %" G_GINT64_FORMAT
694 " got %" G_GINT64_FORMAT " diff %" G_GINT64_FORMAT
695 " %g target-offset %" G_GINT64_FORMAT " %g", entryt, now,
696 now - entryt,
697 (double) (GstClockTimeDiff) (now - entryt) / GST_SECOND,
698 (final - target),
699 ((double) (GstClockTimeDiff) (final - target)) / GST_SECOND);
700 #endif
701 goto done;
702 } else {
703 GST_CAT_DEBUG (GST_CAT_CLOCK,
704 "entry %p restart, diff %" G_GINT64_FORMAT, entry, diff);
705 }
706 }
707 }
708 } else {
709 /* we are right on time or too late */
710 if (G_UNLIKELY (diff == 0))
711 status = GST_CLOCK_OK;
712 else
713 status = GST_CLOCK_EARLY;
715 SET_ENTRY_STATUS (entry, status);
716 }
717 done:
718 return status;
719 }
721 static GstClockReturn
722 gst_system_clock_id_wait_jitter (GstClock * clock, GstClockEntry * entry,
723 GstClockTimeDiff * jitter)
724 {
725 return gst_system_clock_id_wait_jitter_unlocked (clock, entry, jitter, TRUE);
726 }
728 /* Start the async clock thread. Must be called with the object lock
729 * held */
730 static gboolean
731 gst_system_clock_start_async (GstSystemClock * clock)
732 {
733 GError *error = NULL;
735 if (G_LIKELY (clock->thread != NULL))
736 return TRUE; /* Thread already running. Nothing to do */
738 #if !GLIB_CHECK_VERSION (2, 31, 0)
739 clock->thread = g_thread_create ((GThreadFunc) gst_system_clock_async_thread,
740 clock, TRUE, &error);
741 #else
742 clock->thread = g_thread_try_new ("GstSystemClock",
743 (GThreadFunc) gst_system_clock_async_thread, clock, &error);
744 #endif
746 if (G_UNLIKELY (error))
747 goto no_thread;
749 /* wait for it to spin up */
750 GST_CLOCK_WAIT (clock);
752 return TRUE;
754 /* ERRORS */
755 no_thread:
756 {
757 g_warning ("could not create async clock thread: %s", error->message);
758 g_error_free (error);
759 }
760 return FALSE;
761 }
763 /* Add an entry to the list of pending async waits. The entry is inserted
764 * in sorted order. If we inserted the entry at the head of the list, we
765 * need to signal the thread as it might either be waiting on it or waiting
766 * for a new entry.
767 *
768 * MT safe.
769 */
770 static GstClockReturn
771 gst_system_clock_id_wait_async (GstClock * clock, GstClockEntry * entry)
772 {
773 GstSystemClock *sysclock;
774 GstClockEntry *head;
776 sysclock = GST_SYSTEM_CLOCK_CAST (clock);
778 GST_CAT_DEBUG (GST_CAT_CLOCK, "adding async entry %p", entry);
780 GST_OBJECT_LOCK (clock);
781 /* Start the clock async thread if needed */
782 if (G_UNLIKELY (!gst_system_clock_start_async (sysclock)))
783 goto thread_error;
785 if (G_UNLIKELY (GET_ENTRY_STATUS (entry) == GST_CLOCK_UNSCHEDULED))
786 goto was_unscheduled;
788 if (clock->entries)
789 head = clock->entries->data;
790 else
791 head = NULL;
793 /* need to take a ref */
794 gst_clock_id_ref ((GstClockID) entry);
795 /* insert the entry in sorted order */
796 clock->entries = g_list_insert_sorted (clock->entries, entry,
797 gst_clock_id_compare_func);
799 /* only need to send the signal if the entry was added to the
800 * front, else the thread is just waiting for another entry and
801 * will get to this entry automatically. */
802 if (clock->entries->data == entry) {
803 GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry added to head %p", head);
804 if (head == NULL) {
805 /* the list was empty before, signal the cond so that the async thread can
806 * start taking a look at the queue */
807 GST_CAT_DEBUG (GST_CAT_CLOCK, "first entry, sending signal");
808 GST_CLOCK_BROADCAST (clock);
809 } else {
810 GstClockReturn status;
812 status = GET_ENTRY_STATUS (head);
813 GST_CAT_DEBUG (GST_CAT_CLOCK, "head entry %p status %d", head, status);
815 if (status == GST_CLOCK_BUSY) {
816 GST_CAT_DEBUG (GST_CAT_CLOCK, "head entry is busy");
817 /* the async thread was waiting for an entry, unlock the wait so that it
818 * looks at the new head entry instead, we only need to do this once */
819 if (!sysclock->priv->async_wakeup) {
820 GST_CAT_DEBUG (GST_CAT_CLOCK, "wakeup async thread");
821 sysclock->priv->async_wakeup = TRUE;
822 gst_system_clock_add_wakeup (sysclock);
823 }
824 }
825 }
826 }
827 GST_OBJECT_UNLOCK (clock);
829 return GST_CLOCK_OK;
831 /* ERRORS */
832 thread_error:
833 {
834 /* Could not start the async clock thread */
835 GST_OBJECT_UNLOCK (clock);
836 return GST_CLOCK_ERROR;
837 }
838 was_unscheduled:
839 {
840 GST_OBJECT_UNLOCK (clock);
841 return GST_CLOCK_UNSCHEDULED;
842 }
843 }
845 /* unschedule an entry. This will set the state of the entry to GST_CLOCK_UNSCHEDULED
846 * and will signal any thread waiting for entries to recheck their entry.
847 * We cannot really decide if the signal is needed or not because the entry
848 * could be waited on in async or sync mode.
849 *
850 * MT safe.
851 */
852 static void
853 gst_system_clock_id_unschedule (GstClock * clock, GstClockEntry * entry)
854 {
855 GstSystemClock *sysclock;
856 GstClockReturn status;
858 sysclock = GST_SYSTEM_CLOCK_CAST (clock);
860 GST_CAT_DEBUG (GST_CAT_CLOCK, "unscheduling entry %p", entry);
862 GST_OBJECT_LOCK (clock);
863 /* change the entry status to unscheduled */
864 do {
865 status = GET_ENTRY_STATUS (entry);
866 } while (G_UNLIKELY (!CAS_ENTRY_STATUS (entry, status,
867 GST_CLOCK_UNSCHEDULED)));
869 if (G_LIKELY (status == GST_CLOCK_BUSY)) {
870 /* the entry was being busy, wake up all entries so that they recheck their
871 * status. We cannot wake up just one entry because allocating such a
872 * datastructure for each entry would be too heavy and unlocking an entry
873 * is usually done when shutting down or some other exceptional case. */
874 GST_CAT_DEBUG (GST_CAT_CLOCK, "entry was BUSY, doing wakeup");
875 if (!entry->unscheduled && !entry->woken_up) {
876 gst_system_clock_add_wakeup (sysclock);
877 entry->woken_up = TRUE;
878 }
879 }
880 GST_OBJECT_UNLOCK (clock);
881 }