Merge branch 'master' of ssh://git.freedesktop.org/git/gstreamer/gstreamer
[platform/upstream/gstreamer.git] / gst / gsttaskpool.c
1 /* GStreamer
2  * Copyright (C) 2009 Wim Taymans <wim.taymans@gmail.com>
3  *
4  * gsttaskpool.c: Pool for streaming threads
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 /**
23  * SECTION:gsttaskpool
24  * @short_description: Pool of GStreamer streaming threads
25  * @see_also: #GstTask, #GstPad
26  *
27  * This object provides an abstraction for creating threads. The default
28  * implementation uses a regular GThreadPool to start tasks.
29  *
30  * Subclasses can be made to create custom threads.
31  *
32  * Last reviewed on 2009-04-23 (0.10.24)
33  */
34
35 #include "gst_private.h"
36
37 #include "gstinfo.h"
38 #include "gsttaskpool.h"
39
40 GST_DEBUG_CATEGORY_STATIC (taskpool_debug);
41 #define GST_CAT_DEFAULT (taskpool_debug)
42
43 static void gst_task_pool_class_init (GstTaskPoolClass * klass);
44 static void gst_task_pool_init (GstTaskPool * pool);
45 static void gst_task_pool_finalize (GObject * object);
46
47 #define _do_init \
48 { \
49   GST_DEBUG_CATEGORY_INIT (taskpool_debug, "taskpool", 0, "Thread pool"); \
50 }
51
52 G_DEFINE_TYPE_WITH_CODE (GstTaskPool, gst_task_pool, GST_TYPE_OBJECT, _do_init);
53
54 typedef struct
55 {
56   GstTaskPoolFunction func;
57   gpointer user_data;
58 } TaskData;
59
60 static void
61 default_func (TaskData * tdata, GstTaskPool * pool)
62 {
63   GstTaskPoolFunction func;
64   gpointer user_data;
65
66   func = tdata->func;
67   user_data = tdata->user_data;
68   g_slice_free (TaskData, tdata);
69
70   func (user_data);
71 }
72
73 static void
74 default_prepare (GstTaskPool * pool, GError ** error)
75 {
76   GST_OBJECT_LOCK (pool);
77   pool->pool = g_thread_pool_new ((GFunc) default_func, pool, -1, FALSE, NULL);
78   GST_OBJECT_UNLOCK (pool);
79 }
80
81 static void
82 default_cleanup (GstTaskPool * pool)
83 {
84   GST_OBJECT_LOCK (pool);
85   if (pool->pool) {
86     /* Shut down all the threads, we still process the ones scheduled
87      * because the unref happens in the thread function.
88      * Also wait for currently running ones to finish. */
89     g_thread_pool_free (pool->pool, FALSE, TRUE);
90     pool->pool = NULL;
91   }
92   GST_OBJECT_UNLOCK (pool);
93 }
94
95 static gpointer
96 default_push (GstTaskPool * pool, GstTaskPoolFunction func,
97     gpointer user_data, GError ** error)
98 {
99   TaskData *tdata;
100
101   tdata = g_slice_new (TaskData);
102   tdata->func = func;
103   tdata->user_data = user_data;
104
105   GST_OBJECT_LOCK (pool);
106   if (pool->pool)
107     g_thread_pool_push (pool->pool, tdata, error);
108   else {
109     g_slice_free (TaskData, tdata);
110   }
111   GST_OBJECT_UNLOCK (pool);
112
113   return NULL;
114 }
115
116 static void
117 default_join (GstTaskPool * pool, gpointer id)
118 {
119   /* we do nothing here, we can't join from the pools */
120 }
121
122 static void
123 gst_task_pool_class_init (GstTaskPoolClass * klass)
124 {
125   GObjectClass *gobject_class;
126   GstTaskPoolClass *gsttaskpool_class;
127
128   gobject_class = (GObjectClass *) klass;
129   gsttaskpool_class = (GstTaskPoolClass *) klass;
130
131   gobject_class->finalize = gst_task_pool_finalize;
132
133   gsttaskpool_class->prepare = default_prepare;
134   gsttaskpool_class->cleanup = default_cleanup;
135   gsttaskpool_class->push = default_push;
136   gsttaskpool_class->join = default_join;
137 }
138
139 static void
140 gst_task_pool_init (GstTaskPool * pool)
141 {
142 }
143
144 static void
145 gst_task_pool_finalize (GObject * object)
146 {
147   GST_DEBUG ("taskpool %p finalize", object);
148
149   G_OBJECT_CLASS (gst_task_pool_parent_class)->finalize (object);
150 }
151
152 /**
153  * gst_task_pool_new:
154  *
155  * Create a new default task pool. The default task pool will use a regular
156  * GThreadPool for threads.
157  *
158  * Returns: a new #GstTaskPool. gst_object_unref() after usage.
159  *
160  * Since: 0.10.24
161  */
162 GstTaskPool *
163 gst_task_pool_new (void)
164 {
165   GstTaskPool *pool;
166
167   pool = g_object_newv (GST_TYPE_TASK_POOL, 0, NULL);
168
169   return pool;
170 }
171
172 /**
173  * gst_task_pool_prepare:
174  * @pool: a #GstTaskPool
175  * @error: an error return location
176  *
177  * Prepare the taskpool for accepting gst_task_pool_push() operations.
178  *
179  * MT safe.
180  *
181  * Since: 0.10.24
182  */
183 void
184 gst_task_pool_prepare (GstTaskPool * pool, GError ** error)
185 {
186   GstTaskPoolClass *klass;
187
188   g_return_if_fail (GST_IS_TASK_POOL (pool));
189
190   klass = GST_TASK_POOL_GET_CLASS (pool);
191
192   if (klass->prepare)
193     klass->prepare (pool, error);
194 }
195
196 /**
197  * gst_task_pool_cleanup:
198  * @pool: a #GstTaskPool
199  *
200  * Wait for all tasks to be stopped. This is mainly used internally
201  * to ensure proper cleanup of internal data structures in test suites.
202  *
203  * MT safe.
204  *
205  * Since: 0.10.24
206  */
207 void
208 gst_task_pool_cleanup (GstTaskPool * pool)
209 {
210   GstTaskPoolClass *klass;
211
212   g_return_if_fail (GST_IS_TASK_POOL (pool));
213
214   klass = GST_TASK_POOL_GET_CLASS (pool);
215
216   if (klass->cleanup)
217     klass->cleanup (pool);
218 }
219
220 /**
221  * gst_task_pool_push:
222  * @pool: a #GstTaskPool
223  * @func: the function to call
224  * @user_data: data to pass to @func
225  * @error: return location for an error
226  *
227  * Start the execution of a new thread from @pool.
228  *
229  * Returns: a pointer that should be used for the gst_task_pool_join
230  * function. This pointer can be NULL, you must check @error to detect
231  * errors.
232  *
233  * Since: 0.10.24
234  */
235 gpointer
236 gst_task_pool_push (GstTaskPool * pool, GstTaskPoolFunction func,
237     gpointer user_data, GError ** error)
238 {
239   GstTaskPoolClass *klass;
240
241   g_return_val_if_fail (GST_IS_TASK_POOL (pool), NULL);
242
243   klass = GST_TASK_POOL_GET_CLASS (pool);
244
245   if (klass->push == NULL)
246     goto not_supported;
247
248   return klass->push (pool, func, user_data, error);
249
250   /* ERRORS */
251 not_supported:
252   {
253     g_warning ("pushing tasks on pool %p is not supported", pool);
254     return NULL;
255   }
256 }
257
258 /**
259  * gst_task_pool_join:
260  * @pool: a #GstTaskPool
261  * @id: the id
262  *
263  * Join a task and/or return it to the pool. @id is the id obtained from 
264  * gst_task_pool_push().
265  *
266  * Since: 0.10.24
267  */
268 void
269 gst_task_pool_join (GstTaskPool * pool, gpointer id)
270 {
271   GstTaskPoolClass *klass;
272
273   g_return_if_fail (GST_IS_TASK_POOL (pool));
274
275   klass = GST_TASK_POOL_GET_CLASS (pool);
276
277   if (klass->join)
278     klass->join (pool, id);
279 }