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 typedef struct _GRealThreadPool GRealThreadPool;
35 struct _GRealThreadPool
44 GCompareDataFunc sort_func;
45 gpointer sort_user_data;
48 /* The following is just an address to mark the stop order for a
49 * thread, it could be any address (as long, as it isn't a valid
50 * GThreadPool address) */
51 static const gpointer stop_this_thread_marker = (gpointer) &g_thread_pool_new;
53 /* Here all unused threads are waiting */
54 static GAsyncQueue *unused_thread_queue;
55 static gint unused_threads = 0;
56 static gint max_unused_threads = 0;
57 G_LOCK_DEFINE_STATIC (unused_threads);
59 static GMutex *inform_mutex = NULL;
60 static GCond *inform_cond = NULL;
63 static void g_thread_pool_queue_push_unlocked (GRealThreadPool *pool,
65 static void g_thread_pool_free_internal (GRealThreadPool *pool);
66 static gpointer g_thread_pool_thread_proxy (gpointer data);
67 static void g_thread_pool_start_thread (GRealThreadPool *pool,
69 static void g_thread_pool_wakeup_and_stop_all (GRealThreadPool *pool);
72 #define g_thread_should_run(pool, len) \
73 ((pool)->running || (!(pool)->immediate && (len) > 0))
77 g_thread_pool_queue_push_unlocked (GRealThreadPool *pool,
81 g_async_queue_push_sorted_unlocked (pool->queue,
84 pool->sort_user_data);
86 g_async_queue_push_unlocked (pool->queue, data);
90 g_thread_pool_thread_proxy (gpointer data)
92 GRealThreadPool *pool = data;
93 gboolean watcher = FALSE;
95 g_async_queue_lock (pool->queue);
99 gboolean goto_global_pool = !pool->pool.exclusive;
100 gint len = g_async_queue_length_unlocked (pool->queue);
102 if (g_thread_should_run (pool, len))
106 /* This thread is actually not needed here, but it waits
107 * for some time anyway. If during that time a new
108 * request arrives, this saves process
109 * swicthes. Otherwise the thread will go to the global
112 g_get_current_time (&end_time);
113 g_time_val_add (&end_time, G_USEC_PER_SEC / 2); /* 1/2 second */
114 task = g_async_queue_timed_pop_unlocked (pool->queue, &end_time);
117 task = g_async_queue_pop_unlocked (pool->queue);
122 if (pool->num_threads > pool->max_threads &&
123 pool->max_threads != -1)
124 /* We are in fact a superfluous threads, so we go to
125 * the global pool and just hand the data further to
126 * the next one waiting in the queue */
128 g_thread_pool_queue_push_unlocked (pool, task);
129 goto_global_pool = TRUE;
131 else if (pool->running || !pool->immediate)
133 g_async_queue_unlock (pool->queue);
134 pool->pool.func (task, pool->pool.user_data);
135 g_async_queue_lock (pool->queue);
138 len = g_async_queue_length_unlocked (pool->queue);
141 if (!g_thread_should_run (pool, len))
143 g_cond_broadcast (inform_cond);
144 goto_global_pool = TRUE;
148 /* At this pool there are no threads waiting, but tasks are. */
149 goto_global_pool = FALSE;
151 else if (len == 0 && !watcher && !pool->pool.exclusive)
153 /* Here neither threads nor tasks are queued and we didn't
154 * just return from a timed wait. We now wait for a limited
155 * time at this pool for new tasks to avoid costly context
157 goto_global_pool = FALSE;
161 if (goto_global_pool)
165 if (!pool->running && !pool->waiting)
167 if (pool->num_threads == 0)
169 g_async_queue_unlock (pool->queue);
170 g_thread_pool_free_internal (pool);
174 if (len == - pool->num_threads)
175 g_thread_pool_wakeup_and_stop_all (pool);
177 g_async_queue_unlock (pool->queue);
181 g_async_queue_unlock (pool->queue);
183 g_async_queue_lock (unused_thread_queue);
185 G_LOCK (unused_threads);
186 if ((unused_threads >= max_unused_threads &&
187 max_unused_threads != -1))
189 G_UNLOCK (unused_threads);
190 g_async_queue_unlock (unused_thread_queue);
191 /* Stop this thread */
195 G_UNLOCK (unused_threads);
197 pool = g_async_queue_pop_unlocked (unused_thread_queue);
199 G_LOCK (unused_threads);
201 G_UNLOCK (unused_threads);
203 g_async_queue_unlock (unused_thread_queue);
205 if (pool == stop_this_thread_marker)
206 /* Stop this thread */
209 g_async_queue_lock (pool->queue);
211 /* pool->num_threads++ is not done here, but in
212 * g_thread_pool_start_thread to make the new started thread
213 * known to the pool, before itself can do it. */
220 g_thread_pool_start_thread (GRealThreadPool *pool,
223 gboolean success = FALSE;
225 if (pool->num_threads >= pool->max_threads && pool->max_threads != -1)
226 /* Enough threads are already running */
229 g_async_queue_lock (unused_thread_queue);
231 if (g_async_queue_length_unlocked (unused_thread_queue) < 0)
233 g_async_queue_push_unlocked (unused_thread_queue, pool);
237 g_async_queue_unlock (unused_thread_queue);
241 GError *local_error = NULL;
242 /* No thread was found, we have to start a new one */
243 g_thread_create (g_thread_pool_thread_proxy, pool, FALSE, &local_error);
247 g_propagate_error (error, local_error);
252 /* See comment in g_thread_pool_thread_proxy as to why this is done
253 * here and not there */
259 * @func: a function to execute in the threads of the new thread pool
260 * @user_data: user data that is handed over to @func every time it
262 * @max_threads: the maximal number of threads to execute concurrently in
263 * the new thread pool, -1 means no limit
264 * @exclusive: should this thread pool be exclusive?
265 * @error: return location for error
267 * This function creates a new thread pool.
269 * Whenever you call g_thread_pool_push(), either a new thread is
270 * created or an unused one is reused. At most @max_threads threads
271 * are running concurrently for this thread pool. @max_threads = -1
272 * allows unlimited threads to be created for this thread pool. The
273 * newly created or reused thread now executes the function @func with
274 * the two arguments. The first one is the parameter to
275 * g_thread_pool_push() and the second one is @user_data.
277 * The parameter @exclusive determines, whether the thread pool owns
278 * all threads exclusive or whether the threads are shared
279 * globally. If @exclusive is %TRUE, @max_threads threads are started
280 * immediately and they will run exclusively for this thread pool until
281 * it is destroyed by g_thread_pool_free(). If @exclusive is %FALSE,
282 * threads are created, when needed and shared between all
283 * non-exclusive thread pools. This implies that @max_threads may not
284 * be -1 for exclusive thread pools.
286 * @error can be %NULL to ignore errors, or non-%NULL to report
287 * errors. An error can only occur when @exclusive is set to %TRUE and
288 * not all @max_threads threads could be created.
290 * Return value: the new #GThreadPool
293 g_thread_pool_new (GFunc func,
299 GRealThreadPool *retval;
300 G_LOCK_DEFINE_STATIC (init);
302 g_return_val_if_fail (func, NULL);
303 g_return_val_if_fail (!exclusive || max_threads != -1, NULL);
304 g_return_val_if_fail (max_threads >= -1, NULL);
305 g_return_val_if_fail (g_thread_supported (), NULL);
307 retval = g_new (GRealThreadPool, 1);
309 retval->pool.func = func;
310 retval->pool.user_data = user_data;
311 retval->pool.exclusive = exclusive;
312 retval->queue = g_async_queue_new ();
313 retval->max_threads = max_threads;
314 retval->num_threads = 0;
315 retval->running = TRUE;
316 retval->sort_func = NULL;
317 retval->sort_user_data = NULL;
323 inform_mutex = g_mutex_new ();
324 inform_cond = g_cond_new ();
325 unused_thread_queue = g_async_queue_new ();
330 if (retval->pool.exclusive)
332 g_async_queue_lock (retval->queue);
334 while (retval->num_threads < retval->max_threads)
336 GError *local_error = NULL;
337 g_thread_pool_start_thread (retval, &local_error);
340 g_propagate_error (error, local_error);
345 g_async_queue_unlock (retval->queue);
348 return (GThreadPool*) retval;
352 * g_thread_pool_push:
353 * @pool: a #GThreadPool
354 * @data: a new task for @pool
355 * @error: return location for error
357 * Inserts @data into the list of tasks to be executed by @pool. When
358 * the number of currently running threads is lower than the maximal
359 * allowed number of threads, a new thread is started (or reused) with
360 * the properties given to g_thread_pool_new (). Otherwise @data stays
361 * in the queue until a thread in this pool finishes its previous task
362 * and processes @data.
364 * @error can be %NULL to ignore errors, or non-%NULL to report
365 * errors. An error can only occur when a new thread couldn't be
366 * created. In that case @data is simply appended to the queue of work
370 g_thread_pool_push (GThreadPool *pool,
374 GRealThreadPool *real = (GRealThreadPool*) pool;
376 g_return_if_fail (real);
378 g_async_queue_lock (real->queue);
382 g_async_queue_unlock (real->queue);
383 g_return_if_fail (real->running);
386 if (g_async_queue_length_unlocked (real->queue) >= 0)
387 /* No thread is waiting in the queue */
388 g_thread_pool_start_thread (real, error);
390 g_thread_pool_queue_push_unlocked (real, data);
391 g_async_queue_unlock (real->queue);
395 * g_thread_pool_set_max_threads:
396 * @pool: a #GThreadPool
397 * @max_threads: a new maximal number of threads for @pool
398 * @error: return location for error
400 * Sets the maximal allowed number of threads for @pool. A value of -1
401 * means, that the maximal number of threads is unlimited.
403 * Setting @max_threads to 0 means stopping all work for @pool. It is
404 * effectively frozen until @max_threads is set to a non-zero value
407 * A thread is never terminated while calling @func, as supplied by
408 * g_thread_pool_new (). Instead the maximal number of threads only
409 * has effect for the allocation of new threads in g_thread_pool_push().
410 * A new thread is allocated, whenever the number of currently
411 * running threads in @pool is smaller than the maximal number.
413 * @error can be %NULL to ignore errors, or non-%NULL to report
414 * errors. An error can only occur when a new thread couldn't be
418 g_thread_pool_set_max_threads (GThreadPool *pool,
422 GRealThreadPool *real = (GRealThreadPool*) pool;
425 g_return_if_fail (real);
426 g_return_if_fail (real->running);
427 g_return_if_fail (!real->pool.exclusive || max_threads != -1);
428 g_return_if_fail (max_threads >= -1);
430 g_async_queue_lock (real->queue);
432 real->max_threads = max_threads;
435 to_start = real->max_threads - real->num_threads;
437 to_start = g_async_queue_length_unlocked (real->queue);
439 for ( ; to_start > 0; to_start--)
441 GError *local_error = NULL;
442 g_thread_pool_start_thread (real, &local_error);
445 g_propagate_error (error, local_error);
450 g_async_queue_unlock (real->queue);
454 * g_thread_pool_get_max_threads:
455 * @pool: a #GThreadPool
457 * Returns the maximal number of threads for @pool.
459 * Return value: the maximal number of threads
462 g_thread_pool_get_max_threads (GThreadPool *pool)
464 GRealThreadPool *real = (GRealThreadPool*) pool;
467 g_return_val_if_fail (real, 0);
468 g_return_val_if_fail (real->running, 0);
470 g_async_queue_lock (real->queue);
472 retval = real->max_threads;
474 g_async_queue_unlock (real->queue);
480 * g_thread_pool_get_num_threads:
481 * @pool: a #GThreadPool
483 * Returns the number of threads currently running in @pool.
485 * Return value: the number of threads currently running
488 g_thread_pool_get_num_threads (GThreadPool *pool)
490 GRealThreadPool *real = (GRealThreadPool*) pool;
493 g_return_val_if_fail (real, 0);
494 g_return_val_if_fail (real->running, 0);
496 g_async_queue_lock (real->queue);
498 retval = real->num_threads;
500 g_async_queue_unlock (real->queue);
506 * g_thread_pool_unprocessed:
507 * @pool: a #GThreadPool
509 * Returns the number of tasks still unprocessed in @pool.
511 * Return value: the number of unprocessed tasks
514 g_thread_pool_unprocessed (GThreadPool *pool)
516 GRealThreadPool *real = (GRealThreadPool*) pool;
519 g_return_val_if_fail (real, 0);
520 g_return_val_if_fail (real->running, 0);
522 unprocessed = g_async_queue_length (real->queue);
524 return MAX (unprocessed, 0);
528 * g_thread_pool_free:
529 * @pool: a #GThreadPool
530 * @immediate: should @pool shut down immediately?
531 * @wait: should the function wait for all tasks to be finished?
533 * Frees all resources allocated for @pool.
535 * If @immediate is %TRUE, no new task is processed for
536 * @pool. Otherwise @pool is not freed before the last task is
537 * processed. Note however, that no thread of this pool is
538 * interrupted, while processing a task. Instead at least all still
539 * running threads can finish their tasks before the @pool is freed.
541 * If @wait is %TRUE, the functions does not return before all tasks
542 * to be processed (dependent on @immediate, whether all or only the
543 * currently running) are ready. Otherwise the function returns immediately.
545 * After calling this function @pool must not be used anymore.
548 g_thread_pool_free (GThreadPool *pool,
552 GRealThreadPool *real = (GRealThreadPool*) pool;
554 g_return_if_fail (real);
555 g_return_if_fail (real->running);
556 /* It there's no thread allowed here, there is not much sense in
557 * not stopping this pool immediately, when it's not empty */
558 g_return_if_fail (immediate || real->max_threads != 0 ||
559 g_async_queue_length (real->queue) == 0);
561 g_async_queue_lock (real->queue);
563 real->running = FALSE;
564 real->immediate = immediate;
565 real->waiting = wait;
569 g_mutex_lock (inform_mutex);
570 while (g_async_queue_length_unlocked (real->queue) != -real->num_threads &&
571 !(immediate && real->num_threads == 0))
573 g_async_queue_unlock (real->queue);
574 g_cond_wait (inform_cond, inform_mutex);
575 g_async_queue_lock (real->queue);
577 g_mutex_unlock (inform_mutex);
581 g_async_queue_length_unlocked (real->queue) == -real->num_threads)
583 /* No thread is currently doing something (and nothing is left
584 * to process in the queue) */
585 if (real->num_threads == 0) /* No threads left, we clean up */
587 g_async_queue_unlock (real->queue);
588 g_thread_pool_free_internal (real);
592 g_thread_pool_wakeup_and_stop_all (real);
595 real->waiting = FALSE; /* The last thread should cleanup the pool */
596 g_async_queue_unlock (real->queue);
600 g_thread_pool_free_internal (GRealThreadPool* pool)
602 g_return_if_fail (pool);
603 g_return_if_fail (!pool->running);
604 g_return_if_fail (pool->num_threads == 0);
606 g_async_queue_unref (pool->queue);
612 g_thread_pool_wakeup_and_stop_all (GRealThreadPool* pool)
616 g_return_if_fail (pool);
617 g_return_if_fail (!pool->running);
618 g_return_if_fail (pool->num_threads != 0);
619 g_return_if_fail (g_async_queue_length_unlocked (pool->queue) ==
622 pool->immediate = TRUE;
623 for (i = 0; i < pool->num_threads; i++)
624 g_thread_pool_queue_push_unlocked (pool, GUINT_TO_POINTER (1));
628 * g_thread_pool_set_max_unused_threads:
629 * @max_threads: maximal number of unused threads
631 * Sets the maximal number of unused threads to @max_threads. If
632 * @max_threads is -1, no limit is imposed on the number of unused
636 g_thread_pool_set_max_unused_threads (gint max_threads)
638 g_return_if_fail (max_threads >= -1);
640 G_LOCK (unused_threads);
642 max_unused_threads = max_threads;
644 if (max_unused_threads < unused_threads && max_unused_threads != -1)
648 g_async_queue_lock (unused_thread_queue);
649 for (i = unused_threads - max_unused_threads; i > 0; i--)
650 g_async_queue_push_unlocked (unused_thread_queue,
651 stop_this_thread_marker);
652 g_async_queue_unlock (unused_thread_queue);
655 G_UNLOCK (unused_threads);
659 * g_thread_pool_get_max_unused_threads:
661 * Returns the maximal allowed number of unused threads.
663 * Return value: the maximal number of unused threads
666 g_thread_pool_get_max_unused_threads (void)
670 G_LOCK (unused_threads);
671 retval = max_unused_threads;
672 G_UNLOCK (unused_threads);
678 * g_thread_pool_get_num_unused_threads:
680 * Returns the number of currently unused threads.
682 * Return value: the number of currently unused threads
684 guint g_thread_pool_get_num_unused_threads (void)
688 G_LOCK (unused_threads);
689 retval = unused_threads;
690 G_UNLOCK (unused_threads);
696 * g_thread_pool_stop_unused_threads:
698 * Stops all currently unused threads. This does not change the
699 * maximal number of unused threads. This function can be used to
700 * regularly stop all unused threads e.g. from g_timeout_add().
702 void g_thread_pool_stop_unused_threads (void)
704 guint oldval = g_thread_pool_get_max_unused_threads ();
705 g_thread_pool_set_max_unused_threads (0);
706 g_thread_pool_set_max_unused_threads (oldval);
710 * g_thread_pool_set_sort_function:
711 * @pool: a #GThreadPool
712 * @func: the #GCompareDataFunc used to sort the list of tasks.
713 * This function is passed two tasks. It should return
714 * 0 if the order in which they are handled does not matter,
715 * a negative value if the first task should be processed before
716 * the second or a positive value if the second task should be
718 * @user_data: user data passed to @func.
720 * Sets the function used to sort the list of tasks. This allows the
721 * tasks to be processed by a priority determined by @func, and not
722 * just in the order in which they were added to the pool.
726 void g_thread_pool_set_sort_function (GThreadPool *pool,
727 GCompareDataFunc func,
730 GRealThreadPool *real = (GRealThreadPool*) pool;
732 g_return_if_fail (real);
733 g_return_if_fail (real->running);
735 g_async_queue_lock (real->queue);
737 real->sort_func = func;
738 real->sort_user_data = user_data;
741 g_async_queue_sort_unlocked (real->queue,
743 real->sort_user_data);
745 g_async_queue_unlock (real->queue);
748 #define __G_THREADPOOL_C__
749 #include "galiasdef.c"