Version 11, interface, binary age 0.
[glib.git] / glib / gthreadpool.c
blob601022830c4cfe70b66a1ffc305eff747d77ce24
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 */
48 static GAsyncQueue *unused_thread_queue;
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 gpointer g_thread_pool_thread_proxy (gpointer data);
58 static void g_thread_pool_start_thread (GRealThreadPool* pool,
59 GError **error);
60 static void g_thread_pool_wakeup_and_stop_all (GRealThreadPool* pool);
62 #define g_thread_should_run(pool, len) \
63 ((pool)->running || (!(pool)->immediate && (len) > 0))
65 static gpointer
66 g_thread_pool_thread_proxy (gpointer data)
68 GRealThreadPool *pool = data;
69 gboolean watcher = FALSE;
71 g_async_queue_lock (pool->queue);
72 while (TRUE)
74 gpointer task;
75 gboolean goto_global_pool = !pool->pool.exclusive;
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 g_time_val_add (&end_time, G_USEC_PER_SEC / 2); /* 1/2 second */
90 task = g_async_queue_timed_pop_unlocked (pool->queue, &end_time);
92 else
93 task = g_async_queue_pop_unlocked (pool->queue);
95 if (task)
97 watcher = FALSE;
98 if (pool->num_threads > pool->max_threads &&
99 pool->max_threads != -1)
100 /* We are in fact a superfluous threads, so we go to
101 * the global pool and just hand the data further to
102 * the next one waiting in the queue */
104 g_async_queue_push_unlocked (pool->queue, task);
105 goto_global_pool = TRUE;
107 else if (pool->running || !pool->immediate)
109 g_async_queue_unlock (pool->queue);
110 pool->pool.func (task, pool->pool.user_data);
111 g_async_queue_lock (pool->queue);
114 len = g_async_queue_length_unlocked (pool->queue);
117 if (!g_thread_should_run (pool, len))
119 g_cond_broadcast (inform_cond);
120 goto_global_pool = TRUE;
122 else if (len > 0)
124 /* At this pool there are no threads waiting, but tasks are. */
125 goto_global_pool = FALSE;
127 else if (len == 0 && !watcher && !pool->pool.exclusive)
129 /* Here neither threads nor tasks are queued and we didn't
130 * just return from a timed wait. We now wait for a limited
131 * time at this pool for new tasks to avoid costly context
132 * switches. */
133 goto_global_pool = FALSE;
134 watcher = TRUE;
138 if (goto_global_pool)
140 pool->num_threads--;
142 if (!pool->running && !pool->waiting)
144 if (pool->num_threads == 0)
146 g_async_queue_unlock (pool->queue);
147 g_thread_pool_free_internal (pool);
149 else if (len == - pool->num_threads)
151 g_thread_pool_wakeup_and_stop_all (pool);
152 g_async_queue_unlock (pool->queue);
155 else
156 g_async_queue_unlock (pool->queue);
158 g_async_queue_lock (unused_thread_queue);
160 G_LOCK (unused_threads);
161 if ((unused_threads >= max_unused_threads &&
162 max_unused_threads != -1))
164 G_UNLOCK (unused_threads);
165 g_async_queue_unlock (unused_thread_queue);
166 /* Stop this thread */
167 return NULL;
169 unused_threads++;
170 G_UNLOCK (unused_threads);
172 pool = g_async_queue_pop_unlocked (unused_thread_queue);
174 G_LOCK (unused_threads);
175 unused_threads--;
176 G_UNLOCK (unused_threads);
178 g_async_queue_unlock (unused_thread_queue);
180 if (pool == stop_this_thread_marker)
181 /* Stop this thread */
182 return NULL;
184 g_async_queue_lock (pool->queue);
186 /* pool->num_threads++ is not done here, but in
187 * g_thread_pool_start_thread to make the new started thread
188 * known to the pool, before itself can do it. */
191 return NULL;
194 static void
195 g_thread_pool_start_thread (GRealThreadPool *pool,
196 GError **error)
198 gboolean success = FALSE;
200 if (pool->num_threads >= pool->max_threads && pool->max_threads != -1)
201 /* Enough threads are already running */
202 return;
204 g_async_queue_lock (unused_thread_queue);
206 if (g_async_queue_length_unlocked (unused_thread_queue) < 0)
208 g_async_queue_push_unlocked (unused_thread_queue, pool);
209 success = TRUE;
212 g_async_queue_unlock (unused_thread_queue);
214 if (!success)
216 GError *local_error = NULL;
217 /* No thread was found, we have to start a new one */
218 g_thread_create (g_thread_pool_thread_proxy, pool, FALSE, &local_error);
220 if (local_error)
222 g_propagate_error (error, local_error);
223 return;
227 /* See comment in g_thread_pool_thread_proxy as to why this is done
228 * here and not there */
229 pool->num_threads++;
233 * g_thread_pool_new:
234 * @func: a function to execute in the threads of the new thread pool
235 * @user_data: user data that is handed over to @func every time it
236 * is called
237 * @max_threads: the maximal number of threads to execute concurrently in
238 * the new thread pool, -1 means no limit
239 * @exclusive: should this thread pool be exclusive?
240 * @error: return location for error
242 * This function creates a new thread pool.
244 * Whenever you call g_thread_pool_push(), either a new thread is
245 * created or an unused one is reused. At most @max_threads threads
246 * are running concurrently for this thread pool. @max_threads = -1
247 * allows unlimited threads to be created for this thread pool. The
248 * newly created or reused thread now executes the function @func with
249 * the two arguments. The first one is the parameter to
250 * g_thread_pool_push() and the second one is @user_data.
252 * The parameter @exclusive determines, whether the thread pool owns
253 * all threads exclusive or whether the threads are shared
254 * globally. If @exclusive is @TRUE, @max_threads threads are started
255 * immediately and they will run exclusively for this thread pool until
256 * it is destroyed by g_thread_pool_free(). If @exclusive is @FALSE,
257 * threads are created, when needed and shared between all
258 * non-exclusive thread pools. This implies that @max_threads may not
259 * be -1 for exclusive thread pools.
261 * @error can be NULL to ignore errors, or non-NULL to report
262 * errors. An error can only occur, when @exclusive is set to @TRUE and
263 * not all @max_threads threads could be created.
265 * Return value: the new #GThreadPool
267 GThreadPool*
268 g_thread_pool_new (GFunc func,
269 gpointer user_data,
270 gint max_threads,
271 gboolean exclusive,
272 GError **error)
274 GRealThreadPool *retval;
275 G_LOCK_DEFINE_STATIC (init);
277 g_return_val_if_fail (func, NULL);
278 g_return_val_if_fail (!exclusive || max_threads != -1, NULL);
279 g_return_val_if_fail (max_threads >= -1, NULL);
280 g_return_val_if_fail (g_thread_supported (), NULL);
282 retval = g_new (GRealThreadPool, 1);
284 retval->pool.func = func;
285 retval->pool.user_data = user_data;
286 retval->pool.exclusive = exclusive;
287 retval->queue = g_async_queue_new ();
288 retval->max_threads = max_threads;
289 retval->num_threads = 0;
290 retval->running = TRUE;
292 G_LOCK (init);
294 if (!inform_mutex)
296 inform_mutex = g_mutex_new ();
297 inform_cond = g_cond_new ();
298 unused_thread_queue = g_async_queue_new ();
301 G_UNLOCK (init);
303 if (retval->pool.exclusive)
305 g_async_queue_lock (retval->queue);
307 while (retval->num_threads < retval->max_threads)
309 GError *local_error = NULL;
310 g_thread_pool_start_thread (retval, &local_error);
311 if (local_error)
313 g_propagate_error (error, local_error);
314 break;
318 g_async_queue_unlock (retval->queue);
321 return (GThreadPool*) retval;
325 * g_thread_pool_push:
326 * @pool: a #GThreadPool
327 * @data: a new task for @pool
328 * @error: return location for error
330 * Inserts @data into the list of tasks to be executed by @pool. When
331 * the number of currently running threads is lower than the maximal
332 * allowed number of threads, a new thread is started (or reused) with
333 * the properties given to g_thread_pool_new (). Otherwise @data stays
334 * in the queue until a thread in this pool finishes its previous task
335 * and processes @data.
337 * @error can be NULL to ignore errors, or non-NULL to report
338 * errors. An error can only occur, when a new thread couldn't be
339 * created. In that case @data is simply appended to the queue of work
340 * to do.
342 void
343 g_thread_pool_push (GThreadPool *pool,
344 gpointer data,
345 GError **error)
347 GRealThreadPool *real = (GRealThreadPool*) pool;
349 g_return_if_fail (real);
351 g_async_queue_lock (real->queue);
353 if (!real->running)
355 g_async_queue_unlock (real->queue);
356 g_return_if_fail (real->running);
359 if (g_async_queue_length_unlocked (real->queue) >= 0)
360 /* No thread is waiting in the queue */
361 g_thread_pool_start_thread (real, error);
363 g_async_queue_push_unlocked (real->queue, data);
364 g_async_queue_unlock (real->queue);
368 * g_thread_pool_set_max_threads:
369 * @pool: a #GThreadPool
370 * @max_threads: a new maximal number of threads for @pool
371 * @error: return location for error
373 * Sets the maximal allowed number of threads for @pool. A value of -1
374 * means, that the maximal number of threads is unlimited.
376 * Setting @max_threads to 0 means stopping all work for @pool. It is
377 * effectively frozen until @max_threads is set to a non-zero value
378 * again.
380 * A thread is never terminated while calling @func, as supplied by
381 * g_thread_pool_new (). Instead the maximal number of threads only
382 * has effect for the allocation of new threads in g_thread_pool_push
383 * (). A new thread is allocated, whenever the number of currently
384 * running threads in @pool is smaller than the maximal number.
386 * @error can be NULL to ignore errors, or non-NULL to report
387 * errors. An error can only occur, when a new thread couldn't be
388 * created.
390 void
391 g_thread_pool_set_max_threads (GThreadPool *pool,
392 gint max_threads,
393 GError **error)
395 GRealThreadPool *real = (GRealThreadPool*) pool;
396 gint to_start;
398 g_return_if_fail (real);
399 g_return_if_fail (real->running);
400 g_return_if_fail (!real->pool.exclusive || max_threads != -1);
401 g_return_if_fail (max_threads >= -1);
403 g_async_queue_lock (real->queue);
405 real->max_threads = max_threads;
407 if (pool->exclusive)
408 to_start = real->max_threads - real->num_threads;
409 else
410 to_start = g_async_queue_length_unlocked (real->queue);
412 for ( ; to_start > 0; to_start--)
414 GError *local_error = NULL;
415 g_thread_pool_start_thread (real, &local_error);
416 if (local_error)
418 g_propagate_error (error, local_error);
419 break;
423 g_async_queue_unlock (real->queue);
427 * g_thread_pool_get_max_threads:
428 * @pool: a #GThreadPool
430 * Returns the maximal number of threads for @pool.
432 * Return value: the maximal number of threads
434 gint
435 g_thread_pool_get_max_threads (GThreadPool *pool)
437 GRealThreadPool *real = (GRealThreadPool*) pool;
438 gint retval;
440 g_return_val_if_fail (real, 0);
441 g_return_val_if_fail (real->running, 0);
443 g_async_queue_lock (real->queue);
445 retval = real->max_threads;
447 g_async_queue_unlock (real->queue);
449 return retval;
453 * g_thread_pool_get_num_threads:
454 * @pool: a #GThreadPool
456 * Returns the number of threads currently running in @pool.
458 * Return value: the number of threads currently running
460 guint
461 g_thread_pool_get_num_threads (GThreadPool *pool)
463 GRealThreadPool *real = (GRealThreadPool*) pool;
464 guint retval;
466 g_return_val_if_fail (real, 0);
467 g_return_val_if_fail (real->running, 0);
469 g_async_queue_lock (real->queue);
471 retval = real->num_threads;
473 g_async_queue_unlock (real->queue);
475 return retval;
479 * g_thread_pool_unprocessed:
480 * @pool: a #GThreadPool
482 * Returns the number of tasks still unprocessed in @pool.
484 * Return value: the number of unprocessed tasks
486 guint
487 g_thread_pool_unprocessed (GThreadPool *pool)
489 GRealThreadPool *real = (GRealThreadPool*) pool;
490 gint unprocessed;
492 g_return_val_if_fail (real, 0);
493 g_return_val_if_fail (real->running, 0);
495 unprocessed = g_async_queue_length (real->queue);
497 return MAX (unprocessed, 0);
501 * g_thread_pool_free:
502 * @pool: a #GThreadPool
503 * @immediate: should @pool shut down immediately?
504 * @wait: should the function wait for all tasks to be finished?
506 * Frees all resources allocated for @pool.
508 * If @immediate is #TRUE, no new task is processed for
509 * @pool. Otherwise @pool is not freed before the last task is
510 * processed. Note however, that no thread of this pool is
511 * interrupted, while processing a task. Instead at least all still
512 * running threads can finish their tasks before the @pool is freed.
514 * If @wait is #TRUE, the functions does not return before all tasks
515 * to be processed (dependent on @immediate, whether all or only the
516 * currently running) are ready. Otherwise the function returns immediately.
518 * After calling this function @pool must not be used anymore.
520 void
521 g_thread_pool_free (GThreadPool *pool,
522 gboolean immediate,
523 gboolean wait)
525 GRealThreadPool *real = (GRealThreadPool*) pool;
527 g_return_if_fail (real);
528 g_return_if_fail (real->running);
529 /* It there's no thread allowed here, there is not much sense in
530 * not stopping this pool immediately, when it's not empty */
531 g_return_if_fail (immediate || real->max_threads != 0 ||
532 g_async_queue_length (real->queue) == 0);
534 g_async_queue_lock (real->queue);
536 real->running = FALSE;
537 real->immediate = immediate;
538 real->waiting = wait;
540 if (wait)
542 g_mutex_lock (inform_mutex);
543 while (g_async_queue_length_unlocked (real->queue) != -real->num_threads)
545 g_async_queue_unlock (real->queue);
546 g_cond_wait (inform_cond, inform_mutex);
547 g_async_queue_lock (real->queue);
549 g_mutex_unlock (inform_mutex);
552 if (g_async_queue_length_unlocked (real->queue) == -real->num_threads)
554 /* No thread is currently doing something (and nothing is left
555 * to process in the queue) */
556 if (real->num_threads == 0) /* No threads left, we clean up */
558 g_async_queue_unlock (real->queue);
559 g_thread_pool_free_internal (real);
560 return;
563 g_thread_pool_wakeup_and_stop_all (real);
566 real->waiting = FALSE; /* The last thread should cleanup the pool */
567 g_async_queue_unlock (real->queue);
570 static void
571 g_thread_pool_free_internal (GRealThreadPool* pool)
573 g_return_if_fail (pool);
574 g_return_if_fail (!pool->running);
575 g_return_if_fail (pool->num_threads == 0);
577 g_async_queue_unref (pool->queue);
579 g_free (pool);
582 static void
583 g_thread_pool_wakeup_and_stop_all (GRealThreadPool* pool)
585 guint i;
587 g_return_if_fail (pool);
588 g_return_if_fail (!pool->running);
589 g_return_if_fail (pool->num_threads != 0);
590 g_return_if_fail (g_async_queue_length_unlocked (pool->queue) ==
591 -pool->num_threads);
593 pool->immediate = TRUE;
594 for (i = 0; i < pool->num_threads; i++)
595 g_async_queue_push_unlocked (pool->queue, GUINT_TO_POINTER (1));
599 * g_thread_pool_set_max_unused_threads:
600 * @max_threads: maximal number of unused threads
602 * Sets the maximal number of unused threads to @max_threads. If
603 * @max_threads is -1, no limit is imposed on the number of unused
604 * threads.
606 void
607 g_thread_pool_set_max_unused_threads (gint max_threads)
609 g_return_if_fail (max_threads >= -1);
611 G_LOCK (unused_threads);
613 max_unused_threads = max_threads;
615 if (max_unused_threads < unused_threads && max_unused_threads != -1)
617 guint i;
619 g_async_queue_lock (unused_thread_queue);
620 for (i = unused_threads - max_unused_threads; i > 0; i--)
621 g_async_queue_push_unlocked (unused_thread_queue,
622 stop_this_thread_marker);
623 g_async_queue_unlock (unused_thread_queue);
626 G_UNLOCK (unused_threads);
630 * g_thread_pool_get_max_unused_threads:
632 * Returns the maximal allowed number of unused threads.
634 * Return value: the maximal number of unused threads
636 gint
637 g_thread_pool_get_max_unused_threads (void)
639 gint retval;
641 G_LOCK (unused_threads);
642 retval = max_unused_threads;
643 G_UNLOCK (unused_threads);
645 return retval;
649 * g_thread_pool_get_num_unused_threads:
651 * Returns the number of currently unused threads.
653 * Return value: the number of currently unused threads
655 guint g_thread_pool_get_num_unused_threads (void)
657 guint retval;
659 G_LOCK (unused_threads);
660 retval = unused_threads;
661 G_UNLOCK (unused_threads);
663 return retval;
667 * g_thread_pool_stop_unused_threads:
669 * Stops all currently unused threads. This does not change the
670 * maximal number of unused threads. This function can be used to
671 * regularly stop all unused threads e.g. from g_timeout_add().
673 void g_thread_pool_stop_unused_threads (void)
675 guint oldval = g_thread_pool_get_max_unused_threads ();
676 g_thread_pool_set_max_unused_threads (0);
677 g_thread_pool_set_max_unused_threads (oldval);