merge from EVENTS1 on 20011016
[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 "gstthread.h"
29 #include "gstscheduler.h"
30 #include "gstqueue.h"
31
32 GstElementDetails gst_thread_details = {
33   "Threaded container",
34   "Bin",
35   "Container that creates/manages a thread",
36   VERSION,
37   "Erik Walthinsen <omega@cse.ogi.edu>",
38   "(C) 1999, 2000",
39 };
40
41
42 /* Thread signals and args */
43 enum {
44   /* FILL ME */
45   LAST_SIGNAL
46 };
47
48 enum {
49   SPINUP=0,
50   STATECHANGE,
51   STARTUP
52 };
53
54 enum {
55   ARG_0,
56   ARG_CREATE_THREAD,
57 };
58
59
60
61 static void                     gst_thread_class_init           (GstThreadClass *klass);
62 static void                     gst_thread_init                 (GstThread *thread);
63
64 static void                     gst_thread_dispose      (GObject *object);
65
66 static void                     gst_thread_set_property         (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
67 static void                     gst_thread_get_property         (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
68
69 static GstElementStateReturn    gst_thread_change_state         (GstElement *element);
70
71 #ifndef GST_DISABLE_LOADSAVE
72 static xmlNodePtr               gst_thread_save_thyself         (GstObject *object, xmlNodePtr parent);
73 static void                     gst_thread_restore_thyself      (GstObject *object, xmlNodePtr self);
74 #endif
75
76 static void                     gst_thread_signal_thread        (GstThread *thread, gboolean spinning);
77
78 static void*                    gst_thread_main_loop            (void *arg);
79
80 static GstBinClass *parent_class = NULL;
81 //static guint gst_thread_signals[LAST_SIGNAL] = { 0 };
82
83 GType
84 gst_thread_get_type(void) {
85   static GType thread_type = 0;
86
87   if (!thread_type) {
88     static const GTypeInfo thread_info = {
89       sizeof(GstThreadClass),
90       NULL,
91       NULL,
92       (GClassInitFunc)gst_thread_class_init,
93       NULL,
94       NULL,
95       sizeof(GstThread),
96       4,
97       (GInstanceInitFunc)gst_thread_init,
98       NULL
99     };
100     thread_type = g_type_register_static(GST_TYPE_BIN, "GstThread", &thread_info, 0);
101   }
102   return thread_type;
103 }
104
105 static void
106 gst_thread_class_init (GstThreadClass *klass)
107 {
108   GObjectClass *gobject_class;
109   GstObjectClass *gstobject_class;
110   GstElementClass *gstelement_class;
111   GstBinClass *gstbin_class;
112
113   gobject_class =       (GObjectClass*)klass;
114   gstobject_class =     (GstObjectClass*)klass;
115   gstelement_class =    (GstElementClass*)klass;
116   gstbin_class =        (GstBinClass*)klass;
117
118   parent_class = g_type_class_ref (GST_TYPE_BIN);
119
120   g_object_class_install_property(G_OBJECT_CLASS(klass), ARG_CREATE_THREAD,
121     g_param_spec_boolean("create_thread", "Create Thread", "Whether to create a thread.",
122                          TRUE,G_PARAM_READWRITE));
123
124   gobject_class->dispose =              gst_thread_dispose;
125
126 #ifndef GST_DISABLE_LOADSAVE
127   gstobject_class->save_thyself =       GST_DEBUG_FUNCPTR (gst_thread_save_thyself);
128   gstobject_class->restore_thyself =    GST_DEBUG_FUNCPTR(gst_thread_restore_thyself);
129 #endif
130
131   gstelement_class->change_state =      GST_DEBUG_FUNCPTR (gst_thread_change_state);
132
133 //  gstbin_class->schedule = gst_thread_schedule_dummy;
134
135   gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_thread_set_property);
136   gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_thread_get_property);
137
138 }
139
140 static void
141 gst_thread_init (GstThread *thread)
142 {
143
144   GST_DEBUG (GST_CAT_THREAD,"initializing thread\n");
145
146   // we're a manager by default
147   GST_FLAG_SET (thread, GST_BIN_FLAG_MANAGER);
148
149   // default is to create a thread
150   GST_FLAG_SET (thread, GST_THREAD_CREATE);
151
152   thread->lock = g_mutex_new();
153   thread->cond = g_cond_new();
154
155   GST_ELEMENT_SCHED(thread) = gst_schedule_new(GST_ELEMENT(thread));
156   GST_DEBUG(GST_CAT_THREAD, "thread's scheduler is %p\n",GST_ELEMENT_SCHED(thread));
157
158   thread->ppid = getpid();
159
160 //  gst_element_set_manager(GST_ELEMENT(thread),GST_ELEMENT(thread));
161 }
162
163 static void
164 gst_thread_dispose (GObject *object)
165 {
166   GstThread *thread = GST_THREAD (object);
167
168   GST_DEBUG (GST_CAT_REFCOUNTING,"dispose\n");
169
170   g_mutex_free (thread->lock);
171   g_cond_free (thread->cond);
172
173   G_OBJECT_CLASS (parent_class)->dispose (object);
174
175   gst_object_destroy (GST_OBJECT (GST_ELEMENT_SCHED (thread)));
176   gst_object_unref (GST_OBJECT (GST_ELEMENT_SCHED (thread)));
177
178 }
179
180 static void
181 gst_thread_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
182 {
183   /* it's not null if we got it, but it might not be ours */
184   g_return_if_fail (GST_IS_THREAD (object));
185
186   switch (prop_id) {
187     case ARG_CREATE_THREAD:
188       if (g_value_get_boolean(value)) {
189         GST_INFO (GST_CAT_THREAD,"turning ON the creation of the thread");
190         GST_FLAG_SET (object, GST_THREAD_CREATE);
191 //        GST_DEBUG (GST_CAT_THREAD,"flags are 0x%08x\n", GST_FLAGS (object));
192       } else {
193         GST_INFO (GST_CAT_THREAD,"gstthread: turning OFF the creation of the thread");
194         GST_FLAG_UNSET (object, GST_THREAD_CREATE);
195 //        GST_DEBUG (GST_CAT_THREAD,"gstthread: flags are 0x%08x\n", GST_FLAGS (object));
196       }
197       break;
198     default:
199       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
200       break;
201   }
202 }
203
204 static void
205 gst_thread_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
206 {
207   /* it's not null if we got it, but it might not be ours */
208   g_return_if_fail (GST_IS_THREAD (object));
209
210   switch (prop_id) {
211     case ARG_CREATE_THREAD:
212       g_value_set_boolean(value, GST_FLAG_IS_SET (object, GST_THREAD_CREATE));
213       break;
214     default:
215       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
216       break;
217   }
218 }
219
220
221 /**
222  * gst_thread_new:
223  * @name: the name of the thread
224  *
225  * Create a new thread with the given name.
226  *
227  * Returns: The new thread
228  */
229 GstElement*
230 gst_thread_new (const guchar *name)
231 {
232   return gst_elementfactory_make ("thread", name);
233 }
234
235
236 #define THR_INFO(format,args...) \
237   GST_INFO_ELEMENT(GST_CAT_THREAD, thread, "sync(" GST_DEBUG_THREAD_FORMAT "): " format , \
238   GST_DEBUG_THREAD_ARGS(thread->pid) , ## args )
239 #define THR_DEBUG(format,args...) \
240   GST_DEBUG_ELEMENT(GST_CAT_THREAD, thread, "sync(" GST_DEBUG_THREAD_FORMAT "): " format , \
241   GST_DEBUG_THREAD_ARGS(thread->pid) , ## args )
242
243 #define THR_INFO_MAIN(format,args...) \
244   GST_INFO_ELEMENT(GST_CAT_THREAD, thread, "sync-main(" GST_DEBUG_THREAD_FORMAT "): " format , \
245   GST_DEBUG_THREAD_ARGS(thread->ppid) , ## args )
246 #define THR_DEBUG_MAIN(format,args...) \
247   GST_DEBUG_ELEMENT(GST_CAT_THREAD, thread, "sync-main(" GST_DEBUG_THREAD_FORMAT "): " format , \
248   GST_DEBUG_THREAD_ARGS(thread->ppid) , ## args )
249
250
251 static GstElementStateReturn
252 gst_thread_change_state (GstElement *element)
253 {
254   GstThread *thread;
255   gboolean stateset = GST_STATE_SUCCESS;
256   gint transition;
257   pthread_t self = pthread_self();
258
259   g_return_val_if_fail (GST_IS_THREAD(element), FALSE);
260 //  GST_DEBUG_ENTER("(\"%s\")",GST_ELEMENT_NAME(element));
261
262   thread = GST_THREAD (element);
263 //  GST_DEBUG (GST_CAT_THREAD, "**** THREAD %ld changing THREAD %ld ****\n",self,thread->thread_id);
264 //  GST_DEBUG (GST_CAT_THREAD, "**** current pid=%d\n",getpid());
265
266   transition = GST_STATE_TRANSITION (element);
267
268   THR_INFO("changing state from %s to %s",
269            gst_element_statename(GST_STATE (element)),
270            gst_element_statename(GST_STATE_PENDING (element)));
271
272   //GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
273
274   switch (transition) {
275     case GST_STATE_NULL_TO_READY:
276       // set the state to idle
277       GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
278       GST_FLAG_UNSET (thread, GST_THREAD_STATE_REAPING);
279
280       // create the thread if that's what we're supposed to do
281       if (GST_FLAG_IS_SET (thread, GST_THREAD_CREATE)) {
282         THR_DEBUG ("creating thread \"%s\"\n",
283                    GST_ELEMENT_NAME (element));
284
285         g_mutex_lock (thread->lock);
286
287         // create the thread
288         pthread_create (&thread->thread_id, NULL,
289                         gst_thread_main_loop, thread);
290
291         // wait for it to 'spin up'
292         THR_DEBUG("waiting for child thread spinup\n");
293         g_cond_wait(thread->cond,thread->lock);
294         THR_DEBUG("thread claims to be up\n");
295         g_mutex_unlock(thread->lock);
296       } else {
297         GST_INFO (GST_CAT_THREAD, "NOT creating thread \"%s\"",
298                 GST_ELEMENT_NAME (GST_ELEMENT (element)));
299
300         // punt and change state on all the children
301         if (GST_ELEMENT_CLASS (parent_class)->change_state)
302           stateset = GST_ELEMENT_CLASS (parent_class)->change_state (element);
303       }
304       break;
305     case GST_STATE_READY_TO_PAUSED:
306       THR_INFO("readying thread");
307
308       // check to see if the thread is somehow changing its own state.
309       // FIXME this is currently illegal, but must somehow be made legal at some point.
310       if (pthread_equal(self, thread->thread_id))
311       {
312         //FIXME this should not happen
313         g_assert(!pthread_equal(self, thread->thread_id));
314         GST_FLAG_SET(thread, GST_THREAD_STATE_SPINNING);
315         GST_DEBUG(GST_CAT_THREAD,"no sync(" GST_DEBUG_THREAD_FORMAT "): setting own thread's state to spinning\n",
316                   GST_DEBUG_THREAD_ARGS(thread->pid));
317       }
318       else
319       {
320         g_mutex_lock(thread->lock);
321         gst_thread_signal_thread(thread,FALSE);
322       }
323       break;
324     case GST_STATE_PAUSED_TO_PLAYING:
325       THR_INFO("starting thread");
326
327       // check to see if the thread is somehow changing its own state.
328       // FIXME this is currently illegal, but must somehow be made legal at some point.
329       if (pthread_equal(self, thread->thread_id))
330       {
331         //FIXME this should not happen
332         g_assert(!pthread_equal(self, thread->thread_id));
333         GST_FLAG_SET(thread, GST_THREAD_STATE_SPINNING);
334         GST_DEBUG(GST_CAT_THREAD,"no sync(" GST_DEBUG_THREAD_FORMAT "): setting own thread's state to spinning\n",
335                   GST_DEBUG_THREAD_ARGS(thread->pid));
336       }
337       else
338       {
339         THR_DEBUG("telling thread to start spinning\n");
340         g_mutex_lock(thread->lock);
341         gst_thread_signal_thread(thread,TRUE);
342       }
343       break;
344     case GST_STATE_PLAYING_TO_PAUSED:
345       THR_INFO("pausing thread");
346
347       // check to see if the thread is somehow changing its own state.
348       // FIXME this is currently illegal, but must somehow be made legal at some point.
349       if (pthread_equal(self, thread->thread_id))
350       {
351         //FIXME this should not happen
352         GST_DEBUG(GST_CAT_THREAD,"no sync(" GST_DEBUG_THREAD_FORMAT "): setting own thread's state to paused\n",
353                   GST_DEBUG_THREAD_ARGS(thread->pid));
354         GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
355         g_assert(!pthread_equal(self, thread->thread_id));
356       }
357       else
358       {
359         GList *elements = (element->sched)->elements;
360
361         // the following code ensures that the bottom half of thread will run
362         // to perform each elements' change_state() (by calling gstbin.c::
363         // change_state()).
364         // + the pending state was already set by gstelement.c::set_state()
365         // + find every queue we manage, and signal its empty and full conditions
366
367         g_mutex_lock(thread->lock);
368
369         GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
370
371         while (elements)
372         {
373           GstElement *e = GST_ELEMENT(elements->data);
374           g_assert(e);
375           THR_DEBUG("  element \"%s\"\n",GST_ELEMENT_NAME(e));
376           elements = g_list_next(elements);
377           if (GST_IS_QUEUE(e))
378           {
379             //FIXME make this more efficient by only waking queues that are asleep
380             //FIXME and only waking the appropriate condition (depending on if it's
381             //FIXME on up- or down-stream side)
382             //
383             //FIXME also make this more efficient by keeping list of managed queues
384             THR_DEBUG("waking queue \"%s\"\n",GST_ELEMENT_NAME(e));
385             GST_LOCK(e);
386             g_cond_signal((GST_QUEUE(e)->emptycond));
387             g_cond_signal((GST_QUEUE(e)->fullcond));
388             GST_UNLOCK(e);
389           }
390           else
391           {
392             GList *pads = GST_ELEMENT_PADS(e);
393             while (pads)
394             {
395               GstRealPad *peer;
396               GstElement *peerelement;
397               GstPad *p = GST_PAD(pads->data);
398               pads = g_list_next(pads);
399
400               peer = GST_PAD_PEER(p);
401               if (!peer) continue;
402
403               peerelement = GST_PAD_PARENT(peer);
404               if (!peerelement) continue;               // deal with case where there's no peer
405
406               if (!GST_FLAG_IS_SET(peerelement,GST_ELEMENT_DECOUPLED)) {
407                 GST_DEBUG(GST_CAT_THREAD,"peer element isn't DECOUPLED\n");
408                 continue;
409               }
410
411               // FIXME this needs to go away eventually
412               if (!GST_IS_QUEUE(peerelement)) {
413                 GST_DEBUG(GST_CAT_THREAD,"peer element isn't a Queue\n");
414                 continue;
415               }
416
417               if (GST_ELEMENT_SCHED(peerelement) != GST_ELEMENT_SCHED(thread))
418               {
419                 THR_DEBUG("  element \"%s\" has pad cross sched boundary\n",GST_ELEMENT_NAME(e));
420                 GST_LOCK(peerelement);
421                 g_cond_signal(GST_QUEUE(peerelement)->emptycond);
422                 g_cond_signal(GST_QUEUE(peerelement)->fullcond);
423                 GST_UNLOCK(peerelement);
424               }
425             }
426           }
427         }
428         THR_DEBUG("waiting for thread to stop spinning\n");
429         g_cond_wait (thread->cond, thread->lock);
430         THR_DEBUG("telling thread to pause\n");
431         gst_thread_signal_thread(thread,FALSE);
432       }
433       break;
434     case GST_STATE_READY_TO_NULL:
435       THR_INFO("stopping thread");
436
437       GST_FLAG_SET (thread, GST_THREAD_STATE_REAPING);
438
439       // check to see if the thread is somehow changing its own state.
440       // FIXME this is currently illegal, but must somehow be made legal at some point.
441       if (pthread_equal(self, thread->thread_id))
442       {
443         //FIXME this should not happen
444         g_assert(!pthread_equal(self, thread->thread_id));
445         THR_DEBUG("setting own thread's state to NULL (paused)\n");
446         GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
447       }
448       else
449       {
450         THR_DEBUG("telling thread to pause (null) - and joining\n");
451         //MattH FIXME revisit
452         g_mutex_lock(thread->lock);
453         gst_thread_signal_thread(thread,FALSE);
454         pthread_join(thread->thread_id,NULL);
455       }
456
457       GST_FLAG_UNSET(thread,GST_THREAD_STATE_REAPING);
458       GST_FLAG_UNSET(thread,GST_THREAD_STATE_STARTED);
459       GST_FLAG_UNSET(thread,GST_THREAD_STATE_SPINNING);
460       GST_FLAG_UNSET(thread,GST_THREAD_STATE_ELEMENT_CHANGED);
461
462       break;
463     case GST_STATE_PAUSED_TO_READY:
464       THR_INFO("stopping thread");
465
466       // check to see if the thread is somehow changing its own state.
467       // FIXME this is currently illegal, but must somehow be made legal at some point.
468       if (pthread_equal(self, thread->thread_id))
469       {
470         //FIXME this should not happen
471         g_assert(!pthread_equal(self, thread->thread_id));
472         GST_FLAG_SET(thread, GST_THREAD_STATE_SPINNING);
473         GST_DEBUG(GST_CAT_THREAD,"no sync(" GST_DEBUG_THREAD_FORMAT "): setting own thread's state to spinning\n",
474                   GST_DEBUG_THREAD_ARGS(thread->pid));
475       }
476       else
477       {
478         THR_DEBUG("telling thread to stop spinning\n");
479         g_mutex_lock(thread->lock);
480         gst_thread_signal_thread(thread,FALSE);
481       }
482       
483       break;
484     default:
485       GST_DEBUG_ELEMENT(GST_CAT_THREAD, element, "UNHANDLED STATE CHANGE! %x\n",transition);
486       break;
487   }
488
489   return stateset;
490 }
491
492 static void gst_thread_update_state (GstThread *thread)
493 {
494   // check for state change
495   if (GST_STATE_PENDING(thread) != GST_STATE_VOID_PENDING) {
496     // punt and change state on all the children
497     if (GST_ELEMENT_CLASS (parent_class)->change_state)
498       GST_ELEMENT_CLASS (parent_class)->change_state (GST_ELEMENT(thread));
499   }
500 }
501
502 /**
503  * gst_thread_main_loop:
504  * @arg: the thread to start
505  *
506  * The main loop of the thread. The thread will iterate
507  * while the state is GST_THREAD_STATE_SPINNING
508  */
509 static void *
510 gst_thread_main_loop (void *arg)
511 {
512   GstThread *thread = GST_THREAD (arg);
513   gint stateset;
514
515   thread->pid = getpid();
516   THR_INFO_MAIN("thread is running");
517
518   // first we need to change the state of all the children
519   if (GST_ELEMENT_CLASS (parent_class)->change_state)
520     stateset = GST_ELEMENT_CLASS (parent_class)->change_state (GST_ELEMENT(thread));
521
522   // construct the plan and signal back
523 /* DEPRACATED for INCSCHED1
524   THR_DEBUG_MAIN("creating plan for thread\n");
525   if (GST_BIN_CLASS (parent_class)->schedule)
526     GST_BIN_CLASS (parent_class)->schedule (GST_BIN (thread));
527 */
528
529 //  THR_DEBUG_MAIN("indicating spinup\n");
530   g_mutex_lock (thread->lock);
531   g_cond_signal (thread->cond);
532   // don't unlock the mutex because we hold it into the top of the while loop
533   THR_DEBUG_MAIN("thread has indicated spinup to parent process\n");
534
535   /***** THREAD IS NOW IN READY STATE *****/
536
537   while (!GST_FLAG_IS_SET (thread, GST_THREAD_STATE_REAPING)) {
538     // NOTE we hold the thread lock at this point
539     // what we do depends on what state we're in
540     switch (GST_STATE(thread)) {
541       // NOTE: cannot be in NULL, we're not running in that state at all
542       case GST_STATE_READY:
543         // wait to be set to either the NULL or PAUSED states
544         THR_DEBUG_MAIN("thread in %s state, waiting for either %s or %s\n",
545                        gst_element_statename(GST_STATE_READY),
546                        gst_element_statename(GST_STATE_NULL),
547                        gst_element_statename(GST_STATE_PAUSED));
548         g_cond_wait(thread->cond,thread->lock);
549         // been signaled, we need to state transition now and signal back
550         gst_thread_update_state(thread);
551         THR_DEBUG_MAIN("done with state transition, signaling back to parent process\n");
552         g_cond_signal(thread->cond);
553 //        g_mutex_unlock(thread->lock);
554         // now we decide what to do next (FIXME can be collapsed to a continue)
555         if (GST_STATE(thread) == GST_STATE_NULL) {
556           // REAPING must be set, we can simply break this iteration
557           continue;
558         } else {
559           // PAUSED is the next state, we can wait for that next
560           continue;
561         }
562         break;
563       case GST_STATE_PAUSED:
564         // wait to be set to either the READY or PLAYING states
565         THR_DEBUG_MAIN("thread in %s state, waiting for either %s or %s\n",
566                        gst_element_statename(GST_STATE_PAUSED),
567                        gst_element_statename(GST_STATE_READY),
568                        gst_element_statename(GST_STATE_PLAYING));
569         g_cond_wait(thread->cond,thread->lock);
570         // been signaled, we need to state transition now and signal back
571         gst_thread_update_state(thread);
572         g_cond_signal(thread->cond);
573 //        g_mutex_unlock(thread->lock);
574         // now we decide what to do next
575         if (GST_STATE(thread) == GST_STATE_READY) {
576           // READY is the next state, we can wait for that next
577           continue;
578         } else {
579           g_mutex_unlock(thread->lock);
580           // PLAYING is coming up, so we can now start spinning
581           while (GST_FLAG_IS_SET (thread, GST_THREAD_STATE_SPINNING)) {
582             if (!gst_bin_iterate (GST_BIN (thread))) {
583 //              GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
584 //              THR_DEBUG_MAIN("removed spinning state due to failed iteration!\n");
585               // FIXME FIXME FIXME this is ugly!
586               THR_DEBUG_MAIN("iteration failed, something very wrong, spinning to let parent sync\n");
587               while (GST_FLAG_IS_SET(thread, GST_THREAD_STATE_SPINNING)) ;
588             }
589           }
590           g_mutex_lock(thread->lock);
591           // once we're here, SPINNING has stopped, we should signal that we're done
592           THR_DEBUG_MAIN("SPINNING stopped, signaling back to parent process\n");
593           g_cond_signal (thread->cond);
594           // now we can wait for PAUSED
595           continue;
596         }
597         break;
598       case GST_STATE_PLAYING:
599         // wait to be set to PAUSED
600         THR_DEBUG_MAIN("thread in %s state, waiting for %s\n",
601                        gst_element_statename(GST_STATE_PLAYING),
602                        gst_element_statename(GST_STATE_PAUSED));
603         g_cond_wait(thread->cond,thread->lock);
604         // been signaled, we need to state transition now and signal back
605         gst_thread_update_state(thread);
606         g_cond_signal(thread->cond);
607 //        g_mutex_unlock(thread->lock);
608         // now we decide what to do next
609         // there's only PAUSED, we we just wait for it
610         continue;
611         break;
612     }
613
614     // need to grab the lock so we're ready for the top of the loop
615 //    g_mutex_lock(thread->lock);
616   }
617
618 /*
619   while (!GST_FLAG_IS_SET (thread, GST_THREAD_STATE_REAPING)) {
620     // start out by waiting for a state change into spinning
621     THR_DEBUG_MAIN("waiting for signal from parent process (at top of while())\n");
622     g_cond_wait (thread->cond,thread->lock);
623     THR_DEBUG_MAIN("woken up with %s pending\n",gst_element_statename(GST_STATE(thread)));
624     // now is a good time to change the state of the children and the thread itself
625     gst_thread_update_state (thread);
626     THR_DEBUG_MAIN("done changing state, signaling back\n");
627     g_cond_signal (thread->cond);
628     g_mutex_unlock (thread->lock);
629     THR_DEBUG_MAIN("finished sycnronizing with main process\n");
630
631     while (GST_FLAG_IS_SET (thread, GST_THREAD_STATE_SPINNING)) {
632       if (!gst_bin_iterate (GST_BIN (thread))) {
633         GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
634         THR_DEBUG_MAIN("removed spinning state due to failed iteration!\n");
635       }
636     }
637
638     g_mutex_lock (thread->lock);
639
640     if (GST_STATE_PENDING(thread) == GST_STATE_PAUSED) {
641       // we've stopped spinning, because of PLAYING->PAUSED
642       THR_DEBUG_MAIN("SPINNING flag unset, signaling parent process we're stopped\n");
643       // we need to signal back that we've stopped spinning
644       g_cond_signal (thread->cond);
645     }
646
647 //    THR_DEBUG_MAIN("signaling that the thread is out of the SPINNING loop\n");
648 //    g_cond_signal (thread->cond);
649 //    g_cond_wait (thread->cond, thread->lock);
650 //    THR_DEBUG_MAIN("parent process has signaled at bottom of while\n");
651 //    // now change the children's and thread's state
652 //    gst_thread_update_state (thread);
653 //    THR_DEBUG_MAIN("done changing state, signaling back to parent process\n");
654 //    g_cond_signal (thread->cond);
655 //    // don't release the mutex, we hold that into the top of the loop
656 //    THR_DEBUG_MAIN("done syncing with parent process at bottom of while\n");
657   }
658 */
659
660   // since we don't unlock at the end of the while loop, do it here
661   g_mutex_unlock (thread->lock);
662
663   GST_INFO (GST_CAT_THREAD, "gstthread: thread \"%s\" is stopped",
664                   GST_ELEMENT_NAME (thread));
665   return NULL;
666 }
667
668 // the set flag is to say whether it should set TRUE or FALSE
669 //
670 // WARNING: this has synchronization built in!  if you remove or add any
671 // locks, waits, signals, or unlocks you need to be sure they match the 
672 // code above (in gst_thread_main_loop()).  basically, don't change anything.
673 static void
674 gst_thread_signal_thread (GstThread *thread, gboolean spinning)
675 {
676   // set the spinning state
677   if (spinning) GST_FLAG_SET(thread,GST_THREAD_STATE_SPINNING);
678   else GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
679
680   THR_DEBUG("thread locked\n");
681 //  g_mutex_lock(thread->lock);
682
683 //  if (!spinning) {
684 //    THR_DEBUG("waiting for spindown\n");
685 //    g_cond_wait (thread->cond, thread->lock);
686 //  }
687   THR_DEBUG("signaling\n");
688   g_cond_signal (thread->cond);
689   THR_DEBUG("waiting for ack\n");
690   g_cond_wait (thread->cond,thread->lock);
691   THR_DEBUG("got ack\n");
692
693   THR_DEBUG("unlocking\n");
694   g_mutex_unlock(thread->lock);
695   THR_DEBUG("unlocked\n");
696 }
697
698
699 #ifndef GST_DISABLE_LOADSAVE
700 static xmlNodePtr
701 gst_thread_save_thyself (GstObject *object,
702                          xmlNodePtr self)
703 {
704   if (GST_OBJECT_CLASS (parent_class)->save_thyself)
705     GST_OBJECT_CLASS (parent_class)->save_thyself (object, self);
706   return NULL;
707 }
708
709 static void
710 gst_thread_restore_thyself (GstObject *object,
711                             xmlNodePtr self)
712 {
713   GST_DEBUG (GST_CAT_THREAD,"gstthread: restore\n");
714
715   if (GST_OBJECT_CLASS (parent_class)->restore_thyself)
716     GST_OBJECT_CLASS (parent_class)->restore_thyself (object, self);
717 }
718 #endif // GST_DISABLE_LOADSAVE