gstthread.c 23.5 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

33
34
35
36
#define STACK_SIZE 0x200000

#define g_thread_equal(a,b) ((a) == (b))

Erik Walthinsen's avatar
Erik Walthinsen committed
37
38
GstElementDetails gst_thread_details = {
  "Threaded container",
39
  "Generic/Bin",
Andy Wingo Wingo's avatar
Andy Wingo Wingo committed
40
  "LGPL",
Erik Walthinsen's avatar
Erik Walthinsen committed
41
42
43
  "Container that creates/manages a thread",
  VERSION,
  "Erik Walthinsen <omega@cse.ogi.edu>",
44
  "(C) 1999, 2000",
Erik Walthinsen's avatar
Erik Walthinsen committed
45
46
47
48
49
};


/* Thread signals and args */
enum {
50
  SHUTDOWN,
Erik Walthinsen's avatar
Erik Walthinsen committed
51
52
53
54
  /* FILL ME */
  LAST_SIGNAL
};

55
56
57
58
59
60
enum {
  SPINUP=0,
  STATECHANGE,
  STARTUP
};

Erik Walthinsen's avatar
Erik Walthinsen committed
61
62
enum {
  ARG_0,
63
64
  ARG_SCHEDPOLICY,
  ARG_PRIORITY,
Erik Walthinsen's avatar
Erik Walthinsen committed
65
66
67
};


68

69
70
static void			gst_thread_class_init		(GstThreadClass *klass);
static void			gst_thread_init			(GstThread *thread);
Erik Walthinsen's avatar
Erik Walthinsen committed
71

72
static void 			gst_thread_dispose 	(GObject *object);
73

74
75
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
76

77
static GstElementStateReturn	gst_thread_change_state		(GstElement *element);
Erik Walthinsen's avatar
Erik Walthinsen committed
78

79
#ifndef GST_DISABLE_LOADSAVE
80
81
static xmlNodePtr		gst_thread_save_thyself		(GstObject *object, xmlNodePtr parent);
static void			gst_thread_restore_thyself	(GstObject *object, xmlNodePtr self);
82
#endif
Erik Walthinsen's avatar
Erik Walthinsen committed
83

84
static void*			gst_thread_main_loop		(void *arg);
Erik Walthinsen's avatar
Erik Walthinsen committed
85

86
87
88
89
90
#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[] = {
91
92
93
94
    {G_THREAD_PRIORITY_LOW,    "LOW", "Low Priority Scheduling"},
    {G_THREAD_PRIORITY_NORMAL, "NORMAL",  "Normal Scheduling"},
    {G_THREAD_PRIORITY_HIGH,   "HIGH",  "High Priority Scheduling"},
    {G_THREAD_PRIORITY_URGENT, "URGENT", "Urgent Scheduling"},
95
96
97
98
99
100
101
102
    {0, NULL, NULL},
  };
  if (!thread_schedpolicy_type) {
    thread_schedpolicy_type = g_enum_register_static("GstThreadSchedPolicy", thread_schedpolicy);
  }
  return thread_schedpolicy_type;
}

103
static GstBinClass *parent_class = NULL;
104
static guint gst_thread_signals[LAST_SIGNAL] = { 0 }; 
Erik Walthinsen's avatar
Erik Walthinsen committed
105

106
GType
Erik Walthinsen's avatar
Erik Walthinsen committed
107
gst_thread_get_type(void) {
108
  static GType thread_type = 0;
Erik Walthinsen's avatar
Erik Walthinsen committed
109
110

  if (!thread_type) {
111
    static const GTypeInfo thread_info = {
Erik Walthinsen's avatar
Erik Walthinsen committed
112
      sizeof(GstThreadClass),
113
114
115
116
117
118
119
120
      NULL,
      NULL,
      (GClassInitFunc)gst_thread_class_init,
      NULL,
      NULL,
      sizeof(GstThread),
      4,
      (GInstanceInitFunc)gst_thread_init,
121
      NULL
Erik Walthinsen's avatar
Erik Walthinsen committed
122
    };
123
    thread_type = g_type_register_static(GST_TYPE_BIN, "GstThread", &thread_info, 0);
Erik Walthinsen's avatar
Erik Walthinsen committed
124
125
126
127
128
  }
  return thread_type;
}

static void
129
gst_thread_class_init (GstThreadClass *klass)
130
{
131
  GObjectClass *gobject_class;
Erik Walthinsen's avatar
Erik Walthinsen committed
132
133
  GstObjectClass *gstobject_class;
  GstElementClass *gstelement_class;
134
  GstBinClass *gstbin_class;
Erik Walthinsen's avatar
Erik Walthinsen committed
135

136
  gobject_class =	(GObjectClass*)klass;
137
138
139
  gstobject_class =	(GstObjectClass*)klass;
  gstelement_class =	(GstElementClass*)klass;
  gstbin_class =	(GstBinClass*)klass;
Erik Walthinsen's avatar
Erik Walthinsen committed
140

141
  parent_class = g_type_class_ref (GST_TYPE_BIN);
Erik Walthinsen's avatar
Erik Walthinsen committed
142

143
144
  g_object_class_install_property(G_OBJECT_CLASS (klass), ARG_SCHEDPOLICY,
    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
147
148
149
150
151
152
153
154
  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);

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

164
165
  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
166

Erik Walthinsen's avatar
Erik Walthinsen committed
167
168
}

169
170
static void
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 */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
177
  /* CR1: the GstBin code checks these flags */
178
  GST_FLAG_SET (thread, GST_BIN_FLAG_MANAGER);
179
  GST_FLAG_SET (thread, GST_BIN_SELF_SCHEDULABLE);
180

181
  scheduler = gst_scheduler_factory_make (NULL, GST_ELEMENT (thread));
182
183
184
185
186

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

  thread->ppid = getpid ();
187
188
  thread->thread_id = (GThread *) NULL;
  thread->sched_policy = G_THREAD_PRIORITY_NORMAL;
189
  thread->priority = 0;
190
  thread->stack = NULL;
Erik Walthinsen's avatar
Erik Walthinsen committed
191
192
}

193
static void
194
gst_thread_dispose (GObject *object)
195
{
196
  GstThread *thread = GST_THREAD (object);
197

198
  GST_DEBUG (GST_CAT_REFCOUNTING, "dispose");
199
200
201

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

203
  G_OBJECT_CLASS (parent_class)->dispose (object);
204

205
206
207
  if (GST_ELEMENT_SCHED (thread)) {
    gst_object_unref (GST_OBJECT (GST_ELEMENT_SCHED (thread)));
  }
208
209
210
}

static void
211
gst_thread_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
212
{
213
214
  GstThread *thread;

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

218
219
  thread = GST_THREAD (object);

220
  switch (prop_id) {
221
222
223
224
225
226
    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
227
    default:
228
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Erik Walthinsen's avatar
Erik Walthinsen committed
229
230
231
232
      break;
  }
}

233
static void
234
gst_thread_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
235
{
236
237
  GstThread *thread;

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

241
242
  thread = GST_THREAD (object);

243
  switch (prop_id) {
244
245
246
247
248
249
    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
250
    default:
251
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Erik Walthinsen's avatar
Erik Walthinsen committed
252
253
254
255
      break;
  }
}

256

257
258
259
260
/**
 * gst_thread_new:
 * @name: the name of the thread
 *
Erik Walthinsen's avatar
Erik Walthinsen committed
261
 * Create a new thread with the given name.
262
 *
Wim Taymans's avatar
Wim Taymans committed
263
 * Returns: The new thread
264
 */
265
GstElement*
266
gst_thread_new (const gchar *name)
267
{
268
  return gst_element_factory_make ("thread", name);
Erik Walthinsen's avatar
Erik Walthinsen committed
269
270
}

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

273
274
275
#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 )
276
  
277
278
279
280
#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 )

281
282
283
284
/* these two macros are used for debug/info from the gst_thread_main_loop
 * function
 */

285
286
287
#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 )
288

289
290
291
292
#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 )

293
294
295
static GstElementStateReturn 
gst_thread_update_state (GstThread *thread)
{
296
  GST_DEBUG_ELEMENT (GST_CAT_THREAD, thread, "updating state of thread");
297
  /* check for state change */
298
  if (GST_STATE_PENDING (thread) != GST_STATE_VOID_PENDING) {
299
    /* punt and change state on all the children */
300
    if (GST_ELEMENT_CLASS (parent_class)->change_state)
301
      return GST_ELEMENT_CLASS (parent_class)->change_state (GST_ELEMENT (thread));
302
303
  }

304
305
306
  /* 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");
307
308
309
  return GST_STATE_SUCCESS;
}

Erik Walthinsen's avatar
Erik Walthinsen committed
310

311
static GstElementStateReturn
312
gst_thread_change_state (GstElement * element)
313
{
Erik Walthinsen's avatar
Erik Walthinsen committed
314
  GstThread *thread;
Wim Taymans's avatar
Wim Taymans committed
315
  gboolean stateset = GST_STATE_SUCCESS;
316
  gint transition;
317
318
  GThread * self = g_thread_self ();
  GError * error = NULL;
Erik Walthinsen's avatar
Erik Walthinsen committed
319

320
  g_return_val_if_fail (GST_IS_THREAD (element), GST_STATE_FAILURE);
321
  g_return_val_if_fail (gst_has_threads (), GST_STATE_FAILURE);
322

323
  thread = GST_THREAD (element);
Erik Walthinsen's avatar
Erik Walthinsen committed
324

325
  transition = GST_STATE_TRANSITION (element);
326

327
  THR_INFO ("changing state from %s to %s",
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
328
329
	    gst_element_state_get_name (GST_STATE (element)),
	    gst_element_state_get_name (GST_STATE_PENDING (element)));
330

331
  if (g_thread_equal (self, thread->thread_id)) {
332
    GST_DEBUG (GST_CAT_THREAD,
333
	       "no sync(" GST_DEBUG_THREAD_FORMAT "): setting own thread's state to spinning",
334
335
336
	       GST_DEBUG_THREAD_ARGS (thread->pid));
    return gst_thread_update_state (thread);
  }
337

338
339
  switch (transition) {
    case GST_STATE_NULL_TO_READY:
340
      /* set the state to idle */
341
      GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
342

343
      THR_DEBUG ("creating thread \"%s\"", GST_ELEMENT_NAME (element));
344

345
      /* this bit of code handles creation of GThreads
346
347
348
349
       * this is therefor tricky code
       * compare it with the block of code that handles the destruction
       * in GST_STATE_READY_TO_NULL below
       */
350
351
      g_mutex_lock (thread->lock);

352
      /* create a new GThread
353
354
355
       * use the specified attributes
       * make it execute gst_thread_main_loop (thread)
       */
356
357
358
359
360
361
      GST_DEBUG (GST_CAT_THREAD, "going to g_thread_create_full...");
      thread->thread_id = g_thread_create_full(gst_thread_main_loop,
	  thread, STACK_SIZE, TRUE, TRUE, G_THREAD_PRIORITY_NORMAL,
	  &error);
      if (!thread->thread_id){
        GST_DEBUG (GST_CAT_THREAD, "g_thread_create_full failed");
362
	g_mutex_unlock (thread->lock);
363
364
        GST_DEBUG (GST_CAT_THREAD, "could not create thread \"%s\"", 
	           GST_ELEMENT_NAME (element));
365
366
	return GST_STATE_FAILURE;
      }
367
      GST_DEBUG (GST_CAT_THREAD, "GThread created");
368

369
      /* wait for it to 'spin up' */
370
      THR_DEBUG ("waiting for child thread spinup");
371
      g_cond_wait (thread->cond, thread->lock);
372
      THR_DEBUG ("thread claims to be up");
373
      g_mutex_unlock (thread->lock);
374
375
      break;
    case GST_STATE_READY_TO_PAUSED:
376
377
      THR_INFO ("readying thread");
      g_mutex_lock (thread->lock);
378
      THR_DEBUG ("signaling");
379
      g_cond_signal (thread->cond);
380
      THR_DEBUG ("waiting for ack");
381
      g_cond_wait (thread->cond, thread->lock);
382
      THR_DEBUG ("got ack");
383
      g_mutex_unlock (thread->lock);
Erik Walthinsen's avatar
Erik Walthinsen committed
384
      break;
385
    case GST_STATE_PAUSED_TO_PLAYING:
386
387
388
389
390
391
392
393
    {
      /* 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);
      }
      
394
      THR_DEBUG ("telling thread to start spinning");
395
      g_mutex_lock (thread->lock);
396
      THR_DEBUG ("signaling");
397
      g_cond_signal (thread->cond);
398
      THR_DEBUG ("waiting for ack");
399
      g_cond_wait (thread->cond, thread->lock);
400
      THR_DEBUG ("got ack");
401
      g_mutex_unlock (thread->lock);
402
      break;
403
    }
404
    case GST_STATE_PLAYING_TO_PAUSED:
405
    {
406
      const GList *elements = (GList *) gst_bin_get_list (GST_BIN (thread));
407
408
409

      THR_INFO ("pausing thread");

410
411
412
413
      /* 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()
414
       * + unlock all elements so the bottom half can start the state change.
415
       */ 
416
      g_mutex_lock (thread->lock);
417
418
419
420

      GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);

      while (elements) {
Wim Taymans's avatar
Wim Taymans committed
421
	GstElement *element = GST_ELEMENT (elements->data);
422
423
	GList *pads;
	
424

Wim Taymans's avatar
Wim Taymans committed
425
	g_assert (element);
426
	THR_DEBUG ("  waking element \"%s\"", GST_ELEMENT_NAME (element));
427
	elements = g_list_next (elements);
428
429
430

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

433
	pads = GST_ELEMENT_PADS (element);
434

435
	while (pads) {
436
	  GstRealPad *peer = NULL;
437
	  GstElement *peerelement;
438

439
440
441
          if (GST_PAD_PEER (pads->data))
            peer = GST_REAL_PAD (GST_PAD_PEER (pads->data));

442
	  pads = g_list_next (pads);
443

444
445
	  if (!peer)
	    continue;
446

447
448
449
	  peerelement = GST_PAD_PARENT (peer);
	  if (!peerelement)
	    continue;		/* deal with case where there's no peer */
450

451
452
453
454
	  if (!GST_FLAG_IS_SET (peerelement, GST_ELEMENT_DECOUPLED)) {
	    GST_DEBUG (GST_CAT_THREAD, "peer element isn't DECOUPLED");
	    continue;
	  }
455

456
457
	  if (GST_ELEMENT_SCHED (peerelement) != GST_ELEMENT_SCHED (thread)) {
	    THR_DEBUG ("  element \"%s\" has pad cross sched boundary", GST_ELEMENT_NAME (element));
458
459
460
	    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));
461
462
463
	    }
	  }
	}
464

465
      }
466
      THR_DEBUG ("telling thread to pause, signaling");
467
      g_cond_signal (thread->cond);
468
      THR_DEBUG ("waiting for ack");
469
      g_cond_wait (thread->cond, thread->lock);
470
      THR_DEBUG ("got ack");
471
      g_mutex_unlock (thread->lock);
472
473
474
475
476
477

      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
478
      break;
479
    }
480
    case GST_STATE_READY_TO_NULL:
481
      THR_DEBUG ("telling thread to pause (null) - and joining");
482
      /* MattH FIXME revisit */
483
      g_mutex_lock (thread->lock);
484
      THR_DEBUG ("signaling");
485
      g_cond_signal (thread->cond);
486
      THR_DEBUG ("waiting for ack");
487
      g_cond_wait (thread->cond, thread->lock);
488
      THR_DEBUG ("got ack");
489
490
491
492
493
494
495
496

      /* 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
       */

497
498
      GST_DEBUG (GST_CAT_THREAD, "joining GThread %p", thread->thread_id);
      g_thread_join (thread->thread_id);
499

500
      thread->thread_id = NULL;
501
502
503
      
      /* the stack was allocated when we created the thread
       * using scheduler->get_preferred_stack */
504
      if (thread->stack) {
505
506
        GST_DEBUG (GST_CAT_THREAD, "freeing allocated stack (%p)", 
	           thread->stack);
507
508
509
        free (thread->stack);
        thread->stack = NULL;
      }
510
511
512
513
     
      THR_DEBUG ("unlocking mutex");
      g_mutex_unlock (thread->lock);

514
515
516
      GST_FLAG_UNSET (thread, GST_THREAD_STATE_REAPING);
      GST_FLAG_UNSET (thread, GST_THREAD_STATE_STARTED);
      GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
517

518
519
      break;
    case GST_STATE_PAUSED_TO_READY:
520
      THR_DEBUG ("telling thread to stop spinning");
521
      g_mutex_lock (thread->lock);
522
      THR_DEBUG ("signaling");
523
      g_cond_signal (thread->cond);
524
      THR_DEBUG ("waiting for ack");
525
      g_cond_wait (thread->cond, thread->lock);
526
      THR_DEBUG ("got ack");
527
528
      g_mutex_unlock (thread->lock);

529
      break;
Erik Walthinsen's avatar
Erik Walthinsen committed
530
    default:
531
      GST_DEBUG_ELEMENT (GST_CAT_THREAD, element, "UNHANDLED STATE CHANGE! %x", transition);
Erik Walthinsen's avatar
Erik Walthinsen committed
532
533
534
535
536
537
      break;
  }

  return stateset;
}

538
539
540
541
542
/**
 * 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
543
 * while the state is GST_THREAD_STATE_SPINNING.
544
 */
Wim Taymans's avatar
Wim Taymans committed
545
static void *
546
gst_thread_main_loop (void *arg)
Wim Taymans's avatar
Wim Taymans committed
547
{
548
  GstThread *thread = NULL;
549
  gint stateset;
550
551
552
  glong page_size;
  gpointer stack_pointer;
  gulong stack_offset;
553

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
554
  GST_DEBUG (GST_CAT_THREAD, "gst_thread_main_loop started");
555
  thread = GST_THREAD (arg);
556
557
  g_mutex_lock (thread->lock);

558
  /* set up the element's scheduler */
Wim Taymans's avatar
Wim Taymans committed
559
  gst_scheduler_setup (GST_ELEMENT_SCHED (thread));
560
561
  GST_FLAG_UNSET (thread, GST_THREAD_STATE_REAPING);

562
  thread->pid = getpid();
563
  THR_INFO_MAIN ("thread is running");
Erik Walthinsen's avatar
Erik Walthinsen committed
564

565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
  page_size = sysconf(_SC_PAGESIZE);
  stack_pointer = (gpointer) &stack_pointer;

  if(((gulong)stack_pointer & (page_size-1)) < (page_size>>1)){
    /* stack grows up, I think */
    /* FIXME this is probably not true for the main thread */
    stack_offset = (gulong)stack_pointer & (page_size - 1);
  }else{
    /* stack grows down, I think */
    stack_offset = STACK_SIZE - ((gulong)stack_pointer & (page_size - 1));
  }
  /* note the subtlety with pointer arithmetic */
  thread->stack = stack_pointer - stack_offset;
  thread->stack_size = STACK_SIZE;

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

584
    if (stateset != GST_STATE_SUCCESS) {
585
      THR_DEBUG_MAIN ("state change of children failed");
586
587
588
    }
  }

589
  THR_DEBUG_MAIN ("indicating spinup");
590
  g_cond_signal (thread->cond);
591
  /* don't unlock the mutex because we hold it into the top of the while loop */
592
  THR_DEBUG_MAIN ("thread has indicated spinup to parent process");
593

594
  /***** THREAD IS NOW IN READY STATE *****/
595

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
596
597
  /* CR1: most of this code is handshaking */
  /* do this while the thread lives */
598
  while (!GST_FLAG_IS_SET (thread, GST_THREAD_STATE_REAPING)) {
599
600
    /* NOTE we hold the thread lock at this point */
    /* what we do depends on what state we're in */
601
    switch (GST_STATE (thread)) {
602
      /* NOTE: cannot be in NULL, we're not running in that state at all */
603
      case GST_STATE_READY:
604
        /* wait to be set to either the NULL or PAUSED states */
605
        THR_DEBUG_MAIN ("thread in %s state, waiting for either %s or %s",
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
606
607
608
                        gst_element_state_get_name (GST_STATE_READY),
                        gst_element_state_get_name (GST_STATE_NULL),
                        gst_element_state_get_name (GST_STATE_PAUSED));
609
        g_cond_wait (thread->cond, thread->lock);
610
	
Wim Taymans's avatar
Wim Taymans committed
611
612
613
614
615
616
	/* 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;
	}
617

618
        /* been signaled, we need to state transition now and signal back */
619
        gst_thread_update_state (thread);
620
621
        THR_DEBUG_MAIN ("done with state transition, "
	                "signaling back to parent process");
622
        g_cond_signal (thread->cond);
623
        /* now we decide what to do next */
624
        if (GST_STATE (thread) == GST_STATE_NULL) {
625
          /* REAPING must be set, we can simply break this iteration */
626
          THR_DEBUG_MAIN ("set GST_THREAD_STATE_REAPING");
627
628
629
          GST_FLAG_SET (thread, GST_THREAD_STATE_REAPING);
	}
        continue;
630

631
      case GST_STATE_PAUSED:
632
        /* wait to be set to either the READY or PLAYING states */
633
634
635
636
        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
637
        g_cond_wait (thread->cond, thread->lock);
638

Wim Taymans's avatar
Wim Taymans committed
639
640
641
642
643
644
	/* 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;
	}
645

646
        /* been signaled, we need to state transition now and signal back */
647
        gst_thread_update_state (thread);
648
        /* now we decide what to do next */
649
        if (GST_STATE (thread) != GST_STATE_PLAYING) {
650
          /* either READY or the state change failed for some reason */
651
          g_cond_signal (thread->cond);
652
          continue;
653
654
655
        } 
	else {
          GST_FLAG_SET (thread, GST_THREAD_STATE_SPINNING);
656
          /* PLAYING is coming up, so we can now start spinning */
657
          while (GST_FLAG_IS_SET (thread, GST_THREAD_STATE_SPINNING)) {
658
659
660
661
662
663
	    gboolean status;

            g_cond_signal (thread->cond);
            g_mutex_unlock (thread->lock);
            status = gst_bin_iterate (GST_BIN (thread));
            g_mutex_lock (thread->lock);
664
            /* g_cond_signal(thread->cond); */
665
666
667

	    if (!status || GST_STATE_PENDING (thread) != GST_STATE_VOID_PENDING)
              GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
668
          }
669
	  /* looks like we were stopped because of a statechange */
670
671
672
	  if (GST_STATE_PENDING (thread)) {
            gst_thread_update_state (thread);
	  }
673
          /* once we're here, SPINNING has stopped, we should signal that we're done */
674
          THR_DEBUG_MAIN ("SPINNING stopped, signaling back to parent process");
675
          g_cond_signal (thread->cond);
676
          /* now we can wait for PAUSED */
677
678
679
          continue;
        }
      case GST_STATE_PLAYING:
680
        /* wait to be set to PAUSED */
681
        THR_DEBUG_MAIN ("thread in %s state, waiting for %s",
682
683
                        gst_element_state_get_name (GST_STATE_PLAYING),
                        gst_element_state_get_name (GST_STATE_PAUSED));
684
685
        g_cond_wait (thread->cond,thread->lock);

686
        /* been signaled, we need to state transition now and signal back */
687
688
        gst_thread_update_state (thread);
        g_cond_signal (thread->cond);
689
690
        /* now we decide what to do next */
        /* there's only PAUSED, we we just wait for it */
691
        continue;
692
      case GST_STATE_NULL:
693
        THR_DEBUG_MAIN ("thread in %s state, preparing to die",
694
                        gst_element_state_get_name (GST_STATE_NULL));
695
696
697
698
        GST_FLAG_SET (thread, GST_THREAD_STATE_REAPING);
        break;
      default:
	g_assert_not_reached ();
699
700
        break;
    }
Erik Walthinsen's avatar
Erik Walthinsen committed
701
  }
702
703
704

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

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

712
  GST_INFO (GST_CAT_THREAD, "gstthread: thread \"%s\" is stopped",
713
		  GST_ELEMENT_NAME (thread));
714
715
716

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

Erik Walthinsen's avatar
Erik Walthinsen committed
717
718
719
  return NULL;
}

720
#ifndef GST_DISABLE_LOADSAVE
721
722
723
724
725
726
727
728
729
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
730
static void
731
732
gst_thread_restore_thyself (GstObject *object,
		            xmlNodePtr self)
733
{
734
  GST_DEBUG (GST_CAT_THREAD,"gstthread: restore");
735

736
737
  if (GST_OBJECT_CLASS (parent_class)->restore_thyself)
    GST_OBJECT_CLASS (parent_class)->restore_thyself (object, self);
738
}
739
#endif /* GST_DISABLE_LOADSAVE */