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