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.
20 * SECTION:rtsp-thread-pool
21 * @short_description: A pool of threads
22 * @see_also: #GstRTSPMedia, #GstRTSPClient
24 * A #GstRTSPThreadPool manages reusable threads for various server tasks.
25 * Currently the defined thread types can be found in #GstRTSPThreadType.
27 * Threads of type #GST_RTSP_THREAD_TYPE_CLIENT are used to handle requests from
28 * a connected client. With gst_rtsp_thread_pool_get_max_threads() a maximum
29 * number of threads can be set after which the pool will start to reuse the
30 * same thread for multiple clients.
32 * Threads of type #GST_RTSP_THREAD_TYPE_MEDIA will be used to perform the state
33 * changes of the media pipelines and handle its bus messages.
35 * gst_rtsp_thread_pool_get_thread() can be used to create a #GstRTSPThread
36 * object of the right type. The thread object contains a mainloop and context
37 * that run in a seperate thread and can be used to attached sources to.
39 * gst_rtsp_thread_reuse() can be used to reuse a thread for multiple purposes.
40 * If all gst_rtsp_thread_reuse() calls are matched with a
41 * gst_rtsp_thread_stop() call, the mainloop will be quit and the thread will
44 * To configure the threads, a subclass of this object should be made and the
45 * virtual methods should be overriden to implement the desired functionality.
47 * Last reviewed on 2013-07-11 (1.0.0)
55 #include "rtsp-thread-pool.h"
57 typedef struct _GstRTSPThreadImpl
63 /* FIXME, the source has to be part of GstRTSPThreadImpl, due to a bug in GLib:
64 * https://bugzilla.gnome.org/show_bug.cgi?id=720186 */
67 GST_DEFINE_MINI_OBJECT_TYPE (GstRTSPThread, gst_rtsp_thread);
69 static void gst_rtsp_thread_init (GstRTSPThreadImpl * impl);
72 _gst_rtsp_thread_free (GstRTSPThreadImpl * impl)
74 GST_DEBUG ("free thread %p", impl);
76 g_source_unref (impl->source);
77 g_main_loop_unref (impl->thread.loop);
78 g_main_context_unref (impl->thread.context);
79 g_slice_free1 (sizeof (GstRTSPThreadImpl), impl);
82 static GstRTSPThread *
83 _gst_rtsp_thread_copy (GstRTSPThreadImpl * impl)
85 GstRTSPThreadImpl *copy;
87 GST_DEBUG ("copy thread %p", impl);
89 copy = g_slice_new0 (GstRTSPThreadImpl);
90 gst_rtsp_thread_init (copy);
91 copy->thread.context = g_main_context_ref (impl->thread.context);
92 copy->thread.loop = g_main_loop_ref (impl->thread.loop);
94 return GST_RTSP_THREAD (copy);
98 gst_rtsp_thread_init (GstRTSPThreadImpl * impl)
100 gst_mini_object_init (GST_MINI_OBJECT_CAST (impl), 0,
101 GST_TYPE_RTSP_THREAD,
102 (GstMiniObjectCopyFunction) _gst_rtsp_thread_copy, NULL,
103 (GstMiniObjectFreeFunction) _gst_rtsp_thread_free);
105 g_atomic_int_set (&impl->reused, 1);
109 * gst_rtsp_thread_new:
110 * @type: the thread type
112 * Create a new thread object that can run a mainloop.
114 * Returns: (transfer full): a #GstRTSPThread.
117 gst_rtsp_thread_new (GstRTSPThreadType type)
119 GstRTSPThreadImpl *impl;
121 impl = g_slice_new0 (GstRTSPThreadImpl);
123 gst_rtsp_thread_init (impl);
124 impl->thread.type = type;
125 impl->thread.context = g_main_context_new ();
126 impl->thread.loop = g_main_loop_new (impl->thread.context, TRUE);
128 return GST_RTSP_THREAD (impl);
132 * gst_rtsp_thread_reuse:
133 * @thread: (transfer none): a #GstRTSPThread
135 * Reuse the mainloop of @thread
137 * Returns: %TRUE if the mainloop could be reused
140 gst_rtsp_thread_reuse (GstRTSPThread * thread)
142 GstRTSPThreadImpl *impl = (GstRTSPThreadImpl *) thread;
145 g_return_val_if_fail (GST_IS_RTSP_THREAD (thread), FALSE);
147 GST_DEBUG ("reuse thread %p", thread);
149 res = g_atomic_int_add (&impl->reused, 1) > 0;
151 gst_rtsp_thread_ref (thread);
157 do_quit (GstRTSPThread * thread)
159 GST_DEBUG ("stop mainloop of thread %p", thread);
160 g_main_loop_quit (thread->loop);
165 * gst_rtsp_thread_stop:
166 * @thread: (transfer full): a #GstRTSPThread
168 * Stop and unref @thread. When no threads are using the mainloop, the thread
169 * will be stopped and the final ref to @thread will be released.
172 gst_rtsp_thread_stop (GstRTSPThread * thread)
174 GstRTSPThreadImpl *impl = (GstRTSPThreadImpl *) thread;
176 g_return_if_fail (GST_IS_RTSP_THREAD (thread));
178 GST_DEBUG ("stop thread %p", thread);
180 if (g_atomic_int_dec_and_test (&impl->reused)) {
181 GST_DEBUG ("add idle source to quit mainloop of thread %p", thread);
182 impl->source = g_idle_source_new ();
183 g_source_set_callback (impl->source, (GSourceFunc) do_quit,
184 thread, (GDestroyNotify) gst_rtsp_thread_unref);
185 g_source_attach (impl->source, thread->context);
187 gst_rtsp_thread_unref (thread);
190 struct _GstRTSPThreadPoolPrivate
195 /* currently used mainloops */
199 #define DEFAULT_MAX_THREADS 1
208 GST_DEBUG_CATEGORY_STATIC (rtsp_thread_pool_debug);
209 #define GST_CAT_DEFAULT rtsp_thread_pool_debug
211 static GQuark thread_pool;
213 static void gst_rtsp_thread_pool_get_property (GObject * object, guint propid,
214 GValue * value, GParamSpec * pspec);
215 static void gst_rtsp_thread_pool_set_property (GObject * object, guint propid,
216 const GValue * value, GParamSpec * pspec);
217 static void gst_rtsp_thread_pool_finalize (GObject * obj);
219 static gpointer do_loop (GstRTSPThread * thread);
220 static GstRTSPThread *default_get_thread (GstRTSPThreadPool * pool,
221 GstRTSPThreadType type, GstRTSPContext * ctx);
223 G_DEFINE_TYPE_WITH_PRIVATE (GstRTSPThreadPool, gst_rtsp_thread_pool,
227 gst_rtsp_thread_pool_class_init (GstRTSPThreadPoolClass * klass)
229 GObjectClass *gobject_class;
231 gobject_class = G_OBJECT_CLASS (klass);
233 gobject_class->get_property = gst_rtsp_thread_pool_get_property;
234 gobject_class->set_property = gst_rtsp_thread_pool_set_property;
235 gobject_class->finalize = gst_rtsp_thread_pool_finalize;
238 * GstRTSPThreadPool::max-threads:
240 * The maximum amount of threads to use for client connections. A value of
241 * 0 means to use only the mainloop, -1 means an unlimited amount of
244 g_object_class_install_property (gobject_class, PROP_MAX_THREADS,
245 g_param_spec_int ("max-threads", "Max Threads",
246 "The maximum amount of threads to use for client connections "
247 "(0 = only mainloop, -1 = unlimited)", -1, G_MAXINT,
248 DEFAULT_MAX_THREADS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
250 klass->get_thread = default_get_thread;
252 GST_DEBUG_CATEGORY_INIT (rtsp_thread_pool_debug, "rtspthreadpool", 0,
253 "GstRTSPThreadPool");
255 thread_pool = g_quark_from_string ("gst.rtsp.thread.pool");
259 gst_rtsp_thread_pool_init (GstRTSPThreadPool * pool)
261 GstRTSPThreadPoolPrivate *priv;
263 pool->priv = priv = gst_rtsp_thread_pool_get_instance_private (pool);
265 g_mutex_init (&priv->lock);
266 priv->max_threads = DEFAULT_MAX_THREADS;
267 g_queue_init (&priv->threads);
271 gst_rtsp_thread_pool_finalize (GObject * obj)
273 GstRTSPThreadPool *pool = GST_RTSP_THREAD_POOL (obj);
274 GstRTSPThreadPoolPrivate *priv = pool->priv;
276 GST_INFO ("finalize pool %p", pool);
278 g_queue_clear (&priv->threads);
279 g_mutex_clear (&priv->lock);
281 G_OBJECT_CLASS (gst_rtsp_thread_pool_parent_class)->finalize (obj);
285 gst_rtsp_thread_pool_get_property (GObject * object, guint propid,
286 GValue * value, GParamSpec * pspec)
288 GstRTSPThreadPool *pool = GST_RTSP_THREAD_POOL (object);
291 case PROP_MAX_THREADS:
292 g_value_set_int (value, gst_rtsp_thread_pool_get_max_threads (pool));
295 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
300 gst_rtsp_thread_pool_set_property (GObject * object, guint propid,
301 const GValue * value, GParamSpec * pspec)
303 GstRTSPThreadPool *pool = GST_RTSP_THREAD_POOL (object);
306 case PROP_MAX_THREADS:
307 gst_rtsp_thread_pool_set_max_threads (pool, g_value_get_int (value));
310 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
315 do_loop (GstRTSPThread * thread)
317 GstRTSPThreadPoolPrivate *priv;
318 GstRTSPThreadPoolClass *klass;
319 GstRTSPThreadPool *pool;
321 pool = gst_mini_object_get_qdata (GST_MINI_OBJECT (thread), thread_pool);
324 klass = GST_RTSP_THREAD_POOL_GET_CLASS (pool);
326 if (klass->thread_enter)
327 klass->thread_enter (pool, thread);
329 GST_INFO ("enter mainloop of thread %p", thread);
330 g_main_loop_run (thread->loop);
331 GST_INFO ("exit mainloop of thread %p", thread);
333 if (klass->thread_leave)
334 klass->thread_leave (pool, thread);
336 g_mutex_lock (&priv->lock);
337 g_queue_remove (&priv->threads, thread);
338 g_mutex_unlock (&priv->lock);
340 gst_rtsp_thread_unref (thread);
346 * gst_rtsp_thread_pool_new:
348 * Create a new #GstRTSPThreadPool instance.
350 * Returns: (transfer full): a new #GstRTSPThreadPool
353 gst_rtsp_thread_pool_new (void)
355 GstRTSPThreadPool *result;
357 result = g_object_new (GST_TYPE_RTSP_THREAD_POOL, NULL);
363 * gst_rtsp_thread_pool_set_max_threads:
364 * @pool: a #GstRTSPThreadPool
365 * @max_threads: maximum threads
367 * Set the maximum threads used by the pool to handle client requests.
368 * A value of 0 will use the pool mainloop, a value of -1 will use an
369 * unlimited number of threads.
372 gst_rtsp_thread_pool_set_max_threads (GstRTSPThreadPool * pool,
375 GstRTSPThreadPoolPrivate *priv;
377 g_return_if_fail (GST_IS_RTSP_THREAD_POOL (pool));
381 g_mutex_lock (&priv->lock);
382 priv->max_threads = max_threads;
383 g_mutex_unlock (&priv->lock);
387 * gst_rtsp_thread_pool_get_max_threads:
388 * @pool: a #GstRTSPThreadPool
390 * Get the maximum number of threads used for client connections.
391 * See gst_rtsp_thread_pool_set_max_threads().
393 * Returns: the maximum number of threads.
396 gst_rtsp_thread_pool_get_max_threads (GstRTSPThreadPool * pool)
398 GstRTSPThreadPoolPrivate *priv;
401 g_return_val_if_fail (GST_IS_RTSP_THREAD_POOL (pool), -1);
405 g_mutex_lock (&priv->lock);
406 res = priv->max_threads;
407 g_mutex_unlock (&priv->lock);
412 static GstRTSPThread *
413 make_thread (GstRTSPThreadPool * pool, GstRTSPThreadType type,
414 GstRTSPContext * ctx)
416 GstRTSPThreadPoolClass *klass;
417 GstRTSPThread *thread;
419 klass = GST_RTSP_THREAD_POOL_GET_CLASS (pool);
421 thread = gst_rtsp_thread_new (type);
422 gst_mini_object_set_qdata (GST_MINI_OBJECT (thread), thread_pool,
423 g_object_ref (pool), g_object_unref);
425 GST_DEBUG_OBJECT (pool, "new thread %p", thread);
427 if (klass->configure_thread)
428 klass->configure_thread (pool, thread, ctx);
433 static GstRTSPThread *
434 default_get_thread (GstRTSPThreadPool * pool,
435 GstRTSPThreadType type, GstRTSPContext * ctx)
437 GstRTSPThreadPoolPrivate *priv = pool->priv;
438 GstRTSPThreadPoolClass *klass;
439 GstRTSPThread *thread;
440 GError *error = NULL;
442 klass = GST_RTSP_THREAD_POOL_GET_CLASS (pool);
445 case GST_RTSP_THREAD_TYPE_CLIENT:
446 if (priv->max_threads == 0) {
447 /* no threads allowed */
448 GST_DEBUG_OBJECT (pool, "no client threads allowed");
451 g_mutex_lock (&priv->lock);
453 if (priv->max_threads > 0 &&
454 g_queue_get_length (&priv->threads) >= priv->max_threads) {
455 /* max threads reached, recycle from queue */
456 thread = g_queue_pop_head (&priv->threads);
457 GST_DEBUG_OBJECT (pool, "recycle client thread %p", thread);
458 if (!gst_rtsp_thread_reuse (thread)) {
459 GST_DEBUG_OBJECT (pool, "thread %p stopping, retry", thread);
460 /* this can happen if we just decremented the reuse counter of the
461 * thread and signaled the mainloop that it should stop. We leave
462 * the thread out of the queue now, there is no point to add it
463 * again, it will be removed from the mainloop otherwise after it
468 /* make more threads */
469 GST_DEBUG_OBJECT (pool, "make new client thread");
470 thread = make_thread (pool, type, ctx);
472 if (!g_thread_pool_push (klass->pool, gst_rtsp_thread_ref (thread),
476 g_queue_push_tail (&priv->threads, thread);
477 g_mutex_unlock (&priv->lock);
480 case GST_RTSP_THREAD_TYPE_MEDIA:
481 GST_DEBUG_OBJECT (pool, "make new media thread");
482 thread = make_thread (pool, type, ctx);
484 if (!g_thread_pool_push (klass->pool, gst_rtsp_thread_ref (thread),
497 GST_ERROR_OBJECT (pool, "failed to push thread %s", error->message);
498 gst_rtsp_thread_unref (thread);
499 /* drop also the ref dedicated for the pool */
500 gst_rtsp_thread_unref (thread);
501 g_clear_error (&error);
507 * gst_rtsp_thread_pool_get_thread:
508 * @pool: a #GstRTSPThreadPool
509 * @type: the #GstRTSPThreadType
510 * @ctx: (transfer none): a #GstRTSPContext
512 * Get a new #GstRTSPThread for @type and @ctx.
514 * Returns: (transfer full) (nullable): a new #GstRTSPThread,
515 * gst_rtsp_thread_stop() after usage
518 gst_rtsp_thread_pool_get_thread (GstRTSPThreadPool * pool,
519 GstRTSPThreadType type, GstRTSPContext * ctx)
521 GstRTSPThreadPoolClass *klass;
522 GstRTSPThread *result = NULL;
524 g_return_val_if_fail (GST_IS_RTSP_THREAD_POOL (pool), NULL);
526 klass = GST_RTSP_THREAD_POOL_GET_CLASS (pool);
528 /* We want to be thread safe as there might be 2 threads wanting to get new
529 * #GstRTSPThread at the same time
531 if (G_UNLIKELY (!g_atomic_pointer_get (&klass->pool))) {
533 t_pool = g_thread_pool_new ((GFunc) do_loop, klass, -1, FALSE, NULL);
534 if (!g_atomic_pointer_compare_and_exchange (&klass->pool,
535 (GThreadPool *) NULL, t_pool))
536 g_thread_pool_free (t_pool, FALSE, TRUE);
539 if (klass->get_thread)
540 result = klass->get_thread (pool, type, ctx);
546 * gst_rtsp_thread_pool_cleanup:
548 * Wait for all tasks to be stopped and free all allocated resources. This is
549 * mainly used in test suites to ensure proper cleanup of internal data
553 gst_rtsp_thread_pool_cleanup (void)
555 GstRTSPThreadPoolClass *klass;
558 GST_RTSP_THREAD_POOL_CLASS (g_type_class_ref
559 (gst_rtsp_thread_pool_get_type ()));
560 if (klass->pool != NULL) {
561 g_thread_pool_free (klass->pool, FALSE, TRUE);
564 g_type_class_unref (klass);