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 (GstObject *object, xmlNodePtr parent);
62 static void gst_thread_restore_thyself (GstObject *object, xmlNodePtr self);
64 static void gst_thread_signal_thread (GstThread *thread);
65 static void gst_thread_wait_thread (GstThread *thread);
66 static void gst_thread_schedule_dummy (GstBin *bin);
68 static void* gst_thread_main_loop (void *arg);
70 static GstBinClass *parent_class = NULL;
71 //static guint gst_thread_signals[LAST_SIGNAL] = { 0 };
74 gst_thread_get_type(void) {
75 static GtkType thread_type = 0;
78 static const GtkTypeInfo thread_info = {
81 sizeof(GstThreadClass),
82 (GtkClassInitFunc)gst_thread_class_init,
83 (GtkObjectInitFunc)gst_thread_init,
86 (GtkClassInitFunc)NULL,
88 thread_type = gtk_type_unique(GST_TYPE_BIN,&thread_info);
94 gst_thread_class_init (GstThreadClass *klass)
96 GtkObjectClass *gtkobject_class;
97 GstObjectClass *gstobject_class;
98 GstElementClass *gstelement_class;
99 GstBinClass *gstbin_class;
101 gtkobject_class = (GtkObjectClass*)klass;
102 gstobject_class = (GstObjectClass*)klass;
103 gstelement_class = (GstElementClass*)klass;
104 gstbin_class = (GstBinClass*)klass;
106 parent_class = gtk_type_class (GST_TYPE_BIN);
108 gtk_object_add_arg_type ("GstThread::create_thread", GTK_TYPE_BOOL,
109 GTK_ARG_READWRITE, ARG_CREATE_THREAD);
111 gstobject_class->save_thyself = gst_thread_save_thyself;
112 gstobject_class->restore_thyself = gst_thread_restore_thyself;
114 gstelement_class->change_state = gst_thread_change_state;
116 gstbin_class->schedule = gst_thread_schedule_dummy;
118 gtkobject_class->set_arg = gst_thread_set_arg;
119 gtkobject_class->get_arg = gst_thread_get_arg;
124 gst_thread_init (GstThread *thread)
126 GST_DEBUG (0,"initializing thread '%s'\n",GST_ELEMENT_NAME (thread));
128 // we're a manager by default
129 GST_FLAG_SET (thread, GST_BIN_FLAG_MANAGER);
131 // default is to create a thread
132 GST_FLAG_SET (thread, GST_THREAD_CREATE);
133 GST_FLAG_UNSET (thread, GST_THREAD_STATE_REAPING);
135 thread->lock = g_mutex_new();
136 thread->cond = g_cond_new();
140 gst_thread_schedule_dummy (GstBin *bin)
142 g_return_if_fail (GST_IS_THREAD (bin));
144 if (!GST_FLAG_IS_SET (GST_THREAD (bin), GST_THREAD_STATE_SPINNING))
145 GST_INFO (GST_CAT_THREAD,"gstthread: scheduling delayed until thread starts");
149 gst_thread_set_arg (GtkObject *object,
153 /* it's not null if we got it, but it might not be ours */
154 g_return_if_fail (GST_IS_THREAD (object));
157 case ARG_CREATE_THREAD:
158 if (GTK_VALUE_BOOL (*arg)) {
159 GST_INFO (GST_CAT_THREAD,"gstthread: turning ON the creation of the thread");
160 GST_FLAG_SET (object, GST_THREAD_CREATE);
161 GST_DEBUG (0,"gstthread: flags are 0x%08x\n", GST_FLAGS (object));
163 GST_INFO (GST_CAT_THREAD,"gstthread: turning OFF the creation of the thread");
164 GST_FLAG_UNSET (object, GST_THREAD_CREATE);
165 GST_DEBUG (0,"gstthread: flags are 0x%08x\n", GST_FLAGS (object));
174 gst_thread_get_arg (GtkObject *object,
178 /* it's not null if we got it, but it might not be ours */
179 g_return_if_fail (GST_IS_THREAD (object));
182 case ARG_CREATE_THREAD:
183 GTK_VALUE_BOOL (*arg) = GST_FLAG_IS_SET (object, GST_THREAD_CREATE);
193 * @name: the name of the thread
195 * Create a new thread with the given name.
197 * Returns: The new thread
200 gst_thread_new (guchar *name)
202 return gst_elementfactory_make ("thread", name);
207 static GstElementStateReturn
208 gst_thread_change_state (GstElement *element)
211 gboolean stateset = GST_STATE_SUCCESS;
212 gint pending, transition;
214 g_return_val_if_fail (GST_IS_THREAD(element), FALSE);
215 GST_DEBUG_ENTER("(\"%s\")",GST_ELEMENT_NAME(element));
217 thread = GST_THREAD (element);
219 GST_INFO (GST_CAT_THREAD,"gstthread: thread \"%s\" change state %d",
220 GST_ELEMENT_NAME (GST_ELEMENT (element)),
221 GST_STATE_PENDING (element));
223 pending = GST_STATE_PENDING (element);
224 transition = GST_STATE_TRANSITION (element);
226 // if (pending == GST_STATE (element)) return GST_STATE_SUCCESS;
228 GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
230 if (GST_ELEMENT_CLASS (parent_class)->change_state)
231 stateset = GST_ELEMENT_CLASS (parent_class)->change_state (element);
233 GST_INFO (GST_CAT_THREAD, "gstthread: stateset %d %d %d %02x", GST_STATE (element), stateset,
234 GST_STATE_PENDING (element), GST_STATE_TRANSITION (element));
236 switch (transition) {
237 case GST_STATE_NULL_TO_READY:
238 // if (!stateset) return FALSE;
239 // we want to prepare our internal state for doing the iterations
240 GST_INFO (GST_CAT_THREAD, "gstthread: preparing thread \"%s\" for iterations:",
241 GST_ELEMENT_NAME (GST_ELEMENT (element)));
243 // set the state to idle
244 GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
245 // create the thread if that's what we're supposed to do
246 GST_INFO (GST_CAT_THREAD, "gstthread: flags are 0x%08x", GST_FLAGS (thread));
248 if (GST_FLAG_IS_SET (thread, GST_THREAD_CREATE)) {
249 GST_INFO (GST_CAT_THREAD, "gstthread: starting thread \"%s\"",
250 GST_ELEMENT_NAME (GST_ELEMENT (element)));
253 pthread_create (&thread->thread_id, NULL,
254 gst_thread_main_loop, thread);
256 // wait for it to 'spin up'
257 // gst_thread_wait_thread (thread);
259 GST_INFO (GST_CAT_THREAD, "gstthread: NOT starting thread \"%s\"",
260 GST_ELEMENT_NAME (GST_ELEMENT (element)));
263 case GST_STATE_PAUSED_TO_PLAYING:
264 case GST_STATE_READY_TO_PLAYING:
265 if (!stateset) return FALSE;
266 GST_INFO (GST_CAT_THREAD, "gstthread: starting thread \"%s\"",
267 GST_ELEMENT_NAME (GST_ELEMENT (element)));
269 GST_FLAG_SET (thread, GST_THREAD_STATE_SPINNING);
270 gst_thread_signal_thread (thread);
272 case GST_STATE_PLAYING_TO_PAUSED:
273 GST_INFO (GST_CAT_THREAD,"gstthread: pausing thread \"%s\"",
274 GST_ELEMENT_NAME (GST_ELEMENT (element)));
276 //GST_FLAG_UNSET(thread,GST_THREAD_STATE_SPINNING);
277 gst_thread_signal_thread (thread);
279 case GST_STATE_READY_TO_NULL:
280 GST_INFO (GST_CAT_THREAD,"gstthread: stopping thread \"%s\"",
281 GST_ELEMENT_NAME (GST_ELEMENT (element)));
283 GST_FLAG_SET (thread, GST_THREAD_STATE_REAPING);
284 gst_thread_signal_thread (thread);
285 pthread_join(thread->thread_id,NULL);
295 * gst_thread_main_loop:
296 * @arg: the thread to start
298 * The main loop of the thread. The thread will iterate
299 * while the state is GST_THREAD_STATE_SPINNING
302 gst_thread_main_loop (void *arg)
304 GstThread *thread = GST_THREAD (arg);
306 GST_INFO (GST_CAT_THREAD,"gstthread: thread \"%s\" is running with PID %d",
307 GST_ELEMENT_NAME (GST_ELEMENT (thread)), getpid ());
309 // construct the plan and signal back
310 if (GST_BIN_CLASS (parent_class)->schedule)
311 GST_BIN_CLASS (parent_class)->schedule (GST_BIN (thread));
313 gst_thread_signal_thread (thread);
315 while (!GST_FLAG_IS_SET (thread, GST_THREAD_STATE_REAPING)) {
316 if (GST_FLAG_IS_SET (thread, GST_THREAD_STATE_SPINNING)) {
317 if (!gst_bin_iterate (GST_BIN (thread))) {
318 GST_FLAG_UNSET (thread, GST_THREAD_STATE_SPINNING);
322 GST_DEBUG (0, "thread \"%s\" waiting\n", GST_ELEMENT_NAME (GST_ELEMENT (thread)));
323 gst_thread_wait_thread (thread);
327 GST_FLAG_UNSET (thread, GST_THREAD_STATE_REAPING);
328 // FIXME this should be removed (why's it here???)
329 // //pthread_join (thread->thread_id, 0);
331 GST_INFO (GST_CAT_THREAD, "gstthread: thread \"%s\" is stopped",
332 GST_ELEMENT_NAME (thread));
337 gst_thread_signal_thread (GstThread *thread)
339 GST_DEBUG (0,"signaling thread\n");
340 g_mutex_lock (thread->lock);
341 g_cond_signal (thread->cond);
342 g_mutex_unlock (thread->lock);
346 gst_thread_wait_thread (GstThread *thread)
348 GST_DEBUG (0,"waiting for thread\n");
349 g_mutex_lock (thread->lock);
350 g_cond_wait (thread->cond, thread->lock);
351 g_mutex_unlock (thread->lock);
356 gst_thread_restore_thyself (GstObject *object,
359 GST_DEBUG (0,"gstthread: restore\n");
361 if (GST_OBJECT_CLASS (parent_class)->restore_thyself)
362 GST_OBJECT_CLASS (parent_class)->restore_thyself (object, self);
366 gst_thread_save_thyself (GstObject *object,
369 if (GST_OBJECT_CLASS (parent_class)->save_thyself)
370 GST_OBJECT_CLASS (parent_class)->save_thyself (object, self);