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 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
,
168 gboolean success
= FALSE
;
169 GThreadPriority priority
= pool
->pool
.priority
;
170 GAsyncQueue
*queue
= unused_thread_queue
[priority
];
172 if (pool
->num_threads
>= pool
->max_threads
&& pool
->max_threads
!= -1)
173 /* Enough threads are already running */
176 g_async_queue_lock (queue
);
178 if (g_async_queue_length_unlocked (queue
) < 0)
180 /* First we try a thread with the right priority */
181 g_async_queue_push_unlocked (queue
, pool
);
185 g_async_queue_unlock (queue
);
189 /* Now we search for threads with other priorities too, but
190 * only, when there is more than one unused thread with that
192 GThreadPriority priority
;
193 for (priority
= G_THREAD_PRIORITY_LOW
;
194 priority
< G_THREAD_PRIORITY_URGENT
+ 1; priority
++)
196 queue
= unused_thread_queue
[priority
];
197 g_async_queue_lock (queue
);
199 if (g_async_queue_length_unlocked (queue
) < -1)
201 g_async_queue_push_unlocked (queue
, pool
);
205 g_async_queue_unlock (queue
);
211 GError
*local_error
= NULL
;
212 /* No thread was found, we have to start one new */
213 g_thread_create (g_thread_pool_thread_proxy
, pool
,
214 pool
->pool
.stack_size
, FALSE
,
215 pool
->pool
.bound
, priority
, &local_error
);
219 g_propagate_error (error
, local_error
);
224 /* See comment in g_thread_pool_thread_proxy as to why this is done
225 * here and not there */
230 g_thread_pool_new (GFunc thread_func
,
234 GThreadPriority priority
,
239 GRealThreadPool
*retval
;
241 g_return_val_if_fail (thread_func
, NULL
);
242 g_return_val_if_fail (!exclusive
|| max_threads
!= -1, NULL
);
243 g_return_val_if_fail (max_threads
>= -1, NULL
);
244 g_return_val_if_fail (g_thread_supported (), NULL
);
246 retval
= g_new (GRealThreadPool
, 1);
248 retval
->pool
.thread_func
= thread_func
;
249 retval
->pool
.stack_size
= stack_size
;
250 retval
->pool
.bound
= bound
;
251 retval
->pool
.priority
= priority
;
252 retval
->pool
.exclusive
= exclusive
;
253 retval
->pool
.user_data
= user_data
;
255 retval
->queue
= g_async_queue_new ();
256 retval
->max_threads
= max_threads
;
257 retval
->num_threads
= 0;
258 retval
->running
= TRUE
;
262 inform_mutex
= g_mutex_new ();
263 inform_cond
= g_cond_new ();
264 for (priority
= G_THREAD_PRIORITY_LOW
;
265 priority
< G_THREAD_PRIORITY_URGENT
+ 1; priority
++)
266 unused_thread_queue
[priority
] = g_async_queue_new ();
269 if (retval
->pool
.exclusive
)
271 g_async_queue_lock (retval
->queue
);
273 while (retval
->num_threads
< retval
->max_threads
)
275 GError
*local_error
= NULL
;
276 g_thread_pool_start_thread (retval
, &local_error
);
279 g_propagate_error (error
, local_error
);
284 g_async_queue_unlock (retval
->queue
);
287 return (GThreadPool
*) retval
;
291 g_thread_pool_push (GThreadPool
*pool
,
295 GRealThreadPool
*real
= (GRealThreadPool
*) pool
;
297 g_return_if_fail (real
);
299 g_async_queue_lock (real
->queue
);
303 g_async_queue_unlock (real
->queue
);
304 g_return_if_fail (real
->running
);
307 if (!pool
->exclusive
&& g_async_queue_length_unlocked (real
->queue
) >= 0)
308 /* No thread is waiting in the queue */
309 g_thread_pool_start_thread (real
, error
);
311 g_async_queue_push_unlocked (real
->queue
, data
);
312 g_async_queue_unlock (real
->queue
);
316 g_thread_pool_set_max_threads (GThreadPool
*pool
,
320 GRealThreadPool
*real
= (GRealThreadPool
*) pool
;
323 g_return_if_fail (real
);
324 g_return_if_fail (real
->running
);
325 g_return_if_fail (!real
->pool
.exclusive
|| max_threads
!= -1);
326 g_return_if_fail (max_threads
>= -1);
328 g_async_queue_lock (real
->queue
);
330 real
->max_threads
= max_threads
;
333 to_start
= real
->max_threads
- real
->num_threads
;
335 to_start
= g_async_queue_length_unlocked (real
->queue
);
337 for ( ; to_start
> 0; to_start
--)
339 GError
*local_error
= NULL
;
340 g_thread_pool_start_thread (real
, &local_error
);
343 g_propagate_error (error
, local_error
);
348 g_async_queue_unlock (real
->queue
);
352 g_thread_pool_get_max_threads (GThreadPool
*pool
)
354 GRealThreadPool
*real
= (GRealThreadPool
*) pool
;
357 g_return_val_if_fail (real
, 0);
358 g_return_val_if_fail (real
->running
, 0);
360 g_async_queue_lock (real
->queue
);
362 retval
= real
->max_threads
;
364 g_async_queue_unlock (real
->queue
);
370 g_thread_pool_get_num_threads (GThreadPool
*pool
)
372 GRealThreadPool
*real
= (GRealThreadPool
*) pool
;
375 g_return_val_if_fail (real
, 0);
376 g_return_val_if_fail (real
->running
, 0);
378 g_async_queue_lock (real
->queue
);
380 retval
= real
->num_threads
;
382 g_async_queue_unlock (real
->queue
);
388 g_thread_pool_unprocessed (GThreadPool
*pool
)
390 GRealThreadPool
*real
= (GRealThreadPool
*) pool
;
393 g_return_val_if_fail (real
, 0);
394 g_return_val_if_fail (real
->running
, 0);
396 unprocessed
= g_async_queue_length (real
->queue
);
398 return MAX (unprocessed
, 0);
402 g_thread_pool_free (GThreadPool
*pool
,
406 GRealThreadPool
*real
= (GRealThreadPool
*) pool
;
408 g_return_if_fail (real
);
409 g_return_if_fail (real
->running
);
410 /* It there's no thread allowed here, there is not much sense in
411 * not stopping this pool immediatly, when it's not empty */
412 g_return_if_fail (immediate
|| real
->max_threads
!= 0 ||
413 g_async_queue_length (real
->queue
) == 0);
415 g_async_queue_lock (real
->queue
);
417 real
->running
= FALSE
;
418 real
->immediate
= immediate
;
419 real
->waiting
= wait
;
423 g_mutex_lock (inform_mutex
);
424 while (g_async_queue_length_unlocked (real
->queue
) != -real
->num_threads
)
426 g_async_queue_unlock (real
->queue
);
427 g_cond_wait (inform_cond
, inform_mutex
);
428 g_async_queue_lock (real
->queue
);
430 g_mutex_unlock (inform_mutex
);
433 if (g_async_queue_length_unlocked (real
->queue
) == -real
->num_threads
)
435 /* No thread is currently doing something (and nothing is left
436 * to process in the queue) */
437 if (real
->num_threads
== 0) /* No threads left, we clean up */
439 g_async_queue_unlock (real
->queue
);
440 g_thread_pool_free_internal (real
);
444 g_thread_pool_wakeup_and_stop_all (real
);
447 real
->waiting
= FALSE
; /* The last thread should cleanup the pool */
448 g_async_queue_unlock (real
->queue
);
452 g_thread_pool_free_internal (GRealThreadPool
* pool
)
454 g_return_if_fail (pool
);
455 g_return_if_fail (!pool
->running
);
456 g_return_if_fail (pool
->num_threads
== 0);
458 g_async_queue_unref (pool
->queue
);
464 g_thread_pool_wakeup_and_stop_all (GRealThreadPool
* pool
)
468 g_return_if_fail (pool
);
469 g_return_if_fail (!pool
->running
);
470 g_return_if_fail (pool
->num_threads
!= 0);
471 g_return_if_fail (g_async_queue_length_unlocked (pool
->queue
) ==
474 pool
->immediate
= TRUE
;
475 for (i
= 0; i
< pool
->num_threads
; i
++)
476 g_async_queue_push_unlocked (pool
->queue
, GUINT_TO_POINTER (1));
480 g_thread_pool_set_max_unused_threads (gint max_threads
)
482 g_return_if_fail (max_threads
>= -1);
484 G_LOCK (unused_threads
);
486 max_unused_threads
= max_threads
;
488 if (max_unused_threads
< unused_threads
&& max_unused_threads
!= -1)
490 guint close_down_num
= unused_threads
- max_unused_threads
;
491 GThreadPriority priority
;
493 while (close_down_num
> 0)
495 guint old_close_down_num
= close_down_num
;
496 for (priority
= G_THREAD_PRIORITY_LOW
;
497 priority
< G_THREAD_PRIORITY_URGENT
+ 1 && close_down_num
> 0;
500 GAsyncQueue
*queue
= unused_thread_queue
[priority
];
501 g_async_queue_lock (queue
);
503 if (g_async_queue_length_unlocked (queue
) < 0)
505 g_async_queue_push_unlocked (queue
,
506 stop_this_thread_marker
);
510 g_async_queue_unlock (queue
);
513 /* Just to make sure, there are no counting problems */
514 g_assert (old_close_down_num
!= close_down_num
);
518 G_UNLOCK (unused_threads
);
522 g_thread_pool_get_max_unused_threads (void)
526 G_LOCK (unused_threads
);
527 retval
= max_unused_threads
;
528 G_UNLOCK (unused_threads
);
533 guint
g_thread_pool_get_num_unused_threads (void)
537 G_LOCK (unused_threads
);
538 retval
= unused_threads
;
539 G_UNLOCK (unused_threads
);
544 void g_thread_pool_stop_unused_threads (void)
546 guint oldval
= g_thread_pool_get_max_unused_threads ();
547 g_thread_pool_set_max_unused_threads (0);
548 g_thread_pool_set_max_unused_threads (oldval
);