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
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];
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))
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 /* pool->num_threads++ is not done here, but in
155 * g_thread_pool_start_thread to make the new started thread
156 * known to the pool, before itself can do it. */
162 g_thread_pool_start_thread (GRealThreadPool
*pool
,
165 gboolean success
= FALSE
;
166 GThreadPriority priority
= pool
->pool
.priority
;
167 GAsyncQueue
*queue
= unused_thread_queue
[priority
];
169 if (pool
->num_threads
>= pool
->max_threads
&& pool
->max_threads
!= -1)
170 /* Enough threads are already running */
173 g_async_queue_lock (queue
);
175 if (g_async_queue_length_unlocked (queue
) < 0)
177 /* First we try a thread with the right priority */
178 g_async_queue_push_unlocked (queue
, pool
);
182 g_async_queue_unlock (queue
);
184 /* We will not search for threads with other priorities, because changing
185 * priority is quite unportable */
189 GError
*local_error
= NULL
;
190 /* No thread was found, we have to start one new */
191 g_thread_create (g_thread_pool_thread_proxy
, pool
,
192 pool
->pool
.stack_size
, FALSE
,
193 pool
->pool
.bound
, priority
, &local_error
);
197 g_propagate_error (error
, local_error
);
202 /* See comment in g_thread_pool_thread_proxy as to why this is done
203 * here and not there */
208 g_thread_pool_new (GFunc thread_func
,
212 GThreadPriority priority
,
217 GRealThreadPool
*retval
;
219 g_return_val_if_fail (thread_func
, NULL
);
220 g_return_val_if_fail (!exclusive
|| max_threads
!= -1, NULL
);
221 g_return_val_if_fail (max_threads
>= -1, NULL
);
222 g_return_val_if_fail (g_thread_supported (), NULL
);
224 retval
= g_new (GRealThreadPool
, 1);
226 retval
->pool
.thread_func
= thread_func
;
227 retval
->pool
.stack_size
= stack_size
;
228 retval
->pool
.bound
= bound
;
229 retval
->pool
.priority
= priority
;
230 retval
->pool
.exclusive
= exclusive
;
231 retval
->pool
.user_data
= user_data
;
232 retval
->queue
= g_async_queue_new ();
233 retval
->max_threads
= max_threads
;
234 retval
->num_threads
= 0;
235 retval
->running
= TRUE
;
239 inform_mutex
= g_mutex_new ();
240 inform_cond
= g_cond_new ();
241 for (priority
= G_THREAD_PRIORITY_LOW
;
242 priority
< G_THREAD_PRIORITY_URGENT
+ 1; priority
++)
243 unused_thread_queue
[priority
] = g_async_queue_new ();
246 if (retval
->pool
.exclusive
)
248 g_async_queue_lock (retval
->queue
);
250 while (retval
->num_threads
< retval
->max_threads
)
252 GError
*local_error
= NULL
;
253 g_thread_pool_start_thread (retval
, &local_error
);
256 g_propagate_error (error
, local_error
);
261 g_async_queue_unlock (retval
->queue
);
264 return (GThreadPool
*) retval
;
268 g_thread_pool_push (GThreadPool
*pool
,
272 GRealThreadPool
*real
= (GRealThreadPool
*) pool
;
274 g_return_if_fail (real
);
276 g_async_queue_lock (real
->queue
);
280 g_async_queue_unlock (real
->queue
);
281 g_return_if_fail (real
->running
);
284 if (!pool
->exclusive
&& g_async_queue_length_unlocked (real
->queue
) >= 0)
285 /* No thread is waiting in the queue */
286 g_thread_pool_start_thread (real
, error
);
288 g_async_queue_push_unlocked (real
->queue
, data
);
289 g_async_queue_unlock (real
->queue
);
293 g_thread_pool_set_max_threads (GThreadPool
*pool
,
297 GRealThreadPool
*real
= (GRealThreadPool
*) pool
;
300 g_return_if_fail (real
);
301 g_return_if_fail (real
->running
);
302 g_return_if_fail (!real
->pool
.exclusive
|| max_threads
!= -1);
303 g_return_if_fail (max_threads
>= -1);
305 g_async_queue_lock (real
->queue
);
307 real
->max_threads
= max_threads
;
310 to_start
= real
->max_threads
- real
->num_threads
;
312 to_start
= g_async_queue_length_unlocked (real
->queue
);
314 for ( ; to_start
> 0; to_start
--)
316 GError
*local_error
= NULL
;
317 g_thread_pool_start_thread (real
, &local_error
);
320 g_propagate_error (error
, local_error
);
325 g_async_queue_unlock (real
->queue
);
329 g_thread_pool_get_max_threads (GThreadPool
*pool
)
331 GRealThreadPool
*real
= (GRealThreadPool
*) pool
;
334 g_return_val_if_fail (real
, 0);
335 g_return_val_if_fail (real
->running
, 0);
337 g_async_queue_lock (real
->queue
);
339 retval
= real
->max_threads
;
341 g_async_queue_unlock (real
->queue
);
347 g_thread_pool_get_num_threads (GThreadPool
*pool
)
349 GRealThreadPool
*real
= (GRealThreadPool
*) pool
;
352 g_return_val_if_fail (real
, 0);
353 g_return_val_if_fail (real
->running
, 0);
355 g_async_queue_lock (real
->queue
);
357 retval
= real
->num_threads
;
359 g_async_queue_unlock (real
->queue
);
365 g_thread_pool_unprocessed (GThreadPool
*pool
)
367 GRealThreadPool
*real
= (GRealThreadPool
*) pool
;
370 g_return_val_if_fail (real
, 0);
371 g_return_val_if_fail (real
->running
, 0);
373 unprocessed
= g_async_queue_length (real
->queue
);
375 return MAX (unprocessed
, 0);
379 g_thread_pool_free (GThreadPool
*pool
,
383 GRealThreadPool
*real
= (GRealThreadPool
*) pool
;
385 g_return_if_fail (real
);
386 g_return_if_fail (real
->running
);
387 /* It there's no thread allowed here, there is not much sense in
388 * not stopping this pool immediatly, when it's not empty */
389 g_return_if_fail (immediate
|| real
->max_threads
!= 0 ||
390 g_async_queue_length (real
->queue
) == 0);
392 g_async_queue_lock (real
->queue
);
394 real
->running
= FALSE
;
395 real
->immediate
= immediate
;
396 real
->waiting
= wait
;
400 g_mutex_lock (inform_mutex
);
401 while (g_async_queue_length_unlocked (real
->queue
) != -real
->num_threads
)
403 g_async_queue_unlock (real
->queue
);
404 g_cond_wait (inform_cond
, inform_mutex
);
405 g_async_queue_lock (real
->queue
);
407 g_mutex_unlock (inform_mutex
);
410 if (g_async_queue_length_unlocked (real
->queue
) == -real
->num_threads
)
412 /* No thread is currently doing something (and nothing is left
413 * to process in the queue) */
414 if (real
->num_threads
== 0) /* No threads left, we clean up */
416 g_async_queue_unlock (real
->queue
);
417 g_thread_pool_free_internal (real
);
421 g_thread_pool_wakeup_and_stop_all (real
);
424 real
->waiting
= FALSE
; /* The last thread should cleanup the pool */
425 g_async_queue_unlock (real
->queue
);
429 g_thread_pool_free_internal (GRealThreadPool
* pool
)
431 g_return_if_fail (pool
);
432 g_return_if_fail (!pool
->running
);
433 g_return_if_fail (pool
->num_threads
== 0);
435 g_async_queue_unref (pool
->queue
);
441 g_thread_pool_wakeup_and_stop_all (GRealThreadPool
* pool
)
445 g_return_if_fail (pool
);
446 g_return_if_fail (!pool
->running
);
447 g_return_if_fail (pool
->num_threads
!= 0);
448 g_return_if_fail (g_async_queue_length_unlocked (pool
->queue
) ==
451 pool
->immediate
= TRUE
;
452 for (i
= 0; i
< pool
->num_threads
; i
++)
453 g_async_queue_push_unlocked (pool
->queue
, GUINT_TO_POINTER (1));
457 g_thread_pool_set_max_unused_threads (gint max_threads
)
459 g_return_if_fail (max_threads
>= -1);
461 G_LOCK (unused_threads
);
463 max_unused_threads
= max_threads
;
465 if (max_unused_threads
< unused_threads
&& max_unused_threads
!= -1)
467 guint close_down_num
= unused_threads
- max_unused_threads
;
468 GThreadPriority priority
;
470 while (close_down_num
> 0)
472 guint old_close_down_num
= close_down_num
;
473 for (priority
= G_THREAD_PRIORITY_LOW
;
474 priority
< G_THREAD_PRIORITY_URGENT
+ 1 && close_down_num
> 0;
477 GAsyncQueue
*queue
= unused_thread_queue
[priority
];
478 g_async_queue_lock (queue
);
480 if (g_async_queue_length_unlocked (queue
) < 0)
482 g_async_queue_push_unlocked (queue
,
483 stop_this_thread_marker
);
487 g_async_queue_unlock (queue
);
490 /* Just to make sure, there are no counting problems */
491 g_assert (old_close_down_num
!= close_down_num
);
495 G_UNLOCK (unused_threads
);
499 g_thread_pool_get_max_unused_threads (void)
503 G_LOCK (unused_threads
);
504 retval
= max_unused_threads
;
505 G_UNLOCK (unused_threads
);
510 guint
g_thread_pool_get_num_unused_threads (void)
514 G_LOCK (unused_threads
);
515 retval
= unused_threads
;
516 G_UNLOCK (unused_threads
);
521 void g_thread_pool_stop_unused_threads (void)
523 guint oldval
= g_thread_pool_get_max_unused_threads ();
524 g_thread_pool_set_max_unused_threads (0);
525 g_thread_pool_set_max_unused_threads (oldval
);