2 * Copyright (C) 2013 Wim Taymans <wim.taymans at gmail.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
22 #include "rtsp-thread-pool.h"
24 typedef struct _GstRTSPThreadImpl
31 GST_DEFINE_MINI_OBJECT_TYPE (GstRTSPThread, gst_rtsp_thread);
33 static void gst_rtsp_thread_init (GstRTSPThreadImpl * impl);
36 _gst_rtsp_thread_free (GstRTSPThreadImpl * impl)
38 GST_DEBUG ("free thread %p", impl);
40 g_main_loop_unref (impl->thread.loop);
41 g_main_context_unref (impl->thread.context);
42 g_slice_free1 (sizeof (GstRTSPThreadImpl), impl);
45 static GstRTSPThread *
46 _gst_rtsp_thread_copy (GstRTSPThreadImpl * impl)
48 GstRTSPThreadImpl *copy;
50 GST_DEBUG ("copy thread %p", impl);
52 copy = g_slice_new0 (GstRTSPThreadImpl);
53 gst_rtsp_thread_init (copy);
54 copy->thread.context = g_main_context_ref (impl->thread.context);
55 copy->thread.loop = g_main_loop_ref (impl->thread.loop);
57 return GST_RTSP_THREAD (copy);
61 gst_rtsp_thread_init (GstRTSPThreadImpl * impl)
63 gst_mini_object_init (GST_MINI_OBJECT_CAST (impl), 0,
65 (GstMiniObjectCopyFunction) _gst_rtsp_thread_copy, NULL,
66 (GstMiniObjectFreeFunction) _gst_rtsp_thread_free);
68 g_atomic_int_set (&impl->reused, 1);
72 * gst_rtsp_thread_new:
73 * @type: the thread type
75 * Create a new thread object that can run a mainloop.
77 * Returns: a #GstRTSPThread.
80 gst_rtsp_thread_new (GstRTSPThreadType type)
82 GstRTSPThreadImpl *impl;
84 impl = g_slice_new0 (GstRTSPThreadImpl);
86 gst_rtsp_thread_init (impl);
87 impl->thread.type = type;
88 impl->thread.context = g_main_context_new ();
89 impl->thread.loop = g_main_loop_new (impl->thread.context, TRUE);
91 return GST_RTSP_THREAD (impl);
95 * gst_rtsp_thread_reuse:
96 * @thread: a #GstRTSPThread
98 * Reuse the mainloop of @thread
101 gst_rtsp_thread_reuse (GstRTSPThread * thread)
103 GstRTSPThreadImpl *impl = (GstRTSPThreadImpl *) thread;
105 g_return_if_fail (GST_IS_RTSP_THREAD (thread));
107 GST_DEBUG ("reuse thread %p", thread);
108 g_atomic_int_inc (&impl->reused);
112 * gst_rtsp_thread_stop:
113 * @thread: a #GstRTSPThread
115 * Stop @thread. When no threads are using the mainloop, the thread will be
116 * stopped and the final ref to @thread will be released.
119 gst_rtsp_thread_stop (GstRTSPThread * thread)
121 GstRTSPThreadImpl *impl = (GstRTSPThreadImpl *) thread;
123 g_return_if_fail (GST_IS_RTSP_THREAD (thread));
125 GST_DEBUG ("stop thread %p", thread);
127 if (g_atomic_int_dec_and_test (&impl->reused)) {
128 GST_DEBUG ("stop mainloop of thread %p", thread);
129 g_main_loop_quit (thread->loop);
133 #define GST_RTSP_THREAD_POOL_GET_PRIVATE(obj) \
134 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_THREAD_POOL, GstRTSPThreadPoolPrivate))
136 struct _GstRTSPThreadPoolPrivate
141 /* currently used mainloops */
145 #define DEFAULT_MAX_THREADS 1
154 GST_DEBUG_CATEGORY_STATIC (rtsp_thread_pool_debug);
155 #define GST_CAT_DEFAULT rtsp_thread_pool_debug
157 static GQuark thread_pool;
159 static void gst_rtsp_thread_pool_get_property (GObject * object, guint propid,
160 GValue * value, GParamSpec * pspec);
161 static void gst_rtsp_thread_pool_set_property (GObject * object, guint propid,
162 const GValue * value, GParamSpec * pspec);
163 static void gst_rtsp_thread_pool_finalize (GObject * obj);
165 static gpointer do_loop (GstRTSPThread * thread);
166 static GstRTSPThread *default_get_thread (GstRTSPThreadPool * pool,
167 GstRTSPThreadType type, GstRTSPClientState * state);
169 G_DEFINE_TYPE (GstRTSPThreadPool, gst_rtsp_thread_pool, G_TYPE_OBJECT);
172 gst_rtsp_thread_pool_class_init (GstRTSPThreadPoolClass * klass)
174 GObjectClass *gobject_class;
176 g_type_class_add_private (klass, sizeof (GstRTSPThreadPoolPrivate));
178 gobject_class = G_OBJECT_CLASS (klass);
180 gobject_class->get_property = gst_rtsp_thread_pool_get_property;
181 gobject_class->set_property = gst_rtsp_thread_pool_set_property;
182 gobject_class->finalize = gst_rtsp_thread_pool_finalize;
185 * GstRTSPThreadPool::max-threads:
187 * The maximum amount of threads to use for client connections. A value of
188 * 0 means to use only the mainloop, -1 means an unlimited amount of
191 g_object_class_install_property (gobject_class, PROP_MAX_THREADS,
192 g_param_spec_int ("max-threads", "Max Threads",
193 "The maximum amount of threads to use for client connections "
194 "(0 = only mainloop, -1 = unlimited)", -1, G_MAXINT,
195 DEFAULT_MAX_THREADS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
197 klass->get_thread = default_get_thread;
199 klass->pool = g_thread_pool_new ((GFunc) do_loop, klass, -1, FALSE, NULL);
201 GST_DEBUG_CATEGORY_INIT (rtsp_thread_pool_debug, "rtspthreadpool", 0,
202 "GstRTSPThreadPool");
204 thread_pool = g_quark_from_string ("gst.rtsp.thread.pool");
208 gst_rtsp_thread_pool_init (GstRTSPThreadPool * pool)
210 GstRTSPThreadPoolPrivate *priv;
212 pool->priv = priv = GST_RTSP_THREAD_POOL_GET_PRIVATE (pool);
214 g_mutex_init (&priv->lock);
215 priv->max_threads = DEFAULT_MAX_THREADS;
216 g_queue_init (&priv->threads);
220 gst_rtsp_thread_pool_finalize (GObject * obj)
222 GstRTSPThreadPool *pool = GST_RTSP_THREAD_POOL (obj);
223 GstRTSPThreadPoolPrivate *priv = pool->priv;
225 GST_INFO ("finalize pool %p", pool);
227 g_queue_clear (&priv->threads);
228 g_mutex_clear (&priv->lock);
230 G_OBJECT_CLASS (gst_rtsp_thread_pool_parent_class)->finalize (obj);
234 gst_rtsp_thread_pool_get_property (GObject * object, guint propid,
235 GValue * value, GParamSpec * pspec)
237 GstRTSPThreadPool *pool = GST_RTSP_THREAD_POOL (object);
240 case PROP_MAX_THREADS:
241 g_value_set_int (value, gst_rtsp_thread_pool_get_max_threads (pool));
244 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
249 gst_rtsp_thread_pool_set_property (GObject * object, guint propid,
250 const GValue * value, GParamSpec * pspec)
252 GstRTSPThreadPool *pool = GST_RTSP_THREAD_POOL (object);
255 case PROP_MAX_THREADS:
256 gst_rtsp_thread_pool_set_max_threads (pool, g_value_get_int (value));
259 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
264 do_loop (GstRTSPThread * thread)
266 GstRTSPThreadPoolPrivate *priv;
267 GstRTSPThreadPoolClass *klass;
268 GstRTSPThreadPool *pool;
270 pool = gst_mini_object_get_qdata (GST_MINI_OBJECT (thread), thread_pool);
273 klass = GST_RTSP_THREAD_POOL_GET_CLASS (pool);
275 if (klass->thread_enter)
276 klass->thread_enter (pool, thread);
278 GST_INFO ("enter mainloop of thread %p", thread);
279 g_main_loop_run (thread->loop);
280 GST_INFO ("exit mainloop of thread %p", thread);
282 if (klass->thread_leave)
283 klass->thread_leave (pool, thread);
285 g_mutex_lock (&priv->lock);
286 g_queue_remove (&priv->threads, thread);
287 g_mutex_unlock (&priv->lock);
289 gst_rtsp_thread_unref (thread);
295 * gst_rtsp_thread_pool_new:
297 * Create a new #GstRTSPThreadPool instance.
299 * Returns: a new #GstRTSPThreadPool
302 gst_rtsp_thread_pool_new (void)
304 GstRTSPThreadPool *result;
306 result = g_object_new (GST_TYPE_RTSP_THREAD_POOL, NULL);
312 * gst_rtsp_thread_pool_set_max_threads:
313 * @pool: a #GstRTSPThreadPool
314 * @max_threads: maximum threads
316 * Set the maximum threads used by the pool to handle client requests.
317 * A value of 0 will use the pool mainloop, a value of -1 will use an
318 * unlimited number of threads.
321 gst_rtsp_thread_pool_set_max_threads (GstRTSPThreadPool * pool,
324 GstRTSPThreadPoolPrivate *priv;
326 g_return_if_fail (GST_IS_RTSP_THREAD_POOL (pool));
330 g_mutex_lock (&priv->lock);
331 priv->max_threads = max_threads;
332 g_mutex_unlock (&priv->lock);
336 * gst_rtsp_thread_pool_get_max_threads:
337 * @pool: a #GstRTSPThreadPool
339 * Get the maximum number of threads used for client connections.
340 * See gst_rtsp_thread_pool_set_max_threads().
342 * Returns: the maximum number of threads.
345 gst_rtsp_thread_pool_get_max_threads (GstRTSPThreadPool * pool)
347 GstRTSPThreadPoolPrivate *priv;
350 g_return_val_if_fail (GST_IS_RTSP_THREAD_POOL (pool), -1);
354 g_mutex_lock (&priv->lock);
355 res = priv->max_threads;
356 g_mutex_unlock (&priv->lock);
361 static GstRTSPThread *
362 make_thread (GstRTSPThreadPool * pool, GstRTSPThreadType type,
363 GstRTSPClientState * state)
365 GstRTSPThreadPoolClass *klass;
366 GstRTSPThread *thread;
368 klass = GST_RTSP_THREAD_POOL_GET_CLASS (pool);
370 thread = gst_rtsp_thread_new (type);
371 gst_mini_object_set_qdata (GST_MINI_OBJECT (thread), thread_pool,
372 g_object_ref (pool), g_object_unref);
374 GST_DEBUG_OBJECT (pool, "new thread %p", thread);
376 if (klass->configure_thread)
377 klass->configure_thread (pool, thread, state);
382 static GstRTSPThread *
383 default_get_thread (GstRTSPThreadPool * pool,
384 GstRTSPThreadType type, GstRTSPClientState * state)
386 GstRTSPThreadPoolPrivate *priv = pool->priv;
387 GstRTSPThreadPoolClass *klass;
388 GstRTSPThread *thread;
389 GError *error = NULL;
391 klass = GST_RTSP_THREAD_POOL_GET_CLASS (pool);
394 case GST_RTSP_THREAD_TYPE_CLIENT:
395 if (priv->max_threads == 0) {
396 /* no threads allowed */
397 GST_DEBUG_OBJECT (pool, "no client threads allowed");
400 if (priv->max_threads > 0 &&
401 g_queue_get_length (&priv->threads) >= priv->max_threads) {
402 /* max threads reached, recycle from queue */
403 GST_DEBUG_OBJECT (pool, "recycle client thread");
404 thread = g_queue_pop_head (&priv->threads);
405 gst_rtsp_thread_ref (thread);
407 /* make more threads */
408 GST_DEBUG_OBJECT (pool, "make new client thread");
409 thread = make_thread (pool, type, state);
411 if (!g_thread_pool_push (klass->pool, thread, &error))
414 g_queue_push_tail (&priv->threads, thread);
417 case GST_RTSP_THREAD_TYPE_MEDIA:
418 GST_DEBUG_OBJECT (pool, "make new media thread");
419 thread = make_thread (pool, type, state);
421 if (!g_thread_pool_push (klass->pool, thread, &error))
433 GST_ERROR_OBJECT (pool, "failed to push thread %s", error->message);
434 gst_rtsp_thread_unref (thread);
435 g_clear_error (&error);
441 * gst_rtsp_thread_pool_get_thread:
442 * @pool: a #GstRTSPThreadPool
443 * @type: the #GstRTSPThreadType
444 * @state: a #GstRTSPClientState
446 * Get a new #GstRTSPThread for @type and @state.
448 * Returns: a new #GstRTSPThread, gst_rtsp_thread_stop() after usage
451 gst_rtsp_thread_pool_get_thread (GstRTSPThreadPool * pool,
452 GstRTSPThreadType type, GstRTSPClientState * state)
454 GstRTSPThreadPoolClass *klass;
455 GstRTSPThread *result = NULL;
457 g_return_val_if_fail (GST_IS_RTSP_THREAD_POOL (pool), NULL);
458 g_return_val_if_fail (state != NULL, NULL);
460 klass = GST_RTSP_THREAD_POOL_GET_CLASS (pool);
462 if (klass->get_thread)
463 result = klass->get_thread (pool, type, state);