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.
32 #define debug(...) /* g_printerr (__VA_ARGS__) */
34 typedef struct _GRealThreadPool GRealThreadPool;
36 struct _GRealThreadPool
45 GCompareDataFunc sort_func;
46 gpointer sort_user_data;
49 /* The following is just an address to mark the stop order for a
50 * thread, it could be any address (as long, as it isn't a valid
51 * GThreadPool address) */
52 static const gpointer stop_this_thread_marker = (gpointer) &g_thread_pool_new;
54 /* Here all unused threads are waiting */
55 static GAsyncQueue *unused_thread_queue;
56 static gint unused_threads = 0;
57 static gint max_unused_threads = 0;
58 static guint max_idle_time = 0;
59 G_LOCK_DEFINE_STATIC (settings);
61 static GMutex *inform_mutex = NULL;
62 static GCond *inform_cond = NULL;
65 static void g_thread_pool_queue_push_unlocked (GRealThreadPool *pool,
67 static void g_thread_pool_free_internal (GRealThreadPool *pool);
68 static gpointer g_thread_pool_thread_proxy (gpointer data);
69 static void g_thread_pool_start_thread (GRealThreadPool *pool,
71 static void g_thread_pool_wakeup_and_stop_all (GRealThreadPool *pool);
74 #define g_thread_should_run(pool, len) \
75 ((pool)->running || (!(pool)->immediate && (len) > 0))
79 g_thread_pool_queue_push_unlocked (GRealThreadPool *pool,
83 g_async_queue_push_sorted_unlocked (pool->queue,
86 pool->sort_user_data);
88 g_async_queue_push_unlocked (pool->queue, data);
92 g_thread_pool_thread_proxy (gpointer data)
94 GRealThreadPool *pool = data;
95 gboolean watcher = FALSE;
97 debug("pool:0x%.8x entering proxy ...\n", (guint)pool);
99 g_async_queue_lock (pool->queue);
102 gpointer task = NULL;
103 gboolean goto_global_pool = !pool->pool.exclusive;
104 gint len = g_async_queue_length_unlocked (pool->queue);
106 if (g_thread_should_run (pool, len))
110 /* This thread is actually not needed here, but it waits
111 * for some time anyway. If during that time a new
112 * request arrives, this saves process
113 * swicthes. Otherwise the thread will go to the global
116 g_get_current_time (&end_time);
117 g_time_val_add (&end_time, G_USEC_PER_SEC / 2); /* 1/2 second */
118 debug("pool:0x%.8x waiting 1/2 second to pop next item "
119 "in queue (%d running, %d unprocessed) ...\n",
122 g_async_queue_length_unlocked (pool->queue));
123 task = g_async_queue_timed_pop_unlocked (pool->queue, &end_time);
125 else if (g_thread_pool_get_max_idle_time() > 0)
127 /* We always give a maximum time to pop the next task so
128 * we know that when we evaluate task further down, that
129 * it has had the maximum time to get a new task and it
132 g_get_current_time (&end_time);
133 debug("pool:0x%.8x waiting %d ms max to pop next item in "
134 "queue (%d running, %d unprocessed) or exiting ...\n",
136 g_thread_pool_get_max_idle_time (),
138 g_async_queue_length_unlocked (pool->queue));
140 g_time_val_add (&end_time, g_thread_pool_get_max_idle_time () * 1000);
141 task = g_async_queue_timed_pop_unlocked (pool->queue, &end_time);
145 task = g_async_queue_pop_unlocked (pool->queue);
146 debug("pool:0x%.8x new task:0x%.8x poped from pool queue ...\n",
147 (guint)pool, (guint)task);
153 if (pool->num_threads > pool->max_threads &&
154 pool->max_threads != -1)
155 /* We are in fact a superfluous threads, so we go to
156 * the global pool and just hand the data further to
157 * the next one waiting in the queue */
159 debug("pool:0x%.8x, task:0x%.8x we have too many threads "
160 "and max is set, pushing task into queue ...\n",
161 (guint)pool, (guint)task);
162 g_thread_pool_queue_push_unlocked (pool, task);
163 goto_global_pool = TRUE;
165 else if (pool->running || !pool->immediate)
167 g_async_queue_unlock (pool->queue);
168 debug("pool:0x%.8x, task:0x%.8x calling func ...\n",
169 (guint)pool, (guint)task);
170 pool->pool.func (task, pool->pool.user_data);
171 g_async_queue_lock (pool->queue);
174 else if (g_thread_pool_get_max_idle_time() > 0)
177 if (pool->num_threads > max_unused_threads) {
181 debug("pool:0x%.8x queue timed pop has no tasks waiting, "
182 "so stopping thread (%d running, %d unprocessed) ...\n",
185 g_async_queue_length_unlocked (pool->queue));
186 g_async_queue_unlock (pool->queue);
192 len = g_async_queue_length_unlocked (pool->queue);
195 debug("pool:0x%.8x, len:%d, watcher:%s, exclusive:%s, should run:%s\n",
198 watcher ? "true" : "false",
199 pool->pool.exclusive ? "true" : "false",
200 g_thread_should_run (pool, len) ? "true" : "false");
202 if (!g_thread_should_run (pool, len))
204 g_cond_broadcast (inform_cond);
205 goto_global_pool = TRUE;
209 /* At this pool there are no threads waiting, but tasks are. */
210 goto_global_pool = FALSE;
212 else if (len < 1 && g_thread_pool_get_max_idle_time () > 0)
214 goto_global_pool = FALSE;
217 else if (len == 0 && !watcher && !pool->pool.exclusive)
219 /* Here neither threads nor tasks are queued and we didn't
220 * just return from a timed wait. We now wait for a limited
221 * time at this pool for new tasks to avoid costly context
223 goto_global_pool = FALSE;
227 if (goto_global_pool)
229 debug("pool:0x%.8x, now in the global pool\n", (guint)pool);
232 if (!pool->running && !pool->waiting)
234 if (pool->num_threads == 0)
236 g_async_queue_unlock (pool->queue);
237 g_thread_pool_free_internal (pool);
241 if (len == - pool->num_threads)
242 g_thread_pool_wakeup_and_stop_all (pool);
244 g_async_queue_unlock (pool->queue);
248 g_async_queue_unlock (pool->queue);
250 g_async_queue_lock (unused_thread_queue);
253 if ((unused_threads >= max_unused_threads &&
254 max_unused_threads != -1))
257 g_async_queue_unlock (unused_thread_queue);
258 debug("pool:0x%.8x stopping thread (%d running, %d unprocessed) ...\n",
261 g_async_queue_length_unlocked (pool->queue));
262 /* Stop this thread */
268 pool = g_async_queue_pop_unlocked (unused_thread_queue);
274 g_async_queue_unlock (unused_thread_queue);
276 if (pool == stop_this_thread_marker)
277 /* Stop this thread */
280 g_async_queue_lock (pool->queue);
282 /* pool->num_threads++ is not done here, but in
283 * g_thread_pool_start_thread to make the new started thread
284 * known to the pool, before itself can do it. */
291 g_thread_pool_start_thread (GRealThreadPool *pool,
294 gboolean success = FALSE;
296 if (pool->num_threads >= pool->max_threads && pool->max_threads != -1)
297 /* Enough threads are already running */
300 g_async_queue_lock (unused_thread_queue);
302 if (g_async_queue_length_unlocked (unused_thread_queue) < 0)
304 g_async_queue_push_unlocked (unused_thread_queue, pool);
308 g_async_queue_unlock (unused_thread_queue);
312 GError *local_error = NULL;
313 /* No thread was found, we have to start a new one */
314 g_thread_create (g_thread_pool_thread_proxy, pool, FALSE, &local_error);
318 g_propagate_error (error, local_error);
323 /* See comment in g_thread_pool_thread_proxy as to why this is done
324 * here and not there */
326 debug("pool:0x%.8x thread created, (running:%d)\n",
327 (guint)pool, pool->num_threads);
332 * @func: a function to execute in the threads of the new thread pool
333 * @user_data: user data that is handed over to @func every time it
335 * @max_threads: the maximal number of threads to execute concurrently in
336 * the new thread pool, -1 means no limit
337 * @exclusive: should this thread pool be exclusive?
338 * @error: return location for error
340 * This function creates a new thread pool.
342 * Whenever you call g_thread_pool_push(), either a new thread is
343 * created or an unused one is reused. At most @max_threads threads
344 * are running concurrently for this thread pool. @max_threads = -1
345 * allows unlimited threads to be created for this thread pool. The
346 * newly created or reused thread now executes the function @func with
347 * the two arguments. The first one is the parameter to
348 * g_thread_pool_push() and the second one is @user_data.
350 * The parameter @exclusive determines, whether the thread pool owns
351 * all threads exclusive or whether the threads are shared
352 * globally. If @exclusive is %TRUE, @max_threads threads are started
353 * immediately and they will run exclusively for this thread pool until
354 * it is destroyed by g_thread_pool_free(). If @exclusive is %FALSE,
355 * threads are created, when needed and shared between all
356 * non-exclusive thread pools. This implies that @max_threads may not
357 * be -1 for exclusive thread pools.
359 * @error can be %NULL to ignore errors, or non-%NULL to report
360 * errors. An error can only occur when @exclusive is set to %TRUE and
361 * not all @max_threads threads could be created.
363 * Return value: the new #GThreadPool
366 g_thread_pool_new (GFunc func,
372 GRealThreadPool *retval;
373 G_LOCK_DEFINE_STATIC (init);
375 g_return_val_if_fail (func, NULL);
376 g_return_val_if_fail (!exclusive || max_threads != -1, NULL);
377 g_return_val_if_fail (max_threads >= -1, NULL);
378 g_return_val_if_fail (g_thread_supported (), NULL);
380 retval = g_new (GRealThreadPool, 1);
382 retval->pool.func = func;
383 retval->pool.user_data = user_data;
384 retval->pool.exclusive = exclusive;
385 retval->queue = g_async_queue_new ();
386 retval->max_threads = max_threads;
387 retval->num_threads = 0;
388 retval->running = TRUE;
389 retval->sort_func = NULL;
390 retval->sort_user_data = NULL;
396 inform_mutex = g_mutex_new ();
397 inform_cond = g_cond_new ();
398 unused_thread_queue = g_async_queue_new ();
403 if (retval->pool.exclusive)
405 g_async_queue_lock (retval->queue);
407 while (retval->num_threads < retval->max_threads)
409 GError *local_error = NULL;
410 g_thread_pool_start_thread (retval, &local_error);
413 g_propagate_error (error, local_error);
418 g_async_queue_unlock (retval->queue);
421 return (GThreadPool*) retval;
425 * g_thread_pool_push:
426 * @pool: a #GThreadPool
427 * @data: a new task for @pool
428 * @error: return location for error
430 * Inserts @data into the list of tasks to be executed by @pool. When
431 * the number of currently running threads is lower than the maximal
432 * allowed number of threads, a new thread is started (or reused) with
433 * the properties given to g_thread_pool_new (). Otherwise @data stays
434 * in the queue until a thread in this pool finishes its previous task
435 * and processes @data.
437 * @error can be %NULL to ignore errors, or non-%NULL to report
438 * errors. An error can only occur when a new thread couldn't be
439 * created. In that case @data is simply appended to the queue of work
443 g_thread_pool_push (GThreadPool *pool,
447 GRealThreadPool *real = (GRealThreadPool*) pool;
449 g_return_if_fail (real);
451 g_async_queue_lock (real->queue);
455 g_async_queue_unlock (real->queue);
456 g_return_if_fail (real->running);
459 if (g_async_queue_length_unlocked (real->queue) >= 0)
460 /* No thread is waiting in the queue */
461 g_thread_pool_start_thread (real, error);
463 g_thread_pool_queue_push_unlocked (real, data);
464 g_async_queue_unlock (real->queue);
468 * g_thread_pool_set_max_threads:
469 * @pool: a #GThreadPool
470 * @max_threads: a new maximal number of threads for @pool
471 * @error: return location for error
473 * Sets the maximal allowed number of threads for @pool. A value of -1
474 * means, that the maximal number of threads is unlimited.
476 * Setting @max_threads to 0 means stopping all work for @pool. It is
477 * effectively frozen until @max_threads is set to a non-zero value
480 * A thread is never terminated while calling @func, as supplied by
481 * g_thread_pool_new (). Instead the maximal number of threads only
482 * has effect for the allocation of new threads in g_thread_pool_push().
483 * A new thread is allocated, whenever the number of currently
484 * running threads in @pool is smaller than the maximal number.
486 * @error can be %NULL to ignore errors, or non-%NULL to report
487 * errors. An error can only occur when a new thread couldn't be
491 g_thread_pool_set_max_threads (GThreadPool *pool,
495 GRealThreadPool *real = (GRealThreadPool*) pool;
498 g_return_if_fail (real);
499 g_return_if_fail (real->running);
500 g_return_if_fail (!real->pool.exclusive || max_threads != -1);
501 g_return_if_fail (max_threads >= -1);
503 g_async_queue_lock (real->queue);
505 real->max_threads = max_threads;
508 to_start = real->max_threads - real->num_threads;
510 to_start = g_async_queue_length_unlocked (real->queue);
512 for ( ; to_start > 0; to_start--)
514 GError *local_error = NULL;
515 g_thread_pool_start_thread (real, &local_error);
518 g_propagate_error (error, local_error);
523 g_async_queue_unlock (real->queue);
527 * g_thread_pool_get_max_threads:
528 * @pool: a #GThreadPool
530 * Returns the maximal number of threads for @pool.
532 * Return value: the maximal number of threads
535 g_thread_pool_get_max_threads (GThreadPool *pool)
537 GRealThreadPool *real = (GRealThreadPool*) pool;
540 g_return_val_if_fail (real, 0);
541 g_return_val_if_fail (real->running, 0);
543 g_async_queue_lock (real->queue);
545 retval = real->max_threads;
547 g_async_queue_unlock (real->queue);
553 * g_thread_pool_get_num_threads:
554 * @pool: a #GThreadPool
556 * Returns the number of threads currently running in @pool.
558 * Return value: the number of threads currently running
561 g_thread_pool_get_num_threads (GThreadPool *pool)
563 GRealThreadPool *real = (GRealThreadPool*) pool;
566 g_return_val_if_fail (real, 0);
567 g_return_val_if_fail (real->running, 0);
569 g_async_queue_lock (real->queue);
571 retval = real->num_threads;
573 g_async_queue_unlock (real->queue);
579 * g_thread_pool_unprocessed:
580 * @pool: a #GThreadPool
582 * Returns the number of tasks still unprocessed in @pool.
584 * Return value: the number of unprocessed tasks
587 g_thread_pool_unprocessed (GThreadPool *pool)
589 GRealThreadPool *real = (GRealThreadPool*) pool;
592 g_return_val_if_fail (real, 0);
593 g_return_val_if_fail (real->running, 0);
595 unprocessed = g_async_queue_length (real->queue);
597 return MAX (unprocessed, 0);
601 * g_thread_pool_free:
602 * @pool: a #GThreadPool
603 * @immediate: should @pool shut down immediately?
604 * @wait: should the function wait for all tasks to be finished?
606 * Frees all resources allocated for @pool.
608 * If @immediate is %TRUE, no new task is processed for
609 * @pool. Otherwise @pool is not freed before the last task is
610 * processed. Note however, that no thread of this pool is
611 * interrupted, while processing a task. Instead at least all still
612 * running threads can finish their tasks before the @pool is freed.
614 * If @wait is %TRUE, the functions does not return before all tasks
615 * to be processed (dependent on @immediate, whether all or only the
616 * currently running) are ready. Otherwise the function returns immediately.
618 * After calling this function @pool must not be used anymore.
621 g_thread_pool_free (GThreadPool *pool,
625 GRealThreadPool *real = (GRealThreadPool*) pool;
627 g_return_if_fail (real);
628 g_return_if_fail (real->running);
629 /* It there's no thread allowed here, there is not much sense in
630 * not stopping this pool immediately, when it's not empty */
631 g_return_if_fail (immediate || real->max_threads != 0 ||
632 g_async_queue_length (real->queue) == 0);
634 g_async_queue_lock (real->queue);
636 real->running = FALSE;
637 real->immediate = immediate;
638 real->waiting = wait;
642 g_mutex_lock (inform_mutex);
643 while (g_async_queue_length_unlocked (real->queue) != -real->num_threads &&
644 !(immediate && real->num_threads == 0))
646 g_async_queue_unlock (real->queue);
647 g_cond_wait (inform_cond, inform_mutex);
648 g_async_queue_lock (real->queue);
650 g_mutex_unlock (inform_mutex);
654 g_async_queue_length_unlocked (real->queue) == -real->num_threads)
656 /* No thread is currently doing something (and nothing is left
657 * to process in the queue) */
658 if (real->num_threads == 0) /* No threads left, we clean up */
660 g_async_queue_unlock (real->queue);
661 g_thread_pool_free_internal (real);
665 g_thread_pool_wakeup_and_stop_all (real);
668 real->waiting = FALSE; /* The last thread should cleanup the pool */
669 g_async_queue_unlock (real->queue);
673 g_thread_pool_free_internal (GRealThreadPool* pool)
675 g_return_if_fail (pool);
676 g_return_if_fail (!pool->running);
677 g_return_if_fail (pool->num_threads == 0);
679 g_async_queue_unref (pool->queue);
685 g_thread_pool_wakeup_and_stop_all (GRealThreadPool* pool)
689 g_return_if_fail (pool);
690 g_return_if_fail (!pool->running);
691 g_return_if_fail (pool->num_threads != 0);
692 g_return_if_fail (g_async_queue_length_unlocked (pool->queue) ==
695 pool->immediate = TRUE;
696 for (i = 0; i < pool->num_threads; i++)
697 g_thread_pool_queue_push_unlocked (pool, GUINT_TO_POINTER (1));
701 * g_thread_pool_set_max_unused_threads:
702 * @max_threads: maximal number of unused threads
704 * Sets the maximal number of unused threads to @max_threads. If
705 * @max_threads is -1, no limit is imposed on the number of unused
709 g_thread_pool_set_max_unused_threads (gint max_threads)
711 g_return_if_fail (max_threads >= -1);
715 max_unused_threads = max_threads;
717 if (max_unused_threads < unused_threads && max_unused_threads != -1)
721 g_async_queue_lock (unused_thread_queue);
722 for (i = unused_threads - max_unused_threads; i > 0; i--)
723 g_async_queue_push_unlocked (unused_thread_queue,
724 stop_this_thread_marker);
725 g_async_queue_unlock (unused_thread_queue);
732 * g_thread_pool_get_max_unused_threads:
734 * Returns the maximal allowed number of unused threads.
736 * Return value: the maximal number of unused threads
739 g_thread_pool_get_max_unused_threads (void)
744 retval = max_unused_threads;
751 * g_thread_pool_get_num_unused_threads:
753 * Returns the number of currently unused threads.
755 * Return value: the number of currently unused threads
758 g_thread_pool_get_num_unused_threads (void)
763 retval = unused_threads;
770 * g_thread_pool_stop_unused_threads:
772 * Stops all currently unused threads. This does not change the
773 * maximal number of unused threads. This function can be used to
774 * regularly stop all unused threads e.g. from g_timeout_add().
777 g_thread_pool_stop_unused_threads (void)
779 guint oldval = g_thread_pool_get_max_unused_threads ();
780 g_thread_pool_set_max_unused_threads (0);
781 g_thread_pool_set_max_unused_threads (oldval);
785 * g_thread_pool_set_sort_function:
786 * @pool: a #GThreadPool
787 * @func: the #GCompareDataFunc used to sort the list of tasks.
788 * This function is passed two tasks. It should return
789 * 0 if the order in which they are handled does not matter,
790 * a negative value if the first task should be processed before
791 * the second or a positive value if the second task should be
793 * @user_data: user data passed to @func.
795 * Sets the function used to sort the list of tasks. This allows the
796 * tasks to be processed by a priority determined by @func, and not
797 * just in the order in which they were added to the pool.
802 g_thread_pool_set_sort_function (GThreadPool *pool,
803 GCompareDataFunc func,
806 GRealThreadPool *real = (GRealThreadPool*) pool;
808 g_return_if_fail (real);
809 g_return_if_fail (real->running);
811 g_async_queue_lock (real->queue);
813 real->sort_func = func;
814 real->sort_user_data = user_data;
817 g_async_queue_sort_unlocked (real->queue,
819 real->sort_user_data);
821 g_async_queue_unlock (real->queue);
825 * g_thread_pool_set_max_idle_time:
826 * @interval: the maximum @interval (1/1000ths of a second) a thread
829 * This function will set the maximum @interval that a thread waiting
830 * in the pool for new tasks can be idle for before being
831 * stopped. This function is similar to calling
832 * g_thread_pool_stop_unused_threads() on a regular timeout, except,
833 * this is done on a per thread basis.
835 * By setting @interval to 0, idle threads will not be stopped.
837 * This function makes use of g_async_queue_timed_pop () using
843 g_thread_pool_set_max_idle_time (guint interval)
846 max_idle_time = interval;
851 * g_thread_pool_get_max_idle_time:
853 * This function will return the maximum @interval that a thread will
854 * wait in the thread pool for new tasks before being stopped.
856 * If this function returns 0, threads waiting in the thread pool for
857 * new work are not stopped.
859 * Return value: the maximum @interval to wait for new tasks in the
860 * thread pool before stopping the thread (1/1000ths of a second).
865 g_thread_pool_get_max_idle_time (void)
870 retval = max_idle_time;
876 #define __G_THREADPOOL_C__
877 #include "galiasdef.c"