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 */
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 */
35 #include "gst_private.h"
37 #include "gstinfo.h"
38 #include "gsttaskpool.h"
40 GST_DEBUG_CATEGORY_STATIC (taskpool_debug);
41 #define GST_CAT_DEFAULT (taskpool_debug)
43 #ifndef GST_DISABLE_GST_DEBUG
44 static void gst_task_pool_finalize (GObject * object);
45 #endif
47 #define _do_init \
48 { \
49 GST_DEBUG_CATEGORY_INIT (taskpool_debug, "taskpool", 0, "Thread pool"); \
50 }
52 G_DEFINE_TYPE_WITH_CODE (GstTaskPool, gst_task_pool, GST_TYPE_OBJECT, _do_init);
54 typedef struct
55 {
56 GstTaskPoolFunction func;
57 gpointer user_data;
58 } TaskData;
60 static void
61 default_func (TaskData * tdata, GstTaskPool * pool)
62 {
63 GstTaskPoolFunction func;
64 gpointer user_data;
66 func = tdata->func;
67 user_data = tdata->user_data;
68 g_slice_free (TaskData, tdata);
70 func (user_data);
71 }
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 }
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 }
95 static gpointer
96 default_push (GstTaskPool * pool, GstTaskPoolFunction func,
97 gpointer user_data, GError ** error)
98 {
99 TaskData *tdata;
101 tdata = g_slice_new (TaskData);
102 tdata->func = func;
103 tdata->user_data = user_data;
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);
113 return NULL;
114 }
116 static void
117 default_join (GstTaskPool * pool, gpointer id)
118 {
119 /* we do nothing here, we can't join from the pools */
120 }
122 static void
123 gst_task_pool_class_init (GstTaskPoolClass * klass)
124 {
125 GObjectClass *gobject_class;
126 GstTaskPoolClass *gsttaskpool_class;
128 gobject_class = (GObjectClass *) klass;
129 gsttaskpool_class = (GstTaskPoolClass *) klass;
131 #ifndef GST_DISABLE_GST_DEBUG
132 gobject_class->finalize = gst_task_pool_finalize;
133 #endif
135 gsttaskpool_class->prepare = default_prepare;
136 gsttaskpool_class->cleanup = default_cleanup;
137 gsttaskpool_class->push = default_push;
138 gsttaskpool_class->join = default_join;
139 }
141 static void
142 gst_task_pool_init (GstTaskPool * pool)
143 {
144 }
146 #ifndef GST_DISABLE_GST_DEBUG
147 static void
148 gst_task_pool_finalize (GObject * object)
149 {
150 GST_DEBUG ("taskpool %p finalize", object);
152 G_OBJECT_CLASS (gst_task_pool_parent_class)->finalize (object);
153 }
154 #endif
155 /**
156 * gst_task_pool_new:
157 *
158 * Create a new default task pool. The default task pool will use a regular
159 * GThreadPool for threads.
160 *
161 * Returns: (transfer full): a new #GstTaskPool. gst_object_unref() after usage.
162 *
163 * Since: 0.10.24
164 */
165 GstTaskPool *
166 gst_task_pool_new (void)
167 {
168 GstTaskPool *pool;
170 pool = g_object_newv (GST_TYPE_TASK_POOL, 0, NULL);
172 return pool;
173 }
175 /**
176 * gst_task_pool_prepare:
177 * @pool: a #GstTaskPool
178 * @error: an error return location
179 *
180 * Prepare the taskpool for accepting gst_task_pool_push() operations.
181 *
182 * MT safe.
183 *
184 * Since: 0.10.24
185 */
186 void
187 gst_task_pool_prepare (GstTaskPool * pool, GError ** error)
188 {
189 GstTaskPoolClass *klass;
191 g_return_if_fail (GST_IS_TASK_POOL (pool));
193 klass = GST_TASK_POOL_GET_CLASS (pool);
195 if (klass->prepare)
196 klass->prepare (pool, error);
197 }
199 /**
200 * gst_task_pool_cleanup:
201 * @pool: a #GstTaskPool
202 *
203 * Wait for all tasks to be stopped. This is mainly used internally
204 * to ensure proper cleanup of internal data structures in test suites.
205 *
206 * MT safe.
207 *
208 * Since: 0.10.24
209 */
210 void
211 gst_task_pool_cleanup (GstTaskPool * pool)
212 {
213 GstTaskPoolClass *klass;
215 g_return_if_fail (GST_IS_TASK_POOL (pool));
217 klass = GST_TASK_POOL_GET_CLASS (pool);
219 if (klass->cleanup)
220 klass->cleanup (pool);
221 }
223 /**
224 * gst_task_pool_push:
225 * @pool: a #GstTaskPool
226 * @func: the function to call
227 * @user_data: (closure): data to pass to @func
228 * @error: return location for an error
229 *
230 * Start the execution of a new thread from @pool.
231 *
232 * Returns: a pointer that should be used for the gst_task_pool_join
233 * function. This pointer can be NULL, you must check @error to detect
234 * errors.
235 *
236 * Since: 0.10.24
237 */
238 gpointer
239 gst_task_pool_push (GstTaskPool * pool, GstTaskPoolFunction func,
240 gpointer user_data, GError ** error)
241 {
242 GstTaskPoolClass *klass;
244 g_return_val_if_fail (GST_IS_TASK_POOL (pool), NULL);
246 klass = GST_TASK_POOL_GET_CLASS (pool);
248 if (klass->push == NULL)
249 goto not_supported;
251 return klass->push (pool, func, user_data, error);
253 /* ERRORS */
254 not_supported:
255 {
256 g_warning ("pushing tasks on pool %p is not supported", pool);
257 return NULL;
258 }
259 }
261 /**
262 * gst_task_pool_join:
263 * @pool: a #GstTaskPool
264 * @id: the id
265 *
266 * Join a task and/or return it to the pool. @id is the id obtained from
267 * gst_task_pool_push().
268 *
269 * Since: 0.10.24
270 */
271 void
272 gst_task_pool_join (GstTaskPool * pool, gpointer id)
273 {
274 GstTaskPoolClass *klass;
276 g_return_if_fail (GST_IS_TASK_POOL (pool));
278 klass = GST_TASK_POOL_GET_CLASS (pool);
280 if (klass->join)
281 klass->join (pool, id);
282 }