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.
29 typedef struct _GRealThreadPool GRealThreadPool;
31 struct _GRealThreadPool
43 /* The following is just an address to mark the stop order for a
44 * thread, it could be any address (as long, as it isn't a valid
45 * GThreadPool address) */
46 static const gpointer stop_this_thread_marker = (gpointer) &g_thread_pool_new;
48 /* Here all unused threads are waiting, depending on their priority */
49 static GAsyncQueue *unused_thread_queue[G_THREAD_PRIORITY_URGENT + 1][2];
50 static gint unused_threads = 0;
51 static gint max_unused_threads = 0;
52 G_LOCK_DEFINE_STATIC (unused_threads);
54 static GMutex *inform_mutex = NULL;
55 static GCond *inform_cond = NULL;
57 static void g_thread_pool_free_internal (GRealThreadPool* pool);
58 static gpointer g_thread_pool_thread_proxy (gpointer data);
59 static void g_thread_pool_start_thread (GRealThreadPool* pool,
61 static void g_thread_pool_wakeup_and_stop_all (GRealThreadPool* pool);
63 #define g_thread_should_run(pool, len) \
64 ((pool)->running || (!(pool)->immediate && (len) > 0))
67 g_thread_pool_thread_proxy (gpointer data)
69 GRealThreadPool *pool = data;
70 gboolean watcher = FALSE;
72 g_async_queue_lock (pool->queue);
76 gboolean goto_global_pool =
77 !pool->pool.exclusive && pool->stack_size == 0;
78 gint len = g_async_queue_length_unlocked (pool->queue);
80 if (g_thread_should_run (pool, len))
84 /* This thread is actually not needed here, but it waits
85 * for some time anyway. If during that time a new
86 * request arrives, this saves process
87 * swicthes. Otherwise the thread will go to the global
90 g_get_current_time (&end_time);
91 end_time.tv_usec += G_USEC_PER_SEC / 2; /* Halv a second */
92 if (end_time.tv_usec >= G_USEC_PER_SEC)
94 end_time.tv_usec -= G_USEC_PER_SEC;
98 task = g_async_queue_timed_pop_unlocked (pool->queue, &end_time);
102 task = g_async_queue_pop_unlocked (pool->queue);
108 if (pool->num_threads > pool->max_threads &&
109 pool->max_threads != -1)
110 /* We are in fact a superfluous threads, so we go to
111 * the global pool and just hand the data further to
112 * the next one waiting in the queue */
114 g_async_queue_push_unlocked (pool->queue, task);
115 goto_global_pool = TRUE;
117 else if (pool->running || !pool->immediate)
119 g_async_queue_unlock (pool->queue);
120 pool->pool.func (task, pool->pool.user_data);
121 g_async_queue_lock (pool->queue);
124 len = g_async_queue_length_unlocked (pool->queue);
127 if (!g_thread_should_run (pool, len))
129 g_cond_broadcast (inform_cond);
130 goto_global_pool = TRUE;
134 /* At this pool there are no threads waiting, but tasks are. */
135 goto_global_pool = FALSE;
137 else if (len == 0 && !watcher && !pool->pool.exclusive)
139 /* Here neither threads nor tasks are queued and we didn't
140 * just return from a timed wait. We now wait for a limited
141 * time at this pool for new tasks to avoid costly context
143 goto_global_pool = FALSE;
148 if (goto_global_pool)
150 GAsyncQueue *unused_queue =
151 unused_thread_queue[pool->pool.priority][pool->pool.bound ? 1 : 0];
154 if (!pool->running && !pool->waiting)
156 if (pool->num_threads == 0)
158 g_async_queue_unlock (pool->queue);
159 g_thread_pool_free_internal (pool);
161 else if (len == - pool->num_threads)
163 g_thread_pool_wakeup_and_stop_all (pool);
164 g_async_queue_unlock (pool->queue);
168 g_async_queue_unlock (pool->queue);
170 g_async_queue_lock (unused_queue);
172 G_LOCK (unused_threads);
173 if ((unused_threads >= max_unused_threads &&
174 max_unused_threads != -1) || pool->stack_size != 0)
176 G_UNLOCK (unused_threads);
177 g_async_queue_unlock (unused_queue);
178 /* Stop this thread */
182 G_UNLOCK (unused_threads);
184 pool = g_async_queue_pop_unlocked (unused_queue);
186 G_LOCK (unused_threads);
188 G_UNLOCK (unused_threads);
190 g_async_queue_unlock (unused_queue);
192 if (pool == stop_this_thread_marker)
193 /* Stop this thread */
196 g_async_queue_lock (pool->queue);
198 /* pool->num_threads++ is not done here, but in
199 * g_thread_pool_start_thread to make the new started thread
200 * known to the pool, before itself can do it. */
207 g_thread_pool_start_thread (GRealThreadPool *pool,
210 gboolean success = FALSE;
211 GThreadPriority priority = pool->pool.priority;
212 guint bound = pool->pool.bound ? 1 : 0;
213 GAsyncQueue *queue = unused_thread_queue[priority][bound];
215 if (pool->num_threads >= pool->max_threads && pool->max_threads != -1)
216 /* Enough threads are already running */
219 g_async_queue_lock (queue);
221 if (g_async_queue_length_unlocked (queue) < 0)
223 /* First we try a thread with the right priority */
224 g_async_queue_push_unlocked (queue, pool);
228 g_async_queue_unlock (queue);
230 /* We will not search for threads with other priorities, because changing
231 * priority is quite unportable */
235 GError *local_error = NULL;
236 /* No thread was found, we have to start a new one */
237 g_thread_create (g_thread_pool_thread_proxy, pool,
238 pool->stack_size, FALSE,
239 bound, priority, &local_error);
243 g_propagate_error (error, local_error);
248 /* See comment in g_thread_pool_thread_proxy as to why this is done
249 * here and not there */
255 * @func: a function to execute in the threads of the new thread pool
256 * @user_data: user data that is handed over to @func every time it
258 * @max_threads: the maximal number of threads to execute concurrently in
259 * the new thread pool, -1 means no limit
260 * @stack_size: the stack size for the threads of the new thread pool,
261 * 0 means using the standard
262 * @bound: should the threads of the new thread pool be bound?
263 * @priority: a priority for the threads of the new thread pool
264 * @exclusive: should this thread pool be exclusive?
265 * @error: return location for error
267 * This function creates a new thread pool. All threads created within
268 * this thread pool will have the priority @priority and the stack
269 * size @stack_size and will be bound if and only if @bound is
272 * Whenever you call g_thread_pool_push(), either a new thread is
273 * created or an unused one is reused. At most @max_threads threads
274 * are running concurrently for this thread pool. @max_threads = -1
275 * allows unlimited threads to be created for this thread pool. The
276 * newly created or reused thread now executes the function @func with
277 * the two arguments. The first one is the parameter to
278 * g_thread_pool_push() and the second one is @user_data.
280 * The parameter @exclusive determines, whether the thread pool owns
281 * all threads exclusive or whether the threads are shared
282 * globally. If @exclusive is @TRUE, @max_threads threads are started
283 * immediately and they will run exclusively for this thread pool until
284 * it is destroyed by g_thread_pool_free(). If @exclusive is @FALSE,
285 * threads are created, when needed and shared between all
286 * non-exclusive thread pools. This implies that @max_threads may not
287 * be -1 for exclusive thread pools.
289 * Note, that only threads from a thread pool with a @stack_size of 0
290 * (which means using the standard stack size) will be globally
291 * reused. Threads from a thread pool with a non-zero stack size will
292 * stay only in this thread pool until it is freed and can thus not be
293 * controlled by the g_thread_pool_set_unused_threads() function.
295 * @error can be NULL to ignore errors, or non-NULL to report
296 * errors. An error can only occur, when @exclusive is set to @TRUE and
297 * not all @max_threads threads could be created.
299 * Return value: the new #GThreadPool
302 g_thread_pool_new (GFunc func,
307 GThreadPriority priority,
311 GRealThreadPool *retval;
312 G_LOCK_DEFINE_STATIC (init);
314 g_return_val_if_fail (func, NULL);
315 g_return_val_if_fail (!exclusive || max_threads != -1, NULL);
316 g_return_val_if_fail (max_threads >= -1, NULL);
317 g_return_val_if_fail (g_thread_supported (), NULL);
319 retval = g_new (GRealThreadPool, 1);
321 retval->pool.func = func;
322 retval->pool.user_data = user_data;
323 retval->pool.bound = bound;
324 retval->pool.priority = priority;
325 retval->pool.exclusive = exclusive;
326 retval->stack_size = stack_size;
327 retval->queue = g_async_queue_new ();
328 retval->max_threads = max_threads;
329 retval->num_threads = 0;
330 retval->running = TRUE;
336 inform_mutex = g_mutex_new ();
337 inform_cond = g_cond_new ();
338 for (priority = G_THREAD_PRIORITY_LOW;
339 priority < G_THREAD_PRIORITY_URGENT + 1; priority++)
341 unused_thread_queue[priority][0] = g_async_queue_new ();
342 unused_thread_queue[priority][1] = g_async_queue_new ();
348 if (retval->pool.exclusive)
350 g_async_queue_lock (retval->queue);
352 while (retval->num_threads < retval->max_threads)
354 GError *local_error = NULL;
355 g_thread_pool_start_thread (retval, &local_error);
358 g_propagate_error (error, local_error);
363 g_async_queue_unlock (retval->queue);
366 return (GThreadPool*) retval;
370 * g_thread_pool_push:
371 * @pool: a #GThreadPool
372 * @data: a new task for @pool
373 * @error: return location for error
375 * Inserts @data into the list of tasks to be executed by @pool. When
376 * the number of currently running threads is lower than the maximal
377 * allowed number of threads, a new thread is started (or reused) with
378 * the properties given to g_thread_pool_new (). Otherwise @data stays
379 * in the queue until a thread in this pool finishes its previous task
380 * and processes @data.
382 * @error can be NULL to ignore errors, or non-NULL to report
383 * errors. An error can only occur, when a new thread couldn't be
384 * created. In that case @data is simply appended to the queue of work
388 g_thread_pool_push (GThreadPool *pool,
392 GRealThreadPool *real = (GRealThreadPool*) pool;
394 g_return_if_fail (real);
396 g_async_queue_lock (real->queue);
400 g_async_queue_unlock (real->queue);
401 g_return_if_fail (real->running);
404 if (g_async_queue_length_unlocked (real->queue) >= 0)
405 /* No thread is waiting in the queue */
406 g_thread_pool_start_thread (real, error);
408 g_async_queue_push_unlocked (real->queue, data);
409 g_async_queue_unlock (real->queue);
413 * g_thread_pool_set_max_threads:
414 * @pool: a #GThreadPool
415 * @max_threads: a new maximal number of threads for @pool
416 * @error: return location for error
418 * Sets the maximal allowed number of threads for @pool. A value of -1
419 * means, that the maximal number of threads is unlimited.
421 * Setting @max_threads to 0 means stopping all work for @pool. It is
422 * effectively frozen until @max_threads is set to a non-zero value
425 * A thread is never terminated while calling @func, as supplied by
426 * g_thread_pool_new (). Instead the maximal number of threads only
427 * has effect for the allocation of new threads in g_thread_pool_push
428 * (). A new thread is allocated, whenever the number of currently
429 * running threads in @pool is smaller than the maximal number.
431 * @error can be NULL to ignore errors, or non-NULL to report
432 * errors. An error can only occur, when a new thread couldn't be
436 g_thread_pool_set_max_threads (GThreadPool *pool,
440 GRealThreadPool *real = (GRealThreadPool*) pool;
443 g_return_if_fail (real);
444 g_return_if_fail (real->running);
445 g_return_if_fail (!real->pool.exclusive || max_threads != -1);
446 g_return_if_fail (max_threads >= -1);
448 g_async_queue_lock (real->queue);
450 real->max_threads = max_threads;
453 to_start = real->max_threads - real->num_threads;
455 to_start = g_async_queue_length_unlocked (real->queue);
457 for ( ; to_start > 0; to_start--)
459 GError *local_error = NULL;
460 g_thread_pool_start_thread (real, &local_error);
463 g_propagate_error (error, local_error);
468 g_async_queue_unlock (real->queue);
472 * g_thread_pool_get_max_threads:
473 * @pool: a #GThreadPool
475 * Returns the maximal number of threads for @pool.
477 * Return value: the maximal number of threads
480 g_thread_pool_get_max_threads (GThreadPool *pool)
482 GRealThreadPool *real = (GRealThreadPool*) pool;
485 g_return_val_if_fail (real, 0);
486 g_return_val_if_fail (real->running, 0);
488 g_async_queue_lock (real->queue);
490 retval = real->max_threads;
492 g_async_queue_unlock (real->queue);
498 * g_thread_pool_get_num_threads:
499 * @pool: a #GThreadPool
501 * Returns the number of threads currently running in @pool.
503 * Return value: the number of threads currently running
506 g_thread_pool_get_num_threads (GThreadPool *pool)
508 GRealThreadPool *real = (GRealThreadPool*) pool;
511 g_return_val_if_fail (real, 0);
512 g_return_val_if_fail (real->running, 0);
514 g_async_queue_lock (real->queue);
516 retval = real->num_threads;
518 g_async_queue_unlock (real->queue);
524 * g_thread_pool_unprocessed:
525 * @pool: a #GThreadPool
527 * Returns the number of tasks still unprocessed in @pool.
529 * Return value: the number of unprocessed tasks
532 g_thread_pool_unprocessed (GThreadPool *pool)
534 GRealThreadPool *real = (GRealThreadPool*) pool;
537 g_return_val_if_fail (real, 0);
538 g_return_val_if_fail (real->running, 0);
540 unprocessed = g_async_queue_length (real->queue);
542 return MAX (unprocessed, 0);
546 * g_thread_pool_free:
547 * @pool: a #GThreadPool
548 * @immediate: should @pool shut down immediately?
549 * @wait: should the function wait for all tasks to be finished?
551 * Frees all resources allocated for @pool.
553 * If @immediate is #TRUE, no new task is processed for
554 * @pool. Otherwise @pool is not freed before the last task is
555 * processed. Note however, that no thread of this pool is
556 * interrupted, while processing a task. Instead at least all still
557 * running threads can finish their tasks before the @pool is freed.
559 * If @wait is #TRUE, the functions does not return before all tasks
560 * to be processed (dependent on @immediate, whether all or only the
561 * currently running) are ready. Otherwise the function returns immediately.
563 * After calling this function @pool must not be used anymore.
566 g_thread_pool_free (GThreadPool *pool,
570 GRealThreadPool *real = (GRealThreadPool*) pool;
572 g_return_if_fail (real);
573 g_return_if_fail (real->running);
574 /* It there's no thread allowed here, there is not much sense in
575 * not stopping this pool immediately, when it's not empty */
576 g_return_if_fail (immediate || real->max_threads != 0 ||
577 g_async_queue_length (real->queue) == 0);
579 g_async_queue_lock (real->queue);
581 real->running = FALSE;
582 real->immediate = immediate;
583 real->waiting = wait;
587 g_mutex_lock (inform_mutex);
588 while (g_async_queue_length_unlocked (real->queue) != -real->num_threads)
590 g_async_queue_unlock (real->queue);
591 g_cond_wait (inform_cond, inform_mutex);
592 g_async_queue_lock (real->queue);
594 g_mutex_unlock (inform_mutex);
597 if (g_async_queue_length_unlocked (real->queue) == -real->num_threads)
599 /* No thread is currently doing something (and nothing is left
600 * to process in the queue) */
601 if (real->num_threads == 0) /* No threads left, we clean up */
603 g_async_queue_unlock (real->queue);
604 g_thread_pool_free_internal (real);
608 g_thread_pool_wakeup_and_stop_all (real);
611 real->waiting = FALSE; /* The last thread should cleanup the pool */
612 g_async_queue_unlock (real->queue);
616 g_thread_pool_free_internal (GRealThreadPool* pool)
618 g_return_if_fail (pool);
619 g_return_if_fail (!pool->running);
620 g_return_if_fail (pool->num_threads == 0);
622 g_async_queue_unref (pool->queue);
628 g_thread_pool_wakeup_and_stop_all (GRealThreadPool* pool)
632 g_return_if_fail (pool);
633 g_return_if_fail (!pool->running);
634 g_return_if_fail (pool->num_threads != 0);
635 g_return_if_fail (g_async_queue_length_unlocked (pool->queue) ==
638 pool->immediate = TRUE;
639 for (i = 0; i < pool->num_threads; i++)
640 g_async_queue_push_unlocked (pool->queue, GUINT_TO_POINTER (1));
644 * g_thread_pool_set_max_unused_threads:
645 * @max_threads: maximal number of unused threads
647 * Sets the maximal number of unused threads to @max_threads. If
648 * @max_threads is -1, no limit is imposed on the number of unused
652 g_thread_pool_set_max_unused_threads (gint max_threads)
654 g_return_if_fail (max_threads >= -1);
656 G_LOCK (unused_threads);
658 max_unused_threads = max_threads;
660 if (max_unused_threads < unused_threads && max_unused_threads != -1)
662 guint close_down_num = unused_threads - max_unused_threads;
664 while (close_down_num > 0)
666 GThreadPriority priority;
669 guint old_close_down_num = close_down_num;
670 for (priority = G_THREAD_PRIORITY_LOW;
671 priority < G_THREAD_PRIORITY_URGENT + 1 && close_down_num > 0;
674 for (bound = 0; bound < 2; bound++)
676 GAsyncQueue *queue = unused_thread_queue[priority][bound];
677 g_async_queue_lock (queue);
679 if (g_async_queue_length_unlocked (queue) < 0)
681 g_async_queue_push_unlocked (queue,
682 stop_this_thread_marker);
686 g_async_queue_unlock (queue);
690 /* Just to make sure, there are no counting problems */
691 g_assert (old_close_down_num != close_down_num);
695 G_UNLOCK (unused_threads);
699 * g_thread_pool_get_max_unused_threads:
701 * Returns the maximal allowed number of unused threads.
703 * Return value: the maximal number of unused threads
706 g_thread_pool_get_max_unused_threads (void)
710 G_LOCK (unused_threads);
711 retval = max_unused_threads;
712 G_UNLOCK (unused_threads);
718 * g_thread_pool_get_num_unused_threads:
720 * Returns the number of currently unused threads.
722 * Return value: the number of currently unused threads
724 guint g_thread_pool_get_num_unused_threads (void)
728 G_LOCK (unused_threads);
729 retval = unused_threads;
730 G_UNLOCK (unused_threads);
736 * g_thread_pool_stop_unused_threads:
738 * Stops all currently unused threads. This does not change the
739 * maximal number of unused threads. This function can be used to
740 * regularly stop all unused threads e.g. from g_timeout_add().
742 void g_thread_pool_stop_unused_threads (void)
744 guint oldval = g_thread_pool_get_max_unused_threads ();
745 g_thread_pool_set_max_unused_threads (0);
746 g_thread_pool_set_max_unused_threads (oldval);