Make sure we set the reaping flag when going to NULL before we signal the parent.
[platform/upstream/gstreamer.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  *
5  * gstthread.c: Threaded container object
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 <unistd.h>
24
25 /* #define GST_DEBUG_ENABLED */
26 #include "gst_private.h"
27
28 #include "gst.h"
29 #include "gstthread.h"
30 #include "gstscheduler.h"
31 #include "gstqueue.h"
32
33 #define STACK_SIZE 0x200000
34
35 #define g_thread_equal(a,b) ((a) == (b))
36
37 GstElementDetails gst_thread_details = {
38   "Threaded container",
39   "Generic/Bin",
40   "LGPL",
41   "Container that creates/manages a thread",
42   VERSION,
43   "Erik Walthinsen <omega@cse.ogi.edu>",
44   "(C) 1999, 2000",
45 };
46
47
48 /* Thread signals and args */
49 enum {
50   SHUTDOWN,
51   /* FILL ME */
52   LAST_SIGNAL
53 };
54
55 enum {
56   SPINUP=0,
57   STATECHANGE,
58   STARTUP
59 };
60
61 enum {
62   ARG_0,
63   ARG_SCHEDPOLICY,
64   ARG_PRIORITY,
65 };
66
67
68
69 static void             gst_thread_class_init           (GstThreadClass *klass);
70 static void             gst_thread_init                 (GstThread *thread);
71
72 static void             gst_thread_dispose              (GObject *object);
73
74 static void             gst_thread_set_property         (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
75 static void             gst_thread_get_property         (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
76
77 static
78 GstElementStateReturn   gst_thread_change_state         (GstElement *element);
79
80 #ifndef GST_DISABLE_LOADSAVE
81 static xmlNodePtr       gst_thread_save_thyself         (GstObject *object,
82                                                          xmlNodePtr parent);
83 static void             gst_thread_restore_thyself      (GstObject *object,
84                                                          xmlNodePtr self);
85 #endif
86
87 static void*            gst_thread_main_loop            (void *arg);
88
89 #define GST_TYPE_THREAD_SCHEDPOLICY (gst_thread_schedpolicy_get_type())
90 static GType
91 gst_thread_schedpolicy_get_type(void) {
92   static GType thread_schedpolicy_type = 0;
93   static GEnumValue thread_schedpolicy[] = {
94     {G_THREAD_PRIORITY_LOW,    "LOW", "Low Priority Scheduling"},
95     {G_THREAD_PRIORITY_NORMAL, "NORMAL",  "Normal Scheduling"},
96     {G_THREAD_PRIORITY_HIGH,   "HIGH",  "High Priority Scheduling"},
97     {G_THREAD_PRIORITY_URGENT, "URGENT", "Urgent Scheduling"},
98     {0, NULL, NULL},
99   };
100   if (!thread_schedpolicy_type) {
101     thread_schedpolicy_type = g_enum_register_static("GstThreadSchedPolicy", thread_schedpolicy);
102   }
103   return thread_schedpolicy_type;
104 }
105
106 static GstBinClass *parent_class = NULL;
107 static guint gst_thread_signals[LAST_SIGNAL] = { 0 };
108
109 GType
110 gst_thread_get_type(void) {
111   static GType thread_type = 0;
112
113   if (!thread_type) {
114     static const GTypeInfo thread_info = {
115       sizeof (GstThreadClass), NULL, NULL,
116       (GClassInitFunc) gst_thread_class_init, NULL, NULL,
117       sizeof (GstThread),
118       4,
119       (GInstanceInitFunc) gst_thread_init,
120       NULL
121     };
122     thread_type = g_type_register_static (GST_TYPE_BIN, "GstThread",
123                                           &thread_info, 0);
124   }
125   return thread_type;
126 }
127
128 static void
129 gst_thread_class_init (GstThreadClass *klass)
130 {
131   GObjectClass *gobject_class;
132   GstObjectClass *gstobject_class;
133   GstElementClass *gstelement_class;
134   GstBinClass *gstbin_class;
135
136   gobject_class =       (GObjectClass*)klass;
137   gstobject_class =     (GstObjectClass*)klass;
138   gstelement_class =    (GstElementClass*)klass;
139   gstbin_class =        (GstBinClass*)klass;
140
141   parent_class = g_type_class_ref (GST_TYPE_BIN);
142
143   g_object_class_install_property(G_OBJECT_CLASS (klass), ARG_SCHEDPOLICY,
144     g_param_spec_enum("schedpolicy", "Scheduling Policy", "The scheduling policy of the thread",
145                       GST_TYPE_THREAD_SCHEDPOLICY, G_THREAD_PRIORITY_NORMAL, G_PARAM_READWRITE));
146   g_object_class_install_property(G_OBJECT_CLASS (klass), ARG_PRIORITY,
147     g_param_spec_int("priority", "Scheduling Priority", "The scheduling priority of the thread",
148                      0, 99, 0, G_PARAM_READWRITE));
149
150   gst_thread_signals[SHUTDOWN] =
151     g_signal_new ("shutdown", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
152                   G_STRUCT_OFFSET (GstThreadClass, shutdown), NULL, NULL,
153                   gst_marshal_VOID__VOID, G_TYPE_NONE, 0);
154
155   gobject_class->dispose =              gst_thread_dispose;
156
157 #ifndef GST_DISABLE_LOADSAVE
158   gstobject_class->save_thyself =       GST_DEBUG_FUNCPTR (gst_thread_save_thyself);
159   gstobject_class->restore_thyself =    GST_DEBUG_FUNCPTR (gst_thread_restore_thyself);
160 #endif
161
162   gstelement_class->change_state =      GST_DEBUG_FUNCPTR (gst_thread_change_state);
163
164   gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_thread_set_property);
165   gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_thread_get_property);
166
167 }
168
169 static void
170 gst_thread_init (GstThread *thread)
171 {
172   GstScheduler *scheduler;
173
174   GST_DEBUG (GST_CAT_THREAD, "initializing thread");
175
176   /* threads are managing bins and iterate themselves */
177   /* CR1: the GstBin code checks these flags */
178   GST_FLAG_SET (thread, GST_BIN_FLAG_MANAGER);
179   GST_FLAG_SET (thread, GST_BIN_SELF_SCHEDULABLE);
180
181   scheduler = gst_scheduler_factory_make (NULL, GST_ELEMENT (thread));
182
183   thread->lock = g_mutex_new ();
184   thread->cond = g_cond_new ();
185
186   thread->ppid = getpid ();
187   thread->thread_id = (GThread *) NULL; /* set in NULL -> READLY */
188   thread->sched_policy = G_THREAD_PRIORITY_NORMAL;
189   thread->priority = 0;
190   thread->stack = NULL;
191 }
192
193 static void
194 gst_thread_dispose (GObject *object)
195 {
196   GstThread *thread = GST_THREAD (object);
197
198   GST_DEBUG (GST_CAT_REFCOUNTING, "dispose");
199
200   g_mutex_lock (thread->lock);
201   if (GST_STATE (thread) != GST_STATE_NULL) {
202     GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
203   }
204   g_mutex_unlock (thread->lock);
205
206   gst_element_set_state (GST_ELEMENT (thread), GST_STATE_NULL);
207
208   g_mutex_free (thread->lock);
209   g_cond_free (thread->cond);
210
211   G_OBJECT_CLASS (parent_class)->dispose (object);
212
213   if (GST_ELEMENT_SCHED (thread)) {
214     gst_object_unref (GST_OBJECT (GST_ELEMENT_SCHED (thread)));
215   }
216 }
217
218 static void
219 gst_thread_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
220 {
221   GstThread *thread;
222
223   /* it's not null if we got it, but it might not be ours */
224   g_return_if_fail (GST_IS_THREAD (object));
225
226   thread = GST_THREAD (object);
227
228   switch (prop_id) {
229     case ARG_SCHEDPOLICY:
230       thread->sched_policy = g_value_get_enum (value);
231       break;
232     case ARG_PRIORITY:
233       thread->priority = g_value_get_int (value);
234       break;
235     default:
236       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
237       break;
238   }
239 }
240
241 static void
242 gst_thread_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
243 {
244   GstThread *thread;
245
246   /* it's not null if we got it, but it might not be ours */
247   g_return_if_fail (GST_IS_THREAD (object));
248
249   thread = GST_THREAD (object);
250
251   switch (prop_id) {
252     case ARG_SCHEDPOLICY:
253       g_value_set_enum (value, thread->sched_policy);
254       break;
255     case ARG_PRIORITY:
256       g_value_set_int (value, thread->priority);
257       break;
258     default:
259       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
260       break;
261   }
262 }
263
264
265 /**
266  * gst_thread_new:
267  * @name: the name of the thread
268  *
269  * Create a new thread with the given name.
270  *
271  * Returns: The new thread
272  */
273 GstElement*
274 gst_thread_new (const gchar *name)
275 {
276   return gst_element_factory_make ("thread", name);
277 }
278
279 /* these two macros are used for debug/info from the state_change function */
280
281 #define THR_INFO(format,args...) \
282   GST_INFO_ELEMENT(GST_CAT_THREAD, thread, "sync(" GST_DEBUG_THREAD_FORMAT "): " format , \
283   GST_DEBUG_THREAD_ARGS(thread->pid) , ## args )
284   
285 #define THR_DEBUG(format,args...) \
286   GST_DEBUG_ELEMENT(GST_CAT_THREAD, thread, "sync(" GST_DEBUG_THREAD_FORMAT "): " format , \
287   GST_DEBUG_THREAD_ARGS(thread->pid) , ## args )
288
289 /* these two macros are used for debug/info from the gst_thread_main_loop
290  * function
291  */
292
293 #define THR_INFO_MAIN(format,args...) \
294   GST_INFO_ELEMENT(GST_CAT_THREAD, thread, "sync-main(" GST_DEBUG_THREAD_FORMAT "): " format , \
295   GST_DEBUG_THREAD_ARGS(thread->ppid) , ## args )
296
297 #define THR_DEBUG_MAIN(format,args...) \
298   GST_DEBUG_ELEMENT(GST_CAT_THREAD, thread, "sync-main(" GST_DEBUG_THREAD_FORMAT "): " format , \
299   GST_DEBUG_THREAD_ARGS(thread->ppid) , ## args )
300
301 static GstElementStateReturn
302 gst_thread_update_state (GstThread *thread)
303 {
304   GST_DEBUG_ELEMENT (GST_CAT_THREAD, thread, "updating state of thread");
305   /* check for state change */
306   if (GST_STATE_PENDING (thread) != GST_STATE_VOID_PENDING) {
307     /* punt and change state on all the children */
308     if (GST_ELEMENT_CLASS (parent_class)->change_state)
309       return GST_ELEMENT_CLASS (parent_class)->change_state (GST_ELEMENT (thread));
310   }
311
312   /* FIXME: in the case of no change_state function in the parent's class,
313    * shouldn't we actually change the thread's state ? */
314   g_warning ("thread's parent doesn't have change_state, returning success");
315   return GST_STATE_SUCCESS;
316 }
317
318
319 static GstElementStateReturn
320 gst_thread_change_state (GstElement *element)
321 {
322   GstThread *thread;
323   gboolean stateset = GST_STATE_SUCCESS;
324   gint transition;
325   GThread *self = g_thread_self ();
326   GError *error = NULL;
327
328   g_return_val_if_fail (GST_IS_THREAD (element), GST_STATE_FAILURE);
329   g_return_val_if_fail (gst_has_threads (), GST_STATE_FAILURE);
330
331   thread = GST_THREAD (element);
332
333   transition = GST_STATE_TRANSITION (element);
334
335   THR_INFO ("changing state from %s to %s",
336             gst_element_state_get_name (GST_STATE (element)),
337             gst_element_state_get_name (GST_STATE_PENDING (element)));
338
339   if (g_thread_equal (self, thread->thread_id)) {
340     GST_DEBUG (GST_CAT_THREAD,
341                "no sync(" GST_DEBUG_THREAD_FORMAT "): "
342                "setting own thread's state to spinning",
343                GST_DEBUG_THREAD_ARGS (thread->pid));
344     return gst_thread_update_state (thread);
345   }
346
347   switch (transition) {
348     case GST_STATE_NULL_TO_READY:
349       /* set the state to idle */
350       GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
351
352       THR_DEBUG ("creating thread \"%s\"", GST_ELEMENT_NAME (element));
353
354       /* this bit of code handles creation of GThreads
355        * this is therefor tricky code
356        * compare it with the block of code that handles the destruction
357        * in GST_STATE_READY_TO_NULL below
358        */
359       g_mutex_lock (thread->lock);
360
361       /* create a new GThread
362        * use the specified attributes
363        * make it execute gst_thread_main_loop (thread)
364        */
365       GST_DEBUG (GST_CAT_THREAD, "going to g_thread_create_full...");
366       thread->thread_id = g_thread_create_full(gst_thread_main_loop,
367           thread, STACK_SIZE, TRUE, TRUE, G_THREAD_PRIORITY_NORMAL,
368           &error);
369
370       if (!thread->thread_id){
371         GST_DEBUG (GST_CAT_THREAD, "g_thread_create_full failed");
372         g_mutex_unlock (thread->lock);
373         GST_DEBUG (GST_CAT_THREAD, "could not create thread \"%s\"",
374                    GST_ELEMENT_NAME (element));
375
376         return GST_STATE_FAILURE;
377       }
378       GST_DEBUG (GST_CAT_THREAD, "GThread created");
379
380       /* wait for it to 'spin up' */
381       THR_DEBUG ("waiting for child thread spinup");
382       g_cond_wait (thread->cond, thread->lock);
383       THR_DEBUG ("thread claims to be up");
384       g_mutex_unlock (thread->lock);
385       break;
386     case GST_STATE_READY_TO_PAUSED:
387       THR_INFO ("readying thread");
388       g_mutex_lock (thread->lock);
389       THR_DEBUG ("signaling");
390       g_cond_signal (thread->cond);
391       THR_DEBUG ("waiting for ack");
392       g_cond_wait (thread->cond, thread->lock);
393       THR_DEBUG ("got ack");
394       g_mutex_unlock (thread->lock);
395       break;
396     case GST_STATE_PAUSED_TO_PLAYING:
397     {
398       /* fixme: recurse into sub-bins */
399       const GList *elements = gst_bin_get_list (GST_BIN (thread));
400       while (elements) {
401         gst_element_enable_threadsafe_properties ((GstElement*)elements->data);
402         elements = g_list_next (elements);
403       }
404       
405       THR_DEBUG ("telling thread to start spinning");
406       g_mutex_lock (thread->lock);
407       THR_DEBUG ("signaling");
408       g_cond_signal (thread->cond);
409       THR_DEBUG ("waiting for ack");
410       g_cond_wait (thread->cond, thread->lock);
411       THR_DEBUG ("got ack");
412       g_mutex_unlock (thread->lock);
413
414       /* we're actually doing async notification, the state could
415        * have changed already (eg. EOS) here. commented out as
416        * it needs more thinking... */
417       /* stateset = GST_STATE_ASYNC; */
418       break;
419     }
420     case GST_STATE_PLAYING_TO_PAUSED:
421     {
422       const GList *elements = (GList *) gst_bin_get_list (GST_BIN (thread));
423
424       THR_INFO ("pausing thread");
425
426       /* the following code ensures that the bottom half of thread will run
427        * to perform each elements' change_state() (by calling gstbin.c::
428        * change_state()).
429        * + the pending state was already set by gstelement.c::set_state()
430        * + unlock all elements so the bottom half can start the state change.
431        */ 
432       g_mutex_lock (thread->lock);
433
434       GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
435
436       while (elements) {
437         GstElement *element = GST_ELEMENT (elements->data);
438         GList *pads;
439         
440
441         g_assert (element);
442         THR_DEBUG ("  waking element \"%s\"", GST_ELEMENT_NAME (element));
443         elements = g_list_next (elements);
444
445         if (!gst_element_release_locks (element)) {
446           g_warning ("element %s could not release locks", GST_ELEMENT_NAME (element));
447         }
448
449         pads = GST_ELEMENT_PADS (element);
450
451         while (pads) {
452           GstRealPad *peer = NULL;
453           GstElement *peerelement;
454
455           if (GST_PAD_PEER (pads->data))
456             peer = GST_REAL_PAD (GST_PAD_PEER (pads->data));
457
458           pads = g_list_next (pads);
459
460           if (!peer)
461             continue;
462
463           peerelement = GST_PAD_PARENT (peer);
464           if (!peerelement)
465             continue;           /* deal with case where there's no peer */
466
467           if (!GST_FLAG_IS_SET (peerelement, GST_ELEMENT_DECOUPLED)) {
468             GST_DEBUG (GST_CAT_THREAD, "peer element isn't DECOUPLED");
469             continue;
470           }
471
472           if (GST_ELEMENT_SCHED (peerelement) != GST_ELEMENT_SCHED (thread)) {
473             THR_DEBUG ("  element \"%s\" has pad cross sched boundary", GST_ELEMENT_NAME (element));
474             THR_DEBUG ("  waking element \"%s\"", GST_ELEMENT_NAME (peerelement));
475             if (!gst_element_release_locks (peerelement)) {
476               g_warning ("element %s could not release locks", GST_ELEMENT_NAME (peerelement));
477             }
478           }
479         }
480
481       }
482       THR_DEBUG ("telling thread to pause, signaling");
483       g_cond_signal (thread->cond);
484       THR_DEBUG ("waiting for ack");
485       g_cond_wait (thread->cond, thread->lock);
486       THR_DEBUG ("got ack");
487       g_mutex_unlock (thread->lock);
488
489       elements = gst_bin_get_list (GST_BIN (thread));
490       while (elements) {
491         gst_element_disable_threadsafe_properties ((GstElement*)elements->data);
492         elements = g_list_next (elements);
493       }
494       break;
495     }
496     case GST_STATE_READY_TO_NULL:
497       THR_DEBUG ("telling thread to pause (null) - and joining");
498       /* MattH FIXME revisit */
499       g_mutex_lock (thread->lock);
500       THR_DEBUG ("signaling");
501       g_cond_signal (thread->cond);
502       THR_DEBUG ("waiting for ack");
503       g_cond_wait (thread->cond, thread->lock);
504       THR_DEBUG ("got ack");
505
506
507       /* this block of code is very tricky
508        * basically, we try to clean up the whole thread and
509        * everything related to it in the right order without
510        * triggering segfaults
511        * compare this block to the block
512        */
513
514       GST_DEBUG (GST_CAT_THREAD, "joining GThread %p", thread->thread_id);
515       g_thread_join (thread->thread_id);
516
517       thread->thread_id = NULL;
518       thread->stack = NULL;
519      
520       THR_DEBUG ("unlocking mutex");
521       g_mutex_unlock (thread->lock);
522
523       GST_FLAG_UNSET (thread, GST_THREAD_STATE_REAPING);
524       GST_FLAG_UNSET (thread, GST_THREAD_STATE_STARTED);
525       GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
526
527       break;
528     case GST_STATE_PAUSED_TO_READY:
529       THR_DEBUG ("telling thread to stop spinning");
530       g_mutex_lock (thread->lock);
531       THR_DEBUG ("signaling");
532       g_cond_signal (thread->cond);
533       THR_DEBUG ("waiting for ack");
534       g_cond_wait (thread->cond, thread->lock);
535       THR_DEBUG ("got ack");
536       g_mutex_unlock (thread->lock);
537
538       break;
539     default:
540       GST_DEBUG_ELEMENT (GST_CAT_THREAD, element, "UNHANDLED STATE CHANGE! %x", transition);
541       break;
542   }
543
544   return stateset;
545 }
546
547 /**
548  * gst_thread_main_loop:
549  * @arg: the thread to start
550  *
551  * The main loop of the thread. The thread will iterate
552  * while the state is GST_THREAD_STATE_SPINNING.
553  */
554 static void *
555 gst_thread_main_loop (void *arg)
556 {
557   GstThread *thread = NULL;
558   gint stateset;
559   glong page_size;
560   gpointer stack_pointer;
561   gulong stack_offset;
562
563   GST_DEBUG (GST_CAT_THREAD, "gst_thread_main_loop started");
564   thread = GST_THREAD (arg);
565   g_mutex_lock (thread->lock);
566
567   /* set up the element's scheduler */
568   gst_scheduler_setup (GST_ELEMENT_SCHED (thread));
569   GST_FLAG_UNSET (thread, GST_THREAD_STATE_REAPING);
570
571   thread->pid = getpid();
572   THR_INFO_MAIN ("thread is running");
573
574   page_size = sysconf(_SC_PAGESIZE);
575   stack_pointer = (gpointer) &stack_pointer;
576
577   if(((gulong)stack_pointer & (page_size-1)) < (page_size>>1)){
578     /* stack grows up, I think */
579     /* FIXME this is probably not true for the main thread */
580     stack_offset = (gulong)stack_pointer & (page_size - 1);
581   }else{
582     /* stack grows down, I think */
583     stack_offset = STACK_SIZE - ((gulong)stack_pointer & (page_size - 1));
584   }
585   /* note the subtlety with pointer arithmetic */
586   thread->stack = stack_pointer - stack_offset;
587   thread->stack_size = STACK_SIZE;
588
589   /* first we need to change the state of all the children */
590   if (GST_ELEMENT_CLASS (parent_class)->change_state) {
591     stateset = GST_ELEMENT_CLASS (parent_class)->change_state (GST_ELEMENT(thread));
592
593     if (stateset != GST_STATE_SUCCESS) {
594       THR_DEBUG_MAIN ("state change of children failed");
595     }
596   }
597
598   THR_DEBUG_MAIN ("indicating spinup");
599   g_cond_signal (thread->cond);
600   /* don't unlock the mutex because we hold it into the top of the while loop */
601   THR_DEBUG_MAIN ("thread has indicated spinup to parent process");
602
603   /***** THREAD IS NOW IN READY STATE *****/
604
605   /* CR1: most of this code is handshaking */
606   /* do this while the thread lives */
607   while (!GST_FLAG_IS_SET (thread, GST_THREAD_STATE_REAPING)) {
608     /* NOTE we hold the thread lock at this point */
609     /* what we do depends on what state we're in */
610     switch (GST_STATE (thread)) {
611       /* NOTE: cannot be in NULL, we're not running in that state at all */
612       case GST_STATE_READY:
613         /* wait to be set to either the NULL or PAUSED states */
614         THR_DEBUG_MAIN ("thread in %s state, waiting for either %s or %s",
615                         gst_element_state_get_name (GST_STATE_READY),
616                         gst_element_state_get_name (GST_STATE_NULL),
617                         gst_element_state_get_name (GST_STATE_PAUSED));
618         g_cond_wait (thread->cond, thread->lock);
619         
620         /* this must have happened by a state change in the thread context */
621         if (GST_STATE_PENDING (thread) != GST_STATE_NULL &&
622             GST_STATE_PENDING (thread) != GST_STATE_PAUSED) {
623           g_cond_signal (thread->cond);
624           continue;
625         }
626
627         /* been signaled, we need to state transition now and signal back */
628         gst_thread_update_state (thread);
629         /* now we decide what to do next */
630         if (GST_STATE (thread) == GST_STATE_NULL) {
631           /* REAPING must be set, we can simply break this iteration */
632           THR_DEBUG_MAIN ("set GST_THREAD_STATE_REAPING");
633           GST_FLAG_SET (thread, GST_THREAD_STATE_REAPING);
634         }
635         THR_DEBUG_MAIN ("done with state transition, "
636                         "signaling back to parent process");
637         g_cond_signal (thread->cond);
638         continue;
639
640       case GST_STATE_PAUSED:
641         /* wait to be set to either the READY or PLAYING states */
642         THR_DEBUG_MAIN ("thread in %s state, waiting for either %s or %s",
643                         gst_element_state_get_name (GST_STATE_PAUSED),
644                         gst_element_state_get_name (GST_STATE_READY),
645                         gst_element_state_get_name (GST_STATE_PLAYING));
646         g_cond_wait (thread->cond, thread->lock);
647
648         /* this must have happened by a state change in the thread context */
649         if (GST_STATE_PENDING (thread) != GST_STATE_READY &&
650             GST_STATE_PENDING (thread) != GST_STATE_PLAYING) {
651           g_cond_signal (thread->cond);
652           continue;
653         }
654
655         /* been signaled, we need to state transition now and signal back */
656         gst_thread_update_state (thread);
657
658         /* now we decide what to do next */
659         if (GST_STATE (thread) != GST_STATE_PLAYING) {
660           /* either READY or the state change failed for some reason */
661           g_cond_signal (thread->cond);
662           continue;
663         } 
664         else {
665           GST_FLAG_SET (thread, GST_THREAD_STATE_SPINNING);
666           /* PLAYING is coming up, so we can now start spinning */
667           while (GST_FLAG_IS_SET (thread, GST_THREAD_STATE_SPINNING)) {
668             gboolean status;
669
670             g_cond_signal (thread->cond);
671             g_mutex_unlock (thread->lock);
672             status = gst_bin_iterate (GST_BIN (thread));
673             g_mutex_lock (thread->lock);
674             /* g_cond_signal(thread->cond); */
675
676             if (!status || GST_STATE_PENDING (thread) != GST_STATE_VOID_PENDING)
677               GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
678           }
679           /* looks like we were stopped because of a statechange */
680           if (GST_STATE_PENDING (thread) != GST_STATE_VOID_PENDING) {
681             gst_thread_update_state (thread);
682           }
683           /* once we're here, SPINNING has stopped, we should signal that we're done */
684           THR_DEBUG_MAIN ("SPINNING stopped, signaling back to parent process");
685           g_cond_signal (thread->cond);
686           /* now we can wait for PAUSED */
687           continue;
688         }
689       case GST_STATE_PLAYING:
690         /* wait to be set to PAUSED */
691         THR_DEBUG_MAIN ("thread in %s state, waiting for %s",
692                         gst_element_state_get_name (GST_STATE_PLAYING),
693                         gst_element_state_get_name (GST_STATE_PAUSED));
694         g_cond_wait (thread->cond,thread->lock);
695
696         /* been signaled, we need to state transition now and signal back */
697         gst_thread_update_state (thread);
698         g_cond_signal (thread->cond);
699         /* now we decide what to do next */
700         /* there's only PAUSED, we we just wait for it */
701         continue;
702       case GST_STATE_NULL:
703         THR_DEBUG_MAIN ("thread in %s state, preparing to die",
704                         gst_element_state_get_name (GST_STATE_NULL));
705         GST_FLAG_SET (thread, GST_THREAD_STATE_REAPING);
706         break;
707       default:
708         g_assert_not_reached ();
709         break;
710     }
711   }
712
713   /* THREAD HAS STOPPED RUNNING */
714   
715   /* we need to destroy the scheduler here because it has mapped it's
716    * stack into the threads stack space */
717   gst_scheduler_reset (GST_ELEMENT_SCHED (thread));
718
719   /* since we don't unlock at the end of the while loop, do it here */
720   g_mutex_unlock (thread->lock);
721
722   GST_INFO (GST_CAT_THREAD, "gstthread: thread \"%s\" is stopped",
723                   GST_ELEMENT_NAME (thread));
724
725   g_signal_emit (G_OBJECT (thread), gst_thread_signals[SHUTDOWN], 0);
726
727   return NULL;
728 }
729
730 #ifndef GST_DISABLE_LOADSAVE
731 static xmlNodePtr
732 gst_thread_save_thyself (GstObject *object,
733                          xmlNodePtr self)
734 {
735   if (GST_OBJECT_CLASS (parent_class)->save_thyself)
736     GST_OBJECT_CLASS (parent_class)->save_thyself (object, self);
737   return NULL;
738 }
739
740 static void
741 gst_thread_restore_thyself (GstObject *object,
742                             xmlNodePtr self)
743 {
744   GST_DEBUG (GST_CAT_THREAD,"gstthread: restore");
745
746   if (GST_OBJECT_CLASS (parent_class)->restore_thyself)
747     GST_OBJECT_CLASS (parent_class)->restore_thyself (object, self);
748 }
749 #endif /* GST_DISABLE_LOADSAVE */