valgrind unit tests as check-local; add gst_deinit
[platform/upstream/gstreamer.git] / gst / gstsystemclock.c
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  */
22
23 #include "gst_private.h"
24 #include "gstinfo.h"
25
26 #include "gstsystemclock.h"
27
28 /* the one instance of the systemclock */
29 static GstClock *_the_system_clock = NULL;
30
31 static void gst_system_clock_class_init (GstSystemClockClass * klass);
32 static void gst_system_clock_init (GstSystemClock * clock);
33 static void gst_system_clock_dispose (GObject * object);
34
35 static GstClockTime gst_system_clock_get_internal_time (GstClock * clock);
36 static guint64 gst_system_clock_get_resolution (GstClock * clock);
37 static GstClockReturn gst_system_clock_id_wait (GstClock * clock,
38     GstClockEntry * entry);
39 static GstClockReturn gst_system_clock_id_wait_unlocked
40     (GstClock * clock, GstClockEntry * entry);
41 static GstClockReturn gst_system_clock_id_wait_async (GstClock * clock,
42     GstClockEntry * entry);
43 static void gst_system_clock_id_unschedule (GstClock * clock,
44     GstClockEntry * entry);
45 static void gst_system_clock_async_thread (GstClock * clock);
46
47 static GStaticMutex _gst_sysclock_mutex = G_STATIC_MUTEX_INIT;
48
49 static GstClockClass *parent_class = NULL;
50
51 /* static guint gst_system_clock_signals[LAST_SIGNAL] = { 0 }; */
52
53 GType
54 gst_system_clock_get_type (void)
55 {
56   static GType clock_type = 0;
57
58   if (!clock_type) {
59     static const GTypeInfo clock_info = {
60       sizeof (GstSystemClockClass),
61       NULL,
62       NULL,
63       (GClassInitFunc) gst_system_clock_class_init,
64       NULL,
65       NULL,
66       sizeof (GstSystemClock),
67       0,
68       (GInstanceInitFunc) gst_system_clock_init,
69       NULL
70     };
71
72     clock_type = g_type_register_static (GST_TYPE_CLOCK, "GstSystemClock",
73         &clock_info, 0);
74   }
75   return clock_type;
76 }
77
78 static void
79 gst_system_clock_class_init (GstSystemClockClass * klass)
80 {
81   GObjectClass *gobject_class;
82   GstObjectClass *gstobject_class;
83   GstClockClass *gstclock_class;
84
85   gobject_class = (GObjectClass *) klass;
86   gstobject_class = (GstObjectClass *) klass;
87   gstclock_class = (GstClockClass *) klass;
88
89   parent_class = g_type_class_ref (GST_TYPE_CLOCK);
90
91   gobject_class->dispose = gst_system_clock_dispose;
92
93   gstclock_class->get_internal_time = gst_system_clock_get_internal_time;
94   gstclock_class->get_resolution = gst_system_clock_get_resolution;
95   gstclock_class->wait = gst_system_clock_id_wait;
96   gstclock_class->wait_async = gst_system_clock_id_wait_async;
97   gstclock_class->unschedule = gst_system_clock_id_unschedule;
98 }
99
100 static void
101 gst_system_clock_init (GstSystemClock * clock)
102 {
103   GError *error = NULL;
104
105   GST_CLOCK_FLAGS (clock) =
106       GST_CLOCK_FLAG_CAN_DO_SINGLE_SYNC |
107       GST_CLOCK_FLAG_CAN_DO_SINGLE_ASYNC |
108       GST_CLOCK_FLAG_CAN_DO_PERIODIC_SYNC |
109       GST_CLOCK_FLAG_CAN_DO_PERIODIC_ASYNC;
110
111   GST_LOCK (clock);
112   clock->thread = g_thread_create ((GThreadFunc) gst_system_clock_async_thread,
113       clock, TRUE, &error);
114   if (error)
115     goto no_thread;
116
117   /* wait for it to spin up */
118   GST_CLOCK_WAIT (clock);
119   GST_UNLOCK (clock);
120   return;
121
122 no_thread:
123   {
124     g_warning ("could not create async clock thread: %s", error->message);
125     GST_UNLOCK (clock);
126   }
127 }
128
129 static void
130 gst_system_clock_dispose (GObject * object)
131 {
132   GstClock *clock = (GstClock *) object;
133
134   GstSystemClock *sysclock = GST_SYSTEM_CLOCK (clock);
135   GList *entries;
136
137   /* else we have to stop the thread */
138   GST_LOCK (clock);
139   sysclock->stopping = TRUE;
140   /* unschedule all entries */
141   for (entries = clock->entries; entries; entries = g_list_next (entries)) {
142     GstClockEntry *entry = (GstClockEntry *) entries->data;
143
144     GST_CAT_DEBUG (GST_CAT_CLOCK, "unscheduling entry %p", entry);
145     entry->status = GST_CLOCK_UNSCHEDULED;
146   }
147   g_list_free (clock->entries);
148   clock->entries = NULL;
149   GST_CLOCK_BROADCAST (clock);
150   GST_UNLOCK (clock);
151
152   if (sysclock->thread)
153     g_thread_join (sysclock->thread);
154   sysclock->thread = NULL;
155   GST_CAT_DEBUG (GST_CAT_CLOCK, "joined thread");
156
157   G_OBJECT_CLASS (parent_class)->dispose (object);
158
159   if (_the_system_clock == clock) {
160     _the_system_clock = NULL;
161     GST_CAT_DEBUG (GST_CAT_CLOCK, "disposed system clock");
162   }
163 }
164
165 /**
166  * gst_system_clock_obtain:
167  *
168  * Get a handle to the default system clock. The refcount of the
169  * clock will be increased so you need to unref the clock after
170  * usage.
171  *
172  * Returns: the default clock.
173  *
174  * MT safe.
175  */
176 GstClock *
177 gst_system_clock_obtain (void)
178 {
179   GstClock *clock;
180
181   g_static_mutex_lock (&_gst_sysclock_mutex);
182   clock = _the_system_clock;
183
184   if (clock == NULL) {
185     GST_CAT_DEBUG (GST_CAT_CLOCK, "creating new static system clock");
186     clock = g_object_new (GST_TYPE_SYSTEM_CLOCK,
187         "name", "GstSystemClock", NULL);
188
189     /* we created the global clock; take ownership so
190      * we can hand out instances later */
191     gst_object_ref (clock);
192     gst_object_sink (GST_OBJECT (clock));
193
194     _the_system_clock = clock;
195     g_static_mutex_unlock (&_gst_sysclock_mutex);
196   } else {
197     g_static_mutex_unlock (&_gst_sysclock_mutex);
198     GST_CAT_DEBUG (GST_CAT_CLOCK, "returning static system clock");
199   }
200
201   /* we ref it since we are a clock factory. */
202   gst_object_ref (clock);
203   return clock;
204 }
205
206 /* this thread reads the sorted clock entries from the queue. 
207  *
208  * It waits on each of them and fires the callback when the timeout occurs.
209  *
210  * When an entry in the queue was canceled, it is simply skipped.
211  *
212  * When waiting for an entry, it can become canceled, in that case we don't 
213  * call the callback but move to the next item in the queue.
214  *
215  * MT safe.
216  */
217 static void
218 gst_system_clock_async_thread (GstClock * clock)
219 {
220   GstSystemClock *sysclock = GST_SYSTEM_CLOCK (clock);
221
222   GST_CAT_DEBUG (GST_CAT_CLOCK, "enter system clock thread");
223   GST_LOCK (clock);
224   /* signal spinup */
225   GST_CLOCK_BROADCAST (clock);
226   /* now enter our infinite loop */
227   while (!sysclock->stopping) {
228     GstClockEntry *entry;
229     GstClockReturn res;
230
231     /* check if something to be done */
232     while (clock->entries == NULL) {
233       GST_CAT_DEBUG (GST_CAT_CLOCK, "nothing to wait for");
234       /* wait for work to do */
235       GST_CLOCK_WAIT (clock);
236       GST_CAT_DEBUG (GST_CAT_CLOCK, "got signal");
237       /* clock was stopping, exit */
238       if (sysclock->stopping)
239         goto exit;
240     }
241
242     /* pick the next entry */
243     entry = clock->entries->data;
244     /* if it was unscheduled, just move on to the next entry */
245     if (entry->status == GST_CLOCK_UNSCHEDULED) {
246       GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p was unscheduled", entry);
247       goto next_entry;
248     }
249
250     /* now wait for the entry, we already hold the lock */
251     res = gst_system_clock_id_wait_unlocked (clock, (GstClockID) entry);
252
253     switch (res) {
254       case GST_CLOCK_UNSCHEDULED:
255         /* entry was unscheduled, move to the next */
256         GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry %p unscheduled", entry);
257         goto next_entry;
258       case GST_CLOCK_OK:
259       case GST_CLOCK_EARLY:
260       {
261         /* entry timed out normally, fire the callback and move to the next
262          * entry */
263         GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry %p unlocked", entry);
264         if (entry->func) {
265           entry->func (clock, entry->time, (GstClockID) entry,
266               entry->user_data);
267         }
268         if (entry->type == GST_CLOCK_ENTRY_PERIODIC) {
269           /* adjust time now */
270           entry->time += entry->interval;
271           /* and resort the list now */
272           clock->entries =
273               g_list_sort (clock->entries, gst_clock_id_compare_func);
274           /* and restart */
275           continue;
276         } else {
277           goto next_entry;
278         }
279       }
280       case GST_CLOCK_BUSY:
281         /* somebody unlocked the entry but is was not canceled, This means that
282          * either a new entry was added in front of the queue or some other entry 
283          * was canceled. Whatever it is, pick the head entry of the list and
284          * continue waiting. */
285         GST_CAT_DEBUG (GST_CAT_CLOCK, "async entry %p needs restart", entry);
286         continue;
287       default:
288         GST_CAT_DEBUG (GST_CAT_CLOCK,
289             "strange result %d waiting for %p, skipping", res, entry);
290         goto next_entry;
291     }
292   next_entry:
293     /* we remove the current entry and unref it */
294     clock->entries = g_list_remove (clock->entries, entry);
295     gst_clock_id_unref ((GstClockID) entry);
296   }
297 exit:
298   /* signal exit */
299   GST_CLOCK_BROADCAST (clock);
300   GST_UNLOCK (clock);
301   GST_CAT_DEBUG (GST_CAT_CLOCK, "exit system clock thread");
302 }
303
304 /* MT safe */
305 static GstClockTime
306 gst_system_clock_get_internal_time (GstClock * clock)
307 {
308   GTimeVal timeval;
309
310   g_get_current_time (&timeval);
311
312   return GST_TIMEVAL_TO_TIME (timeval);
313 }
314
315 static guint64
316 gst_system_clock_get_resolution (GstClock * clock)
317 {
318   return 1 * GST_USECOND;
319 }
320
321 /* synchronously wait on the given GstClockEntry.
322  *
323  * We do this by blocking on the global clock GCond variable with
324  * the requested time as a timeout. This allows us to unblock the
325  * entry by signaling the GCond variable.
326  *
327  * Note that signaling the global GCond unlocks all waiting entries. So
328  * we need to check if an unlocked entry has changed when it unlocks.
329  *
330  * Entries that arrive too late are simply not waited on and a
331  * GST_CLOCK_EARLY result is returned.
332  *
333  * MT safe.
334  */
335 static GstClockReturn
336 gst_system_clock_id_wait_unlocked (GstClock * clock, GstClockEntry * entry)
337 {
338   GstClockTime entryt, real, now, target;
339   GstClockTimeDiff diff;
340
341   /* need to call the overridden method */
342   real = GST_CLOCK_GET_CLASS (clock)->get_internal_time (clock);
343   entryt = GST_CLOCK_ENTRY_TIME (entry);
344
345   now = gst_clock_adjust_unlocked (clock, real);
346   diff = entryt - now;
347   target = gst_system_clock_get_internal_time (clock) + diff;
348
349   GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p"
350       " target %" GST_TIME_FORMAT
351       " entry %" GST_TIME_FORMAT
352       " now %" GST_TIME_FORMAT
353       " real %" GST_TIME_FORMAT
354       " diff %" G_GINT64_FORMAT,
355       entry,
356       GST_TIME_ARGS (target),
357       GST_TIME_ARGS (entryt), GST_TIME_ARGS (now), GST_TIME_ARGS (real), diff);
358
359   if (diff > 0) {
360     GTimeVal tv;
361
362     GST_TIME_TO_TIMEVAL (target, tv);
363
364     while (TRUE) {
365       /* now wait on the entry, it either times out or the cond is signaled. */
366       if (!GST_CLOCK_TIMED_WAIT (clock, &tv)) {
367         /* timeout, this is fine, we can report success now */
368         GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p unlocked after timeout", entry);
369         entry->status = GST_CLOCK_OK;
370         break;
371       } else {
372         /* the waiting is interrupted because the GCond was signaled. This can
373          * be because this or some other entry was unscheduled. */
374         GST_CAT_DEBUG (GST_CAT_CLOCK, "entry %p unlocked with signal", entry);
375         /* if the entry is unscheduled, we can stop waiting for it */
376         if (entry->status == GST_CLOCK_UNSCHEDULED)
377           break;
378       }
379     }
380   } else {
381     entry->status = GST_CLOCK_EARLY;
382   }
383   return entry->status;
384 }
385
386 static GstClockReturn
387 gst_system_clock_id_wait (GstClock * clock, GstClockEntry * entry)
388 {
389   GstClockReturn ret;
390
391   GST_LOCK (clock);
392   ret = gst_system_clock_id_wait_unlocked (clock, entry);
393   GST_UNLOCK (clock);
394
395   return ret;
396 }
397
398 /* Add an entry to the list of pending async waits. The entry is inserted
399  * in sorted order. If we inserted the entry at the head of the list, we
400  * need to signal the thread as it might either be waiting on it or waiting
401  * for a new entry. 
402  *
403  * MT safe.
404  */
405 static GstClockReturn
406 gst_system_clock_id_wait_async (GstClock * clock, GstClockEntry * entry)
407 {
408   GST_CAT_DEBUG (GST_CAT_CLOCK, "adding entry %p", entry);
409
410   GST_LOCK (clock);
411   /* need to take a ref */
412   gst_clock_id_ref ((GstClockID) entry);
413   /* insert the entry in sorted order */
414   clock->entries = g_list_insert_sorted (clock->entries, entry,
415       gst_clock_id_compare_func);
416
417   /* only need to send the signal if the entry was added to the
418    * front, else the thread is just waiting for another entry and
419    * will get to this entry automatically. */
420   if (clock->entries->data == entry) {
421     GST_CAT_DEBUG (GST_CAT_CLOCK, "send signal");
422     GST_CLOCK_BROADCAST (clock);
423   }
424   GST_UNLOCK (clock);
425
426   return GST_CLOCK_OK;
427 }
428
429 /* unschedule an entry. This will set the state of the entry to GST_CLOCK_UNSCHEDULED
430  * and will signal any thread waiting for entries to recheck their entry. 
431  * We cannot really decide if the signal is needed or not because the entry
432  * could be waited on in async or sync mode.
433  *
434  * MT safe.
435  */
436 static void
437 gst_system_clock_id_unschedule (GstClock * clock, GstClockEntry * entry)
438 {
439   GST_CAT_DEBUG (GST_CAT_CLOCK, "unscheduling entry %p", entry);
440
441   GST_LOCK (clock);
442   entry->status = GST_CLOCK_UNSCHEDULED;
443   GST_CAT_DEBUG (GST_CAT_CLOCK, "send signal");
444   GST_CLOCK_BROADCAST (clock);
445   GST_UNLOCK (clock);
446 }