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