]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - glsdk/gstreamer0-10.git/blob - gst/gstthread.c
don't mix tabs and spaces
[glsdk/gstreamer0-10.git] / gst / gstthread.c
1 /* GStreamer
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>
5  *
6  * gstthread.c: Threaded container object
7  *
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.
12  *
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.
17  *
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.
22  */
24 #include "gst_private.h"
26 #include "gstthread.h"
27 #include "gstmarshal.h"
28 #include "gstscheduler.h"
29 #include "gstinfo.h"
30 #include "gstlog.h"
32 #define GST_CAT_DEFAULT GST_CAT_THREAD
33 #define STACK_SIZE 0x200000
35 static GstElementDetails gst_thread_details =
36 GST_ELEMENT_DETAILS ("Threaded container",
37     "Generic/Bin",
38     "Container that creates/manages a thread",
39     "Erik Walthinsen <omega@cse.ogi.edu>, "
40     "Benjamin Otte <in7y118@informatik.uni-hamburg.de");
42 /* Thread signals and args */
43 enum
44 {
45   SHUTDOWN,
46   /* FILL ME */
47   LAST_SIGNAL
48 };
50 enum
51 {
52   SPINUP = 0,
53   STATECHANGE,
54   STARTUP
55 };
57 enum
58 {
59   ARG_0,
60   ARG_PRIORITY
61 };
64 static void gst_thread_base_init (gpointer g_class);
65 static void gst_thread_class_init (gpointer g_class, gpointer class_data);
66 static void gst_thread_init (GTypeInstance * instance, gpointer g_class);
68 static void gst_thread_dispose (GObject * object);
70 static void gst_thread_set_property (GObject * object, guint prop_id,
71     const GValue * value, GParamSpec * pspec);
72 static void gst_thread_get_property (GObject * object, guint prop_id,
73     GValue * value, GParamSpec * pspec);
74 static GstElementStateReturn gst_thread_change_state (GstElement * element);
75 static void gst_thread_child_state_change (GstBin * bin,
76     GstElementState oldstate, GstElementState newstate, GstElement * element);
78 static void gst_thread_catch (GstThread * thread);
79 static void gst_thread_release (GstThread * thread);
81 #ifndef GST_DISABLE_LOADSAVE
82 static xmlNodePtr gst_thread_save_thyself (GstObject * object,
83     xmlNodePtr parent);
84 static void gst_thread_restore_thyself (GstObject * object, xmlNodePtr self);
85 #endif
87 static void *gst_thread_main_loop (void *arg);
89 #define GST_TYPE_THREAD_PRIORITY (gst_thread_priority_get_type())
90 static GType
91 gst_thread_priority_get_type (void)
92 {
93   static GType thread_priority_type = 0;
94   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"},
99     {0, NULL, NULL},
100   };
102   if (!thread_priority_type) {
103     thread_priority_type =
104         g_enum_register_static ("GstThreadPriority", thread_priority);
105   }
106   return thread_priority_type;
109 static GstBinClass *parent_class = NULL;
110 static guint gst_thread_signals[LAST_SIGNAL] = { 0 };
111 GPrivate *gst_thread_current;
113 GType
114 gst_thread_get_type (void)
116   static GType thread_type = 0;
118   if (!thread_type) {
119     static const GTypeInfo thread_info = {
120       sizeof (GstThreadClass),
121       gst_thread_base_init,
122       NULL,
123       gst_thread_class_init,
124       NULL,
125       NULL,
126       sizeof (GstThread),
127       4,
128       gst_thread_init,
129       NULL
130     };
132     thread_type = g_type_register_static (GST_TYPE_BIN, "GstThread",
133         &thread_info, 0);
134   }
135   return thread_type;
138 static void
139 gst_thread_base_init (gpointer g_class)
141   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
143   gst_element_class_set_details (gstelement_class, &gst_thread_details);
145 static void
146 do_nothing (gpointer hi)
149 static void
150 gst_thread_class_init (gpointer g_class, gpointer class_data)
152   GObjectClass *gobject_class = G_OBJECT_CLASS (g_class);
154 #ifndef GST_DISABLE_LOADSAVE
155   GstObjectClass *gstobject_class = GST_OBJECT_CLASS (g_class);
156 #endif
157   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
158   GstBinClass *gstbin_class = GST_BIN_CLASS (g_class);
159   GstThreadClass *klass = GST_THREAD_CLASS (g_class);
161   /* setup gst_thread_current */
162   gst_thread_current = g_private_new (do_nothing);
164   parent_class = g_type_class_peek_parent (g_class);
166   g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_PRIORITY,
167       g_param_spec_enum ("priority", "Scheduling Policy",
168           "The scheduling priority of the thread", GST_TYPE_THREAD_PRIORITY,
169           G_THREAD_PRIORITY_NORMAL, G_PARAM_READWRITE));
171   gst_thread_signals[SHUTDOWN] =
172       g_signal_new ("shutdown", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
173       G_STRUCT_OFFSET (GstThreadClass, shutdown), NULL, NULL,
174       gst_marshal_VOID__VOID, G_TYPE_NONE, 0);
176   gobject_class->dispose = gst_thread_dispose;
178 #ifndef GST_DISABLE_LOADSAVE
179   gstobject_class->save_thyself = GST_DEBUG_FUNCPTR (gst_thread_save_thyself);
180   gstobject_class->restore_thyself =
181       GST_DEBUG_FUNCPTR (gst_thread_restore_thyself);
182 #endif
184   gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_thread_change_state);
186   gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_thread_set_property);
187   gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_thread_get_property);
189   gstbin_class->child_state_change =
190       GST_DEBUG_FUNCPTR (gst_thread_child_state_change);
193 static void
194 gst_thread_init (GTypeInstance * instance, gpointer g_class)
196   GstScheduler *scheduler;
197   GstThread *thread = GST_THREAD (instance);
199   GST_DEBUG ("initializing thread");
201   /* threads are managing bins and iterate themselves */
202   /* CR1: the GstBin code checks these flags */
203   GST_FLAG_SET (thread, GST_BIN_FLAG_MANAGER);
204   GST_FLAG_SET (thread, GST_BIN_SELF_SCHEDULABLE);
206   scheduler = gst_scheduler_factory_make (NULL, GST_ELEMENT (thread));
207   g_assert (scheduler);
209   thread->lock = g_mutex_new ();
210   thread->cond = g_cond_new ();
212   thread->thread_id = (GThread *) NULL; /* set in NULL -> READY */
213   thread->priority = G_THREAD_PRIORITY_NORMAL;
216 static void
217 gst_thread_dispose (GObject * object)
219   GstThread *thread = GST_THREAD (object);
221   GST_CAT_DEBUG (GST_CAT_REFCOUNTING, "GstThread: dispose");
223   G_OBJECT_CLASS (parent_class)->dispose (object);
225   g_assert (GST_STATE (thread) == GST_STATE_NULL);
227   g_mutex_free (thread->lock);
228   g_cond_free (thread->cond);
230   gst_object_replace ((GstObject **) & GST_ELEMENT_SCHED (thread), NULL);
233 /**
234  * gst_thread_set_priority:
235  * @thread: the thread to change 
236  * @priority: the new priority for the thread
237  *
238  * change the thread's priority
239  */
240 void
241 gst_thread_set_priority (GstThread * thread, GThreadPriority priority)
243   g_return_if_fail (GST_IS_THREAD (thread));
245   thread->priority = priority;
248 static void
249 gst_thread_set_property (GObject * object, guint prop_id, const GValue * value,
250     GParamSpec * pspec)
252   GstThread *thread;
254   thread = GST_THREAD (object);
256   switch (prop_id) {
257     case ARG_PRIORITY:
258       thread->priority = g_value_get_enum (value);
259       break;
260     default:
261       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
262       break;
263   }
266 static void
267 gst_thread_get_property (GObject * object, guint prop_id, GValue * value,
268     GParamSpec * pspec)
270   GstThread *thread;
272   thread = GST_THREAD (object);
274   switch (prop_id) {
275     case ARG_PRIORITY:
276       g_value_set_enum (value, thread->priority);
277       break;
278     default:
279       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
280       break;
281   }
285 /**
286  * gst_thread_new:
287  * @name: the name of the thread
288  *
289  * Create a new thread with the given name.
290  *
291  * Returns: The new thread
292  */
293 GstElement *
294 gst_thread_new (const gchar * name)
296   return gst_element_factory_make ("thread", name);
299 /**
300  * gst_thread_get_current:
301  *
302  * Create a new thread with the given name.
303  *
304  * Returns: The current GstThread or NULL if you are not running inside a 
305  *          #GstThread.
306  */
307 GstThread *
308 gst_thread_get_current (void)
310   return (GstThread *) g_private_get (gst_thread_current);
313 static inline void
314 gst_thread_release_children_locks (GstThread * thread)
316   GstRealPad *peer = NULL;
317   GstElement *peerelement;
318   GList *elements = (GList *) gst_bin_get_list (GST_BIN (thread));
320   while (elements) {
321     GstElement *element = GST_ELEMENT (elements->data);
322     GList *pads;
324     g_assert (element);
325     GST_DEBUG_OBJECT (thread, "waking element \"%s\"",
326         GST_ELEMENT_NAME (element));
327     elements = g_list_next (elements);
329     if (!gst_element_release_locks (element))
330       g_warning ("element %s could not release locks",
331           GST_ELEMENT_NAME (element));
333     pads = GST_ELEMENT_PADS (element);
335     while (pads) {
336       if (GST_PAD_PEER (pads->data)) {
337         peer = GST_REAL_PAD (GST_PAD_PEER (pads->data));
338         pads = g_list_next (pads);
339       } else {
340         pads = g_list_next (pads);
341         continue;
342       }
344       if (!peer)
345         continue;
347       peerelement = GST_PAD_PARENT (peer);
348       if (!peerelement)
349         continue;               /* FIXME: deal with case where there's no peer */
351       if (GST_ELEMENT_SCHED (peerelement) != GST_ELEMENT_SCHED (thread)) {
352         GST_LOG_OBJECT (thread, "element \"%s\" has pad cross sched boundary",
353             GST_ELEMENT_NAME (element));
354         GST_LOG_OBJECT (thread, "waking element \"%s\"",
355             GST_ELEMENT_NAME (peerelement));
356         if (!gst_element_release_locks (peerelement))
357           g_warning ("element %s could not release locks",
358               GST_ELEMENT_NAME (peerelement));
359       }
360     }
361   }
364 /* stops the main thread, if there is one and grabs the thread's mutex */
365 static void
366 gst_thread_catch (GstThread * thread)
368   gboolean wait;
370   if (thread == gst_thread_get_current ()) {
371     /* we're trying to catch ourself */
372     if (!GST_FLAG_IS_SET (thread, GST_THREAD_MUTEX_LOCKED)) {
373       g_mutex_lock (thread->lock);
374       GST_FLAG_SET (thread, GST_THREAD_MUTEX_LOCKED);
375     }
376     GST_DEBUG_OBJECT (thread, "catching itself");
377     GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
378   } else {
379     /* another thread is trying to catch us */
380     g_mutex_lock (thread->lock);
381     wait = !GST_FLAG_IS_SET (thread, GST_THREAD_STATE_SPINNING);
382     while (!wait) {
383       GTimeVal tv;
385       GST_LOG_OBJECT (thread, "catching thread...");
386       GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
387       g_cond_signal (thread->cond);
388       gst_thread_release_children_locks (thread);
389       g_get_current_time (&tv);
390       g_time_val_add (&tv, 1000);       /* wait a millisecond to catch the thread */
391       wait = g_cond_timed_wait (thread->cond, thread->lock, &tv);
392     }
393     GST_LOG_OBJECT (thread, "caught thread");
394   }
395   g_assert (!GST_FLAG_IS_SET (thread, GST_THREAD_STATE_SPINNING));
398 static void
399 gst_thread_release (GstThread * thread)
401   if (thread != gst_thread_get_current ()) {
402     g_cond_signal (thread->cond);
403     g_mutex_unlock (thread->lock);
404   }
407 static GstElementStateReturn
408 gst_thread_change_state (GstElement * element)
410   GstThread *thread;
411   GstElementStateReturn ret;
412   gint transition;
414   g_return_val_if_fail (GST_IS_THREAD (element), GST_STATE_FAILURE);
415   transition = GST_STATE_TRANSITION (element);
417   thread = GST_THREAD (element);
419   GST_DEBUG_OBJECT (element, "changing state from %s to %s",
420       gst_element_state_get_name (GST_STATE (element)),
421       gst_element_state_get_name (GST_STATE_PENDING (element)));
423   gst_thread_catch (thread);
425   /* FIXME: (or GStreamers ideas about "threading"): the element variables are
426      commonly accessed by multiple threads at the same time (see bug #111146
427      for an example) */
428   if (transition != GST_STATE_TRANSITION (element)) {
429     g_warning ("inconsistent state information, fix threading please");
430   }
432   switch (transition) {
433     case GST_STATE_NULL_TO_READY:
434       /* create the thread */
435       GST_FLAG_UNSET (thread, GST_THREAD_STATE_REAPING);
436       thread->thread_id = g_thread_create_full (gst_thread_main_loop,
437           thread, STACK_SIZE, FALSE, TRUE, thread->priority, NULL);
438       if (!thread->thread_id) {
439         GST_ERROR_OBJECT (element, "g_thread_create_full failed");
440         goto error_out;
441       }
442       GST_LOG_OBJECT (element, "GThread created");
444       /* wait for it to 'spin up' */
445       g_cond_wait (thread->cond, thread->lock);
446       break;
447     case GST_STATE_READY_TO_PAUSED:
448       break;
449     case GST_STATE_PAUSED_TO_PLAYING:
450     {
451       /* FIXME: recurse into sub-bins */
452       GList *elements = (GList *) gst_bin_get_list (GST_BIN (thread));
454       while (elements) {
455         gst_element_enable_threadsafe_properties ((GstElement *) elements->
456             data);
457         elements = g_list_next (elements);
458       }
459       /* reset self to spinning */
460       if (thread == gst_thread_get_current ())
461         GST_FLAG_SET (thread, GST_THREAD_STATE_SPINNING);
462       break;
463     }
464     case GST_STATE_PLAYING_TO_PAUSED:
465     {
466       GList *elements = (GList *) gst_bin_get_list (GST_BIN (thread));
468       while (elements) {
469         gst_element_disable_threadsafe_properties ((GstElement *) elements->
470             data);
471         elements = g_list_next (elements);
472       }
473       break;
474     }
475     case GST_STATE_PAUSED_TO_READY:
476       break;
477     case GST_STATE_READY_TO_NULL:
478       /* we can't join the threads here, because this could have been triggered
479          by ourself (ouch) */
480       GST_LOG_OBJECT (thread, "destroying GThread %p", thread->thread_id);
481       GST_FLAG_SET (thread, GST_THREAD_STATE_REAPING);
482       thread->thread_id = NULL;
483       if (thread == gst_thread_get_current ()) {
484         /* or should we continue? */
485         g_warning
486             ("Thread %s is destroying itself. Function call will not return!",
487             GST_ELEMENT_NAME (thread));
488         gst_scheduler_reset (GST_ELEMENT_SCHED (thread));
490         /* unlock and signal - we are out */
491         gst_thread_release (thread);
493         GST_INFO_OBJECT (thread, "GThread %p is exiting", g_thread_self ());
495         g_signal_emit (G_OBJECT (thread), gst_thread_signals[SHUTDOWN], 0);
497         g_thread_exit (NULL);
498       }
499       /* now wait for the thread to destroy itself */
500       g_cond_signal (thread->cond);
501       g_cond_wait (thread->cond, thread->lock);
502       /* it should be dead now */
503       break;
504     default:
505       GST_ERROR_OBJECT (element, "unhandled state change! %x",
506           GST_STATE_TRANSITION (element));
507       g_warning ("thread %s: UNHANDLED STATE CHANGE! %x",
508           GST_STR_NULL (GST_OBJECT_NAME (element)),
509           GST_STATE_TRANSITION (element));
510       /* FIXME: not doable with current threading mess:
511          g_assert_not_reached ();
512        */
513       break;
514   }
516   if (GST_ELEMENT_CLASS (parent_class)->change_state) {
517     ret = GST_ELEMENT_CLASS (parent_class)->change_state (GST_ELEMENT (thread));
518   } else {
519     ret = GST_STATE_SUCCESS;
520   }
522   gst_thread_release (thread);
523   return ret;
525 error_out:
526   GST_CAT_DEBUG (GST_CAT_STATES, "changing state from %s to %s failed for %s",
527       gst_element_state_get_name (GST_STATE (element)),
528       gst_element_state_get_name (GST_STATE_PENDING (element)),
529       GST_ELEMENT_NAME (element));
530   gst_thread_release (thread);
531   return GST_STATE_FAILURE;
534 /* state changes work this way: We grab the lock and stop the thread from 
535    spinning (via gst_thread_catch) - then we change the state. After that the
536    thread may spin on. */
537 static void
538 gst_thread_child_state_change (GstBin * bin, GstElementState oldstate,
539     GstElementState newstate, GstElement * element)
541   GST_LOG_OBJECT (bin, "(from thread %s) child %s changed state from %s to %s",
542       gst_thread_get_current ()? GST_ELEMENT_NAME (gst_thread_get_current ()) :
543       "(none)", GST_ELEMENT_NAME (element),
544       gst_element_state_get_name (oldstate),
545       gst_element_state_get_name (newstate));
546   if (parent_class->child_state_change)
547     parent_class->child_state_change (bin, oldstate, newstate, element);
548   /* We'll wake up the main thread now. Note that we can't lock the thread here, 
549      because we might be called from inside gst_thread_change_state when holding
550      the lock. But this doesn't cause any problems. */
551   if (newstate == GST_STATE_PLAYING)
552     g_cond_signal (GST_THREAD (bin)->cond);
555 /**
556  * gst_thread_main_loop:
557  * @arg: the thread to start
558  *
559  * The main loop of the thread. The thread will iterate
560  * while the state is GST_THREAD_STATE_SPINNING.
561  */
562 static void *
563 gst_thread_main_loop (void *arg)
565   GstThread *thread = NULL;
566   gboolean status;
568   thread = GST_THREAD (arg);
569   g_mutex_lock (thread->lock);
570   GST_LOG_OBJECT (thread, "Started main loop");
572   /* initialize gst_thread_current */
573   g_private_set (gst_thread_current, thread);
575   /* set up the element's scheduler */
576   gst_scheduler_setup (GST_ELEMENT_SCHED (thread));
577   GST_FLAG_UNSET (thread, GST_THREAD_STATE_REAPING);
579   g_cond_signal (thread->cond);
580   while (!(GST_FLAG_IS_SET (thread, GST_THREAD_STATE_REAPING))) {
581     if (GST_STATE (thread) == GST_STATE_PLAYING) {
582       GST_FLAG_SET (thread, GST_THREAD_STATE_SPINNING);
583       status = TRUE;
584       GST_LOG_OBJECT (thread, "starting to iterate");
585       while (status && GST_FLAG_IS_SET (thread, GST_THREAD_STATE_SPINNING)) {
586         g_mutex_unlock (thread->lock);
587         status = gst_bin_iterate (GST_BIN (thread));
588         if (GST_FLAG_IS_SET (thread, GST_THREAD_MUTEX_LOCKED)) {
589           GST_FLAG_UNSET (thread, GST_THREAD_MUTEX_LOCKED);
590         } else {
591           g_mutex_lock (thread->lock);
592         }
593       }
594       GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
595     }
596     if (GST_FLAG_IS_SET (thread, GST_THREAD_STATE_REAPING))
597       break;
598     GST_LOG_OBJECT (thread, "we're caught");
599     g_cond_signal (thread->cond);
600     g_cond_wait (thread->cond, thread->lock);
601   }
603   /* we need to destroy the scheduler here because it has mapped it's
604    * stack into the threads stack space */
605   gst_scheduler_reset (GST_ELEMENT_SCHED (thread));
607   /* must do that before releasing the lock - we might get disposed before being done */
608   g_signal_emit (G_OBJECT (thread), gst_thread_signals[SHUTDOWN], 0);
610   /* unlock and signal - we are out */
612   GST_LOG_OBJECT (thread, "Thread %p exits main loop", g_thread_self ());
613   g_cond_signal (thread->cond);
614   g_mutex_unlock (thread->lock);
615   /* don't assume the GstThread object exists anymore now */
617   return NULL;
620 #ifndef GST_DISABLE_LOADSAVE
621 static xmlNodePtr
622 gst_thread_save_thyself (GstObject * object, xmlNodePtr self)
624   if (GST_OBJECT_CLASS (parent_class)->save_thyself)
625     GST_OBJECT_CLASS (parent_class)->save_thyself (object, self);
626   return NULL;
629 static void
630 gst_thread_restore_thyself (GstObject * object, xmlNodePtr self)
632   GST_LOG_OBJECT (object, "restoring");
634   if (GST_OBJECT_CLASS (parent_class)->restore_thyself)
635     GST_OBJECT_CLASS (parent_class)->restore_thyself (object, self);
637 #endif /* GST_DISABLE_LOADSAVE */