1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * GThreadPool: thread pool implementation.
5 * Copyright (C) 2000 Sebastian Wilhelmi; University of Karlsruhe
7 * SPDX-License-Identifier: LGPL-2.1-or-later
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
29 #include "gthreadpool.h"
31 #include "gasyncqueue.h"
32 #include "gasyncqueueprivate.h"
33 #include "glib-private.h"
35 #include "gtestutils.h"
36 #include "gthreadprivate.h"
41 * SECTION:thread_pools
42 * @title: Thread Pools
43 * @short_description: pools of threads to execute work concurrently
46 * Sometimes you wish to asynchronously fork out the execution of work
47 * and continue working in your own thread. If that will happen often,
48 * the overhead of starting and destroying a thread each time might be
49 * too high. In such cases reusing already started threads seems like a
50 * good idea. And it indeed is, but implementing this can be tedious
53 * Therefore GLib provides thread pools for your convenience. An added
54 * advantage is, that the threads can be shared between the different
55 * subsystems of your program, when they are using GLib.
57 * To create a new thread pool, you use g_thread_pool_new().
58 * It is destroyed by g_thread_pool_free().
60 * If you want to execute a certain task within a thread pool,
61 * you call g_thread_pool_push().
63 * To get the current number of running threads you call
64 * g_thread_pool_get_num_threads(). To get the number of still
65 * unprocessed tasks you call g_thread_pool_unprocessed(). To control
66 * the maximal number of threads for a thread pool, you use
67 * g_thread_pool_get_max_threads() and g_thread_pool_set_max_threads().
69 * Finally you can control the number of unused threads, that are kept
70 * alive by GLib for future use. The current number can be fetched with
71 * g_thread_pool_get_num_unused_threads(). The maximal number can be
72 * controlled by g_thread_pool_get_max_unused_threads() and
73 * g_thread_pool_set_max_unused_threads(). All currently unused threads
74 * can be stopped by calling g_thread_pool_stop_unused_threads().
78 /* #define DEBUG_MSG(args) g_printerr args ; g_printerr ("\n"); */
80 typedef struct _GRealThreadPool GRealThreadPool;
84 * @func: the function to execute in the threads of this pool
85 * @user_data: the user data for the threads of this pool
86 * @exclusive: are all threads exclusive to this pool
88 * The #GThreadPool struct represents a thread pool. It has three
89 * public read-only members, but the underlying struct is bigger,
90 * so you must not copy this struct.
92 struct _GRealThreadPool
102 GCompareDataFunc sort_func;
103 gpointer sort_user_data;
106 /* The following is just an address to mark the wakeup order for a
107 * thread, it could be any address (as long, as it isn't a valid
108 * GThreadPool address)
110 static const gpointer wakeup_thread_marker = (gpointer) &g_thread_pool_new;
111 static gint wakeup_thread_serial = 0;
113 /* Here all unused threads are waiting */
114 static GAsyncQueue *unused_thread_queue = NULL;
115 static gint unused_threads = 0;
116 static gint max_unused_threads = 2;
117 static gint kill_unused_threads = 0;
118 static guint max_idle_time = 15 * 1000;
122 /* Either thread or error are set in the end. Both transfer-full. */
128 static GCond spawn_thread_cond;
129 static GAsyncQueue *spawn_thread_queue;
131 static void g_thread_pool_queue_push_unlocked (GRealThreadPool *pool,
133 static void g_thread_pool_free_internal (GRealThreadPool *pool);
134 static gpointer g_thread_pool_thread_proxy (gpointer data);
135 static gboolean g_thread_pool_start_thread (GRealThreadPool *pool,
137 static void g_thread_pool_wakeup_and_stop_all (GRealThreadPool *pool);
138 static GRealThreadPool* g_thread_pool_wait_for_new_pool (void);
139 static gpointer g_thread_pool_wait_for_new_task (GRealThreadPool *pool);
142 g_thread_pool_queue_push_unlocked (GRealThreadPool *pool,
146 g_async_queue_push_sorted_unlocked (pool->queue,
149 pool->sort_user_data);
151 g_async_queue_push_unlocked (pool->queue, data);
154 static GRealThreadPool*
155 g_thread_pool_wait_for_new_pool (void)
157 GRealThreadPool *pool;
158 gint local_wakeup_thread_serial;
159 guint local_max_unused_threads;
160 gint local_max_idle_time;
161 gint last_wakeup_thread_serial;
162 gboolean have_relayed_thread_marker = FALSE;
164 local_max_unused_threads = (guint) g_atomic_int_get (&max_unused_threads);
165 local_max_idle_time = g_atomic_int_get (&max_idle_time);
166 last_wakeup_thread_serial = g_atomic_int_get (&wakeup_thread_serial);
170 if ((guint) g_atomic_int_get (&unused_threads) >= local_max_unused_threads)
172 /* If this is a superfluous thread, stop it. */
175 else if (local_max_idle_time > 0)
177 /* If a maximal idle time is given, wait for the given time. */
178 DEBUG_MSG (("thread %p waiting in global pool for %f seconds.",
179 g_thread_self (), local_max_idle_time / 1000.0));
181 pool = g_async_queue_timeout_pop (unused_thread_queue,
182 local_max_idle_time * 1000);
186 /* If no maximal idle time is given, wait indefinitely. */
187 DEBUG_MSG (("thread %p waiting in global pool.", g_thread_self ()));
188 pool = g_async_queue_pop (unused_thread_queue);
191 if (pool == wakeup_thread_marker)
193 local_wakeup_thread_serial = g_atomic_int_get (&wakeup_thread_serial);
194 if (last_wakeup_thread_serial == local_wakeup_thread_serial)
196 if (!have_relayed_thread_marker)
198 /* If this wakeup marker has been received for
199 * the second time, relay it.
201 DEBUG_MSG (("thread %p relaying wakeup message to "
202 "waiting thread with lower serial.",
205 g_async_queue_push (unused_thread_queue, wakeup_thread_marker);
206 have_relayed_thread_marker = TRUE;
208 /* If a wakeup marker has been relayed, this thread
209 * will get out of the way for 100 microseconds to
210 * avoid receiving this marker again.
217 if (g_atomic_int_add (&kill_unused_threads, -1) > 0)
223 DEBUG_MSG (("thread %p updating to new limits.",
226 local_max_unused_threads = (guint) g_atomic_int_get (&max_unused_threads);
227 local_max_idle_time = g_atomic_int_get (&max_idle_time);
228 last_wakeup_thread_serial = local_wakeup_thread_serial;
230 have_relayed_thread_marker = FALSE;
234 while (pool == wakeup_thread_marker);
240 g_thread_pool_wait_for_new_task (GRealThreadPool *pool)
242 gpointer task = NULL;
244 if (pool->running || (!pool->immediate &&
245 g_async_queue_length_unlocked (pool->queue) > 0))
247 /* This thread pool is still active. */
248 if (pool->max_threads != -1 && pool->num_threads > (guint) pool->max_threads)
250 /* This is a superfluous thread, so it goes to the global pool. */
251 DEBUG_MSG (("superfluous thread %p in pool %p.",
252 g_thread_self (), pool));
254 else if (pool->pool.exclusive)
256 /* Exclusive threads stay attached to the pool. */
257 task = g_async_queue_pop_unlocked (pool->queue);
259 DEBUG_MSG (("thread %p in exclusive pool %p waits for task "
260 "(%d running, %d unprocessed).",
261 g_thread_self (), pool, pool->num_threads,
262 g_async_queue_length_unlocked (pool->queue)));
266 /* A thread will wait for new tasks for at most 1/2
267 * second before going to the global pool.
269 DEBUG_MSG (("thread %p in pool %p waits for up to a 1/2 second for task "
270 "(%d running, %d unprocessed).",
271 g_thread_self (), pool, pool->num_threads,
272 g_async_queue_length_unlocked (pool->queue)));
274 task = g_async_queue_timeout_pop_unlocked (pool->queue,
280 /* This thread pool is inactive, it will no longer process tasks. */
281 DEBUG_MSG (("pool %p not active, thread %p will go to global pool "
282 "(running: %s, immediate: %s, len: %d).",
283 pool, g_thread_self (),
284 pool->running ? "true" : "false",
285 pool->immediate ? "true" : "false",
286 g_async_queue_length_unlocked (pool->queue)));
293 g_thread_pool_spawn_thread (gpointer data)
297 SpawnThreadData *spawn_thread_data;
298 GThread *thread = NULL;
299 GError *error = NULL;
300 const gchar *prgname = g_get_prgname ();
301 gchar name[16] = "pool";
304 g_snprintf (name, sizeof (name), "pool-%s", prgname);
306 g_async_queue_lock (spawn_thread_queue);
307 /* Spawn a new thread for the given pool and wake the requesting thread
308 * up again with the result. This new thread will have the scheduler
309 * settings inherited from this thread and in extension of the thread
310 * that created the first non-exclusive thread-pool. */
311 spawn_thread_data = g_async_queue_pop_unlocked (spawn_thread_queue);
312 thread = g_thread_try_new (name, g_thread_pool_thread_proxy, spawn_thread_data->pool, &error);
314 spawn_thread_data->thread = g_steal_pointer (&thread);
315 spawn_thread_data->error = g_steal_pointer (&error);
317 g_cond_broadcast (&spawn_thread_cond);
318 g_async_queue_unlock (spawn_thread_queue);
325 g_thread_pool_thread_proxy (gpointer data)
327 GRealThreadPool *pool;
331 DEBUG_MSG (("thread %p started for pool %p.", g_thread_self (), pool));
333 g_async_queue_lock (pool->queue);
339 task = g_thread_pool_wait_for_new_task (pool);
342 if (pool->running || !pool->immediate)
344 /* A task was received and the thread pool is active,
345 * so execute the function.
347 g_async_queue_unlock (pool->queue);
348 DEBUG_MSG (("thread %p in pool %p calling func.",
349 g_thread_self (), pool));
350 pool->pool.func (task, pool->pool.user_data);
351 g_async_queue_lock (pool->queue);
356 /* No task was received, so this thread goes to the global pool. */
357 gboolean free_pool = FALSE;
359 DEBUG_MSG (("thread %p leaving pool %p for global pool.",
360 g_thread_self (), pool));
367 if (pool->num_threads == 0)
369 /* If the pool is not running and no other
370 * thread is waiting for this thread pool to
371 * finish and this is the last thread of this
372 * pool, free the pool.
378 /* If the pool is not running and no other
379 * thread is waiting for this thread pool to
380 * finish and this is not the last thread of
381 * this pool and there are no tasks left in the
382 * queue, wakeup the remaining threads.
384 if (g_async_queue_length_unlocked (pool->queue) ==
385 (gint) -pool->num_threads)
386 g_thread_pool_wakeup_and_stop_all (pool);
389 else if (pool->immediate ||
390 g_async_queue_length_unlocked (pool->queue) <= 0)
392 /* If the pool is not running and another thread is
393 * waiting for this thread pool to finish and there
394 * are either no tasks left or the pool shall stop
395 * immediately, inform the waiting thread of a change
396 * of the thread pool state.
398 g_cond_broadcast (&pool->cond);
402 g_atomic_int_inc (&unused_threads);
403 g_async_queue_unlock (pool->queue);
406 g_thread_pool_free_internal (pool);
408 pool = g_thread_pool_wait_for_new_pool ();
409 g_atomic_int_add (&unused_threads, -1);
414 g_async_queue_lock (pool->queue);
416 DEBUG_MSG (("thread %p entering pool %p from global pool.",
417 g_thread_self (), pool));
419 /* pool->num_threads++ is not done here, but in
420 * g_thread_pool_start_thread to make the new started
421 * thread known to the pool before itself can do it.
430 g_thread_pool_start_thread (GRealThreadPool *pool,
433 gboolean success = FALSE;
435 if (pool->max_threads != -1 && pool->num_threads >= (guint) pool->max_threads)
436 /* Enough threads are already running */
439 g_async_queue_lock (unused_thread_queue);
441 if (g_async_queue_length_unlocked (unused_thread_queue) < 0)
443 g_async_queue_push_unlocked (unused_thread_queue, pool);
447 g_async_queue_unlock (unused_thread_queue);
451 const gchar *prgname = g_get_prgname ();
452 gchar name[16] = "pool";
456 g_snprintf (name, sizeof (name), "pool-%s", prgname);
458 /* No thread was found, we have to start a new one */
459 if (pool->pool.exclusive)
461 /* For exclusive thread-pools this is directly called from new() and
462 * we simply start new threads that inherit the scheduler settings
463 * from the current thread.
465 thread = g_thread_try_new (name, g_thread_pool_thread_proxy, pool, error);
469 /* For non-exclusive thread-pools this can be called at any time
470 * when a new thread is needed. We make sure to create a new thread
471 * here with the correct scheduler settings by going via our helper
474 SpawnThreadData spawn_thread_data = { (GThreadPool *) pool, NULL, NULL };
476 g_async_queue_lock (spawn_thread_queue);
478 g_async_queue_push_unlocked (spawn_thread_queue, &spawn_thread_data);
480 while (!spawn_thread_data.thread && !spawn_thread_data.error)
481 g_cond_wait (&spawn_thread_cond, _g_async_queue_get_mutex (spawn_thread_queue));
483 thread = spawn_thread_data.thread;
485 g_propagate_error (error, g_steal_pointer (&spawn_thread_data.error));
486 g_async_queue_unlock (spawn_thread_queue);
492 g_thread_unref (thread);
495 /* See comment in g_thread_pool_thread_proxy as to why this is done
505 * @func: a function to execute in the threads of the new thread pool
506 * @user_data: user data that is handed over to @func every time it
508 * @max_threads: the maximal number of threads to execute concurrently
509 * in the new thread pool, -1 means no limit
510 * @exclusive: should this thread pool be exclusive?
511 * @error: return location for error, or %NULL
513 * This function creates a new thread pool.
515 * Whenever you call g_thread_pool_push(), either a new thread is
516 * created or an unused one is reused. At most @max_threads threads
517 * are running concurrently for this thread pool. @max_threads = -1
518 * allows unlimited threads to be created for this thread pool. The
519 * newly created or reused thread now executes the function @func
520 * with the two arguments. The first one is the parameter to
521 * g_thread_pool_push() and the second one is @user_data.
523 * Pass g_get_num_processors() to @max_threads to create as many threads as
524 * there are logical processors on the system. This will not pin each thread to
525 * a specific processor.
527 * The parameter @exclusive determines whether the thread pool owns
528 * all threads exclusive or shares them with other thread pools.
529 * If @exclusive is %TRUE, @max_threads threads are started
530 * immediately and they will run exclusively for this thread pool
531 * until it is destroyed by g_thread_pool_free(). If @exclusive is
532 * %FALSE, threads are created when needed and shared between all
533 * non-exclusive thread pools. This implies that @max_threads may
534 * not be -1 for exclusive thread pools. Besides, exclusive thread
535 * pools are not affected by g_thread_pool_set_max_idle_time()
536 * since their threads are never considered idle and returned to the
539 * Note that the threads used by exclusive thread pools will all inherit the
540 * scheduler settings of the current thread while the threads used by
541 * non-exclusive thread pools will inherit the scheduler settings from the
542 * first thread that created such a thread pool.
544 * At least one thread will be spawned when this function is called, either to
545 * create the @max_threads exclusive threads, or to preserve the scheduler
546 * settings of the current thread for future spawns.
548 * @error can be %NULL to ignore errors, or non-%NULL to report
549 * errors. An error can only occur when @exclusive is set to %TRUE
550 * and not all @max_threads threads could be created.
551 * See #GThreadError for possible errors that may occur.
552 * Note, even in case of error a valid #GThreadPool is returned.
554 * Returns: the new #GThreadPool
557 g_thread_pool_new (GFunc func,
563 return g_thread_pool_new_full (func, user_data, NULL, max_threads, exclusive, error);
567 * g_thread_pool_new_full:
568 * @func: a function to execute in the threads of the new thread pool
569 * @user_data: user data that is handed over to @func every time it
571 * @item_free_func: (nullable): used to pass as a free function to
572 * g_async_queue_new_full()
573 * @max_threads: the maximal number of threads to execute concurrently
574 * in the new thread pool, `-1` means no limit
575 * @exclusive: should this thread pool be exclusive?
576 * @error: return location for error, or %NULL
578 * This function creates a new thread pool similar to g_thread_pool_new()
579 * but allowing @item_free_func to be specified to free the data passed
580 * to g_thread_pool_push() in the case that the #GThreadPool is stopped
581 * and freed before all tasks have been executed.
583 * @item_free_func will *not* be called on items successfully passed to @func.
584 * @func is responsible for freeing the items passed to it.
586 * Returns: (transfer full): the new #GThreadPool
591 g_thread_pool_new_full (GFunc func,
593 GDestroyNotify item_free_func,
598 GRealThreadPool *retval;
599 G_LOCK_DEFINE_STATIC (init);
601 g_return_val_if_fail (func, NULL);
602 g_return_val_if_fail (!exclusive || max_threads != -1, NULL);
603 g_return_val_if_fail (max_threads >= -1, NULL);
605 retval = g_new (GRealThreadPool, 1);
607 retval->pool.func = func;
608 retval->pool.user_data = user_data;
609 retval->pool.exclusive = exclusive;
610 retval->queue = g_async_queue_new_full (item_free_func);
611 g_cond_init (&retval->cond);
612 retval->max_threads = max_threads;
613 retval->num_threads = 0;
614 retval->running = TRUE;
615 retval->immediate = FALSE;
616 retval->waiting = FALSE;
617 retval->sort_func = NULL;
618 retval->sort_user_data = NULL;
621 if (!unused_thread_queue)
622 unused_thread_queue = g_async_queue_new ();
625 * Spawn a helper thread that is only responsible for spawning new threads
626 * with the scheduler settings of the current thread.
628 * This is then used for making sure that all threads created on the
629 * non-exclusive thread-pool have the same scheduler settings, and more
630 * importantly don't just inherit them from the thread that just happened to
631 * push a new task and caused a new thread to be created.
633 * Not doing so could cause real-time priority threads or otherwise
634 * threads with problematic scheduler settings to be part of the
635 * non-exclusive thread-pools.
637 * For exclusive thread-pools this is not required as all threads are
638 * created immediately below and are running forever, so they will
639 * automatically inherit the scheduler settings from this very thread.
641 if (!exclusive && !spawn_thread_queue)
643 GThread *pool_spawner = NULL;
645 spawn_thread_queue = g_async_queue_new ();
646 g_cond_init (&spawn_thread_cond);
647 pool_spawner = g_thread_new ("pool-spawner", g_thread_pool_spawn_thread, NULL);
648 g_ignore_leak (pool_spawner);
652 if (retval->pool.exclusive)
654 g_async_queue_lock (retval->queue);
656 while (retval->num_threads < (guint) retval->max_threads)
658 GError *local_error = NULL;
660 if (!g_thread_pool_start_thread (retval, &local_error))
662 g_propagate_error (error, local_error);
667 g_async_queue_unlock (retval->queue);
670 return (GThreadPool*) retval;
674 * g_thread_pool_push:
675 * @pool: a #GThreadPool
676 * @data: a new task for @pool
677 * @error: return location for error, or %NULL
679 * Inserts @data into the list of tasks to be executed by @pool.
681 * When the number of currently running threads is lower than the
682 * maximal allowed number of threads, a new thread is started (or
683 * reused) with the properties given to g_thread_pool_new().
684 * Otherwise, @data stays in the queue until a thread in this pool
685 * finishes its previous task and processes @data.
687 * @error can be %NULL to ignore errors, or non-%NULL to report
688 * errors. An error can only occur when a new thread couldn't be
689 * created. In that case @data is simply appended to the queue of
692 * Before version 2.32, this function did not return a success status.
694 * Returns: %TRUE on success, %FALSE if an error occurred
697 g_thread_pool_push (GThreadPool *pool,
701 GRealThreadPool *real;
704 real = (GRealThreadPool*) pool;
706 g_return_val_if_fail (real, FALSE);
707 g_return_val_if_fail (real->running, FALSE);
711 g_async_queue_lock (real->queue);
713 if (g_async_queue_length_unlocked (real->queue) >= 0)
715 /* No thread is waiting in the queue */
716 GError *local_error = NULL;
718 if (!g_thread_pool_start_thread (real, &local_error))
720 g_propagate_error (error, local_error);
725 g_thread_pool_queue_push_unlocked (real, data);
726 g_async_queue_unlock (real->queue);
732 * g_thread_pool_set_max_threads:
733 * @pool: a #GThreadPool
734 * @max_threads: a new maximal number of threads for @pool,
735 * or -1 for unlimited
736 * @error: return location for error, or %NULL
738 * Sets the maximal allowed number of threads for @pool.
739 * A value of -1 means that the maximal number of threads
740 * is unlimited. If @pool is an exclusive thread pool, setting
741 * the maximal number of threads to -1 is not allowed.
743 * Setting @max_threads to 0 means stopping all work for @pool.
744 * It is effectively frozen until @max_threads is set to a non-zero
747 * A thread is never terminated while calling @func, as supplied by
748 * g_thread_pool_new(). Instead the maximal number of threads only
749 * has effect for the allocation of new threads in g_thread_pool_push().
750 * A new thread is allocated, whenever the number of currently
751 * running threads in @pool is smaller than the maximal number.
753 * @error can be %NULL to ignore errors, or non-%NULL to report
754 * errors. An error can only occur when a new thread couldn't be
757 * Before version 2.32, this function did not return a success status.
759 * Returns: %TRUE on success, %FALSE if an error occurred
762 g_thread_pool_set_max_threads (GThreadPool *pool,
766 GRealThreadPool *real;
770 real = (GRealThreadPool*) pool;
772 g_return_val_if_fail (real, FALSE);
773 g_return_val_if_fail (real->running, FALSE);
774 g_return_val_if_fail (!real->pool.exclusive || max_threads != -1, FALSE);
775 g_return_val_if_fail (max_threads >= -1, FALSE);
779 g_async_queue_lock (real->queue);
781 real->max_threads = max_threads;
784 to_start = real->max_threads - real->num_threads;
786 to_start = g_async_queue_length_unlocked (real->queue);
788 for ( ; to_start > 0; to_start--)
790 GError *local_error = NULL;
792 if (!g_thread_pool_start_thread (real, &local_error))
794 g_propagate_error (error, local_error);
800 g_async_queue_unlock (real->queue);
806 * g_thread_pool_get_max_threads:
807 * @pool: a #GThreadPool
809 * Returns the maximal number of threads for @pool.
811 * Returns: the maximal number of threads
814 g_thread_pool_get_max_threads (GThreadPool *pool)
816 GRealThreadPool *real;
819 real = (GRealThreadPool*) pool;
821 g_return_val_if_fail (real, 0);
822 g_return_val_if_fail (real->running, 0);
824 g_async_queue_lock (real->queue);
825 retval = real->max_threads;
826 g_async_queue_unlock (real->queue);
832 * g_thread_pool_get_num_threads:
833 * @pool: a #GThreadPool
835 * Returns the number of threads currently running in @pool.
837 * Returns: the number of threads currently running
840 g_thread_pool_get_num_threads (GThreadPool *pool)
842 GRealThreadPool *real;
845 real = (GRealThreadPool*) pool;
847 g_return_val_if_fail (real, 0);
848 g_return_val_if_fail (real->running, 0);
850 g_async_queue_lock (real->queue);
851 retval = real->num_threads;
852 g_async_queue_unlock (real->queue);
858 * g_thread_pool_unprocessed:
859 * @pool: a #GThreadPool
861 * Returns the number of tasks still unprocessed in @pool.
863 * Returns: the number of unprocessed tasks
866 g_thread_pool_unprocessed (GThreadPool *pool)
868 GRealThreadPool *real;
871 real = (GRealThreadPool*) pool;
873 g_return_val_if_fail (real, 0);
874 g_return_val_if_fail (real->running, 0);
876 unprocessed = g_async_queue_length (real->queue);
878 return MAX (unprocessed, 0);
882 * g_thread_pool_free:
883 * @pool: a #GThreadPool
884 * @immediate: should @pool shut down immediately?
885 * @wait_: should the function wait for all tasks to be finished?
887 * Frees all resources allocated for @pool.
889 * If @immediate is %TRUE, no new task is processed for @pool.
890 * Otherwise @pool is not freed before the last task is processed.
891 * Note however, that no thread of this pool is interrupted while
892 * processing a task. Instead at least all still running threads
893 * can finish their tasks before the @pool is freed.
895 * If @wait_ is %TRUE, this function does not return before all
896 * tasks to be processed (dependent on @immediate, whether all
897 * or only the currently running) are ready.
898 * Otherwise this function returns immediately.
900 * After calling this function @pool must not be used anymore.
903 g_thread_pool_free (GThreadPool *pool,
907 GRealThreadPool *real;
909 real = (GRealThreadPool*) pool;
911 g_return_if_fail (real);
912 g_return_if_fail (real->running);
914 /* If there's no thread allowed here, there is not much sense in
915 * not stopping this pool immediately, when it's not empty
917 g_return_if_fail (immediate ||
918 real->max_threads != 0 ||
919 g_async_queue_length (real->queue) == 0);
921 g_async_queue_lock (real->queue);
923 real->running = FALSE;
924 real->immediate = immediate;
925 real->waiting = wait_;
929 while (g_async_queue_length_unlocked (real->queue) != (gint) -real->num_threads &&
930 !(immediate && real->num_threads == 0))
931 g_cond_wait (&real->cond, _g_async_queue_get_mutex (real->queue));
934 if (immediate || g_async_queue_length_unlocked (real->queue) == (gint) -real->num_threads)
936 /* No thread is currently doing something (and nothing is left
937 * to process in the queue)
939 if (real->num_threads == 0)
941 /* No threads left, we clean up */
942 g_async_queue_unlock (real->queue);
943 g_thread_pool_free_internal (real);
947 g_thread_pool_wakeup_and_stop_all (real);
950 /* The last thread should cleanup the pool */
951 real->waiting = FALSE;
952 g_async_queue_unlock (real->queue);
956 g_thread_pool_free_internal (GRealThreadPool* pool)
958 g_return_if_fail (pool);
959 g_return_if_fail (pool->running == FALSE);
960 g_return_if_fail (pool->num_threads == 0);
962 /* Ensure the dummy item pushed on by g_thread_pool_wakeup_and_stop_all() is
963 * removed, before it’s potentially passed to the user-provided
964 * @item_free_func. */
965 g_async_queue_remove (pool->queue, GUINT_TO_POINTER (1));
967 g_async_queue_unref (pool->queue);
968 g_cond_clear (&pool->cond);
974 g_thread_pool_wakeup_and_stop_all (GRealThreadPool *pool)
978 g_return_if_fail (pool);
979 g_return_if_fail (pool->running == FALSE);
980 g_return_if_fail (pool->num_threads != 0);
982 pool->immediate = TRUE;
985 * So here we're sending bogus data to the pool threads, which
986 * should cause them each to wake up, and check the above
987 * pool->immediate condition. However we don't want that
988 * data to be sorted (since it'll crash the sorter).
990 for (i = 0; i < pool->num_threads; i++)
991 g_async_queue_push_unlocked (pool->queue, GUINT_TO_POINTER (1));
995 * g_thread_pool_set_max_unused_threads:
996 * @max_threads: maximal number of unused threads
998 * Sets the maximal number of unused threads to @max_threads.
999 * If @max_threads is -1, no limit is imposed on the number
1000 * of unused threads.
1002 * The default value is 2.
1005 g_thread_pool_set_max_unused_threads (gint max_threads)
1007 g_return_if_fail (max_threads >= -1);
1009 g_atomic_int_set (&max_unused_threads, max_threads);
1011 if (max_threads != -1)
1013 max_threads -= g_atomic_int_get (&unused_threads);
1014 if (max_threads < 0)
1016 g_atomic_int_set (&kill_unused_threads, -max_threads);
1017 g_atomic_int_inc (&wakeup_thread_serial);
1019 g_async_queue_lock (unused_thread_queue);
1023 g_async_queue_push_unlocked (unused_thread_queue,
1024 wakeup_thread_marker);
1026 while (++max_threads);
1028 g_async_queue_unlock (unused_thread_queue);
1034 * g_thread_pool_get_max_unused_threads:
1036 * Returns the maximal allowed number of unused threads.
1038 * Returns: the maximal number of unused threads
1041 g_thread_pool_get_max_unused_threads (void)
1043 return g_atomic_int_get (&max_unused_threads);
1047 * g_thread_pool_get_num_unused_threads:
1049 * Returns the number of currently unused threads.
1051 * Returns: the number of currently unused threads
1054 g_thread_pool_get_num_unused_threads (void)
1056 return (guint) g_atomic_int_get (&unused_threads);
1060 * g_thread_pool_stop_unused_threads:
1062 * Stops all currently unused threads. This does not change the
1063 * maximal number of unused threads. This function can be used to
1064 * regularly stop all unused threads e.g. from g_timeout_add().
1067 g_thread_pool_stop_unused_threads (void)
1071 oldval = g_thread_pool_get_max_unused_threads ();
1073 g_thread_pool_set_max_unused_threads (0);
1074 g_thread_pool_set_max_unused_threads (oldval);
1078 * g_thread_pool_set_sort_function:
1079 * @pool: a #GThreadPool
1080 * @func: the #GCompareDataFunc used to sort the list of tasks.
1081 * This function is passed two tasks. It should return
1082 * 0 if the order in which they are handled does not matter,
1083 * a negative value if the first task should be processed before
1084 * the second or a positive value if the second task should be
1086 * @user_data: user data passed to @func
1088 * Sets the function used to sort the list of tasks. This allows the
1089 * tasks to be processed by a priority determined by @func, and not
1090 * just in the order in which they were added to the pool.
1092 * Note, if the maximum number of threads is more than 1, the order
1093 * that threads are executed cannot be guaranteed 100%. Threads are
1094 * scheduled by the operating system and are executed at random. It
1095 * cannot be assumed that threads are executed in the order they are
1101 g_thread_pool_set_sort_function (GThreadPool *pool,
1102 GCompareDataFunc func,
1105 GRealThreadPool *real;
1107 real = (GRealThreadPool*) pool;
1109 g_return_if_fail (real);
1110 g_return_if_fail (real->running);
1112 g_async_queue_lock (real->queue);
1114 real->sort_func = func;
1115 real->sort_user_data = user_data;
1118 g_async_queue_sort_unlocked (real->queue,
1120 real->sort_user_data);
1122 g_async_queue_unlock (real->queue);
1126 * g_thread_pool_move_to_front:
1127 * @pool: a #GThreadPool
1128 * @data: an unprocessed item in the pool
1130 * Moves the item to the front of the queue of unprocessed
1131 * items, so that it will be processed next.
1133 * Returns: %TRUE if the item was found and moved
1138 g_thread_pool_move_to_front (GThreadPool *pool,
1141 GRealThreadPool *real = (GRealThreadPool*) pool;
1144 g_async_queue_lock (real->queue);
1146 found = g_async_queue_remove_unlocked (real->queue, data);
1148 g_async_queue_push_front_unlocked (real->queue, data);
1150 g_async_queue_unlock (real->queue);
1156 * g_thread_pool_set_max_idle_time:
1157 * @interval: the maximum @interval (in milliseconds)
1158 * a thread can be idle
1160 * This function will set the maximum @interval that a thread
1161 * waiting in the pool for new tasks can be idle for before
1162 * being stopped. This function is similar to calling
1163 * g_thread_pool_stop_unused_threads() on a regular timeout,
1164 * except this is done on a per thread basis.
1166 * By setting @interval to 0, idle threads will not be stopped.
1168 * The default value is 15000 (15 seconds).
1173 g_thread_pool_set_max_idle_time (guint interval)
1177 g_atomic_int_set (&max_idle_time, interval);
1179 i = (guint) g_atomic_int_get (&unused_threads);
1182 g_atomic_int_inc (&wakeup_thread_serial);
1183 g_async_queue_lock (unused_thread_queue);
1187 g_async_queue_push_unlocked (unused_thread_queue,
1188 wakeup_thread_marker);
1192 g_async_queue_unlock (unused_thread_queue);
1197 * g_thread_pool_get_max_idle_time:
1199 * This function will return the maximum @interval that a
1200 * thread will wait in the thread pool for new tasks before
1203 * If this function returns 0, threads waiting in the thread
1204 * pool for new work are not stopped.
1206 * Returns: the maximum @interval (milliseconds) to wait
1207 * for new tasks in the thread pool before stopping the
1213 g_thread_pool_get_max_idle_time (void)
1215 return (guint) g_atomic_int_get (&max_idle_time);