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