fixed a typo in a comment.
[glib.git] / gthreadpool.c
blobaf62d89b2bfc254f0bcc248394b5aa90d2be64b5
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.
24 * MT safe
27 #include "glib.h"
29 typedef struct _GRealThreadPool GRealThreadPool;
31 struct _GRealThreadPool
33 GThreadPool pool;
34 GAsyncQueue* queue;
35 gint max_threads;
36 gint num_threads;
37 gboolean running;
38 gboolean immediate;
39 gboolean waiting;
42 /* The following is just an address to mark the stop order for a
43 * thread, it could be any address (as long, as it isn't a valid
44 * GThreadPool address) */
45 static const gpointer stop_this_thread_marker = (gpointer) &g_thread_pool_new;
47 /* Here all unused threads are waiting, depending on their priority */
48 static GAsyncQueue *unused_thread_queue[G_THREAD_PRIORITY_URGENT + 1][2];
49 static gint unused_threads = 0;
50 static gint max_unused_threads = 0;
51 G_LOCK_DEFINE_STATIC (unused_threads);
53 static GMutex *inform_mutex = NULL;
54 static GCond *inform_cond = NULL;
56 static void g_thread_pool_free_internal (GRealThreadPool* pool);
57 static void g_thread_pool_thread_proxy (gpointer data);
58 static void g_thread_pool_start_thread (GRealThreadPool* pool, GError **error);
59 static void g_thread_pool_wakeup_and_stop_all (GRealThreadPool* pool);
61 #define g_thread_should_run(pool, len) \
62 ((pool)->running || (!(pool)->immediate && (len) > 0))
64 static void
65 g_thread_pool_thread_proxy (gpointer data)
67 GRealThreadPool *pool = data;
68 gboolean watcher = FALSE;
70 g_async_queue_lock (pool->queue);
71 while (TRUE)
73 gpointer task;
74 gboolean goto_global_pool =
75 !pool->pool.exclusive && pool->pool.stack_size == 0;
76 gint len = g_async_queue_length_unlocked (pool->queue);
78 if (g_thread_should_run (pool, len))
80 if (watcher)
82 /* This thread is actually not needed here, but it waits
83 * for some time anyway. If during that time a new
84 * request arrives, this saves process
85 * swicthes. Otherwise the thread will go to the global
86 * pool afterwards */
87 GTimeVal end_time;
88 g_get_current_time (&end_time);
89 end_time.tv_usec += G_USEC_PER_SEC / 2; /* Halv a second */
90 if (end_time.tv_usec >= G_USEC_PER_SEC)
92 end_time.tv_usec -= G_USEC_PER_SEC;
93 end_time.tv_sec += 1;
96 task = g_async_queue_timed_pop_unlocked (pool->queue, &end_time);
98 else
100 task = g_async_queue_pop_unlocked (pool->queue);
103 if (task)
105 watcher = FALSE;
106 if (pool->num_threads > pool->max_threads &&
107 pool->max_threads != -1)
108 /* We are in fact a superfluous threads, so we go to
109 * the global pool and just hand the data further to
110 * the next one waiting in the queue */
112 g_async_queue_push_unlocked (pool->queue, task);
113 goto_global_pool = TRUE;
115 else if (pool->running || !pool->immediate)
117 g_async_queue_unlock (pool->queue);
118 pool->pool.thread_func (task, pool->pool.user_data);
119 g_async_queue_lock (pool->queue);
122 len = g_async_queue_length_unlocked (pool->queue);
125 if (!g_thread_should_run (pool, len))
127 g_cond_broadcast (inform_cond);
128 goto_global_pool = TRUE;
130 else if (len > 0)
132 /* At this pool there are no threads waiting, but tasks are. */
133 goto_global_pool = FALSE;
135 else if (len == 0 && !watcher && !pool->pool.exclusive)
137 /* Here neither threads nor tasks are queued and we didn't
138 * just return from a timed wait. We now wait for a limited
139 * time at this pool for new tasks to avoid costly context
140 * switches. */
141 goto_global_pool = FALSE;
142 watcher = TRUE;
146 if (goto_global_pool)
148 GAsyncQueue *unused_queue =
149 unused_thread_queue[pool->pool.priority][pool->pool.bound ? 1 : 0];
150 pool->num_threads--;
152 if (!pool->running && !pool->waiting)
154 if (pool->num_threads == 0)
156 g_async_queue_unlock (pool->queue);
157 g_thread_pool_free_internal (pool);
159 else if (len == - pool->num_threads)
161 g_thread_pool_wakeup_and_stop_all (pool);
162 g_async_queue_unlock (pool->queue);
165 else
166 g_async_queue_unlock (pool->queue);
168 g_async_queue_lock (unused_queue);
170 G_LOCK (unused_threads);
171 if ((unused_threads >= max_unused_threads &&
172 max_unused_threads != -1) || pool->pool.stack_size != 0)
174 G_UNLOCK (unused_threads);
175 g_async_queue_unlock (unused_queue);
176 /* Stop this thread */
177 return;
179 unused_threads++;
180 G_UNLOCK (unused_threads);
182 pool = g_async_queue_pop_unlocked (unused_queue);
184 G_LOCK (unused_threads);
185 unused_threads--;
186 G_UNLOCK (unused_threads);
188 g_async_queue_unlock (unused_queue);
190 if (pool == stop_this_thread_marker)
191 /* Stop this thread */
192 return;
194 g_async_queue_lock (pool->queue);
196 /* pool->num_threads++ is not done here, but in
197 * g_thread_pool_start_thread to make the new started thread
198 * known to the pool, before itself can do it. */
203 static void
204 g_thread_pool_start_thread (GRealThreadPool *pool,
205 GError **error)
207 gboolean success = FALSE;
208 GThreadPriority priority = pool->pool.priority;
209 guint bound = pool->pool.bound ? 1 : 0;
210 GAsyncQueue *queue = unused_thread_queue[priority][bound];
212 if (pool->num_threads >= pool->max_threads && pool->max_threads != -1)
213 /* Enough threads are already running */
214 return;
216 g_async_queue_lock (queue);
218 if (g_async_queue_length_unlocked (queue) < 0)
220 /* First we try a thread with the right priority */
221 g_async_queue_push_unlocked (queue, pool);
222 success = TRUE;
225 g_async_queue_unlock (queue);
227 /* We will not search for threads with other priorities, because changing
228 * priority is quite unportable */
230 if (!success)
232 GError *local_error = NULL;
233 /* No thread was found, we have to start a new one */
234 g_thread_create (g_thread_pool_thread_proxy, pool,
235 pool->pool.stack_size, FALSE,
236 bound, priority, &local_error);
238 if (local_error)
240 g_propagate_error (error, local_error);
241 return;
245 /* See comment in g_thread_pool_thread_proxy as to why this is done
246 * here and not there */
247 pool->num_threads++;
251 * g_thread_pool_new:
252 * @thread_func: a function to execute in the threads of the new thread pool
253 * @max_threads: the maximal number of threads to execute concurrently in
254 * the new thread pool, -1 means no limit
255 * @stack_size: the stack size for the threads of the new thread pool,
256 * 0 means using the standard
257 * @bound: should the threads of the new thread pool be bound?
258 * @priority: a priority for the threads of the new thread pool
259 * @exclusive: should this thread pool be exclusive?
260 * @user_data: user data that is handed over to @thread_func every time it
261 * is called
262 * @error: return location for error
264 * This function creates a new thread pool. All threads created within
265 * this thread pool will have the priority @priority and the stack
266 * size @stack_size and will be bound if and only if @bound is
267 * true.
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
274 * @thread_func with the two arguments. The first one is the parameter
275 * to 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 * Note, that only threads from a thread pool with a @stack_size of 0
287 * (which means using the standard stack size) will be globally
288 * reused. Threads from a thread pool with a non-zero stack size will
289 * stay only in this thread pool until it is freed and can thus not be
290 * controlled by the g_thread_pool_set_unused_threads() function.
292 * @error can be NULL to ignore errors, or non-NULL to report
293 * errors. An error can only occur, when @exclusive is set to @TRUE and
294 * not all @max_threads threads could be created.
296 * Return value: the new #GThreadPool
298 GThreadPool*
299 g_thread_pool_new (GFunc thread_func,
300 gint max_threads,
301 gulong stack_size,
302 gboolean bound,
303 GThreadPriority priority,
304 gboolean exclusive,
305 gpointer user_data,
306 GError **error)
308 GRealThreadPool *retval;
309 G_LOCK_DEFINE_STATIC (init);
311 g_return_val_if_fail (thread_func, NULL);
312 g_return_val_if_fail (!exclusive || max_threads != -1, NULL);
313 g_return_val_if_fail (max_threads >= -1, NULL);
314 g_return_val_if_fail (g_thread_supported (), NULL);
316 retval = g_new (GRealThreadPool, 1);
318 retval->pool.thread_func = thread_func;
319 retval->pool.stack_size = stack_size;
320 retval->pool.bound = bound;
321 retval->pool.priority = priority;
322 retval->pool.exclusive = exclusive;
323 retval->pool.user_data = user_data;
324 retval->queue = g_async_queue_new ();
325 retval->max_threads = max_threads;
326 retval->num_threads = 0;
327 retval->running = TRUE;
329 G_LOCK (init);
331 if (!inform_mutex)
333 inform_mutex = g_mutex_new ();
334 inform_cond = g_cond_new ();
335 for (priority = G_THREAD_PRIORITY_LOW;
336 priority < G_THREAD_PRIORITY_URGENT + 1; priority++)
338 unused_thread_queue[priority][0] = g_async_queue_new ();
339 unused_thread_queue[priority][1] = g_async_queue_new ();
343 G_UNLOCK (init);
345 if (retval->pool.exclusive)
347 g_async_queue_lock (retval->queue);
349 while (retval->num_threads < retval->max_threads)
351 GError *local_error = NULL;
352 g_thread_pool_start_thread (retval, &local_error);
353 if (local_error)
355 g_propagate_error (error, local_error);
356 break;
360 g_async_queue_unlock (retval->queue);
363 return (GThreadPool*) retval;
367 * g_thread_pool_push:
368 * @pool: a #GThreadPool
369 * @data: a new task for @pool
370 * @error: return location for error
372 * Inserts @data into the list of tasks to be executed by @pool. When
373 * the number of currently running threads is lower than the maximal
374 * allowed number of threads, a new thread is started (or reused) with
375 * the properties given to g_thread_pool_new (). Otherwise @data stays
376 * in the queue until a thread in this pool finishes its previous task
377 * and processes @data.
379 * @error can be NULL to ignore errors, or non-NULL to report
380 * errors. An error can only occur, when a new thread couldn't be
381 * created. In that case @data is simply appended to the queue of work
382 * to do.
384 void
385 g_thread_pool_push (GThreadPool *pool,
386 gpointer data,
387 GError **error)
389 GRealThreadPool *real = (GRealThreadPool*) pool;
391 g_return_if_fail (real);
393 g_async_queue_lock (real->queue);
395 if (!real->running)
397 g_async_queue_unlock (real->queue);
398 g_return_if_fail (real->running);
401 if (g_async_queue_length_unlocked (real->queue) >= 0)
402 /* No thread is waiting in the queue */
403 g_thread_pool_start_thread (real, error);
405 g_async_queue_push_unlocked (real->queue, data);
406 g_async_queue_unlock (real->queue);
410 * g_thread_pool_set_max_threads:
411 * @pool: a #GThreadPool
412 * @max_threads: a new maximal number of threads for @pool
413 * @error: return location for error
415 * Sets the maximal allowed number of threads for @pool. A value of -1
416 * means, that the maximal number of threads is unlimited.
418 * Setting @max_threads to 0 means stopping all work for @pool. It is
419 * effectively frozen until @max_threads is set to a non-zero value
420 * again.
422 * A thread is never terminated while calling @thread_func, as
423 * supplied by g_thread_pool_new (). Instead the maximal number of
424 * threads only has effect for the allocation of new threads in
425 * g_thread_pool_push (). A new thread is allocated, whenever the
426 * number of currently running threads in @pool is smaller than the
427 * maximal number.
429 * @error can be NULL to ignore errors, or non-NULL to report
430 * errors. An error can only occur, when a new thread couldn't be
431 * created.
433 void
434 g_thread_pool_set_max_threads (GThreadPool *pool,
435 gint max_threads,
436 GError **error)
438 GRealThreadPool *real = (GRealThreadPool*) pool;
439 gint to_start;
441 g_return_if_fail (real);
442 g_return_if_fail (real->running);
443 g_return_if_fail (!real->pool.exclusive || max_threads != -1);
444 g_return_if_fail (max_threads >= -1);
446 g_async_queue_lock (real->queue);
448 real->max_threads = max_threads;
450 if (pool->exclusive)
451 to_start = real->max_threads - real->num_threads;
452 else
453 to_start = g_async_queue_length_unlocked (real->queue);
455 for ( ; to_start > 0; to_start--)
457 GError *local_error = NULL;
458 g_thread_pool_start_thread (real, &local_error);
459 if (local_error)
461 g_propagate_error (error, local_error);
462 break;
466 g_async_queue_unlock (real->queue);
470 * g_thread_pool_get_max_threads:
471 * @pool: a #GThreadPool
473 * Returns the maximal number of threads for @pool.
475 * Return value: the maximal number of threads
477 gint
478 g_thread_pool_get_max_threads (GThreadPool *pool)
480 GRealThreadPool *real = (GRealThreadPool*) pool;
481 gint retval;
483 g_return_val_if_fail (real, 0);
484 g_return_val_if_fail (real->running, 0);
486 g_async_queue_lock (real->queue);
488 retval = real->max_threads;
490 g_async_queue_unlock (real->queue);
492 return retval;
496 * g_thread_pool_get_num_threads:
497 * @pool: a #GThreadPool
499 * Returns the number of threads currently running in @pool.
501 * Return value: the number of threads currently running
503 guint
504 g_thread_pool_get_num_threads (GThreadPool *pool)
506 GRealThreadPool *real = (GRealThreadPool*) pool;
507 guint retval;
509 g_return_val_if_fail (real, 0);
510 g_return_val_if_fail (real->running, 0);
512 g_async_queue_lock (real->queue);
514 retval = real->num_threads;
516 g_async_queue_unlock (real->queue);
518 return retval;
522 * g_thread_pool_unprocessed:
523 * @pool: a #GThreadPool
525 * Returns the number of tasks still unprocessed in @pool.
527 * Return value: the number of unprocessed tasks
529 guint
530 g_thread_pool_unprocessed (GThreadPool *pool)
532 GRealThreadPool *real = (GRealThreadPool*) pool;
533 gint unprocessed;
535 g_return_val_if_fail (real, 0);
536 g_return_val_if_fail (real->running, 0);
538 unprocessed = g_async_queue_length (real->queue);
540 return MAX (unprocessed, 0);
544 * g_thread_pool_free:
545 * @pool: a #GThreadPool
546 * @immediate: should @pool shut down immediately?
547 * @wait: should the function wait for all tasks to be finished?
549 * Frees all resources allocated for @pool.
551 * If @immediate is #TRUE, no new task is processed for
552 * @pool. Otherwise @pool is not freed before the last task is
553 * processed. Note however, that no thread of this pool is
554 * interrupted, while processing a task. Instead at least all still
555 * running threads can finish their tasks before the @pool is freed.
557 * If @wait is #TRUE, the functions does not return before all tasks
558 * to be processed (dependent on @immediate, whether all or only the
559 * currently running) are ready. Otherwise the function returns immediately.
561 * After calling this function @pool must not be used anymore.
563 void
564 g_thread_pool_free (GThreadPool *pool,
565 gboolean immediate,
566 gboolean wait)
568 GRealThreadPool *real = (GRealThreadPool*) pool;
570 g_return_if_fail (real);
571 g_return_if_fail (real->running);
572 /* It there's no thread allowed here, there is not much sense in
573 * not stopping this pool immediately, when it's not empty */
574 g_return_if_fail (immediate || real->max_threads != 0 ||
575 g_async_queue_length (real->queue) == 0);
577 g_async_queue_lock (real->queue);
579 real->running = FALSE;
580 real->immediate = immediate;
581 real->waiting = wait;
583 if (wait)
585 g_mutex_lock (inform_mutex);
586 while (g_async_queue_length_unlocked (real->queue) != -real->num_threads)
588 g_async_queue_unlock (real->queue);
589 g_cond_wait (inform_cond, inform_mutex);
590 g_async_queue_lock (real->queue);
592 g_mutex_unlock (inform_mutex);
595 if (g_async_queue_length_unlocked (real->queue) == -real->num_threads)
597 /* No thread is currently doing something (and nothing is left
598 * to process in the queue) */
599 if (real->num_threads == 0) /* No threads left, we clean up */
601 g_async_queue_unlock (real->queue);
602 g_thread_pool_free_internal (real);
603 return;
606 g_thread_pool_wakeup_and_stop_all (real);
609 real->waiting = FALSE; /* The last thread should cleanup the pool */
610 g_async_queue_unlock (real->queue);
613 static void
614 g_thread_pool_free_internal (GRealThreadPool* pool)
616 g_return_if_fail (pool);
617 g_return_if_fail (!pool->running);
618 g_return_if_fail (pool->num_threads == 0);
620 g_async_queue_unref (pool->queue);
622 g_free (pool);
625 static void
626 g_thread_pool_wakeup_and_stop_all (GRealThreadPool* pool)
628 guint i;
630 g_return_if_fail (pool);
631 g_return_if_fail (!pool->running);
632 g_return_if_fail (pool->num_threads != 0);
633 g_return_if_fail (g_async_queue_length_unlocked (pool->queue) ==
634 -pool->num_threads);
636 pool->immediate = TRUE;
637 for (i = 0; i < pool->num_threads; i++)
638 g_async_queue_push_unlocked (pool->queue, GUINT_TO_POINTER (1));
642 * g_thread_pool_set_max_unused_threads:
643 * @max_threads: maximal number of unused threads
645 * Sets the maximal number of unused threads to @max_threads. If
646 * @max_threads is -1, no limit is imposed on the number of unused
647 * threads.
649 void
650 g_thread_pool_set_max_unused_threads (gint max_threads)
652 g_return_if_fail (max_threads >= -1);
654 G_LOCK (unused_threads);
656 max_unused_threads = max_threads;
658 if (max_unused_threads < unused_threads && max_unused_threads != -1)
660 guint close_down_num = unused_threads - max_unused_threads;
662 while (close_down_num > 0)
664 GThreadPriority priority;
665 guint bound;
667 guint old_close_down_num = close_down_num;
668 for (priority = G_THREAD_PRIORITY_LOW;
669 priority < G_THREAD_PRIORITY_URGENT + 1 && close_down_num > 0;
670 priority++)
672 for (bound = 0; bound < 2; bound++)
674 GAsyncQueue *queue = unused_thread_queue[priority][bound];
675 g_async_queue_lock (queue);
677 if (g_async_queue_length_unlocked (queue) < 0)
679 g_async_queue_push_unlocked (queue,
680 stop_this_thread_marker);
681 close_down_num--;
684 g_async_queue_unlock (queue);
688 /* Just to make sure, there are no counting problems */
689 g_assert (old_close_down_num != close_down_num);
693 G_UNLOCK (unused_threads);
697 * g_thread_pool_get_max_unused_threads:
699 * Returns the maximal allowed number of unused threads.
701 * Return value: the maximal number of unused threads
703 gint
704 g_thread_pool_get_max_unused_threads (void)
706 gint retval;
708 G_LOCK (unused_threads);
709 retval = max_unused_threads;
710 G_UNLOCK (unused_threads);
712 return retval;
716 * g_thread_pool_get_num_unused_threads:
718 * Returns the number of currently unused threads.
720 * Return value: the number of currently unused threads
722 guint g_thread_pool_get_num_unused_threads (void)
724 guint retval;
726 G_LOCK (unused_threads);
727 retval = unused_threads;
728 G_UNLOCK (unused_threads);
730 return retval;
734 * g_thread_pool_stop_unused_threads:
736 * Stops all currently unused threads. This does not change the
737 * maximal number of unused threads. This function can be used to
738 * regularly stop all unused threads e.g. from g_timeout_add().
740 void g_thread_pool_stop_unused_threads (void)
742 guint oldval = g_thread_pool_get_max_unused_threads ();
743 g_thread_pool_set_max_unused_threads (0);
744 g_thread_pool_set_max_unused_threads (oldval);