1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * GAsyncQueue: 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.
33 * SECTION: thread_pools
34 * @title: Thread Pools
35 * @short_description: pools of threads to execute work concurrently
36 * @see_also: <para> <variablelist> <varlistentry>
37 * <term>#GThread</term> <listitem><para>GLib thread
38 * system.</para></listitem> </varlistentry> </variablelist>
41 * Sometimes you wish to asynchronously fork out the execution of work
42 * and continue working in your own thread. If that will happen often,
43 * the overhead of starting and destroying a thread each time might be
44 * too high. In such cases reusing already started threads seems like a
45 * good idea. And it indeed is, but implementing this can be tedious
48 * Therefore GLib provides thread pools for your convenience. An added
49 * advantage is, that the threads can be shared between the different
50 * subsystems of your program, when they are using GLib.
52 * To create a new thread pool, you use g_thread_pool_new(). It is
53 * destroyed by g_thread_pool_free().
55 * If you want to execute a certain task within a thread pool, you call
56 * g_thread_pool_push().
58 * To get the current number of running threads you call
59 * g_thread_pool_get_num_threads(). To get the number of still
60 * unprocessed tasks you call g_thread_pool_unprocessed(). To control
61 * the maximal number of threads for a thread pool, you use
62 * g_thread_pool_get_max_threads() and g_thread_pool_set_max_threads().
64 * Finally you can control the number of unused threads, that are kept
65 * alive by GLib for future use. The current number can be fetched with
66 * g_thread_pool_get_num_unused_threads(). The maximal number can be
67 * controlled by g_thread_pool_get_max_unused_threads() and
68 * g_thread_pool_set_max_unused_threads(). All currently unused threads
69 * can be stopped by calling g_thread_pool_stop_unused_threads().
73 /* #define DEBUG_MSG(args) g_printerr args ; g_printerr ("\n"); */
75 typedef struct _GRealThreadPool GRealThreadPool;
79 * @func: the function to execute in the threads of this pool
80 * @user_data: the user data for the threads of this pool
81 * @exclusive: are all threads exclusive to this pool
83 * The #GThreadPool struct represents a thread pool. It has three
84 * public read-only members, but the underlying struct is bigger, so
85 * you must not copy this struct.
87 struct _GRealThreadPool
97 GCompareDataFunc sort_func;
98 gpointer sort_user_data;
101 /* The following is just an address to mark the wakeup order for a
102 * thread, it could be any address (as long, as it isn't a valid
103 * GThreadPool address) */
104 static const gpointer wakeup_thread_marker = (gpointer) &g_thread_pool_new;
105 static gint wakeup_thread_serial = 0;
107 /* Here all unused threads are waiting */
108 static GAsyncQueue *unused_thread_queue = NULL;
109 static gint unused_threads = 0;
110 static gint max_unused_threads = 0;
111 static gint kill_unused_threads = 0;
112 static guint max_idle_time = 0;
114 static void g_thread_pool_queue_push_unlocked (GRealThreadPool *pool,
116 static void g_thread_pool_free_internal (GRealThreadPool *pool);
117 static gpointer g_thread_pool_thread_proxy (gpointer data);
118 static void g_thread_pool_start_thread (GRealThreadPool *pool,
120 static void g_thread_pool_wakeup_and_stop_all (GRealThreadPool *pool);
121 static GRealThreadPool* g_thread_pool_wait_for_new_pool (void);
122 static gpointer g_thread_pool_wait_for_new_task (GRealThreadPool *pool);
125 g_thread_pool_queue_push_unlocked (GRealThreadPool *pool,
129 g_async_queue_push_sorted_unlocked (pool->queue,
132 pool->sort_user_data);
134 g_async_queue_push_unlocked (pool->queue, data);
137 static GRealThreadPool*
138 g_thread_pool_wait_for_new_pool (void)
140 GRealThreadPool *pool;
141 gint local_wakeup_thread_serial;
142 guint local_max_unused_threads;
143 gint local_max_idle_time;
144 gint last_wakeup_thread_serial;
145 gboolean have_relayed_thread_marker = FALSE;
147 local_max_unused_threads = g_atomic_int_get (&max_unused_threads);
148 local_max_idle_time = g_atomic_int_get (&max_idle_time);
149 last_wakeup_thread_serial = g_atomic_int_get (&wakeup_thread_serial);
151 g_atomic_int_inc (&unused_threads);
155 if (g_atomic_int_get (&unused_threads) >= local_max_unused_threads)
157 /* If this is a superfluous thread, stop it. */
160 else if (local_max_idle_time > 0)
162 /* If a maximal idle time is given, wait for the given time. */
165 g_get_current_time (&end_time);
166 g_time_val_add (&end_time, local_max_idle_time * 1000);
168 DEBUG_MSG (("thread %p waiting in global pool for %f seconds.",
169 g_thread_self (), local_max_idle_time / 1000.0));
171 pool = g_async_queue_timed_pop (unused_thread_queue, &end_time);
175 /* If no maximal idle time is given, wait indefinitely. */
176 DEBUG_MSG (("thread %p waiting in global pool.",
178 pool = g_async_queue_pop (unused_thread_queue);
181 if (pool == wakeup_thread_marker)
183 local_wakeup_thread_serial = g_atomic_int_get (&wakeup_thread_serial);
184 if (last_wakeup_thread_serial == local_wakeup_thread_serial)
186 if (!have_relayed_thread_marker)
188 /* If this wakeup marker has been received for
189 * the second time, relay it.
191 DEBUG_MSG (("thread %p relaying wakeup message to "
192 "waiting thread with lower serial.",
195 g_async_queue_push (unused_thread_queue, wakeup_thread_marker);
196 have_relayed_thread_marker = TRUE;
198 /* If a wakeup marker has been relayed, this thread
199 * will get out of the way for 100 microseconds to
200 * avoid receiving this marker again. */
206 if (g_atomic_int_exchange_and_add (&kill_unused_threads, -1) > 0)
212 DEBUG_MSG (("thread %p updating to new limits.",
215 local_max_unused_threads = g_atomic_int_get (&max_unused_threads);
216 local_max_idle_time = g_atomic_int_get (&max_idle_time);
217 last_wakeup_thread_serial = local_wakeup_thread_serial;
219 have_relayed_thread_marker = FALSE;
223 while (pool == wakeup_thread_marker);
225 g_atomic_int_add (&unused_threads, -1);
231 g_thread_pool_wait_for_new_task (GRealThreadPool *pool)
233 gpointer task = NULL;
235 if (pool->running || (!pool->immediate &&
236 g_async_queue_length_unlocked (pool->queue) > 0))
238 /* This thread pool is still active. */
239 if (pool->num_threads > pool->max_threads && pool->max_threads != -1)
241 /* This is a superfluous thread, so it goes to the global pool. */
242 DEBUG_MSG (("superfluous thread %p in pool %p.",
243 g_thread_self (), pool));
245 else if (pool->pool.exclusive)
247 /* Exclusive threads stay attached to the pool. */
248 task = g_async_queue_pop_unlocked (pool->queue);
250 DEBUG_MSG (("thread %p in exclusive pool %p waits for task "
251 "(%d running, %d unprocessed).",
252 g_thread_self (), pool, pool->num_threads,
253 g_async_queue_length_unlocked (pool->queue)));
257 /* A thread will wait for new tasks for at most 1/2
258 * second before going to the global pool.
262 g_get_current_time (&end_time);
263 g_time_val_add (&end_time, G_USEC_PER_SEC / 2); /* 1/2 second */
265 DEBUG_MSG (("thread %p in pool %p waits for up to a 1/2 second for task "
266 "(%d running, %d unprocessed).",
267 g_thread_self (), pool, pool->num_threads,
268 g_async_queue_length_unlocked (pool->queue)));
270 task = g_async_queue_timed_pop_unlocked (pool->queue, &end_time);
275 /* This thread pool is inactive, it will no longer process tasks. */
276 DEBUG_MSG (("pool %p not active, thread %p will go to global pool "
277 "(running: %s, immediate: %s, len: %d).",
278 pool, g_thread_self (),
279 pool->running ? "true" : "false",
280 pool->immediate ? "true" : "false",
281 g_async_queue_length_unlocked (pool->queue)));
289 g_thread_pool_thread_proxy (gpointer data)
291 GRealThreadPool *pool;
295 DEBUG_MSG (("thread %p started for pool %p.",
296 g_thread_self (), pool));
298 g_async_queue_lock (pool->queue);
304 task = g_thread_pool_wait_for_new_task (pool);
307 if (pool->running || !pool->immediate)
309 /* A task was received and the thread pool is active, so
310 * execute the function.
312 g_async_queue_unlock (pool->queue);
313 DEBUG_MSG (("thread %p in pool %p calling func.",
314 g_thread_self (), pool));
315 pool->pool.func (task, pool->pool.user_data);
316 g_async_queue_lock (pool->queue);
321 /* No task was received, so this thread goes to the global
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 * immediatly, 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 thread
384 * 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;
415 /* No thread was found, we have to start a new one */
416 g_thread_create (g_thread_pool_thread_proxy, pool, FALSE, &local_error);
420 g_propagate_error (error, local_error);
425 /* See comment in g_thread_pool_thread_proxy as to why this is done
433 * @func: a function to execute in the threads of the new thread pool
434 * @user_data: user data that is handed over to @func every time it
436 * @max_threads: the maximal number of threads to execute concurrently in
437 * the new thread pool, -1 means no limit
438 * @exclusive: should this thread pool be exclusive?
439 * @error: return location for error
441 * This function creates a new thread pool.
443 * Whenever you call g_thread_pool_push(), either a new thread is
444 * created or an unused one is reused. At most @max_threads threads
445 * are running concurrently for this thread pool. @max_threads = -1
446 * allows unlimited threads to be created for this thread pool. The
447 * newly created or reused thread now executes the function @func with
448 * the two arguments. The first one is the parameter to
449 * g_thread_pool_push() and the second one is @user_data.
451 * The parameter @exclusive determines, whether the thread pool owns
452 * all threads exclusive or whether the threads are shared
453 * globally. If @exclusive is %TRUE, @max_threads threads are started
454 * immediately and they will run exclusively for this thread pool until
455 * it is destroyed by g_thread_pool_free(). If @exclusive is %FALSE,
456 * threads are created, when needed and shared between all
457 * non-exclusive thread pools. This implies that @max_threads may not
458 * be -1 for exclusive thread pools.
460 * @error can be %NULL to ignore errors, or non-%NULL to report
461 * errors. An error can only occur when @exclusive is set to %TRUE and
462 * not all @max_threads threads could be created.
464 * Return value: the new #GThreadPool
467 g_thread_pool_new (GFunc func,
473 GRealThreadPool *retval;
474 G_LOCK_DEFINE_STATIC (init);
476 g_return_val_if_fail (func, NULL);
477 g_return_val_if_fail (!exclusive || max_threads != -1, NULL);
478 g_return_val_if_fail (max_threads >= -1, NULL);
479 g_return_val_if_fail (g_thread_supported (), 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 ();
488 retval->max_threads = max_threads;
489 retval->num_threads = 0;
490 retval->running = TRUE;
491 retval->sort_func = NULL;
492 retval->sort_user_data = NULL;
495 if (!unused_thread_queue)
496 unused_thread_queue = g_async_queue_new ();
499 if (retval->pool.exclusive)
501 g_async_queue_lock (retval->queue);
503 while (retval->num_threads < retval->max_threads)
505 GError *local_error = NULL;
506 g_thread_pool_start_thread (retval, &local_error);
509 g_propagate_error (error, local_error);
514 g_async_queue_unlock (retval->queue);
517 return (GThreadPool*) retval;
521 * g_thread_pool_push:
522 * @pool: a #GThreadPool
523 * @data: a new task for @pool
524 * @error: return location for error
526 * Inserts @data into the list of tasks to be executed by @pool. When
527 * the number of currently running threads is lower than the maximal
528 * allowed number of threads, a new thread is started (or reused) with
529 * the properties given to g_thread_pool_new (). Otherwise @data stays
530 * in the queue until a thread in this pool finishes its previous task
531 * and processes @data.
533 * @error can be %NULL to ignore errors, or non-%NULL to report
534 * errors. An error can only occur when a new thread couldn't be
535 * created. In that case @data is simply appended to the queue of work
539 g_thread_pool_push (GThreadPool *pool,
543 GRealThreadPool *real;
545 real = (GRealThreadPool*) pool;
547 g_return_if_fail (real);
548 g_return_if_fail (real->running);
550 g_async_queue_lock (real->queue);
552 if (g_async_queue_length_unlocked (real->queue) >= 0)
553 /* No thread is waiting in the queue */
554 g_thread_pool_start_thread (real, error);
556 g_thread_pool_queue_push_unlocked (real, data);
557 g_async_queue_unlock (real->queue);
561 * g_thread_pool_set_max_threads:
562 * @pool: a #GThreadPool
563 * @max_threads: a new maximal number of threads for @pool
564 * @error: return location for error
566 * Sets the maximal allowed number of threads for @pool. A value of -1
567 * means, that the maximal number of threads is unlimited.
569 * Setting @max_threads to 0 means stopping all work for @pool. It is
570 * effectively frozen until @max_threads is set to a non-zero value
573 * A thread is never terminated while calling @func, as supplied by
574 * g_thread_pool_new (). Instead the maximal number of threads only
575 * has effect for the allocation of new threads in g_thread_pool_push().
576 * A new thread is allocated, whenever the number of currently
577 * running threads in @pool is smaller than the maximal number.
579 * @error can be %NULL to ignore errors, or non-%NULL to report
580 * errors. An error can only occur when a new thread couldn't be
584 g_thread_pool_set_max_threads (GThreadPool *pool,
588 GRealThreadPool *real;
591 real = (GRealThreadPool*) pool;
593 g_return_if_fail (real);
594 g_return_if_fail (real->running);
595 g_return_if_fail (!real->pool.exclusive || max_threads != -1);
596 g_return_if_fail (max_threads >= -1);
598 g_async_queue_lock (real->queue);
600 real->max_threads = max_threads;
603 to_start = real->max_threads - real->num_threads;
605 to_start = g_async_queue_length_unlocked (real->queue);
607 for ( ; to_start > 0; to_start--)
609 GError *local_error = NULL;
611 g_thread_pool_start_thread (real, &local_error);
614 g_propagate_error (error, local_error);
619 g_async_queue_unlock (real->queue);
623 * g_thread_pool_get_max_threads:
624 * @pool: a #GThreadPool
626 * Returns the maximal number of threads for @pool.
628 * Return value: the maximal number of threads
631 g_thread_pool_get_max_threads (GThreadPool *pool)
633 GRealThreadPool *real;
636 real = (GRealThreadPool*) pool;
638 g_return_val_if_fail (real, 0);
639 g_return_val_if_fail (real->running, 0);
641 g_async_queue_lock (real->queue);
642 retval = real->max_threads;
643 g_async_queue_unlock (real->queue);
649 * g_thread_pool_get_num_threads:
650 * @pool: a #GThreadPool
652 * Returns the number of threads currently running in @pool.
654 * Return value: the number of threads currently running
657 g_thread_pool_get_num_threads (GThreadPool *pool)
659 GRealThreadPool *real;
662 real = (GRealThreadPool*) pool;
664 g_return_val_if_fail (real, 0);
665 g_return_val_if_fail (real->running, 0);
667 g_async_queue_lock (real->queue);
668 retval = real->num_threads;
669 g_async_queue_unlock (real->queue);
675 * g_thread_pool_unprocessed:
676 * @pool: a #GThreadPool
678 * Returns the number of tasks still unprocessed in @pool.
680 * Return value: the number of unprocessed tasks
683 g_thread_pool_unprocessed (GThreadPool *pool)
685 GRealThreadPool *real;
688 real = (GRealThreadPool*) pool;
690 g_return_val_if_fail (real, 0);
691 g_return_val_if_fail (real->running, 0);
693 unprocessed = g_async_queue_length (real->queue);
695 return MAX (unprocessed, 0);
699 * g_thread_pool_free:
700 * @pool: a #GThreadPool
701 * @immediate: should @pool shut down immediately?
702 * @wait_: should the function wait for all tasks to be finished?
704 * Frees all resources allocated for @pool.
706 * If @immediate is %TRUE, no new task is processed for
707 * @pool. Otherwise @pool is not freed before the last task is
708 * processed. Note however, that no thread of this pool is
709 * interrupted, while processing a task. Instead at least all still
710 * running threads can finish their tasks before the @pool is freed.
712 * If @wait_ is %TRUE, the functions does not return before all tasks
713 * to be processed (dependent on @immediate, whether all or only the
714 * currently running) are ready. Otherwise the function returns immediately.
716 * After calling this function @pool must not be used anymore.
719 g_thread_pool_free (GThreadPool *pool,
723 GRealThreadPool *real;
725 real = (GRealThreadPool*) pool;
727 g_return_if_fail (real);
728 g_return_if_fail (real->running);
730 /* If there's no thread allowed here, there is not much sense in
731 * not stopping this pool immediately, when it's not empty
733 g_return_if_fail (immediate ||
734 real->max_threads != 0 ||
735 g_async_queue_length (real->queue) == 0);
737 g_async_queue_lock (real->queue);
739 real->running = FALSE;
740 real->immediate = immediate;
741 real->waiting = wait_;
745 real->cond = g_cond_new ();
747 while (g_async_queue_length_unlocked (real->queue) != -real->num_threads &&
748 !(immediate && real->num_threads == 0))
749 g_cond_wait (real->cond, _g_async_queue_get_mutex (real->queue));
752 if (immediate || g_async_queue_length_unlocked (real->queue) == -real->num_threads)
754 /* No thread is currently doing something (and nothing is left
755 * to process in the queue)
757 if (real->num_threads == 0)
759 /* No threads left, we clean up */
760 g_async_queue_unlock (real->queue);
761 g_thread_pool_free_internal (real);
765 g_thread_pool_wakeup_and_stop_all (real);
768 /* The last thread should cleanup the pool */
769 real->waiting = FALSE;
770 g_async_queue_unlock (real->queue);
774 g_thread_pool_free_internal (GRealThreadPool* pool)
776 g_return_if_fail (pool);
777 g_return_if_fail (pool->running == FALSE);
778 g_return_if_fail (pool->num_threads == 0);
780 g_async_queue_unref (pool->queue);
783 g_cond_free (pool->cond);
789 g_thread_pool_wakeup_and_stop_all (GRealThreadPool* pool)
793 g_return_if_fail (pool);
794 g_return_if_fail (pool->running == FALSE);
795 g_return_if_fail (pool->num_threads != 0);
797 pool->immediate = TRUE;
799 for (i = 0; i < pool->num_threads; i++)
800 g_thread_pool_queue_push_unlocked (pool, GUINT_TO_POINTER (1));
804 * g_thread_pool_set_max_unused_threads:
805 * @max_threads: maximal number of unused threads
807 * Sets the maximal number of unused threads to @max_threads. If
808 * @max_threads is -1, no limit is imposed on the number of unused
812 g_thread_pool_set_max_unused_threads (gint max_threads)
814 g_return_if_fail (max_threads >= -1);
816 g_atomic_int_set (&max_unused_threads, max_threads);
818 if (max_threads != -1)
820 max_threads -= g_atomic_int_get (&unused_threads);
823 g_atomic_int_set (&kill_unused_threads, -max_threads);
824 g_atomic_int_inc (&wakeup_thread_serial);
826 g_async_queue_lock (unused_thread_queue);
830 g_async_queue_push_unlocked (unused_thread_queue,
831 wakeup_thread_marker);
833 while (++max_threads);
835 g_async_queue_unlock (unused_thread_queue);
841 * g_thread_pool_get_max_unused_threads:
843 * Returns the maximal allowed number of unused threads.
845 * Return value: the maximal number of unused threads
848 g_thread_pool_get_max_unused_threads (void)
850 return g_atomic_int_get (&max_unused_threads);
854 * g_thread_pool_get_num_unused_threads:
856 * Returns the number of currently unused threads.
858 * Return value: the number of currently unused threads
861 g_thread_pool_get_num_unused_threads (void)
863 return g_atomic_int_get (&unused_threads);
867 * g_thread_pool_stop_unused_threads:
869 * Stops all currently unused threads. This does not change the
870 * maximal number of unused threads. This function can be used to
871 * regularly stop all unused threads e.g. from g_timeout_add().
874 g_thread_pool_stop_unused_threads (void)
878 oldval = g_thread_pool_get_max_unused_threads ();
880 g_thread_pool_set_max_unused_threads (0);
881 g_thread_pool_set_max_unused_threads (oldval);
885 * g_thread_pool_set_sort_function:
886 * @pool: a #GThreadPool
887 * @func: the #GCompareDataFunc used to sort the list of tasks.
888 * This function is passed two tasks. It should return
889 * 0 if the order in which they are handled does not matter,
890 * a negative value if the first task should be processed before
891 * the second or a positive value if the second task should be
893 * @user_data: user data passed to @func.
895 * Sets the function used to sort the list of tasks. This allows the
896 * tasks to be processed by a priority determined by @func, and not
897 * just in the order in which they were added to the pool.
899 * Note, if the maximum number of threads is more than 1, the order
900 * that threads are executed can not be guranteed 100%. Threads are
901 * scheduled by the operating system and are executed at random. It
902 * cannot be assumed that threads are executed in the order they are
908 g_thread_pool_set_sort_function (GThreadPool *pool,
909 GCompareDataFunc func,
912 GRealThreadPool *real;
914 real = (GRealThreadPool*) pool;
916 g_return_if_fail (real);
917 g_return_if_fail (real->running);
919 g_async_queue_lock (real->queue);
921 real->sort_func = func;
922 real->sort_user_data = user_data;
925 g_async_queue_sort_unlocked (real->queue,
927 real->sort_user_data);
929 g_async_queue_unlock (real->queue);
933 * g_thread_pool_set_max_idle_time:
934 * @interval: the maximum @interval (1/1000ths of a second) a thread
937 * This function will set the maximum @interval that a thread waiting
938 * in the pool for new tasks can be idle for before being
939 * stopped. This function is similar to calling
940 * g_thread_pool_stop_unused_threads() on a regular timeout, except,
941 * this is done on a per thread basis.
943 * By setting @interval to 0, idle threads will not be stopped.
945 * This function makes use of g_async_queue_timed_pop () using
951 g_thread_pool_set_max_idle_time (guint interval)
955 g_atomic_int_set (&max_idle_time, interval);
957 i = g_atomic_int_get (&unused_threads);
960 g_atomic_int_inc (&wakeup_thread_serial);
961 g_async_queue_lock (unused_thread_queue);
965 g_async_queue_push_unlocked (unused_thread_queue,
966 wakeup_thread_marker);
970 g_async_queue_unlock (unused_thread_queue);
975 * g_thread_pool_get_max_idle_time:
977 * This function will return the maximum @interval that a thread will
978 * wait in the thread pool for new tasks before being stopped.
980 * If this function returns 0, threads waiting in the thread pool for
981 * new work are not stopped.
983 * Return value: the maximum @interval to wait for new tasks in the
984 * thread pool before stopping the thread (1/1000ths of a second).
989 g_thread_pool_get_max_idle_time (void)
991 return g_atomic_int_get (&max_idle_time);
994 #define __G_THREADPOOL_C__
995 #include "galiasdef.c"