gstpad: Probes that return HANDLED can reset the data info field
[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., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21
22 /**
23  * SECTION:gsttaskpool
24  * @title: GstTaskPool
25  * @short_description: Pool of GStreamer streaming threads
26  * @see_also: #GstTask, #GstPad
27  *
28  * This object provides an abstraction for creating threads. The default
29  * implementation uses a regular GThreadPool to start tasks.
30  *
31  * Subclasses can be made to create custom threads.
32  */
33
34 #include "gst_private.h"
35
36 #include "gstinfo.h"
37 #include "gsttaskpool.h"
38 #include "gsterror.h"
39
40 GST_DEBUG_CATEGORY_STATIC (taskpool_debug);
41 #define GST_CAT_DEFAULT (taskpool_debug)
42
43 #ifndef GST_DISABLE_GST_DEBUG
44 static void gst_task_pool_finalize (GObject * object);
45 #endif
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, error);
78   GST_OBJECT_UNLOCK (pool);
79 }
80
81 static void
82 default_cleanup (GstTaskPool * pool)
83 {
84   GThreadPool *pool_;
85
86   GST_OBJECT_LOCK (pool);
87   pool_ = pool->pool;
88   pool->pool = NULL;
89   GST_OBJECT_UNLOCK (pool);
90
91   if (pool_) {
92     /* Shut down all the threads, we still process the ones scheduled
93      * because the unref happens in the thread function.
94      * Also wait for currently running ones to finish. */
95     g_thread_pool_free (pool_, FALSE, TRUE);
96   }
97 }
98
99 static gpointer
100 default_push (GstTaskPool * pool, GstTaskPoolFunction func,
101     gpointer user_data, GError ** error)
102 {
103   TaskData *tdata;
104
105   tdata = g_slice_new (TaskData);
106   tdata->func = func;
107   tdata->user_data = user_data;
108
109   GST_OBJECT_LOCK (pool);
110   if (pool->pool)
111     g_thread_pool_push (pool->pool, tdata, error);
112   else {
113     g_slice_free (TaskData, tdata);
114     g_set_error_literal (error, GST_CORE_ERROR, GST_CORE_ERROR_FAILED,
115         "No thread pool");
116
117   }
118   GST_OBJECT_UNLOCK (pool);
119
120   return NULL;
121 }
122
123 static void
124 default_join (GstTaskPool * pool, gpointer id)
125 {
126   /* we do nothing here, we can't join from the pools */
127 }
128
129 static void
130 gst_task_pool_class_init (GstTaskPoolClass * klass)
131 {
132   GObjectClass *gobject_class;
133   GstTaskPoolClass *gsttaskpool_class;
134
135   gobject_class = (GObjectClass *) klass;
136   gsttaskpool_class = (GstTaskPoolClass *) klass;
137
138 #ifndef GST_DISABLE_GST_DEBUG
139   gobject_class->finalize = gst_task_pool_finalize;
140 #endif
141
142   gsttaskpool_class->prepare = default_prepare;
143   gsttaskpool_class->cleanup = default_cleanup;
144   gsttaskpool_class->push = default_push;
145   gsttaskpool_class->join = default_join;
146 }
147
148 static void
149 gst_task_pool_init (GstTaskPool * pool)
150 {
151 }
152
153 #ifndef GST_DISABLE_GST_DEBUG
154 static void
155 gst_task_pool_finalize (GObject * object)
156 {
157   GST_DEBUG ("taskpool %p finalize", object);
158
159   G_OBJECT_CLASS (gst_task_pool_parent_class)->finalize (object);
160 }
161 #endif
162 /**
163  * gst_task_pool_new:
164  *
165  * Create a new default task pool. The default task pool will use a regular
166  * GThreadPool for threads.
167  *
168  * Returns: (transfer full): a new #GstTaskPool. gst_object_unref() after usage.
169  */
170 GstTaskPool *
171 gst_task_pool_new (void)
172 {
173   GstTaskPool *pool;
174
175   pool = g_object_new (GST_TYPE_TASK_POOL, NULL);
176
177   /* clear floating flag */
178   gst_object_ref_sink (pool);
179
180   return pool;
181 }
182
183 /**
184  * gst_task_pool_prepare:
185  * @pool: a #GstTaskPool
186  * @error: an error return location
187  *
188  * Prepare the taskpool for accepting gst_task_pool_push() operations.
189  *
190  * MT safe.
191  */
192 void
193 gst_task_pool_prepare (GstTaskPool * pool, GError ** error)
194 {
195   GstTaskPoolClass *klass;
196
197   g_return_if_fail (GST_IS_TASK_POOL (pool));
198
199   klass = GST_TASK_POOL_GET_CLASS (pool);
200
201   if (klass->prepare)
202     klass->prepare (pool, error);
203 }
204
205 /**
206  * gst_task_pool_cleanup:
207  * @pool: a #GstTaskPool
208  *
209  * Wait for all tasks to be stopped. This is mainly used internally
210  * to ensure proper cleanup of internal data structures in test suites.
211  *
212  * MT safe.
213  */
214 void
215 gst_task_pool_cleanup (GstTaskPool * pool)
216 {
217   GstTaskPoolClass *klass;
218
219   g_return_if_fail (GST_IS_TASK_POOL (pool));
220
221   klass = GST_TASK_POOL_GET_CLASS (pool);
222
223   if (klass->cleanup)
224     klass->cleanup (pool);
225 }
226
227 /**
228  * gst_task_pool_push:
229  * @pool: a #GstTaskPool
230  * @func: (scope async): the function to call
231  * @user_data: (closure): data to pass to @func
232  * @error: return location for an error
233  *
234  * Start the execution of a new thread from @pool.
235  *
236  * Returns: (transfer none) (nullable): a pointer that should be used
237  * for the gst_task_pool_join function. This pointer can be %NULL, you
238  * must check @error to detect errors.
239  */
240 gpointer
241 gst_task_pool_push (GstTaskPool * pool, GstTaskPoolFunction func,
242     gpointer user_data, GError ** error)
243 {
244   GstTaskPoolClass *klass;
245
246   g_return_val_if_fail (GST_IS_TASK_POOL (pool), NULL);
247
248   klass = GST_TASK_POOL_GET_CLASS (pool);
249
250   if (klass->push == NULL)
251     goto not_supported;
252
253   return klass->push (pool, func, user_data, error);
254
255   /* ERRORS */
256 not_supported:
257   {
258     g_warning ("pushing tasks on pool %p is not supported", pool);
259     return NULL;
260   }
261 }
262
263 /**
264  * gst_task_pool_join:
265  * @pool: a #GstTaskPool
266  * @id: the id
267  *
268  * Join a task and/or return it to the pool. @id is the id obtained from
269  * gst_task_pool_push().
270  */
271 void
272 gst_task_pool_join (GstTaskPool * pool, gpointer id)
273 {
274   GstTaskPoolClass *klass;
275
276   g_return_if_fail (GST_IS_TASK_POOL (pool));
277
278   klass = GST_TASK_POOL_GET_CLASS (pool);
279
280   if (klass->join)
281     klass->join (pool, id);
282 }