Merge branch 'work'
[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_finalize (GObject * object);
44
45 #define _do_init \
46 { \
47   GST_DEBUG_CATEGORY_INIT (taskpool_debug, "taskpool", 0, "Thread pool"); \
48 }
49
50 G_DEFINE_TYPE_WITH_CODE (GstTaskPool, gst_task_pool, GST_TYPE_OBJECT, _do_init);
51
52 typedef struct
53 {
54   GstTaskPoolFunction func;
55   gpointer user_data;
56 } TaskData;
57
58 static void
59 default_func (TaskData * tdata, GstTaskPool * pool)
60 {
61   GstTaskPoolFunction func;
62   gpointer user_data;
63
64   func = tdata->func;
65   user_data = tdata->user_data;
66   g_slice_free (TaskData, tdata);
67
68   func (user_data);
69 }
70
71 static void
72 default_prepare (GstTaskPool * pool, GError ** error)
73 {
74   GST_OBJECT_LOCK (pool);
75   pool->pool = g_thread_pool_new ((GFunc) default_func, pool, -1, FALSE, NULL);
76   GST_OBJECT_UNLOCK (pool);
77 }
78
79 static void
80 default_cleanup (GstTaskPool * pool)
81 {
82   GST_OBJECT_LOCK (pool);
83   if (pool->pool) {
84     /* Shut down all the threads, we still process the ones scheduled
85      * because the unref happens in the thread function.
86      * Also wait for currently running ones to finish. */
87     g_thread_pool_free (pool->pool, FALSE, TRUE);
88     pool->pool = NULL;
89   }
90   GST_OBJECT_UNLOCK (pool);
91 }
92
93 static gpointer
94 default_push (GstTaskPool * pool, GstTaskPoolFunction func,
95     gpointer user_data, GError ** error)
96 {
97   TaskData *tdata;
98
99   tdata = g_slice_new (TaskData);
100   tdata->func = func;
101   tdata->user_data = user_data;
102
103   GST_OBJECT_LOCK (pool);
104   if (pool->pool)
105     g_thread_pool_push (pool->pool, tdata, error);
106   else {
107     g_slice_free (TaskData, tdata);
108   }
109   GST_OBJECT_UNLOCK (pool);
110
111   return NULL;
112 }
113
114 static void
115 default_join (GstTaskPool * pool, gpointer id)
116 {
117   /* we do nothing here, we can't join from the pools */
118 }
119
120 static void
121 gst_task_pool_class_init (GstTaskPoolClass * klass)
122 {
123   GObjectClass *gobject_class;
124   GstTaskPoolClass *gsttaskpool_class;
125
126   gobject_class = (GObjectClass *) klass;
127   gsttaskpool_class = (GstTaskPoolClass *) klass;
128
129   gobject_class->finalize = gst_task_pool_finalize;
130
131   gsttaskpool_class->prepare = default_prepare;
132   gsttaskpool_class->cleanup = default_cleanup;
133   gsttaskpool_class->push = default_push;
134   gsttaskpool_class->join = default_join;
135 }
136
137 static void
138 gst_task_pool_init (GstTaskPool * pool)
139 {
140 }
141
142 static void
143 gst_task_pool_finalize (GObject * object)
144 {
145   GST_DEBUG ("taskpool %p finalize", object);
146
147   G_OBJECT_CLASS (gst_task_pool_parent_class)->finalize (object);
148 }
149
150 /**
151  * gst_task_pool_new:
152  *
153  * Create a new default task pool. The default task pool will use a regular
154  * GThreadPool for threads.
155  *
156  * Returns: a new #GstTaskPool. gst_object_unref() after usage.
157  *
158  * Since: 0.10.24
159  */
160 GstTaskPool *
161 gst_task_pool_new (void)
162 {
163   GstTaskPool *pool;
164
165   pool = g_object_newv (GST_TYPE_TASK_POOL, 0, NULL);
166
167   return pool;
168 }
169
170 /**
171  * gst_task_pool_prepare:
172  * @pool: a #GstTaskPool
173  * @error: an error return location
174  *
175  * Prepare the taskpool for accepting gst_task_pool_push() operations.
176  *
177  * MT safe.
178  *
179  * Since: 0.10.24
180  */
181 void
182 gst_task_pool_prepare (GstTaskPool * pool, GError ** error)
183 {
184   GstTaskPoolClass *klass;
185
186   g_return_if_fail (GST_IS_TASK_POOL (pool));
187
188   klass = GST_TASK_POOL_GET_CLASS (pool);
189
190   if (klass->prepare)
191     klass->prepare (pool, error);
192 }
193
194 /**
195  * gst_task_pool_cleanup:
196  * @pool: a #GstTaskPool
197  *
198  * Wait for all tasks to be stopped. This is mainly used internally
199  * to ensure proper cleanup of internal data structures in test suites.
200  *
201  * MT safe.
202  *
203  * Since: 0.10.24
204  */
205 void
206 gst_task_pool_cleanup (GstTaskPool * pool)
207 {
208   GstTaskPoolClass *klass;
209
210   g_return_if_fail (GST_IS_TASK_POOL (pool));
211
212   klass = GST_TASK_POOL_GET_CLASS (pool);
213
214   if (klass->cleanup)
215     klass->cleanup (pool);
216 }
217
218 /**
219  * gst_task_pool_push:
220  * @pool: a #GstTaskPool
221  * @func: the function to call
222  * @user_data: data to pass to @func
223  * @error: return location for an error
224  *
225  * Start the execution of a new thread from @pool.
226  *
227  * Returns: a pointer that should be used for the gst_task_pool_join
228  * function. This pointer can be NULL, you must check @error to detect
229  * errors.
230  *
231  * Since: 0.10.24
232  */
233 gpointer
234 gst_task_pool_push (GstTaskPool * pool, GstTaskPoolFunction func,
235     gpointer user_data, GError ** error)
236 {
237   GstTaskPoolClass *klass;
238
239   g_return_val_if_fail (GST_IS_TASK_POOL (pool), NULL);
240
241   klass = GST_TASK_POOL_GET_CLASS (pool);
242
243   if (klass->push == NULL)
244     goto not_supported;
245
246   return klass->push (pool, func, user_data, error);
247
248   /* ERRORS */
249 not_supported:
250   {
251     g_warning ("pushing tasks on pool %p is not supported", pool);
252     return NULL;
253   }
254 }
255
256 /**
257  * gst_task_pool_join:
258  * @pool: a #GstTaskPool
259  * @id: the id
260  *
261  * Join a task and/or return it to the pool. @id is the id obtained from 
262  * gst_task_pool_push().
263  *
264  * Since: 0.10.24
265  */
266 void
267 gst_task_pool_join (GstTaskPool * pool, gpointer id)
268 {
269   GstTaskPoolClass *klass;
270
271   g_return_if_fail (GST_IS_TASK_POOL (pool));
272
273   klass = GST_TASK_POOL_GET_CLASS (pool);
274
275   if (klass->join)
276     klass->join (pool, id);
277 }