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