2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
4 * 2003 Benjamin Otte <in7y118@public.uni-hamburg.de>
6 * gstthread.c: Threaded container object
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
24 #include "gstthread.h"
25 #include "gstscheduler.h"
29 #define STACK_SIZE 0x200000
31 GstElementDetails gst_thread_details = {
35 "Container that creates/manages a thread",
37 "Erik Walthinsen <omega@cse.ogi.edu>,"
38 "Benjamin Otte <in7y118@informatik.uni-hamburg.de",
43 /* Thread signals and args */
63 static void gst_thread_class_init (GstThreadClass *klass);
64 static void gst_thread_init (GstThread *thread);
66 static void gst_thread_dispose (GObject *object);
68 static void gst_thread_set_property (GObject *object, guint prop_id,
69 const GValue *value, GParamSpec *pspec);
70 static void gst_thread_get_property (GObject *object, guint prop_id,
71 GValue *value, GParamSpec *pspec);
72 static GstElementStateReturn gst_thread_change_state (GstElement *element);
73 static void gst_thread_child_state_change (GstBin *bin, GstElementState oldstate,
74 GstElementState newstate, GstElement *element);
76 static void gst_thread_catch (GstThread *thread);
77 static void gst_thread_release (GstThread *thread);
79 #ifndef GST_DISABLE_LOADSAVE
80 static xmlNodePtr gst_thread_save_thyself (GstObject *object,
82 static void gst_thread_restore_thyself (GstObject *object,
86 static void * gst_thread_main_loop (void *arg);
88 #define GST_TYPE_THREAD_PRIORITY (gst_thread_priority_get_type())
90 gst_thread_priority_get_type(void)
92 static GType thread_priority_type = 0;
93 static GEnumValue thread_priority[] =
95 { G_THREAD_PRIORITY_LOW, "LOW", "Low Priority Scheduling" },
96 { G_THREAD_PRIORITY_NORMAL, "NORMAL", "Normal Scheduling" },
97 { G_THREAD_PRIORITY_HIGH, "HIGH", "High Priority Scheduling" },
98 { G_THREAD_PRIORITY_URGENT, "URGENT", "Urgent Scheduling" },
101 if (!thread_priority_type) {
102 thread_priority_type = g_enum_register_static("GstThreadPriority", thread_priority);
104 return thread_priority_type;
107 static GstBinClass *parent_class = NULL;
108 static guint gst_thread_signals[LAST_SIGNAL] = { 0 };
109 GPrivate *gst_thread_current;
112 gst_thread_get_type(void) {
113 static GType thread_type = 0;
116 static const GTypeInfo thread_info = {
117 sizeof (GstThreadClass), NULL, NULL,
118 (GClassInitFunc) gst_thread_class_init, NULL, NULL,
121 (GInstanceInitFunc) gst_thread_init,
124 thread_type = g_type_register_static (GST_TYPE_BIN, "GstThread",
130 static void do_nothing (gpointer hi) {}
132 gst_thread_class_init (GstThreadClass *klass)
134 GObjectClass *gobject_class;
135 GstObjectClass *gstobject_class;
136 GstElementClass *gstelement_class;
137 GstBinClass *gstbin_class;
139 /* setup gst_thread_current */
140 gst_thread_current = g_private_new (do_nothing);
142 gobject_class = (GObjectClass*)klass;
143 gstobject_class = (GstObjectClass*)klass;
144 gstelement_class = (GstElementClass*)klass;
145 gstbin_class = (GstBinClass*)klass;
147 parent_class = g_type_class_ref (GST_TYPE_BIN);
149 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_PRIORITY,
150 g_param_spec_enum ("priority", "Scheduling Policy", "The scheduling priority of the thread",
151 GST_TYPE_THREAD_PRIORITY, G_THREAD_PRIORITY_NORMAL, G_PARAM_READWRITE));
153 gst_thread_signals[SHUTDOWN] =
154 g_signal_new ("shutdown", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
155 G_STRUCT_OFFSET (GstThreadClass, shutdown), NULL, NULL,
156 gst_marshal_VOID__VOID, G_TYPE_NONE, 0);
158 gobject_class->dispose = gst_thread_dispose;
160 #ifndef GST_DISABLE_LOADSAVE
161 gstobject_class->save_thyself = GST_DEBUG_FUNCPTR (gst_thread_save_thyself);
162 gstobject_class->restore_thyself = GST_DEBUG_FUNCPTR (gst_thread_restore_thyself);
165 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_thread_change_state);
167 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_thread_set_property);
168 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_thread_get_property);
170 gstbin_class->child_state_change = GST_DEBUG_FUNCPTR (gst_thread_child_state_change);
174 gst_thread_init (GstThread *thread)
176 GstScheduler *scheduler;
178 GST_DEBUG (GST_CAT_THREAD, "initializing thread");
180 /* threads are managing bins and iterate themselves */
181 /* CR1: the GstBin code checks these flags */
182 GST_FLAG_SET (thread, GST_BIN_FLAG_MANAGER);
183 GST_FLAG_SET (thread, GST_BIN_SELF_SCHEDULABLE);
185 scheduler = gst_scheduler_factory_make (NULL, GST_ELEMENT (thread));
186 g_assert (scheduler);
188 thread->lock = g_mutex_new ();
189 thread->cond = g_cond_new ();
191 thread->thread_id = (GThread *) NULL; /* set in NULL -> READY */
192 thread->priority = G_THREAD_PRIORITY_NORMAL;
196 gst_thread_dispose (GObject *object)
198 GstThread *thread = GST_THREAD (object);
200 GST_DEBUG (GST_CAT_REFCOUNTING, "GstThread: dispose");
202 G_OBJECT_CLASS (parent_class)->dispose (object);
204 g_assert (GST_STATE (thread) == GST_STATE_NULL);
206 g_mutex_free (thread->lock);
207 g_cond_free (thread->cond);
209 gst_object_replace ((GstObject **)&GST_ELEMENT_SCHED (thread), NULL);
213 * gst_thread_set_priority:
214 * @thread: the thread to change
215 * @priority: the new priority for the thread
217 * change the thread's priority
220 gst_thread_set_priority (GstThread *thread, GThreadPriority priority)
222 g_return_if_fail (GST_IS_THREAD (thread));
224 thread->priority = priority;
228 gst_thread_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
232 thread = GST_THREAD (object);
236 thread->priority = g_value_get_enum (value);
239 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
245 gst_thread_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
249 thread = GST_THREAD (object);
253 g_value_set_enum (value, thread->priority);
256 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
264 * @name: the name of the thread
266 * Create a new thread with the given name.
268 * Returns: The new thread
271 gst_thread_new (const gchar *name)
273 return gst_element_factory_make ("thread", name);
277 * gst_thread_get_current:
279 * Create a new thread with the given name.
281 * Returns: The current GstThread or NULL if you are not running inside a
285 gst_thread_get_current (void)
287 return (GstThread *) g_private_get (gst_thread_current);
291 gst_thread_release_children_locks (GstThread *thread)
293 GstRealPad *peer = NULL;
294 GstElement *peerelement;
295 GList *elements = (GList *) gst_bin_get_list (GST_BIN (thread));
298 GstElement *element = GST_ELEMENT (elements->data);
302 GST_DEBUG (GST_CAT_THREAD, "waking element \"%s\"", GST_ELEMENT_NAME (element));
303 elements = g_list_next (elements);
305 if (!gst_element_release_locks (element))
306 g_warning ("element %s could not release locks", GST_ELEMENT_NAME (element));
308 pads = GST_ELEMENT_PADS (element);
311 if (GST_PAD_PEER (pads->data)) {
312 peer = GST_REAL_PAD (GST_PAD_PEER (pads->data));
313 pads = g_list_next (pads);
315 pads = g_list_next (pads);
322 peerelement = GST_PAD_PARENT (peer);
324 continue; /* FIXME: deal with case where there's no peer */
326 if (GST_ELEMENT_SCHED (peerelement) != GST_ELEMENT_SCHED (thread)) {
327 GST_DEBUG (GST_CAT_THREAD, "element \"%s\" has pad cross sched boundary", GST_ELEMENT_NAME (element));
328 GST_DEBUG (GST_CAT_THREAD, "waking element \"%s\"", GST_ELEMENT_NAME (peerelement));
329 if (!gst_element_release_locks (peerelement))
330 g_warning ("element %s could not release locks", GST_ELEMENT_NAME (peerelement));
335 /* stops the main thread, if there is one and grabs the thread's mutex */
337 gst_thread_catch (GstThread *thread)
340 if (thread == gst_thread_get_current()) {
341 /* we're trying to catch ourself */
342 if (!GST_FLAG_IS_SET (thread, GST_THREAD_MUTEX_LOCKED)) {
343 g_mutex_lock (thread->lock);
344 GST_FLAG_SET (thread, GST_THREAD_MUTEX_LOCKED);
346 GST_DEBUG (GST_CAT_THREAD, "%s is catching itself", GST_ELEMENT_NAME (thread));
347 GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
349 /* another thread is trying to catch us */
350 g_mutex_lock (thread->lock);
351 wait = !GST_FLAG_IS_SET (thread, GST_THREAD_STATE_SPINNING);
354 GST_DEBUG (GST_CAT_THREAD, "catching %s...", GST_ELEMENT_NAME (thread));
355 GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
356 g_cond_signal (thread->cond);
357 gst_thread_release_children_locks (thread);
358 g_get_current_time (&tv);
359 g_time_val_add (&tv, 1000); /* wait a millisecond to catch the thread */
360 wait = g_cond_timed_wait (thread->cond, thread->lock, &tv);
362 GST_DEBUG (GST_CAT_THREAD, "caught %s", GST_ELEMENT_NAME (thread));
364 g_assert (!GST_FLAG_IS_SET (thread, GST_THREAD_STATE_SPINNING));
368 gst_thread_release (GstThread *thread)
370 if (thread != gst_thread_get_current()) {
371 g_cond_signal (thread->cond);
372 g_mutex_unlock (thread->lock);
376 static GstElementStateReturn
377 gst_thread_change_state (GstElement *element)
380 GstElementStateReturn ret;
383 g_return_val_if_fail (GST_IS_THREAD (element), GST_STATE_FAILURE);
384 transition = GST_STATE_TRANSITION (element);
386 thread = GST_THREAD (element);
388 GST_DEBUG (GST_CAT_STATES, "%s is changing state from %s to %s",
389 GST_ELEMENT_NAME (element), gst_element_state_get_name (GST_STATE (element)),
390 gst_element_state_get_name (GST_STATE_PENDING (element)));
392 gst_thread_catch (thread);
394 /* FIXME: (or GStreamers ideas about "threading"): the element variables are
395 commonly accessed by multiple threads at the same time (see bug #111146
397 if (transition != GST_STATE_TRANSITION (element)) {
398 g_warning ("inconsistent state information, fix threading please");
401 switch (transition) {
402 case GST_STATE_NULL_TO_READY:
403 /* create the thread */
404 GST_FLAG_UNSET (thread, GST_THREAD_STATE_REAPING);
405 thread->thread_id = g_thread_create_full(gst_thread_main_loop,
406 thread, STACK_SIZE, FALSE, TRUE, thread->priority,
408 if (!thread->thread_id){
409 GST_DEBUG (GST_CAT_THREAD, "g_thread_create_full for %sfailed", GST_ELEMENT_NAME (element));
412 GST_DEBUG (GST_CAT_THREAD, "GThread created");
414 /* wait for it to 'spin up' */
415 g_cond_wait (thread->cond, thread->lock);
417 case GST_STATE_READY_TO_PAUSED:
419 case GST_STATE_PAUSED_TO_PLAYING:
421 /* FIXME: recurse into sub-bins */
422 GList *elements = (GList *) gst_bin_get_list (GST_BIN (thread));
424 gst_element_enable_threadsafe_properties ((GstElement*)elements->data);
425 elements = g_list_next (elements);
429 case GST_STATE_PLAYING_TO_PAUSED:
431 GList *elements = (GList *) gst_bin_get_list (GST_BIN (thread));
433 gst_element_disable_threadsafe_properties ((GstElement*)elements->data);
434 elements = g_list_next (elements);
438 case GST_STATE_PAUSED_TO_READY:
440 case GST_STATE_READY_TO_NULL:
441 /* we can't join the threads here, because this could have been triggered
443 GST_DEBUG (GST_CAT_THREAD, "destroying GThread %p", thread->thread_id);
444 GST_FLAG_SET (thread, GST_THREAD_STATE_REAPING);
445 thread->thread_id = NULL;
446 if (thread == gst_thread_get_current()) {
447 /* or should we continue? */
448 g_warning ("Thread %s is destroying itself. Function call will not return!", GST_ELEMENT_NAME (thread));
449 gst_scheduler_reset (GST_ELEMENT_SCHED (thread));
451 /* unlock and signal - we are out */
452 gst_thread_release (thread);
454 GST_INFO (GST_CAT_THREAD, "gstthread: thread \"%s\" is stopped",
455 GST_ELEMENT_NAME (thread));
457 g_signal_emit (G_OBJECT (thread), gst_thread_signals[SHUTDOWN], 0);
459 g_thread_exit (NULL);
461 /* now wait for the thread to destroy itself */
462 g_cond_signal (thread->cond);
463 g_cond_wait (thread->cond, thread->lock);
464 /* it should be dead now */
467 GST_DEBUG_ELEMENT (GST_CAT_THREAD, element, "UNHANDLED STATE CHANGE! %x",
468 GST_STATE_TRANSITION (element));
469 g_assert_not_reached ();
473 if (GST_ELEMENT_CLASS (parent_class)->change_state) {
474 ret = GST_ELEMENT_CLASS (parent_class)->change_state (GST_ELEMENT (thread));
476 ret = GST_STATE_SUCCESS;
479 gst_thread_release (thread);
483 GST_DEBUG (GST_CAT_STATES, "changing state from %s to %s failed for %s",
484 gst_element_state_get_name (GST_STATE (element)),
485 gst_element_state_get_name (GST_STATE_PENDING (element)),
486 GST_ELEMENT_NAME (element));
487 gst_thread_release (thread);
488 return GST_STATE_FAILURE;
491 /* state changes work this way: We grab the lock and stop the thread from
492 spinning (via gst_thread_catch) - then we change the state. After that the
493 thread may spin on. */
495 gst_thread_child_state_change (GstBin *bin, GstElementState oldstate,
496 GstElementState newstate, GstElement *element)
498 GST_DEBUG (GST_CAT_THREAD, "%s (from thread %s) child %s changed state from %s to %s",
499 GST_ELEMENT_NAME (bin),
500 gst_thread_get_current() ? GST_ELEMENT_NAME (gst_thread_get_current()) : "(none)",
501 GST_ELEMENT_NAME (element), gst_element_state_get_name (oldstate),
502 gst_element_state_get_name (newstate));
503 if (parent_class->child_state_change)
504 parent_class->child_state_change (bin, oldstate, newstate, element);
505 /* We'll wake up the main thread now. Note that we can't lock the thread here,
506 because we might be called from inside gst_thread_change_state when holding
507 the lock. But this doesn't cause any problems. */
508 if (newstate == GST_STATE_PLAYING)
509 g_cond_signal (GST_THREAD (bin)->cond);
512 * gst_thread_main_loop:
513 * @arg: the thread to start
515 * The main loop of the thread. The thread will iterate
516 * while the state is GST_THREAD_STATE_SPINNING.
519 gst_thread_main_loop (void *arg)
521 GstThread *thread = NULL;
524 thread = GST_THREAD (arg);
525 g_mutex_lock (thread->lock);
526 GST_DEBUG (GST_CAT_THREAD, "Thread %s started main loop", GST_ELEMENT_NAME (thread));
528 /* initialize gst_thread_current */
529 g_private_set (gst_thread_current, thread);
531 /* set up the element's scheduler */
532 gst_scheduler_setup (GST_ELEMENT_SCHED (thread));
533 GST_FLAG_UNSET (thread, GST_THREAD_STATE_REAPING);
535 g_cond_signal (thread->cond);
536 while (!(GST_FLAG_IS_SET (thread, GST_THREAD_STATE_REAPING))) {
537 if (GST_STATE (thread) == GST_STATE_PLAYING) {
538 GST_FLAG_SET (thread, GST_THREAD_STATE_SPINNING);
540 GST_DEBUG (GST_CAT_THREAD, "%s starts iterating", GST_ELEMENT_NAME (thread));
541 while (status && GST_FLAG_IS_SET (thread, GST_THREAD_STATE_SPINNING)) {
542 g_mutex_unlock (thread->lock);
543 status = gst_bin_iterate (GST_BIN (thread));
544 if (GST_FLAG_IS_SET (thread, GST_THREAD_MUTEX_LOCKED)) {
545 GST_FLAG_UNSET (thread, GST_THREAD_MUTEX_LOCKED);
547 g_mutex_lock (thread->lock);
550 GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
552 if (GST_FLAG_IS_SET (thread, GST_THREAD_STATE_REAPING))
554 GST_DEBUG (GST_CAT_THREAD, "%s was caught", GST_ELEMENT_NAME (thread));
555 g_cond_signal (thread->cond);
556 g_cond_wait (thread->cond, thread->lock);
559 /* we need to destroy the scheduler here because it has mapped it's
560 * stack into the threads stack space */
561 gst_scheduler_reset (GST_ELEMENT_SCHED (thread));
563 /* must do that before releasing the lock - we might get disposed before being done */
564 g_signal_emit (G_OBJECT (thread), gst_thread_signals[SHUTDOWN], 0);
566 /* unlock and signal - we are out */
567 g_cond_signal (thread->cond);
568 g_mutex_unlock (thread->lock);
570 GST_INFO (GST_CAT_THREAD, "gstthread: thread \"%s\" is stopped",
571 GST_ELEMENT_NAME (thread));
576 #ifndef GST_DISABLE_LOADSAVE
578 gst_thread_save_thyself (GstObject *object,
581 if (GST_OBJECT_CLASS (parent_class)->save_thyself)
582 GST_OBJECT_CLASS (parent_class)->save_thyself (object, self);
587 gst_thread_restore_thyself (GstObject *object,
590 GST_DEBUG (GST_CAT_THREAD,"gstthread: restore");
592 if (GST_OBJECT_CLASS (parent_class)->restore_thyself)
593 GST_OBJECT_CLASS (parent_class)->restore_thyself (object, self);
595 #endif /* GST_DISABLE_LOADSAVE */