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 Library 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 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library 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
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 = &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];
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);
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))
65 g_thread_pool_thread_proxy (gpointer data)
67 GRealThreadPool *pool = data;
68 GThread* self = g_thread_self ();
70 g_async_queue_lock (pool->queue);
74 gboolean goto_global_pool = !pool->pool.exclusive;
75 gint len = g_async_queue_length_unlocked (pool->queue);
77 if (g_thread_should_run (pool, len))
79 task = g_async_queue_pop_unlocked (pool->queue);
81 if (pool->num_threads > pool->max_threads && pool->max_threads != -1)
82 /* We are in fact a superfluous threads, so we go to the
83 * global pool and just hand the data further to the next one
84 * waiting in the queue */
86 g_async_queue_push_unlocked (pool->queue, task);
87 goto_global_pool = TRUE;
89 else if (pool->running || !pool->immediate)
91 g_async_queue_unlock (pool->queue);
92 pool->pool.thread_func (task, pool->pool.user_data);
93 g_async_queue_lock (pool->queue);
96 len = g_async_queue_length_unlocked (pool->queue);
99 if (!g_thread_should_run (pool, len))
100 g_cond_broadcast (inform_cond);
102 if (!pool->running && (pool->immediate || len <= 0))
103 goto_global_pool = TRUE;
105 /* At this pool there is no thread waiting */
106 goto_global_pool = FALSE;
108 if (goto_global_pool)
110 GThreadPriority priority = pool->pool.priority;
113 if (!pool->running && !pool->waiting)
115 if (pool->num_threads == 0)
117 g_async_queue_unlock (pool->queue);
118 g_thread_pool_free_internal (pool);
120 else if (len == - pool->num_threads)
121 g_thread_pool_wakeup_and_stop_all (pool);
124 g_async_queue_unlock (pool->queue);
126 g_async_queue_lock (unused_thread_queue[priority]);
128 G_LOCK (unused_threads);
129 if (unused_threads >= max_unused_threads)
131 G_UNLOCK (unused_threads);
132 g_async_queue_unlock (unused_thread_queue[priority]);
133 /* Stop this thread */
137 G_UNLOCK (unused_threads);
140 g_async_queue_pop_unlocked (unused_thread_queue[priority]);
142 G_LOCK (unused_threads);
144 G_UNLOCK (unused_threads);
146 g_async_queue_unlock (unused_thread_queue[priority]);
148 if (pool == stop_this_thread_marker)
149 /* Stop this thread */
152 g_async_queue_lock (pool->queue);
154 if (pool->pool.priority != self->priority)
155 g_thread_set_priority (self, pool->pool.priority);
157 /* pool->num_threads++ is not done here, but in
158 * g_thread_pool_start_thread to make the new started thread
159 * known to the pool, before itself can do it. */
165 g_thread_pool_start_thread (GRealThreadPool* pool)
167 gboolean success = FALSE;
168 GThreadPriority priority = pool->pool.priority;
169 GAsyncQueue *queue = unused_thread_queue[priority];
171 if (pool->num_threads >= pool->max_threads && pool->max_threads != -1)
172 /* Enough threads are already running */
175 g_async_queue_lock (queue);
177 if (g_async_queue_length_unlocked (queue) < 0)
179 /* First we try a thread with the right priority */
180 g_async_queue_push_unlocked (queue, pool);
184 g_async_queue_unlock (queue);
188 /* Now we search for threads with other priorities too, but
189 * only, when there is more than one unused thread with that
191 GThreadPriority priority;
192 for (priority = G_THREAD_PRIORITY_LOW;
193 priority < G_THREAD_PRIORITY_URGENT + 1; priority++)
195 queue = unused_thread_queue[priority];
196 g_async_queue_lock (queue);
198 if (g_async_queue_length_unlocked (queue) < -1)
200 g_async_queue_push_unlocked (queue, pool);
204 g_async_queue_unlock (queue);
209 /* No thread was found, we have to start one new */
210 g_thread_create (g_thread_pool_thread_proxy, pool, pool->pool.stack_size,
211 FALSE, pool->pool.bound, priority);
213 /* See comment in g_thread_pool_thread_proxy as to why this is done
214 * here and not there */
219 g_thread_pool_new (GFunc thread_func,
223 GThreadPriority priority,
227 GRealThreadPool *retval;
229 g_return_val_if_fail (thread_func, NULL);
230 g_return_val_if_fail (!exclusive || max_threads != -1, NULL);
231 g_return_val_if_fail (max_threads >= -1, NULL);
232 g_return_val_if_fail (g_thread_supported (), NULL);
234 retval = g_new (GRealThreadPool, 1);
236 retval->pool.thread_func = thread_func;
237 retval->pool.stack_size = stack_size;
238 retval->pool.bound = bound;
239 retval->pool.priority = priority;
240 retval->pool.exclusive = exclusive;
241 retval->pool.user_data = user_data;
243 retval->queue = g_async_queue_new ();
244 retval->max_threads = max_threads;
245 retval->num_threads = 0;
246 retval->running = TRUE;
250 inform_mutex = g_mutex_new ();
251 inform_cond = g_cond_new ();
252 for (priority = G_THREAD_PRIORITY_LOW;
253 priority < G_THREAD_PRIORITY_URGENT + 1; priority++)
254 unused_thread_queue[priority] = g_async_queue_new ();
257 if (retval->pool.exclusive)
259 g_async_queue_lock (retval->queue);
261 while (retval->num_threads < retval->max_threads)
262 g_thread_pool_start_thread (retval);
264 g_async_queue_unlock (retval->queue);
267 return (GThreadPool*) retval;
271 g_thread_pool_push (GThreadPool *pool,
274 GRealThreadPool *real = (GRealThreadPool*) pool;
276 g_return_if_fail (real);
278 g_async_queue_lock (real->queue);
282 g_async_queue_unlock (real->queue);
283 g_return_if_fail (real->running);
286 if (!pool->exclusive && g_async_queue_length_unlocked (real->queue) >= 0)
288 /* No thread is waiting in the queue */
289 g_thread_pool_start_thread (real);
291 g_async_queue_push_unlocked (real->queue, data);
292 g_async_queue_unlock (real->queue);
296 g_thread_pool_set_max_threads (GThreadPool *pool,
299 GRealThreadPool *real = (GRealThreadPool*) pool;
302 g_return_if_fail (real);
303 g_return_if_fail (real->running);
304 g_return_if_fail (!real->pool.exclusive || max_threads != -1);
305 g_return_if_fail (max_threads >= -1);
307 g_async_queue_lock (real->queue);
309 real->max_threads = max_threads;
312 to_start = real->max_threads - real->num_threads;
314 to_start = g_async_queue_length_unlocked (real->queue);
316 for ( ; to_start > 0; to_start--)
317 g_thread_pool_start_thread (real);
319 g_async_queue_unlock (real->queue);
323 g_thread_pool_get_max_threads (GThreadPool *pool)
325 GRealThreadPool *real = (GRealThreadPool*) pool;
328 g_return_val_if_fail (real, 0);
329 g_return_val_if_fail (real->running, 0);
331 g_async_queue_lock (real->queue);
333 retval = real->max_threads;
335 g_async_queue_unlock (real->queue);
341 g_thread_pool_get_num_threads (GThreadPool *pool)
343 GRealThreadPool *real = (GRealThreadPool*) pool;
346 g_return_val_if_fail (real, 0);
347 g_return_val_if_fail (real->running, 0);
349 g_async_queue_lock (real->queue);
351 retval = real->num_threads;
353 g_async_queue_unlock (real->queue);
359 g_thread_pool_unprocessed (GThreadPool *pool)
361 GRealThreadPool *real = (GRealThreadPool*) pool;
364 g_return_val_if_fail (real, 0);
365 g_return_val_if_fail (real->running, 0);
367 unprocessed = g_async_queue_length (real->queue);
369 return MAX (unprocessed, 0);
373 g_thread_pool_free (GThreadPool *pool,
377 GRealThreadPool *real = (GRealThreadPool*) pool;
379 g_return_if_fail (real);
380 g_return_if_fail (real->running);
381 /* It there's no thread allowed here, there is not much sense in
382 * not stopping this pool immediatly, when it's not empty */
383 g_return_if_fail (immediate || real->max_threads != 0 ||
384 g_async_queue_length (real->queue) == 0);
386 g_async_queue_lock (real->queue);
388 real->running = FALSE;
389 real->immediate = immediate;
390 real->waiting = wait;
394 g_mutex_lock (inform_mutex);
395 while (g_async_queue_length_unlocked (real->queue) != -real->num_threads)
397 g_async_queue_unlock (real->queue);
398 g_cond_wait (inform_cond, inform_mutex);
399 g_async_queue_lock (real->queue);
401 g_mutex_unlock (inform_mutex);
404 if (g_async_queue_length_unlocked (real->queue) == -real->num_threads)
406 /* No thread is currently doing something (and nothing is left
407 * to process in the queue) */
408 if (real->num_threads == 0) /* No threads left, we clean up */
410 g_async_queue_unlock (real->queue);
411 g_thread_pool_free_internal (real);
415 g_thread_pool_wakeup_and_stop_all (real);
418 real->waiting = FALSE; /* The last thread should cleanup the pool */
419 g_async_queue_unlock (real->queue);
423 g_thread_pool_free_internal (GRealThreadPool* pool)
425 g_return_if_fail (pool);
426 g_return_if_fail (!pool->running);
427 g_return_if_fail (pool->num_threads == 0);
429 g_async_queue_unref (pool->queue);
435 g_thread_pool_wakeup_and_stop_all (GRealThreadPool* pool)
439 g_return_if_fail (pool);
440 g_return_if_fail (!pool->running);
441 g_return_if_fail (pool->num_threads != 0);
442 g_return_if_fail (g_async_queue_length_unlocked (pool->queue) ==
445 pool->immediate = TRUE;
446 for (i = 0; i < pool->num_threads; i++)
447 g_async_queue_push_unlocked (pool->queue, GUINT_TO_POINTER (1));
451 g_thread_pool_set_max_unused_threads (gint max_threads)
453 g_return_if_fail (max_threads >= -1);
455 G_LOCK (unused_threads);
457 max_unused_threads = max_threads;
459 if (max_unused_threads < unused_threads && max_unused_threads != -1)
461 guint close_down_num = unused_threads - max_unused_threads;
462 GThreadPriority priority;
464 while (close_down_num > 0)
466 guint old_close_down_num = close_down_num;
467 for (priority = G_THREAD_PRIORITY_LOW;
468 priority < G_THREAD_PRIORITY_URGENT + 1 && close_down_num > 0;
471 GAsyncQueue *queue = unused_thread_queue[priority];
472 g_async_queue_lock (queue);
474 if (g_async_queue_length_unlocked (queue) < 0)
476 g_async_queue_push_unlocked (queue,
477 stop_this_thread_marker);
481 g_async_queue_unlock (queue);
484 /* Just to make sure, there are no counting problems */
485 g_assert (old_close_down_num != close_down_num);
489 G_UNLOCK (unused_threads);
493 g_thread_pool_get_max_unused_threads (void)
497 G_LOCK (unused_threads);
498 retval = max_unused_threads;
499 G_UNLOCK (unused_threads);
504 guint g_thread_pool_get_num_unused_threads (void)
508 G_LOCK (unused_threads);
509 retval = unused_threads;
510 G_UNLOCK (unused_threads);
515 void g_thread_pool_stop_unused_threads (void)
517 guint oldval = g_thread_pool_get_max_unused_threads ();
518 g_thread_pool_set_max_unused_threads (0);
519 g_thread_pool_set_max_unused_threads (oldval);