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 /* #define DEBUG_MSG(args) g_printerr args ; g_printerr ("\n"); */
35 typedef struct _GRealThreadPool GRealThreadPool;
37 struct _GRealThreadPool
46 GCompareDataFunc sort_func;
47 gpointer sort_user_data;
50 /* The following is just an address to mark the wakeup order for a
51 * thread, it could be any address (as long, as it isn't a valid
52 * GThreadPool address) */
53 static const gpointer wakeup_thread_marker = (gpointer) &g_thread_pool_new;
54 static gint wakeup_thread_serial = 0;
56 /* Here all unused threads are waiting */
57 static GAsyncQueue *unused_thread_queue;
58 static gint unused_threads = 0;
59 static gint max_unused_threads = 0;
60 static guint max_idle_time = 0;
62 static GMutex *inform_mutex = NULL;
63 static GCond *inform_cond = NULL;
66 static void g_thread_pool_queue_push_unlocked (GRealThreadPool *pool,
68 static void g_thread_pool_free_internal (GRealThreadPool *pool);
69 static gpointer g_thread_pool_thread_proxy (gpointer data);
70 static void g_thread_pool_start_thread (GRealThreadPool *pool,
72 static void g_thread_pool_wakeup_and_stop_all (GRealThreadPool *pool);
76 g_thread_pool_queue_push_unlocked (GRealThreadPool *pool,
80 g_async_queue_push_sorted_unlocked (pool->queue,
83 pool->sort_user_data);
85 g_async_queue_push_unlocked (pool->queue, data);
89 g_thread_pool_thread_proxy (gpointer data)
91 GRealThreadPool *pool = data;
92 guint last_wakeup_thread_serial = 0;
94 DEBUG_MSG (("thread %p started for pool %p.",
95 g_thread_self (), pool));
97 g_async_queue_lock (pool->queue);
100 gpointer task = NULL;
102 if (pool->running || (!pool->immediate &&
103 g_async_queue_length_unlocked (pool->queue) > 0))
105 /* This thread pool is still active. */
106 if (pool->num_threads > pool->max_threads &&
107 pool->max_threads != -1)
109 /* This is a superfluous thread, so it goes to the
111 DEBUG_MSG (("superfluous thread %p in pool %p.",
112 g_thread_self (), pool));
114 else if (pool->pool.exclusive)
116 /* Exclusive threads stay attached to the pool. */
117 task = g_async_queue_pop_unlocked (pool->queue);
118 DEBUG_MSG (("thread %p in exclusive pool %p waits for task "
119 "(%d running, %d unprocessed).",
120 g_thread_self (), pool, pool->num_threads,
121 g_async_queue_length_unlocked (pool->queue)));
125 /* A thread will wait for new tasks for at most 1/2
126 * second before going to the global pool. */
128 g_get_current_time (&end_time);
129 g_time_val_add (&end_time, G_USEC_PER_SEC / 2); /* 1/2 second */
130 DEBUG_MSG (("thread %p in pool %p waits 1/2 second for task "
131 "(%d running, %d unprocessed).",
132 g_thread_self (), pool, pool->num_threads,
133 g_async_queue_length_unlocked (pool->queue)));
134 task = g_async_queue_timed_pop_unlocked (pool->queue, &end_time);
139 /* This thread pool should is inactive, it will no longer
141 DEBUG_MSG (("pool %p not active, thread %p will go to global pool "
142 "(running: %s, immediate: %s, len: %d).",
143 pool, g_thread_self (),
144 pool->running ? "true" : "false",
145 pool->immediate ? "true" : "false",
146 g_async_queue_length_unlocked (pool->queue)));
151 if (pool->running || !pool->immediate)
153 /* A task was received and the thread pool is active, so
154 * execute the function. */
155 g_async_queue_unlock (pool->queue);
156 DEBUG_MSG (("thread %p in pool %p calling func.",
157 g_thread_self (), pool));
158 pool->pool.func (task, pool->pool.user_data);
159 g_async_queue_lock (pool->queue);
164 /* No task was received, so this thread goes to the global
166 gboolean free_pool = FALSE;
168 DEBUG_MSG (("thread %p leaving pool %p for global pool.",
169 g_thread_self (), pool));
176 if (pool->num_threads == 0)
178 /* If the pool is not running and no other
179 * thread is waiting for this thread pool to
180 * finish and this is the last thread of this
181 * pool, free the pool. */
186 /* If the pool is not running and no other
187 * thread is waiting for this thread pool to
188 * finish and this is not the last thread of
189 * this pool and there are no tasks left in the
190 * queue, wakeup the remaining threads. */
191 if (g_async_queue_length_unlocked (pool->queue) ==
193 g_thread_pool_wakeup_and_stop_all (pool);
196 else if (pool->immediate ||
197 g_async_queue_length_unlocked (pool->queue) <= 0)
199 /* If the pool is not running and another thread is
200 * waiting for this thread pool to finish and there
201 * are either no tasks left or the pool shall stop
202 * immediatly, inform the waiting thread of a change
203 * of the thread pool state. */
204 g_cond_broadcast (inform_cond);
207 g_async_queue_unlock (pool->queue);
210 g_thread_pool_free_internal (pool);
212 g_async_queue_lock (unused_thread_queue);
218 if ((unused_threads >= max_unused_threads &&
219 max_unused_threads != -1))
221 /* If this is a superflous thread, stop it. */
223 g_async_queue_unlock (unused_thread_queue);
224 DEBUG_MSG (("stopping thread %p.", g_thread_self ()));
228 if (max_idle_time > 0)
230 /* If a maximal idle time is given, wait for the
233 g_get_current_time (&end_time);
234 g_time_val_add (&end_time, max_idle_time * 1000);
236 DEBUG_MSG (("thread %p waiting in global pool for "
238 g_thread_self (), max_idle_time / 1000.0));
240 pool = g_async_queue_timed_pop_unlocked (unused_thread_queue,
244 /* If no new task was received in the given
245 * time, stop this thread. */
247 g_async_queue_unlock (unused_thread_queue);
248 DEBUG_MSG (("stopping thread %p after max-idle-time.",
250 /* Stop this thread */
256 /* If no maximal idle time is given, wait
258 DEBUG_MSG (("thread %p waiting in global pool.",
260 pool = g_async_queue_pop_unlocked (unused_thread_queue);
263 if (pool == wakeup_thread_marker)
265 if (last_wakeup_thread_serial == wakeup_thread_serial)
267 /* If this wakeup marker has been received for
268 * the second time, relay it. */
269 DEBUG_MSG (("thread %p relaying wakeup message to "
270 "waiting thread with lower serial.",
272 g_async_queue_push_unlocked (unused_thread_queue,
273 wakeup_thread_marker);
277 last_wakeup_thread_serial = wakeup_thread_serial;
280 /* If a wakeup marker has been received, this thread
281 * will get out of the way for 100 microseconds to
282 * avoid receiving this marker again. */
283 g_async_queue_unlock (unused_thread_queue);
285 g_async_queue_lock (unused_thread_queue);
288 } while (pool == wakeup_thread_marker);
291 g_async_queue_unlock (unused_thread_queue);
293 g_async_queue_lock (pool->queue);
295 DEBUG_MSG (("thread %p entering pool %p from global pool.",
296 g_thread_self (), pool));
298 /* pool->num_threads++ is not done here, but in
299 * g_thread_pool_start_thread to make the new started thread
300 * known to the pool, before itself can do it. */
307 g_thread_pool_start_thread (GRealThreadPool *pool,
310 gboolean success = FALSE;
312 if (pool->num_threads >= pool->max_threads && pool->max_threads != -1)
313 /* Enough threads are already running */
316 g_async_queue_lock (unused_thread_queue);
318 if (g_async_queue_length_unlocked (unused_thread_queue) < 0)
320 g_async_queue_push_unlocked (unused_thread_queue, pool);
324 g_async_queue_unlock (unused_thread_queue);
328 GError *local_error = NULL;
329 /* No thread was found, we have to start a new one */
330 g_thread_create (g_thread_pool_thread_proxy, pool, FALSE, &local_error);
334 g_propagate_error (error, local_error);
339 /* See comment in g_thread_pool_thread_proxy as to why this is done
340 * here and not there */
346 * @func: a function to execute in the threads of the new thread pool
347 * @user_data: user data that is handed over to @func every time it
349 * @max_threads: the maximal number of threads to execute concurrently in
350 * the new thread pool, -1 means no limit
351 * @exclusive: should this thread pool be exclusive?
352 * @error: return location for error
354 * This function creates a new thread pool.
356 * Whenever you call g_thread_pool_push(), either a new thread is
357 * created or an unused one is reused. At most @max_threads threads
358 * are running concurrently for this thread pool. @max_threads = -1
359 * allows unlimited threads to be created for this thread pool. The
360 * newly created or reused thread now executes the function @func with
361 * the two arguments. The first one is the parameter to
362 * g_thread_pool_push() and the second one is @user_data.
364 * The parameter @exclusive determines, whether the thread pool owns
365 * all threads exclusive or whether the threads are shared
366 * globally. If @exclusive is %TRUE, @max_threads threads are started
367 * immediately and they will run exclusively for this thread pool until
368 * it is destroyed by g_thread_pool_free(). If @exclusive is %FALSE,
369 * threads are created, when needed and shared between all
370 * non-exclusive thread pools. This implies that @max_threads may not
371 * be -1 for exclusive thread pools.
373 * @error can be %NULL to ignore errors, or non-%NULL to report
374 * errors. An error can only occur when @exclusive is set to %TRUE and
375 * not all @max_threads threads could be created.
377 * Return value: the new #GThreadPool
380 g_thread_pool_new (GFunc func,
386 GRealThreadPool *retval;
387 G_LOCK_DEFINE_STATIC (init);
389 g_return_val_if_fail (func, NULL);
390 g_return_val_if_fail (!exclusive || max_threads != -1, NULL);
391 g_return_val_if_fail (max_threads >= -1, NULL);
392 g_return_val_if_fail (g_thread_supported (), NULL);
394 retval = g_new (GRealThreadPool, 1);
396 retval->pool.func = func;
397 retval->pool.user_data = user_data;
398 retval->pool.exclusive = exclusive;
399 retval->queue = g_async_queue_new ();
400 retval->max_threads = max_threads;
401 retval->num_threads = 0;
402 retval->running = TRUE;
403 retval->sort_func = NULL;
404 retval->sort_user_data = NULL;
410 inform_mutex = g_mutex_new ();
411 inform_cond = g_cond_new ();
412 unused_thread_queue = g_async_queue_new ();
417 if (retval->pool.exclusive)
419 g_async_queue_lock (retval->queue);
421 while (retval->num_threads < retval->max_threads)
423 GError *local_error = NULL;
424 g_thread_pool_start_thread (retval, &local_error);
427 g_propagate_error (error, local_error);
432 g_async_queue_unlock (retval->queue);
435 return (GThreadPool*) retval;
439 * g_thread_pool_push:
440 * @pool: a #GThreadPool
441 * @data: a new task for @pool
442 * @error: return location for error
444 * Inserts @data into the list of tasks to be executed by @pool. When
445 * the number of currently running threads is lower than the maximal
446 * allowed number of threads, a new thread is started (or reused) with
447 * the properties given to g_thread_pool_new (). Otherwise @data stays
448 * in the queue until a thread in this pool finishes its previous task
449 * and processes @data.
451 * @error can be %NULL to ignore errors, or non-%NULL to report
452 * errors. An error can only occur when a new thread couldn't be
453 * created. In that case @data is simply appended to the queue of work
457 g_thread_pool_push (GThreadPool *pool,
461 GRealThreadPool *real = (GRealThreadPool*) pool;
463 g_return_if_fail (real);
465 g_async_queue_lock (real->queue);
469 g_async_queue_unlock (real->queue);
470 g_return_if_fail (real->running);
473 if (g_async_queue_length_unlocked (real->queue) >= 0)
474 /* No thread is waiting in the queue */
475 g_thread_pool_start_thread (real, error);
477 g_thread_pool_queue_push_unlocked (real, data);
478 g_async_queue_unlock (real->queue);
482 * g_thread_pool_set_max_threads:
483 * @pool: a #GThreadPool
484 * @max_threads: a new maximal number of threads for @pool
485 * @error: return location for error
487 * Sets the maximal allowed number of threads for @pool. A value of -1
488 * means, that the maximal number of threads is unlimited.
490 * Setting @max_threads to 0 means stopping all work for @pool. It is
491 * effectively frozen until @max_threads is set to a non-zero value
494 * A thread is never terminated while calling @func, as supplied by
495 * g_thread_pool_new (). Instead the maximal number of threads only
496 * has effect for the allocation of new threads in g_thread_pool_push().
497 * A new thread is allocated, whenever the number of currently
498 * running threads in @pool is smaller than the maximal number.
500 * @error can be %NULL to ignore errors, or non-%NULL to report
501 * errors. An error can only occur when a new thread couldn't be
505 g_thread_pool_set_max_threads (GThreadPool *pool,
509 GRealThreadPool *real = (GRealThreadPool*) pool;
512 g_return_if_fail (real);
513 g_return_if_fail (real->running);
514 g_return_if_fail (!real->pool.exclusive || max_threads != -1);
515 g_return_if_fail (max_threads >= -1);
517 g_async_queue_lock (real->queue);
519 real->max_threads = max_threads;
522 to_start = real->max_threads - real->num_threads;
524 to_start = g_async_queue_length_unlocked (real->queue);
526 for ( ; to_start > 0; to_start--)
528 GError *local_error = NULL;
529 g_thread_pool_start_thread (real, &local_error);
532 g_propagate_error (error, local_error);
537 g_async_queue_unlock (real->queue);
541 * g_thread_pool_get_max_threads:
542 * @pool: a #GThreadPool
544 * Returns the maximal number of threads for @pool.
546 * Return value: the maximal number of threads
549 g_thread_pool_get_max_threads (GThreadPool *pool)
551 GRealThreadPool *real = (GRealThreadPool*) pool;
554 g_return_val_if_fail (real, 0);
555 g_return_val_if_fail (real->running, 0);
557 g_async_queue_lock (real->queue);
559 retval = real->max_threads;
561 g_async_queue_unlock (real->queue);
567 * g_thread_pool_get_num_threads:
568 * @pool: a #GThreadPool
570 * Returns the number of threads currently running in @pool.
572 * Return value: the number of threads currently running
575 g_thread_pool_get_num_threads (GThreadPool *pool)
577 GRealThreadPool *real = (GRealThreadPool*) pool;
580 g_return_val_if_fail (real, 0);
581 g_return_val_if_fail (real->running, 0);
583 g_async_queue_lock (real->queue);
585 retval = real->num_threads;
587 g_async_queue_unlock (real->queue);
593 * g_thread_pool_unprocessed:
594 * @pool: a #GThreadPool
596 * Returns the number of tasks still unprocessed in @pool.
598 * Return value: the number of unprocessed tasks
601 g_thread_pool_unprocessed (GThreadPool *pool)
603 GRealThreadPool *real = (GRealThreadPool*) pool;
606 g_return_val_if_fail (real, 0);
607 g_return_val_if_fail (real->running, 0);
609 unprocessed = g_async_queue_length (real->queue);
611 return MAX (unprocessed, 0);
615 * g_thread_pool_free:
616 * @pool: a #GThreadPool
617 * @immediate: should @pool shut down immediately?
618 * @wait: should the function wait for all tasks to be finished?
620 * Frees all resources allocated for @pool.
622 * If @immediate is %TRUE, no new task is processed for
623 * @pool. Otherwise @pool is not freed before the last task is
624 * processed. Note however, that no thread of this pool is
625 * interrupted, while processing a task. Instead at least all still
626 * running threads can finish their tasks before the @pool is freed.
628 * If @wait is %TRUE, the functions does not return before all tasks
629 * to be processed (dependent on @immediate, whether all or only the
630 * currently running) are ready. Otherwise the function returns immediately.
632 * After calling this function @pool must not be used anymore.
635 g_thread_pool_free (GThreadPool *pool,
639 GRealThreadPool *real = (GRealThreadPool*) pool;
641 g_return_if_fail (real);
642 g_return_if_fail (real->running);
643 /* It there's no thread allowed here, there is not much sense in
644 * not stopping this pool immediately, when it's not empty */
645 g_return_if_fail (immediate || real->max_threads != 0 ||
646 g_async_queue_length (real->queue) == 0);
648 g_async_queue_lock (real->queue);
650 real->running = FALSE;
651 real->immediate = immediate;
652 real->waiting = wait;
656 g_mutex_lock (inform_mutex);
657 while (g_async_queue_length_unlocked (real->queue) != -real->num_threads &&
658 !(immediate && real->num_threads == 0))
660 g_async_queue_unlock (real->queue);
661 g_cond_wait (inform_cond, inform_mutex);
662 g_async_queue_lock (real->queue);
664 g_mutex_unlock (inform_mutex);
668 g_async_queue_length_unlocked (real->queue) == -real->num_threads)
670 /* No thread is currently doing something (and nothing is left
671 * to process in the queue) */
672 if (real->num_threads == 0) /* No threads left, we clean up */
674 g_async_queue_unlock (real->queue);
675 g_thread_pool_free_internal (real);
679 g_thread_pool_wakeup_and_stop_all (real);
682 real->waiting = FALSE; /* The last thread should cleanup the pool */
683 g_async_queue_unlock (real->queue);
687 g_thread_pool_free_internal (GRealThreadPool* pool)
689 g_return_if_fail (pool);
690 g_return_if_fail (!pool->running);
691 g_return_if_fail (pool->num_threads == 0);
693 g_async_queue_unref (pool->queue);
699 g_thread_pool_wakeup_and_stop_all (GRealThreadPool* pool)
703 g_return_if_fail (pool);
704 g_return_if_fail (!pool->running);
705 g_return_if_fail (pool->num_threads != 0);
706 g_return_if_fail (g_async_queue_length_unlocked (pool->queue) ==
709 pool->immediate = TRUE;
710 for (i = 0; i < pool->num_threads; i++)
711 g_thread_pool_queue_push_unlocked (pool, GUINT_TO_POINTER (1));
715 * g_thread_pool_set_max_unused_threads:
716 * @max_threads: maximal number of unused threads
718 * Sets the maximal number of unused threads to @max_threads. If
719 * @max_threads is -1, no limit is imposed on the number of unused
723 g_thread_pool_set_max_unused_threads (gint max_threads)
725 g_return_if_fail (max_threads >= -1);
727 g_async_queue_lock (unused_thread_queue);
729 max_unused_threads = max_threads;
731 if (max_unused_threads < unused_threads && max_unused_threads != -1)
734 wakeup_thread_serial++;
736 for (i = unused_threads - max_unused_threads; i > 0; i--)
737 g_async_queue_push_unlocked (unused_thread_queue,
738 wakeup_thread_marker);
741 g_async_queue_unlock (unused_thread_queue);
745 * g_thread_pool_get_max_unused_threads:
747 * Returns the maximal allowed number of unused threads.
749 * Return value: the maximal number of unused threads
752 g_thread_pool_get_max_unused_threads (void)
756 g_async_queue_lock (unused_thread_queue);
757 retval = max_unused_threads;
758 g_async_queue_unlock (unused_thread_queue);
764 * g_thread_pool_get_num_unused_threads:
766 * Returns the number of currently unused threads.
768 * Return value: the number of currently unused threads
771 g_thread_pool_get_num_unused_threads (void)
775 g_async_queue_lock (unused_thread_queue);
776 retval = unused_threads;
777 g_async_queue_unlock (unused_thread_queue);
783 * g_thread_pool_stop_unused_threads:
785 * Stops all currently unused threads. This does not change the
786 * maximal number of unused threads. This function can be used to
787 * regularly stop all unused threads e.g. from g_timeout_add().
790 g_thread_pool_stop_unused_threads (void)
792 guint oldval = g_thread_pool_get_max_unused_threads ();
793 g_thread_pool_set_max_unused_threads (0);
794 g_thread_pool_set_max_unused_threads (oldval);
798 * g_thread_pool_set_sort_function:
799 * @pool: a #GThreadPool
800 * @func: the #GCompareDataFunc used to sort the list of tasks.
801 * This function is passed two tasks. It should return
802 * 0 if the order in which they are handled does not matter,
803 * a negative value if the first task should be processed before
804 * the second or a positive value if the second task should be
806 * @user_data: user data passed to @func.
808 * Sets the function used to sort the list of tasks. This allows the
809 * tasks to be processed by a priority determined by @func, and not
810 * just in the order in which they were added to the pool.
815 g_thread_pool_set_sort_function (GThreadPool *pool,
816 GCompareDataFunc func,
819 GRealThreadPool *real = (GRealThreadPool*) pool;
821 g_return_if_fail (real);
822 g_return_if_fail (real->running);
824 g_async_queue_lock (real->queue);
826 real->sort_func = func;
827 real->sort_user_data = user_data;
830 g_async_queue_sort_unlocked (real->queue,
832 real->sort_user_data);
834 g_async_queue_unlock (real->queue);
838 * g_thread_pool_set_max_idle_time:
839 * @interval: the maximum @interval (1/1000ths of a second) a thread
842 * This function will set the maximum @interval that a thread waiting
843 * in the pool for new tasks can be idle for before being
844 * stopped. This function is similar to calling
845 * g_thread_pool_stop_unused_threads() on a regular timeout, except,
846 * this is done on a per thread basis.
848 * By setting @interval to 0, idle threads will not be stopped.
850 * This function makes use of g_async_queue_timed_pop () using
856 g_thread_pool_set_max_idle_time (guint interval)
860 g_async_queue_lock (unused_thread_queue);
861 max_idle_time = interval;
862 wakeup_thread_serial++;
864 for (i = 0; i < unused_threads; i++)
865 g_async_queue_push_unlocked (unused_thread_queue, wakeup_thread_marker);
867 g_async_queue_unlock (unused_thread_queue);
871 * g_thread_pool_get_max_idle_time:
873 * This function will return the maximum @interval that a thread will
874 * wait in the thread pool for new tasks before being stopped.
876 * If this function returns 0, threads waiting in the thread pool for
877 * new work are not stopped.
879 * Return value: the maximum @interval to wait for new tasks in the
880 * thread pool before stopping the thread (1/1000ths of a second).
885 g_thread_pool_get_max_idle_time (void)
889 g_async_queue_lock (unused_thread_queue);
890 retval = max_idle_time;
891 g_async_queue_unlock (unused_thread_queue);
896 #define __G_THREADPOOL_C__
897 #include "galiasdef.c"