2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
5 * gstthread.c: Threaded container object
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.
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.
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.
25 //#define GST_DEBUG_ENABLED
26 #include "gst_private.h"
28 #include "gstthread.h"
31 GstElementDetails gst_thread_details = {
34 "Container that creates/manages a thread",
36 "Erik Walthinsen <omega@cse.ogi.edu>",
41 /* Thread signals and args */
53 static void gst_thread_class_init (GstThreadClass *klass);
54 static void gst_thread_init (GstThread *thread);
56 static void gst_thread_set_arg (GtkObject *object,GtkArg *arg,guint id);
57 static void gst_thread_get_arg (GtkObject *object,GtkArg *arg,guint id);
59 static GstElementStateReturn gst_thread_change_state (GstElement *element);
61 static xmlNodePtr gst_thread_save_thyself (GstElement *element,xmlNodePtr parent);
62 static void gst_thread_restore_thyself (GstElement *element,xmlNodePtr parent,
63 GHashTable *elements);
65 static void gst_thread_signal_thread (GstThread *thread);
66 static void gst_thread_wait_thread (GstThread *thread);
67 static void gst_thread_create_plan_dummy (GstBin *bin);
68 static void gst_thread_schedule_dummy (GstBin *bin);
70 static void* gst_thread_main_loop (void *arg);
72 static GstBinClass *parent_class = NULL;
73 //static guint gst_thread_signals[LAST_SIGNAL] = { 0 };
76 gst_thread_get_type(void) {
77 static GtkType thread_type = 0;
80 static const GtkTypeInfo thread_info = {
83 sizeof(GstThreadClass),
84 (GtkClassInitFunc)gst_thread_class_init,
85 (GtkObjectInitFunc)gst_thread_init,
88 (GtkClassInitFunc)NULL,
90 thread_type = gtk_type_unique(GST_TYPE_BIN,&thread_info);
96 gst_thread_class_init (GstThreadClass *klass)
98 GtkObjectClass *gtkobject_class;
99 GstObjectClass *gstobject_class;
100 GstElementClass *gstelement_class;
101 GstBinClass *gstbin_class;
103 gtkobject_class = (GtkObjectClass*)klass;
104 gstobject_class = (GstObjectClass*)klass;
105 gstelement_class = (GstElementClass*)klass;
106 gstbin_class = (GstBinClass*)klass;
108 parent_class = gtk_type_class (GST_TYPE_BIN);
110 gtk_object_add_arg_type ("GstThread::create_thread", GTK_TYPE_BOOL,
111 GTK_ARG_READWRITE, ARG_CREATE_THREAD);
113 gstelement_class->change_state = gst_thread_change_state;
114 gstelement_class->save_thyself = gst_thread_save_thyself;
115 gstelement_class->restore_thyself = gst_thread_restore_thyself;
117 //gstbin_class->create_plan = gst_thread_create_plan_dummy;
118 gstbin_class->schedule = gst_thread_schedule_dummy;
120 gtkobject_class->set_arg = gst_thread_set_arg;
121 gtkobject_class->get_arg = gst_thread_get_arg;
126 gst_thread_init (GstThread *thread)
128 GST_DEBUG (0,"initializing thread '%s'\n",gst_element_get_name(GST_ELEMENT(thread)));
130 // we're a manager by default
131 GST_FLAG_SET (thread, GST_BIN_FLAG_MANAGER);
133 // default is to create a thread
134 GST_FLAG_SET (thread, GST_THREAD_CREATE);
135 GST_FLAG_UNSET (thread, GST_THREAD_STATE_REAPING);
137 thread->lock = g_mutex_new();
138 thread->cond = g_cond_new();
142 gst_thread_schedule_dummy (GstBin *bin)
144 g_return_if_fail (GST_IS_THREAD (bin));
146 if (!GST_FLAG_IS_SET (GST_THREAD (bin), GST_THREAD_STATE_SPINNING))
147 GST_INFO (GST_CAT_THREAD,"gstthread: scheduling delayed until thread starts");
151 gst_thread_create_plan_dummy (GstBin *bin)
153 g_return_if_fail (GST_IS_THREAD (bin));
155 if (!GST_FLAG_IS_SET (GST_THREAD (bin), GST_THREAD_STATE_SPINNING))
156 GST_INFO (GST_CAT_THREAD,"gstthread: create plan delayed until thread starts");
160 gst_thread_set_arg (GtkObject *object,
164 /* it's not null if we got it, but it might not be ours */
165 g_return_if_fail (GST_IS_THREAD (object));
168 case ARG_CREATE_THREAD:
169 if (GTK_VALUE_BOOL (*arg)) {
170 GST_INFO (GST_CAT_THREAD,"gstthread: turning ON the creation of the thread");
171 GST_FLAG_SET (object, GST_THREAD_CREATE);
172 GST_DEBUG (0,"gstthread: flags are 0x%08x\n", GST_FLAGS (object));
174 GST_INFO (GST_CAT_THREAD,"gstthread: turning OFF the creation of the thread");
175 GST_FLAG_UNSET (object, GST_THREAD_CREATE);
176 GST_DEBUG (0,"gstthread: flags are 0x%08x\n", GST_FLAGS (object));
185 gst_thread_get_arg (GtkObject *object,
189 /* it's not null if we got it, but it might not be ours */
190 g_return_if_fail (GST_IS_THREAD (object));
193 case ARG_CREATE_THREAD:
194 GTK_VALUE_BOOL (*arg) = GST_FLAG_IS_SET (object, GST_THREAD_CREATE);
204 * @name: the name of the thread
206 * Create a new thread with the given name.
208 * Returns: The new thread
211 gst_thread_new (guchar *name)
213 return gst_elementfactory_make ("thread", name);
218 static GstElementStateReturn
219 gst_thread_change_state (GstElement *element)
222 gboolean stateset = GST_STATE_SUCCESS;
223 gint pending, transition;
225 g_return_val_if_fail (GST_IS_THREAD(element), FALSE);
226 GST_DEBUG_ENTER("(\"%s\")",gst_element_get_name(element));
228 thread = GST_THREAD (element);
230 GST_INFO (GST_CAT_THREAD,"gstthread: thread \"%s\" change state %d",
231 gst_element_get_name (GST_ELEMENT (element)),
232 GST_STATE_PENDING (element));
234 pending = GST_STATE_PENDING (element);
235 transition = GST_STATE_TRANSITION (element);
237 // if (pending == GST_STATE (element)) return GST_STATE_SUCCESS;
239 GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
241 if (GST_ELEMENT_CLASS (parent_class)->change_state)
242 stateset = GST_ELEMENT_CLASS (parent_class)->change_state (element);
244 GST_INFO (GST_CAT_THREAD, "gstthread: stateset %d %d %d %02x", GST_STATE (element), stateset,
245 GST_STATE_PENDING (element), GST_STATE_TRANSITION (element));
247 switch (transition) {
248 case GST_STATE_NULL_TO_READY:
249 // if (!stateset) return FALSE;
250 // we want to prepare our internal state for doing the iterations
251 GST_INFO (GST_CAT_THREAD, "gstthread: preparing thread \"%s\" for iterations:",
252 gst_element_get_name (GST_ELEMENT (element)));
254 // set the state to idle
255 GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
256 // create the thread if that's what we're supposed to do
257 GST_INFO (GST_CAT_THREAD, "gstthread: flags are 0x%08x", GST_FLAGS (thread));
259 if (GST_FLAG_IS_SET (thread, GST_THREAD_CREATE)) {
260 GST_INFO (GST_CAT_THREAD, "gstthread: starting thread \"%s\"",
261 gst_element_get_name (GST_ELEMENT (element)));
264 pthread_create (&thread->thread_id, NULL,
265 gst_thread_main_loop, thread);
267 // wait for it to 'spin up'
268 // gst_thread_wait_thread (thread);
270 GST_INFO (GST_CAT_THREAD, "gstthread: NOT starting thread \"%s\"",
271 gst_element_get_name (GST_ELEMENT (element)));
274 case GST_STATE_PAUSED_TO_PLAYING:
275 case GST_STATE_READY_TO_PLAYING:
276 if (!stateset) return FALSE;
277 GST_INFO (GST_CAT_THREAD, "gstthread: starting thread \"%s\"",
278 gst_element_get_name (GST_ELEMENT (element)));
280 GST_FLAG_SET (thread, GST_THREAD_STATE_SPINNING);
281 gst_thread_signal_thread (thread);
283 case GST_STATE_PLAYING_TO_PAUSED:
284 GST_INFO (GST_CAT_THREAD,"gstthread: pausing thread \"%s\"",
285 gst_element_get_name (GST_ELEMENT (element)));
287 //GST_FLAG_UNSET(thread,GST_THREAD_STATE_SPINNING);
288 gst_thread_signal_thread (thread);
290 case GST_STATE_READY_TO_NULL:
291 GST_INFO (GST_CAT_THREAD,"gstthread: stopping thread \"%s\"",
292 gst_element_get_name (GST_ELEMENT (element)));
294 GST_FLAG_SET (thread, GST_THREAD_STATE_REAPING);
295 gst_thread_signal_thread (thread);
305 * gst_thread_main_loop:
306 * @arg: the thread to start
308 * The main loop of the thread. The thread will iterate
309 * while the state is GST_THREAD_STATE_SPINNING
312 gst_thread_main_loop (void *arg)
314 GstThread *thread = GST_THREAD (arg);
316 GST_INFO (GST_CAT_THREAD,"gstthread: thread \"%s\" is running with PID %d",
317 gst_element_get_name (GST_ELEMENT (thread)), getpid ());
319 // construct the plan and signal back
320 if (GST_BIN_CLASS (parent_class)->schedule)
321 GST_BIN_CLASS (parent_class)->schedule (GST_BIN (thread));
323 gst_thread_signal_thread (thread);
325 while (!GST_FLAG_IS_SET (thread, GST_THREAD_STATE_REAPING)) {
326 if (GST_FLAG_IS_SET (thread, GST_THREAD_STATE_SPINNING)) {
327 if (!gst_bin_iterate (GST_BIN (thread))) {
328 GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
332 GST_DEBUG (0, "thread \"%s\" waiting\n", gst_element_get_name (GST_ELEMENT (thread)));
333 gst_thread_wait_thread (thread);
337 GST_FLAG_UNSET (thread, GST_THREAD_STATE_REAPING);
338 //pthread_join (thread->thread_id, 0);
340 GST_INFO (GST_CAT_THREAD, "gstthread: thread \"%s\" is stopped",
341 gst_element_get_name (GST_ELEMENT (thread)));
346 gst_thread_signal_thread (GstThread *thread)
348 GST_DEBUG (0,"signaling thread\n");
349 g_mutex_lock (thread->lock);
350 g_cond_signal (thread->cond);
351 g_mutex_unlock (thread->lock);
355 gst_thread_wait_thread (GstThread *thread)
357 GST_DEBUG (0,"waiting for thread\n");
358 g_mutex_lock (thread->lock);
359 g_cond_wait (thread->cond, thread->lock);
360 g_mutex_unlock (thread->lock);
365 gst_thread_restore_thyself (GstElement *element,
367 GHashTable *elements)
369 GST_DEBUG (0,"gstthread: restore\n");
371 if (GST_ELEMENT_CLASS (parent_class)->restore_thyself)
372 GST_ELEMENT_CLASS (parent_class)->restore_thyself (element,parent, elements);
376 gst_thread_save_thyself (GstElement *element,
379 if (GST_ELEMENT_CLASS (parent_class)->save_thyself)
380 GST_ELEMENT_CLASS (parent_class)->save_thyself (element,parent);