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 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
29 #include "gthreadpool.h"
31 #include "gasyncqueue.h"
32 #include "gasyncqueueprivate.h"
34 #include "gtestutils.h"
38 * SECTION:thread_pools
39 * @title: Thread Pools
40 * @short_description: pools of threads to execute work concurrently
43 * Sometimes you wish to asynchronously fork out the execution of work
44 * and continue working in your own thread. If that will happen often,
45 * the overhead of starting and destroying a thread each time might be
46 * too high. In such cases reusing already started threads seems like a
47 * good idea. And it indeed is, but implementing this can be tedious
50 * Therefore GLib provides thread pools for your convenience. An added
51 * advantage is, that the threads can be shared between the different
52 * subsystems of your program, when they are using GLib.
54 * To create a new thread pool, you use g_thread_pool_new().
55 * It is destroyed by g_thread_pool_free().
57 * If you want to execute a certain task within a thread pool,
58 * you call g_thread_pool_push().
60 * To get the current number of running threads you call
61 * g_thread_pool_get_num_threads(). To get the number of still
62 * unprocessed tasks you call g_thread_pool_unprocessed(). To control
63 * the maximal number of threads for a thread pool, you use
64 * g_thread_pool_get_max_threads() and g_thread_pool_set_max_threads().
66 * Finally you can control the number of unused threads, that are kept
67 * alive by GLib for future use. The current number can be fetched with
68 * g_thread_pool_get_num_unused_threads(). The maximal number can be
69 * controlled by g_thread_pool_get_max_unused_threads() and
70 * g_thread_pool_set_max_unused_threads(). All currently unused threads
71 * can be stopped by calling g_thread_pool_stop_unused_threads().
75 /* #define DEBUG_MSG(args) g_printerr args ; g_printerr ("\n"); */
77 typedef struct _GRealThreadPool GRealThreadPool;
81 * @func: the function to execute in the threads of this pool
82 * @user_data: the user data for the threads of this pool
83 * @exclusive: are all threads exclusive to this pool
85 * The #GThreadPool struct represents a thread pool. It has three
86 * public read-only members, but the underlying struct is bigger,
87 * so you must not copy this struct.
89 struct _GRealThreadPool
99 GCompareDataFunc sort_func;
100 gpointer sort_user_data;
103 /* The following is just an address to mark the wakeup order for a
104 * thread, it could be any address (as long, as it isn't a valid
105 * GThreadPool address)
107 static const gpointer wakeup_thread_marker = (gpointer) &g_thread_pool_new;
108 static gint wakeup_thread_serial = 0;
110 /* Here all unused threads are waiting */
111 static GAsyncQueue *unused_thread_queue = NULL;
112 static gint unused_threads = 0;
113 static gint max_unused_threads = 0;
114 static gint kill_unused_threads = 0;
115 static guint max_idle_time = 0;
117 static void g_thread_pool_queue_push_unlocked (GRealThreadPool *pool,
119 static void g_thread_pool_free_internal (GRealThreadPool *pool);
120 static gpointer g_thread_pool_thread_proxy (gpointer data);
121 static gboolean g_thread_pool_start_thread (GRealThreadPool *pool,
123 static void g_thread_pool_wakeup_and_stop_all (GRealThreadPool *pool);
124 static GRealThreadPool* g_thread_pool_wait_for_new_pool (void);
125 static gpointer g_thread_pool_wait_for_new_task (GRealThreadPool *pool);
128 g_thread_pool_queue_push_unlocked (GRealThreadPool *pool,
132 g_async_queue_push_sorted_unlocked (pool->queue,
135 pool->sort_user_data);
137 g_async_queue_push_unlocked (pool->queue, data);
140 static GRealThreadPool*
141 g_thread_pool_wait_for_new_pool (void)
143 GRealThreadPool *pool;
144 gint local_wakeup_thread_serial;
145 guint local_max_unused_threads;
146 gint local_max_idle_time;
147 gint last_wakeup_thread_serial;
148 gboolean have_relayed_thread_marker = FALSE;
150 local_max_unused_threads = g_atomic_int_get (&max_unused_threads);
151 local_max_idle_time = g_atomic_int_get (&max_idle_time);
152 last_wakeup_thread_serial = g_atomic_int_get (&wakeup_thread_serial);
154 g_atomic_int_inc (&unused_threads);
158 if (g_atomic_int_get (&unused_threads) >= local_max_unused_threads)
160 /* If this is a superfluous thread, stop it. */
163 else if (local_max_idle_time > 0)
165 /* If a maximal idle time is given, wait for the given time. */
168 g_get_current_time (&end_time);
169 g_time_val_add (&end_time, local_max_idle_time * 1000);
171 DEBUG_MSG (("thread %p waiting in global pool for %f seconds.",
172 g_thread_self (), local_max_idle_time / 1000.0));
174 pool = g_async_queue_timed_pop (unused_thread_queue, &end_time);
178 /* If no maximal idle time is given, wait indefinitely. */
179 DEBUG_MSG (("thread %p waiting in global pool.", g_thread_self ()));
180 pool = g_async_queue_pop (unused_thread_queue);
183 if (pool == wakeup_thread_marker)
185 local_wakeup_thread_serial = g_atomic_int_get (&wakeup_thread_serial);
186 if (last_wakeup_thread_serial == local_wakeup_thread_serial)
188 if (!have_relayed_thread_marker)
190 /* If this wakeup marker has been received for
191 * the second time, relay it.
193 DEBUG_MSG (("thread %p relaying wakeup message to "
194 "waiting thread with lower serial.",
197 g_async_queue_push (unused_thread_queue, wakeup_thread_marker);
198 have_relayed_thread_marker = TRUE;
200 /* If a wakeup marker has been relayed, this thread
201 * will get out of the way for 100 microseconds to
202 * avoid receiving this marker again.
209 if (g_atomic_int_add (&kill_unused_threads, -1) > 0)
215 DEBUG_MSG (("thread %p updating to new limits.",
218 local_max_unused_threads = g_atomic_int_get (&max_unused_threads);
219 local_max_idle_time = g_atomic_int_get (&max_idle_time);
220 last_wakeup_thread_serial = local_wakeup_thread_serial;
222 have_relayed_thread_marker = FALSE;
226 while (pool == wakeup_thread_marker);
228 g_atomic_int_add (&unused_threads, -1);
234 g_thread_pool_wait_for_new_task (GRealThreadPool *pool)
236 gpointer task = NULL;
238 if (pool->running || (!pool->immediate &&
239 g_async_queue_length_unlocked (pool->queue) > 0))
241 /* This thread pool is still active. */
242 if (pool->num_threads > pool->max_threads && pool->max_threads != -1)
244 /* This is a superfluous thread, so it goes to the global pool. */
245 DEBUG_MSG (("superfluous thread %p in pool %p.",
246 g_thread_self (), pool));
248 else if (pool->pool.exclusive)
250 /* Exclusive threads stay attached to the pool. */
251 task = g_async_queue_pop_unlocked (pool->queue);
253 DEBUG_MSG (("thread %p in exclusive pool %p waits for task "
254 "(%d running, %d unprocessed).",
255 g_thread_self (), pool, pool->num_threads,
256 g_async_queue_length_unlocked (pool->queue)));
260 /* A thread will wait for new tasks for at most 1/2
261 * second before going to the global pool.
265 g_get_current_time (&end_time);
266 g_time_val_add (&end_time, G_USEC_PER_SEC / 2); /* 1/2 second */
268 DEBUG_MSG (("thread %p in pool %p waits for up to a 1/2 second for task "
269 "(%d running, %d unprocessed).",
270 g_thread_self (), pool, pool->num_threads,
271 g_async_queue_length_unlocked (pool->queue)));
273 task = g_async_queue_timed_pop_unlocked (pool->queue, &end_time);
278 /* This thread pool is inactive, it will no longer process tasks. */
279 DEBUG_MSG (("pool %p not active, thread %p will go to global pool "
280 "(running: %s, immediate: %s, len: %d).",
281 pool, g_thread_self (),
282 pool->running ? "true" : "false",
283 pool->immediate ? "true" : "false",
284 g_async_queue_length_unlocked (pool->queue)));
292 g_thread_pool_thread_proxy (gpointer data)
294 GRealThreadPool *pool;
298 DEBUG_MSG (("thread %p started for pool %p.", g_thread_self (), pool));
300 g_async_queue_lock (pool->queue);
306 task = g_thread_pool_wait_for_new_task (pool);
309 if (pool->running || !pool->immediate)
311 /* A task was received and the thread pool is active,
312 * so execute the function.
314 g_async_queue_unlock (pool->queue);
315 DEBUG_MSG (("thread %p in pool %p calling func.",
316 g_thread_self (), pool));
317 pool->pool.func (task, pool->pool.user_data);
318 g_async_queue_lock (pool->queue);
323 /* No task was received, so this thread goes to the global pool. */
324 gboolean free_pool = FALSE;
326 DEBUG_MSG (("thread %p leaving pool %p for global pool.",
327 g_thread_self (), pool));
334 if (pool->num_threads == 0)
336 /* If the pool is not running and no other
337 * thread is waiting for this thread pool to
338 * finish and this is the last thread of this
339 * pool, free the pool.
345 /* If the pool is not running and no other
346 * thread is waiting for this thread pool to
347 * finish and this is not the last thread of
348 * this pool and there are no tasks left in the
349 * queue, wakeup the remaining threads.
351 if (g_async_queue_length_unlocked (pool->queue) ==
353 g_thread_pool_wakeup_and_stop_all (pool);
356 else if (pool->immediate ||
357 g_async_queue_length_unlocked (pool->queue) <= 0)
359 /* If the pool is not running and another thread is
360 * waiting for this thread pool to finish and there
361 * are either no tasks left or the pool shall stop
362 * immediately, inform the waiting thread of a change
363 * of the thread pool state.
365 g_cond_broadcast (&pool->cond);
369 g_async_queue_unlock (pool->queue);
372 g_thread_pool_free_internal (pool);
374 if ((pool = g_thread_pool_wait_for_new_pool ()) == NULL)
377 g_async_queue_lock (pool->queue);
379 DEBUG_MSG (("thread %p entering pool %p from global pool.",
380 g_thread_self (), pool));
382 /* pool->num_threads++ is not done here, but in
383 * g_thread_pool_start_thread to make the new started
384 * thread known to the pool before itself can do it.
393 g_thread_pool_start_thread (GRealThreadPool *pool,
396 gboolean success = FALSE;
398 if (pool->num_threads >= pool->max_threads && pool->max_threads != -1)
399 /* Enough threads are already running */
402 g_async_queue_lock (unused_thread_queue);
404 if (g_async_queue_length_unlocked (unused_thread_queue) < 0)
406 g_async_queue_push_unlocked (unused_thread_queue, pool);
410 g_async_queue_unlock (unused_thread_queue);
414 GError *local_error = NULL;
416 /* No thread was found, we have to start a new one */
417 if (!g_thread_new ("pool", g_thread_pool_thread_proxy, pool, FALSE, &local_error))
419 g_propagate_error (error, local_error);
424 /* See comment in g_thread_pool_thread_proxy as to why this is done
434 * @func: a function to execute in the threads of the new thread pool
435 * @user_data: user data that is handed over to @func every time it
437 * @max_threads: the maximal number of threads to execute concurrently
438 * in the new thread pool, -1 means no limit
439 * @exclusive: should this thread pool be exclusive?
440 * @error: return location for error, or %NULL
442 * This function creates a new thread pool.
444 * Whenever you call g_thread_pool_push(), either a new thread is
445 * created or an unused one is reused. At most @max_threads threads
446 * are running concurrently for this thread pool. @max_threads = -1
447 * allows unlimited threads to be created for this thread pool. The
448 * newly created or reused thread now executes the function @func
449 * with the two arguments. The first one is the parameter to
450 * g_thread_pool_push() and the second one is @user_data.
452 * The parameter @exclusive determines whether the thread pool owns
453 * all threads exclusive or shares them with other thread pools.
454 * If @exclusive is %TRUE, @max_threads threads are started
455 * immediately and they will run exclusively for this thread pool
456 * until it is destroyed by g_thread_pool_free(). If @exclusive is
457 * %FALSE, threads are created when needed and shared between all
458 * non-exclusive thread pools. This implies that @max_threads may
459 * not be -1 for exclusive thread pools.
461 * @error can be %NULL to ignore errors, or non-%NULL to report
462 * errors. An error can only occur when @exclusive is set to %TRUE
463 * and not all @max_threads threads could be created.
465 * Return value: the new #GThreadPool
468 g_thread_pool_new (GFunc func,
474 GRealThreadPool *retval;
475 G_LOCK_DEFINE_STATIC (init);
477 g_return_val_if_fail (func, NULL);
478 g_return_val_if_fail (!exclusive || max_threads != -1, NULL);
479 g_return_val_if_fail (max_threads >= -1, NULL);
481 retval = g_new (GRealThreadPool, 1);
483 retval->pool.func = func;
484 retval->pool.user_data = user_data;
485 retval->pool.exclusive = exclusive;
486 retval->queue = g_async_queue_new ();
487 g_cond_init (&retval->cond);
488 retval->max_threads = max_threads;
489 retval->num_threads = 0;
490 retval->running = TRUE;
491 retval->immediate = FALSE;
492 retval->waiting = FALSE;
493 retval->sort_func = NULL;
494 retval->sort_user_data = NULL;
497 if (!unused_thread_queue)
498 unused_thread_queue = g_async_queue_new ();
501 if (retval->pool.exclusive)
503 g_async_queue_lock (retval->queue);
505 while (retval->num_threads < retval->max_threads)
507 GError *local_error = NULL;
509 if (!g_thread_pool_start_thread (retval, &local_error))
511 g_propagate_error (error, local_error);
516 g_async_queue_unlock (retval->queue);
519 return (GThreadPool*) retval;
523 * g_thread_pool_push:
524 * @pool: a #GThreadPool
525 * @data: a new task for @pool
526 * @error: return location for error, or %NULL
528 * Inserts @data into the list of tasks to be executed by @pool.
530 * When the number of currently running threads is lower than the
531 * maximal allowed number of threads, a new thread is started (or
532 * reused) with the properties given to g_thread_pool_new().
533 * Otherwise, @data stays in the queue until a thread in this pool
534 * finishes its previous task and processes @data.
536 * @error can be %NULL to ignore errors, or non-%NULL to report
537 * errors. An error can only occur when a new thread couldn't be
538 * created. In that case @data is simply appended to the queue of
541 * Before version 2.32, this function did not return a success status.
543 * Return value: %TRUE on success, %FALSE if an error occurred
546 g_thread_pool_push (GThreadPool *pool,
550 GRealThreadPool *real;
553 real = (GRealThreadPool*) pool;
555 g_return_val_if_fail (real, FALSE);
556 g_return_val_if_fail (real->running, FALSE);
560 g_async_queue_lock (real->queue);
562 if (g_async_queue_length_unlocked (real->queue) >= 0)
564 /* No thread is waiting in the queue */
565 GError *local_error = NULL;
567 if (!g_thread_pool_start_thread (real, &local_error))
569 g_propagate_error (error, local_error);
574 g_thread_pool_queue_push_unlocked (real, data);
575 g_async_queue_unlock (real->queue);
581 * g_thread_pool_set_max_threads:
582 * @pool: a #GThreadPool
583 * @max_threads: a new maximal number of threads for @pool,
584 * or -1 for unlimited
585 * @error: return location for error, or %NULL
587 * Sets the maximal allowed number of threads for @pool.
588 * A value of -1 means that the maximal number of threads
589 * is unlimited. If @pool is an exclusive thread pool, setting
590 * the maximal number of threads to -1 is not allowed.
592 * Setting @max_threads to 0 means stopping all work for @pool.
593 * It is effectively frozen until @max_threads is set to a non-zero
596 * A thread is never terminated while calling @func, as supplied by
597 * g_thread_pool_new(). Instead the maximal number of threads only
598 * has effect for the allocation of new threads in g_thread_pool_push().
599 * A new thread is allocated, whenever the number of currently
600 * running threads in @pool is smaller than the maximal number.
602 * @error can be %NULL to ignore errors, or non-%NULL to report
603 * errors. An error can only occur when a new thread couldn't be
606 * Before version 2.32, this function did not return a success status.
608 * Return value: %TRUE on success, %FALSE if an error occurred
611 g_thread_pool_set_max_threads (GThreadPool *pool,
615 GRealThreadPool *real;
619 real = (GRealThreadPool*) pool;
621 g_return_val_if_fail (real, FALSE);
622 g_return_val_if_fail (real->running, FALSE);
623 g_return_val_if_fail (!real->pool.exclusive || max_threads != -1, FALSE);
624 g_return_val_if_fail (max_threads >= -1, FALSE);
628 g_async_queue_lock (real->queue);
630 real->max_threads = max_threads;
633 to_start = real->max_threads - real->num_threads;
635 to_start = g_async_queue_length_unlocked (real->queue);
637 for ( ; to_start > 0; to_start--)
639 GError *local_error = NULL;
641 if (!g_thread_pool_start_thread (real, &local_error))
643 g_propagate_error (error, local_error);
649 g_async_queue_unlock (real->queue);
655 * g_thread_pool_get_max_threads:
656 * @pool: a #GThreadPool
658 * Returns the maximal number of threads for @pool.
660 * Return value: the maximal number of threads
663 g_thread_pool_get_max_threads (GThreadPool *pool)
665 GRealThreadPool *real;
668 real = (GRealThreadPool*) pool;
670 g_return_val_if_fail (real, 0);
671 g_return_val_if_fail (real->running, 0);
673 g_async_queue_lock (real->queue);
674 retval = real->max_threads;
675 g_async_queue_unlock (real->queue);
681 * g_thread_pool_get_num_threads:
682 * @pool: a #GThreadPool
684 * Returns the number of threads currently running in @pool.
686 * Return value: the number of threads currently running
689 g_thread_pool_get_num_threads (GThreadPool *pool)
691 GRealThreadPool *real;
694 real = (GRealThreadPool*) pool;
696 g_return_val_if_fail (real, 0);
697 g_return_val_if_fail (real->running, 0);
699 g_async_queue_lock (real->queue);
700 retval = real->num_threads;
701 g_async_queue_unlock (real->queue);
707 * g_thread_pool_unprocessed:
708 * @pool: a #GThreadPool
710 * Returns the number of tasks still unprocessed in @pool.
712 * Return value: the number of unprocessed tasks
715 g_thread_pool_unprocessed (GThreadPool *pool)
717 GRealThreadPool *real;
720 real = (GRealThreadPool*) pool;
722 g_return_val_if_fail (real, 0);
723 g_return_val_if_fail (real->running, 0);
725 unprocessed = g_async_queue_length (real->queue);
727 return MAX (unprocessed, 0);
731 * g_thread_pool_free:
732 * @pool: a #GThreadPool
733 * @immediate: should @pool shut down immediately?
734 * @wait_: should the function wait for all tasks to be finished?
736 * Frees all resources allocated for @pool.
738 * If @immediate is %TRUE, no new task is processed for @pool.
739 * Otherwise @pool is not freed before the last task is processed.
740 * Note however, that no thread of this pool is interrupted while
741 * processing a task. Instead at least all still running threads
742 * can finish their tasks before the @pool is freed.
744 * If @wait_ is %TRUE, the functions does not return before all
745 * tasks to be processed (dependent on @immediate, whether all
746 * or only the currently running) are ready. Otherwise the function
747 * returns immediately.
749 * After calling this function @pool must not be used anymore.
752 g_thread_pool_free (GThreadPool *pool,
756 GRealThreadPool *real;
758 real = (GRealThreadPool*) pool;
760 g_return_if_fail (real);
761 g_return_if_fail (real->running);
763 /* If there's no thread allowed here, there is not much sense in
764 * not stopping this pool immediately, when it's not empty
766 g_return_if_fail (immediate ||
767 real->max_threads != 0 ||
768 g_async_queue_length (real->queue) == 0);
770 g_async_queue_lock (real->queue);
772 real->running = FALSE;
773 real->immediate = immediate;
774 real->waiting = wait_;
778 while (g_async_queue_length_unlocked (real->queue) != -real->num_threads &&
779 !(immediate && real->num_threads == 0))
780 g_cond_wait (&real->cond, _g_async_queue_get_mutex (real->queue));
783 if (immediate || g_async_queue_length_unlocked (real->queue) == -real->num_threads)
785 /* No thread is currently doing something (and nothing is left
786 * to process in the queue)
788 if (real->num_threads == 0)
790 /* No threads left, we clean up */
791 g_async_queue_unlock (real->queue);
792 g_thread_pool_free_internal (real);
796 g_thread_pool_wakeup_and_stop_all (real);
799 /* The last thread should cleanup the pool */
800 real->waiting = FALSE;
801 g_async_queue_unlock (real->queue);
805 g_thread_pool_free_internal (GRealThreadPool* pool)
807 g_return_if_fail (pool);
808 g_return_if_fail (pool->running == FALSE);
809 g_return_if_fail (pool->num_threads == 0);
811 g_async_queue_unref (pool->queue);
812 g_cond_clear (&pool->cond);
818 g_thread_pool_wakeup_and_stop_all (GRealThreadPool *pool)
822 g_return_if_fail (pool);
823 g_return_if_fail (pool->running == FALSE);
824 g_return_if_fail (pool->num_threads != 0);
826 pool->immediate = TRUE;
828 for (i = 0; i < pool->num_threads; i++)
829 g_thread_pool_queue_push_unlocked (pool, GUINT_TO_POINTER (1));
833 * g_thread_pool_set_max_unused_threads:
834 * @max_threads: maximal number of unused threads
836 * Sets the maximal number of unused threads to @max_threads.
837 * If @max_threads is -1, no limit is imposed on the number
841 g_thread_pool_set_max_unused_threads (gint max_threads)
843 g_return_if_fail (max_threads >= -1);
845 g_atomic_int_set (&max_unused_threads, max_threads);
847 if (max_threads != -1)
849 max_threads -= g_atomic_int_get (&unused_threads);
852 g_atomic_int_set (&kill_unused_threads, -max_threads);
853 g_atomic_int_inc (&wakeup_thread_serial);
855 g_async_queue_lock (unused_thread_queue);
859 g_async_queue_push_unlocked (unused_thread_queue,
860 wakeup_thread_marker);
862 while (++max_threads);
864 g_async_queue_unlock (unused_thread_queue);
870 * g_thread_pool_get_max_unused_threads:
872 * Returns the maximal allowed number of unused threads.
874 * Return value: the maximal number of unused threads
877 g_thread_pool_get_max_unused_threads (void)
879 return g_atomic_int_get (&max_unused_threads);
883 * g_thread_pool_get_num_unused_threads:
885 * Returns the number of currently unused threads.
887 * Return value: the number of currently unused threads
890 g_thread_pool_get_num_unused_threads (void)
892 return g_atomic_int_get (&unused_threads);
896 * g_thread_pool_stop_unused_threads:
898 * Stops all currently unused threads. This does not change the
899 * maximal number of unused threads. This function can be used to
900 * regularly stop all unused threads e.g. from g_timeout_add().
903 g_thread_pool_stop_unused_threads (void)
907 oldval = g_thread_pool_get_max_unused_threads ();
909 g_thread_pool_set_max_unused_threads (0);
910 g_thread_pool_set_max_unused_threads (oldval);
914 * g_thread_pool_set_sort_function:
915 * @pool: a #GThreadPool
916 * @func: the #GCompareDataFunc used to sort the list of tasks.
917 * This function is passed two tasks. It should return
918 * 0 if the order in which they are handled does not matter,
919 * a negative value if the first task should be processed before
920 * the second or a positive value if the second task should be
922 * @user_data: user data passed to @func
924 * Sets the function used to sort the list of tasks. This allows the
925 * tasks to be processed by a priority determined by @func, and not
926 * just in the order in which they were added to the pool.
928 * Note, if the maximum number of threads is more than 1, the order
929 * that threads are executed cannot be guaranteed 100%. Threads are
930 * scheduled by the operating system and are executed at random. It
931 * cannot be assumed that threads are executed in the order they are
937 g_thread_pool_set_sort_function (GThreadPool *pool,
938 GCompareDataFunc func,
941 GRealThreadPool *real;
943 real = (GRealThreadPool*) pool;
945 g_return_if_fail (real);
946 g_return_if_fail (real->running);
948 g_async_queue_lock (real->queue);
950 real->sort_func = func;
951 real->sort_user_data = user_data;
954 g_async_queue_sort_unlocked (real->queue,
956 real->sort_user_data);
958 g_async_queue_unlock (real->queue);
962 * g_thread_pool_set_max_idle_time:
963 * @interval: the maximum @interval (in milliseconds)
964 * a thread can be idle
966 * This function will set the maximum @interval that a thread
967 * waiting in the pool for new tasks can be idle for before
968 * being stopped. This function is similar to calling
969 * g_thread_pool_stop_unused_threads() on a regular timeout,
970 * except this is done on a per thread basis.
972 * By setting @interval to 0, idle threads will not be stopped.
974 * This function makes use of g_async_queue_timed_pop () using
980 g_thread_pool_set_max_idle_time (guint interval)
984 g_atomic_int_set (&max_idle_time, interval);
986 i = g_atomic_int_get (&unused_threads);
989 g_atomic_int_inc (&wakeup_thread_serial);
990 g_async_queue_lock (unused_thread_queue);
994 g_async_queue_push_unlocked (unused_thread_queue,
995 wakeup_thread_marker);
999 g_async_queue_unlock (unused_thread_queue);
1004 * g_thread_pool_get_max_idle_time:
1006 * This function will return the maximum @interval that a
1007 * thread will wait in the thread pool for new tasks before
1010 * If this function returns 0, threads waiting in the thread
1011 * pool for new work are not stopped.
1013 * Return value: the maximum @interval (milliseconds) to wait
1014 * for new tasks in the thread pool before stopping the
1020 g_thread_pool_get_max_idle_time (void)
1022 return g_atomic_int_get (&max_idle_time);