gstthread.c 24.9 KB
Newer Older
1
2
3
4
5
/* GStreamer
 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
 *                    2000 Wim Taymans <wtay@chello.be>
 *
 * gstthread.c: Threaded container object
Erik Walthinsen's avatar
Erik Walthinsen committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

23
#include <unistd.h>
Erik Walthinsen's avatar
Erik Walthinsen committed
24

25
/* #define GST_DEBUG_ENABLED */
26
27
#include "gst_private.h"

28
#include "gst.h"
29
#include "gstthread.h"
30
31
#include "gstscheduler.h"
#include "gstqueue.h"
32

Erik Walthinsen's avatar
Erik Walthinsen committed
33
34
GstElementDetails gst_thread_details = {
  "Threaded container",
35
  "Generic/Bin",
Erik Walthinsen's avatar
Erik Walthinsen committed
36
37
38
  "Container that creates/manages a thread",
  VERSION,
  "Erik Walthinsen <omega@cse.ogi.edu>",
39
  "(C) 1999, 2000",
Erik Walthinsen's avatar
Erik Walthinsen committed
40
41
42
43
44
};


/* Thread signals and args */
enum {
45
  SHUTDOWN,
Erik Walthinsen's avatar
Erik Walthinsen committed
46
47
48
49
  /* FILL ME */
  LAST_SIGNAL
};

50
51
52
53
54
55
enum {
  SPINUP=0,
  STATECHANGE,
  STARTUP
};

Erik Walthinsen's avatar
Erik Walthinsen committed
56
57
enum {
  ARG_0,
58
59
  ARG_SCHEDPOLICY,
  ARG_PRIORITY,
Erik Walthinsen's avatar
Erik Walthinsen committed
60
61
62
};


63

64
65
static void			gst_thread_class_init		(GstThreadClass *klass);
static void			gst_thread_init			(GstThread *thread);
Erik Walthinsen's avatar
Erik Walthinsen committed
66

67
static void 			gst_thread_dispose 	(GObject *object);
68

69
70
static void			gst_thread_set_property		(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
static void			gst_thread_get_property		(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
Erik Walthinsen's avatar
Erik Walthinsen committed
71

72
static GstElementStateReturn	gst_thread_change_state		(GstElement *element);
Erik Walthinsen's avatar
Erik Walthinsen committed
73

74
#ifndef GST_DISABLE_LOADSAVE
75
76
static xmlNodePtr		gst_thread_save_thyself		(GstObject *object, xmlNodePtr parent);
static void			gst_thread_restore_thyself	(GstObject *object, xmlNodePtr self);
77
#endif
Erik Walthinsen's avatar
Erik Walthinsen committed
78

79
static void*			gst_thread_main_loop		(void *arg);
Erik Walthinsen's avatar
Erik Walthinsen committed
80

81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#define GST_TYPE_THREAD_SCHEDPOLICY (gst_thread_schedpolicy_get_type())
static GType
gst_thread_schedpolicy_get_type(void) {
  static GType thread_schedpolicy_type = 0;
  static GEnumValue thread_schedpolicy[] = {
    {SCHED_OTHER, "SCHED_OTHER", "Normal Scheduling"},
    {SCHED_FIFO,  "SCHED_FIFO",  "FIFO Scheduling (requires root)"},
    {SCHED_RR,    "SCHED_RR",    "Round-Robin Scheduling (requires root)"},
    {0, NULL, NULL},
  };
  if (!thread_schedpolicy_type) {
    thread_schedpolicy_type = g_enum_register_static("GstThreadSchedPolicy", thread_schedpolicy);
  }
  return thread_schedpolicy_type;
}

97
static GstBinClass *parent_class = NULL;
98
static guint gst_thread_signals[LAST_SIGNAL] = { 0 }; 
Erik Walthinsen's avatar
Erik Walthinsen committed
99

100
GType
Erik Walthinsen's avatar
Erik Walthinsen committed
101
gst_thread_get_type(void) {
102
  static GType thread_type = 0;
Erik Walthinsen's avatar
Erik Walthinsen committed
103
104

  if (!thread_type) {
105
    static const GTypeInfo thread_info = {
Erik Walthinsen's avatar
Erik Walthinsen committed
106
      sizeof(GstThreadClass),
107
108
109
110
111
112
113
114
      NULL,
      NULL,
      (GClassInitFunc)gst_thread_class_init,
      NULL,
      NULL,
      sizeof(GstThread),
      4,
      (GInstanceInitFunc)gst_thread_init,
115
      NULL
Erik Walthinsen's avatar
Erik Walthinsen committed
116
    };
117
    thread_type = g_type_register_static(GST_TYPE_BIN, "GstThread", &thread_info, 0);
Erik Walthinsen's avatar
Erik Walthinsen committed
118
119
120
121
122
  }
  return thread_type;
}

static void
123
gst_thread_class_init (GstThreadClass *klass)
124
{
125
  GObjectClass *gobject_class;
Erik Walthinsen's avatar
Erik Walthinsen committed
126
127
  GstObjectClass *gstobject_class;
  GstElementClass *gstelement_class;
128
  GstBinClass *gstbin_class;
Erik Walthinsen's avatar
Erik Walthinsen committed
129

130
  gobject_class =	(GObjectClass*)klass;
131
132
133
  gstobject_class =	(GstObjectClass*)klass;
  gstelement_class =	(GstElementClass*)klass;
  gstbin_class =	(GstBinClass*)klass;
Erik Walthinsen's avatar
Erik Walthinsen committed
134

135
  parent_class = g_type_class_ref (GST_TYPE_BIN);
Erik Walthinsen's avatar
Erik Walthinsen committed
136

137
138
139
140
141
142
143
144
145
146
147
148
  g_object_class_install_property(G_OBJECT_CLASS (klass), ARG_SCHEDPOLICY,
    g_param_spec_enum("schedpolicy", "Scheduling Policy", "The scheduling policy of the thread",
                      GST_TYPE_THREAD_SCHEDPOLICY, SCHED_OTHER, G_PARAM_READWRITE));
  g_object_class_install_property(G_OBJECT_CLASS (klass), ARG_PRIORITY,
    g_param_spec_int("priority", "Scheduling Priority", "The scheduling priority of the thread",
                     0, 99, 0, G_PARAM_READWRITE));

  gst_thread_signals[SHUTDOWN] =
    g_signal_new ("shutdown", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GstThreadClass, shutdown), NULL, NULL,
                  gst_marshal_VOID__VOID, G_TYPE_NONE, 0);

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);
Erik Walthinsen's avatar
Erik Walthinsen committed
157

158
159
  gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_thread_set_property);
  gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_thread_get_property);
Wim Taymans's avatar
Wim Taymans committed
160

Erik Walthinsen's avatar
Erik Walthinsen committed
161
162
}

163
164
static void
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 */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
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
178
179
180

  thread->lock = g_mutex_new ();
  thread->cond = g_cond_new ();

  thread->ppid = getpid ();
181
  thread->thread_id = (pthread_t) -1;
182
183
  thread->sched_policy = SCHED_OTHER;
  thread->priority = 0;
184
  thread->stack = NULL;
Erik Walthinsen's avatar
Erik Walthinsen committed
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
195

  g_mutex_free (thread->lock);
  g_cond_free (thread->cond);
196

197
  G_OBJECT_CLASS (parent_class)->dispose (object);
198

199
200
201
  if (GST_ELEMENT_SCHED (thread)) {
    gst_object_unref (GST_OBJECT (GST_ELEMENT_SCHED (thread)));
  }
202
203
204
}

static void
205
gst_thread_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
206
{
207
208
  GstThread *thread;

Erik Walthinsen's avatar
Erik Walthinsen committed
209
  /* it's not null if we got it, but it might not be ours */
210
  g_return_if_fail (GST_IS_THREAD (object));
Erik Walthinsen's avatar
Erik Walthinsen committed
211

212
213
  thread = GST_THREAD (object);

214
  switch (prop_id) {
215
216
217
218
219
220
    case ARG_SCHEDPOLICY:
      thread->sched_policy = g_value_get_enum (value);
      break;
    case ARG_PRIORITY:
      thread->priority = g_value_get_int (value);
      break;
Erik Walthinsen's avatar
Erik Walthinsen committed
221
    default:
222
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Erik Walthinsen's avatar
Erik Walthinsen committed
223
224
225
226
      break;
  }
}

227
static void
228
gst_thread_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
229
{
230
231
  GstThread *thread;

Erik Walthinsen's avatar
Erik Walthinsen committed
232
  /* it's not null if we got it, but it might not be ours */
233
  g_return_if_fail (GST_IS_THREAD (object));
Erik Walthinsen's avatar
Erik Walthinsen committed
234

235
236
  thread = GST_THREAD (object);

237
  switch (prop_id) {
238
239
240
241
242
243
    case ARG_SCHEDPOLICY:
      g_value_set_enum (value, thread->sched_policy);
      break;
    case ARG_PRIORITY:
      g_value_set_int (value, thread->priority);
      break;
Erik Walthinsen's avatar
Erik Walthinsen committed
244
    default:
245
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Erik Walthinsen's avatar
Erik Walthinsen committed
246
247
248
249
      break;
  }
}

250

251
252
253
254
/**
 * gst_thread_new:
 * @name: the name of the thread
 *
Erik Walthinsen's avatar
Erik Walthinsen committed
255
 * Create a new thread with the given name.
256
 *
Wim Taymans's avatar
Wim Taymans committed
257
 * Returns: The new thread
258
 */
259
GstElement*
260
gst_thread_new (const gchar *name)
261
{
262
  return gst_element_factory_make ("thread", name);
Erik Walthinsen's avatar
Erik Walthinsen committed
263
264
}

265
/* these two macros are used for debug/info from the state_change function */
Erik Walthinsen's avatar
Erik Walthinsen committed
266

267
268
269
#define THR_INFO(format,args...) \
  GST_INFO_ELEMENT(GST_CAT_THREAD, thread, "sync(" GST_DEBUG_THREAD_FORMAT "): " format , \
  GST_DEBUG_THREAD_ARGS(thread->pid) , ## args )
270
  
271
272
273
274
#define THR_DEBUG(format,args...) \
  GST_DEBUG_ELEMENT(GST_CAT_THREAD, thread, "sync(" GST_DEBUG_THREAD_FORMAT "): " format , \
  GST_DEBUG_THREAD_ARGS(thread->pid) , ## args )

275
276
277
278
/* these two macros are used for debug/info from the gst_thread_main_loop
 * function
 */

279
280
281
#define THR_INFO_MAIN(format,args...) \
  GST_INFO_ELEMENT(GST_CAT_THREAD, thread, "sync-main(" GST_DEBUG_THREAD_FORMAT "): " format , \
  GST_DEBUG_THREAD_ARGS(thread->ppid) , ## args )
282

283
284
285
286
#define THR_DEBUG_MAIN(format,args...) \
  GST_DEBUG_ELEMENT(GST_CAT_THREAD, thread, "sync-main(" GST_DEBUG_THREAD_FORMAT "): " format , \
  GST_DEBUG_THREAD_ARGS(thread->ppid) , ## args )

287
288
289
static GstElementStateReturn 
gst_thread_update_state (GstThread *thread)
{
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
299
300
  /* FIXME: in the case of no change_state function in the parent's class,
   * shouldn't we actually change the thread's state ? */
  g_warning ("thread's parent doesn't have change_state, returning success");
301
302
303
  return GST_STATE_SUCCESS;
}

Erik Walthinsen's avatar
Erik Walthinsen committed
304

305
static GstElementStateReturn
306
gst_thread_change_state (GstElement * element)
307
{
Erik Walthinsen's avatar
Erik Walthinsen committed
308
  GstThread *thread;
Wim Taymans's avatar
Wim Taymans committed
309
  gboolean stateset = GST_STATE_SUCCESS;
310
  gint transition;
311
  pthread_t self = pthread_self ();
312
  glong stacksize;
Erik Walthinsen's avatar
Erik Walthinsen committed
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);
Erik Walthinsen's avatar
Erik Walthinsen committed
318

319
  transition = GST_STATE_TRANSITION (element);
320

321
  THR_INFO ("changing state from %s to %s",
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
322
323
	    gst_element_state_get_name (GST_STATE (element)),
	    gst_element_state_get_name (GST_STATE_PENDING (element)));
324

325
326
  if (pthread_equal (self, thread->thread_id)) {
    GST_DEBUG (GST_CAT_THREAD,
327
	       "no sync(" GST_DEBUG_THREAD_FORMAT "): setting own thread's state to spinning",
328
329
330
	       GST_DEBUG_THREAD_ARGS (thread->pid));
    return gst_thread_update_state (thread);
  }
331

332
333
  switch (transition) {
    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
340
341
342
343
      /* this bit of code handles creation of pthreads
       * this is therefor tricky code
       * compare it with the block of code that handles the destruction
       * in GST_STATE_READY_TO_NULL below
       */
344
345
      g_mutex_lock (thread->lock);

346
347
348
349
350
351
352
353
354
355
      /* create attribute struct for pthread
       * and assign stack pointer and size to it
       *
       * the default state of a pthread is PTHREAD_CREATE_JOINABLE
       * (see man pthread_attr_init)
       * - other thread can sync on termination
       * - thread resources are kept allocated until other thread performs
       *   pthread_join
       */

356
357
      if (pthread_attr_init (&thread->attr) != 0)
	g_warning ("pthread_attr_init returned an error !");
358

359
360
361
362
363
      /* this function should return a newly allocated stack
       * (using whatever method)
       * which we can initiate the pthreads with
       * the stack should be freed in 
       */
364
365
366
      if (gst_scheduler_get_preferred_stack (GST_ELEMENT_SCHED (element), 
	                                     &thread->stack, &stacksize)) {
#ifdef HAVE_PTHREAD_ATTR_SETSTACK
367
368
        if (pthread_attr_setstack (&thread->attr, 
	                           thread->stack, stacksize) != 0) {
369
          g_warning ("pthread_attr_setstack failed\n");
370
371
          return GST_STATE_FAILURE;
        }
372
373
374
375
376
377
378
379
380
381
#else
        if (pthread_attr_setstackaddr (&thread->attr, thread->stack) != 0) {
	  g_warning ("pthread_attr_setstackaddr failed\n");
	  return GST_STATE_FAILURE;
	}
        if (pthread_attr_setstacksize (&thread->attr, stacksize) != 0) {
	  g_warning ("pthread_attr_setstacksize failed\n");
	  return GST_STATE_FAILURE;
	}
#endif
382
383
	GST_DEBUG (GST_CAT_THREAD, "pthread attr set stack at %p of size %ld", 
		   thread->stack, stacksize);
384
      }
385
386
387
      else {
	g_warning ("scheduler did not return a preferred stack");
      }
388

389
390
391
392
393
394
395
396
      /* create a new pthread
       * use the specified attributes
       * make it execute gst_thread_main_loop (thread)
       */
      GST_DEBUG (GST_CAT_THREAD, "going to pthread_create...");
      if (pthread_create (&thread->thread_id, &thread->attr, 
	  gst_thread_main_loop, thread) != 0) {
        GST_DEBUG (GST_CAT_THREAD, "pthread_create failed");
397
	g_mutex_unlock (thread->lock);
398
399
        GST_DEBUG (GST_CAT_THREAD, "could not create thread \"%s\"", 
	           GST_ELEMENT_NAME (element));
400
401
	return GST_STATE_FAILURE;
      }
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
410
      break;
    case GST_STATE_READY_TO_PAUSED:
411
412
      THR_INFO ("readying thread");
      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);
Erik Walthinsen's avatar
Erik Walthinsen committed
419
      break;
420
    case GST_STATE_PAUSED_TO_PLAYING:
421
422
423
424
425
426
427
428
    {
      /* fixme: recurse into sub-bins */
      const GList *elements = gst_bin_get_list (GST_BIN (thread));
      while (elements) {
        gst_element_enable_threadsafe_properties ((GstElement*)elements->data);
        elements = g_list_next (elements);
      }
      
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
444

      THR_INFO ("pausing thread");

445
446
447
448
      /* the following code ensures that the bottom half of thread will run
       * to perform each elements' change_state() (by calling gstbin.c::
       * change_state()).
       * + 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
454
455

      GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);

      while (elements) {
Wim Taymans's avatar
Wim Taymans committed
456
	GstElement *element = GST_ELEMENT (elements->data);
457
458
	GList *pads;
	
459

Wim Taymans's avatar
Wim Taymans committed
460
	g_assert (element);
461
	THR_DEBUG ("  waking element \"%s\"", GST_ELEMENT_NAME (element));
462
	elements = g_list_next (elements);
463
464
465

	if (!gst_element_release_locks (element)) {
          g_warning ("element %s could not release locks", GST_ELEMENT_NAME (element));
466
467
	}

468
	pads = GST_ELEMENT_PADS (element);
469

470
471
472
	while (pads) {
	  GstRealPad *peer = GST_REAL_PAD (GST_PAD_PEER (pads->data));
	  GstElement *peerelement;
473

474
	  pads = g_list_next (pads);
475

476
477
	  if (!peer)
	    continue;
478

479
480
481
	  peerelement = GST_PAD_PARENT (peer);
	  if (!peerelement)
	    continue;		/* deal with case where there's no peer */
482

483
484
485
486
	  if (!GST_FLAG_IS_SET (peerelement, GST_ELEMENT_DECOUPLED)) {
	    GST_DEBUG (GST_CAT_THREAD, "peer element isn't DECOUPLED");
	    continue;
	  }
487

488
489
	  if (GST_ELEMENT_SCHED (peerelement) != GST_ELEMENT_SCHED (thread)) {
	    THR_DEBUG ("  element \"%s\" has pad cross sched boundary", GST_ELEMENT_NAME (element));
490
491
492
	    THR_DEBUG ("  waking element \"%s\"", GST_ELEMENT_NAME (peerelement));
	    if (!gst_element_release_locks (peerelement)) {
              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
506
507
508
509

      elements = gst_bin_get_list (GST_BIN (thread));
      while (elements) {
        gst_element_disable_threadsafe_properties ((GstElement*)elements->data);
        elements = g_list_next (elements);
      }
Erik Walthinsen's avatar
Erik Walthinsen committed
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
523
524
525
526
527
528
529
530
531
532
533
534
535

      /* this block of code is very tricky
       * basically, we try to clean up the whole thread and
       * everything related to it in the right order without
       * triggering segfaults
       * compare this block to the block
       */

      
      /* in glibc 2.2.5, pthread_attr_destroy does nothing more
       * than return 0 */
      if (pthread_attr_destroy (&thread->attr) != 0)
	g_warning ("pthread_attr_destroy has failed !");

      GST_DEBUG (GST_CAT_THREAD, "joining pthread %ld", thread->thread_id);
536
537
      if (pthread_join (thread->thread_id, NULL) != 0)
        g_warning ("pthread_join has failed !\n");
538

539
540
541
542
      thread->thread_id = -1;
      
      /* the stack was allocated when we created the thread
       * using scheduler->get_preferred_stack */
543
      if (thread->stack) {
544
545
        GST_DEBUG (GST_CAT_THREAD, "freeing allocated stack (%p)", 
	           thread->stack);
546
547
548
        free (thread->stack);
        thread->stack = NULL;
      }
549
550
551
552
     
      THR_DEBUG ("unlocking mutex");
      g_mutex_unlock (thread->lock);

553
554
555
      GST_FLAG_UNSET (thread, GST_THREAD_STATE_REAPING);
      GST_FLAG_UNSET (thread, GST_THREAD_STATE_STARTED);
      GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
556

557
558
      break;
    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
567
      g_mutex_unlock (thread->lock);

568
      break;
Erik Walthinsen's avatar
Erik Walthinsen committed
569
    default:
570
      GST_DEBUG_ELEMENT (GST_CAT_THREAD, element, "UNHANDLED STATE CHANGE! %x", transition);
Erik Walthinsen's avatar
Erik Walthinsen committed
571
572
573
574
575
576
      break;
  }

  return stateset;
}

577
578
579
580
581
/**
 * gst_thread_main_loop:
 * @arg: the thread to start
 *
 * The main loop of the thread. The thread will iterate
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
582
 * while the state is GST_THREAD_STATE_SPINNING.
583
 */
Wim Taymans's avatar
Wim Taymans committed
584
static void *
585
gst_thread_main_loop (void *arg)
Wim Taymans's avatar
Wim Taymans committed
586
{
587
  GstThread *thread = NULL;
588
589
  gint stateset;

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
590
  GST_DEBUG (GST_CAT_THREAD, "gst_thread_main_loop started");
591
  thread = GST_THREAD (arg);
592
593
  g_mutex_lock (thread->lock);

594
  /* handle scheduler policy */
595
596
597
598
599
600
601
602
603
604
605
606
607
  if (thread->sched_policy != SCHED_OTHER) {
    struct sched_param sched_param;

    memset (&sched_param, 0, sizeof (sched_param));
    if (thread->priority == 0) {
      thread->priority = sched_get_priority_max (thread->sched_policy);
    }
    sched_param.sched_priority = thread->priority;

    if (sched_setscheduler (0, thread->sched_policy, &sched_param) != 0) {
      GST_DEBUG (GST_CAT_THREAD, "not running with real-time priority");
    }
  }
608
609
  else
    g_warning ("thread has SCHED_OTHER policy, unhandled !");
610

611
  /* set up the element's scheduler */
Wim Taymans's avatar
Wim Taymans committed
612
  gst_scheduler_setup (GST_ELEMENT_SCHED (thread));
613
614
  GST_FLAG_UNSET (thread, GST_THREAD_STATE_REAPING);

615
  thread->pid = getpid();
616
  THR_INFO_MAIN ("thread is running");
Erik Walthinsen's avatar
Erik Walthinsen committed
617

618
  /* first we need to change the state of all the children */
619
  if (GST_ELEMENT_CLASS (parent_class)->change_state) {
620
    stateset = GST_ELEMENT_CLASS (parent_class)->change_state (GST_ELEMENT(thread));
Erik Walthinsen's avatar
Erik Walthinsen committed
621

622
    if (stateset != GST_STATE_SUCCESS) {
623
      THR_DEBUG_MAIN ("state change of children failed");
624
625
626
    }
  }

627
  THR_DEBUG_MAIN ("indicating spinup");
628
  g_cond_signal (thread->cond);
629
  /* don't unlock the mutex because we hold it into the top of the while loop */
630
  THR_DEBUG_MAIN ("thread has indicated spinup to parent process");
631

632
  /***** THREAD IS NOW IN READY STATE *****/
633

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
634
635
  /* CR1: most of this code is handshaking */
  /* do this while the thread lives */
636
  while (!GST_FLAG_IS_SET (thread, GST_THREAD_STATE_REAPING)) {
637
638
    /* NOTE we hold the thread lock at this point */
    /* what we do depends on what state we're in */
639
    switch (GST_STATE (thread)) {
640
      /* NOTE: cannot be in NULL, we're not running in that state at all */
641
      case GST_STATE_READY:
642
        /* wait to be set to either the NULL or PAUSED states */
643
        THR_DEBUG_MAIN ("thread in %s state, waiting for either %s or %s",
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
644
645
646
                        gst_element_state_get_name (GST_STATE_READY),
                        gst_element_state_get_name (GST_STATE_NULL),
                        gst_element_state_get_name (GST_STATE_PAUSED));
647
        g_cond_wait (thread->cond, thread->lock);
648
	
Wim Taymans's avatar
Wim Taymans committed
649
650
651
652
653
654
	/* this must have happened by a state change in the thread context */
	if (GST_STATE_PENDING (thread) != GST_STATE_NULL &&
	    GST_STATE_PENDING (thread) != GST_STATE_PAUSED) {
          g_cond_signal (thread->cond);
	  continue;
	}
655

656
        /* been signaled, we need to state transition now and signal back */
657
        gst_thread_update_state (thread);
658
659
        THR_DEBUG_MAIN ("done with state transition, "
	                "signaling back to parent process");
660
        g_cond_signal (thread->cond);
661
        /* now we decide what to do next */
662
        if (GST_STATE (thread) == GST_STATE_NULL) {
663
          /* REAPING must be set, we can simply break this iteration */
664
          THR_DEBUG_MAIN ("set GST_THREAD_STATE_REAPING");
665
666
667
          GST_FLAG_SET (thread, GST_THREAD_STATE_REAPING);
	}
        continue;
668

669
      case GST_STATE_PAUSED:
670
        /* wait to be set to either the READY or PLAYING states */
671
672
673
674
        THR_DEBUG_MAIN ("thread in %s state, waiting for either %s or %s",
                        gst_element_state_get_name (GST_STATE_PAUSED),
                        gst_element_state_get_name (GST_STATE_READY),
                        gst_element_state_get_name (GST_STATE_PLAYING));
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
675
        g_cond_wait (thread->cond, thread->lock);
676

Wim Taymans's avatar
Wim Taymans committed
677
678
679
680
681
682
	/* this must have happened by a state change in the thread context */
	if (GST_STATE_PENDING (thread) != GST_STATE_READY &&
	    GST_STATE_PENDING (thread) != GST_STATE_PLAYING) {
          g_cond_signal (thread->cond);
	  continue;
	}
683

684
        /* been signaled, we need to state transition now and signal back */
685
        gst_thread_update_state (thread);
686
        /* now we decide what to do next */
687
        if (GST_STATE (thread) != GST_STATE_PLAYING) {
688
          /* either READY or the state change failed for some reason */
689
          g_cond_signal (thread->cond);
690
          continue;
691
692
693
        } 
	else {
          GST_FLAG_SET (thread, GST_THREAD_STATE_SPINNING);
694
          /* PLAYING is coming up, so we can now start spinning */
695
          while (GST_FLAG_IS_SET (thread, GST_THREAD_STATE_SPINNING)) {
696
697
698
699
700
701
	    gboolean status;

            g_cond_signal (thread->cond);
            g_mutex_unlock (thread->lock);
            status = gst_bin_iterate (GST_BIN (thread));
            g_mutex_lock (thread->lock);
702
            /* g_cond_signal(thread->cond); */
703
704
705

	    if (!status || GST_STATE_PENDING (thread) != GST_STATE_VOID_PENDING)
              GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
706
          }
707
	  /* looks like we were stopped because of a statechange */
708
709
710
	  if (GST_STATE_PENDING (thread)) {
            gst_thread_update_state (thread);
	  }
711
          /* once we're here, SPINNING has stopped, we should signal that we're done */
712
          THR_DEBUG_MAIN ("SPINNING stopped, signaling back to parent process");
713
          g_cond_signal (thread->cond);
714
          /* now we can wait for PAUSED */
715
716
717
          continue;
        }
      case GST_STATE_PLAYING:
718
        /* wait to be set to PAUSED */
719
        THR_DEBUG_MAIN ("thread in %s state, waiting for %s",
720
721
                        gst_element_state_get_name (GST_STATE_PLAYING),
                        gst_element_state_get_name (GST_STATE_PAUSED));
722
723
        g_cond_wait (thread->cond,thread->lock);

724
        /* been signaled, we need to state transition now and signal back */
725
726
        gst_thread_update_state (thread);
        g_cond_signal (thread->cond);
727
728
        /* now we decide what to do next */
        /* there's only PAUSED, we we just wait for it */
729
        continue;
730
      case GST_STATE_NULL:
731
        THR_DEBUG_MAIN ("thread in %s state, preparing to die",
732
                        gst_element_state_get_name (GST_STATE_NULL));
733
734
735
736
        GST_FLAG_SET (thread, GST_THREAD_STATE_REAPING);
        break;
      default:
	g_assert_not_reached ();
737
738
        break;
    }
Erik Walthinsen's avatar
Erik Walthinsen committed
739
  }
740
741
742

  /* THREAD HAS STOPPED RUNNING */
  
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
743
  /* we need to destroy the scheduler here because it has mapped it's
Wim Taymans's avatar
Wim Taymans committed
744
745
746
   * stack into the threads stack space */
  gst_scheduler_reset (GST_ELEMENT_SCHED (thread));

747
  /* since we don't unlock at the end of the while loop, do it here */
748
  g_mutex_unlock (thread->lock);
Erik Walthinsen's avatar
Erik Walthinsen committed
749

750
  GST_INFO (GST_CAT_THREAD, "gstthread: thread \"%s\" is stopped",
751
		  GST_ELEMENT_NAME (thread));
752
753
754

  g_signal_emit (G_OBJECT (thread), gst_thread_signals[SHUTDOWN], 0);

Erik Walthinsen's avatar
Erik Walthinsen committed
755
756
757
  return NULL;
}

758
#ifndef GST_DISABLE_LOADSAVE
759
760
761
762
763
764
765
766
767
static xmlNodePtr
gst_thread_save_thyself (GstObject *object,
		         xmlNodePtr self)
{
  if (GST_OBJECT_CLASS (parent_class)->save_thyself)
    GST_OBJECT_CLASS (parent_class)->save_thyself (object, self);
  return NULL;
}

Wim Taymans's avatar
Wim Taymans committed
768
static void
769
770
gst_thread_restore_thyself (GstObject *object,
		            xmlNodePtr self)
771
{
772
  GST_DEBUG (GST_CAT_THREAD,"gstthread: restore");
773

774
775
  if (GST_OBJECT_CLASS (parent_class)->restore_thyself)
    GST_OBJECT_CLASS (parent_class)->restore_thyself (object, self);
776
}
777
#endif /* GST_DISABLE_LOADSAVE */