.cvsignore updates
[platform/upstream/glib.git] / gthreadpool.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * GAsyncQueue: thread pool implementation.
5  * Copyright (C) 2000 Sebastian Wilhelmi; University of Karlsruhe
6  *
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.
11  *
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.
16  *
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.
21  */
22
23 /*
24  * MT safe
25  */
26
27 #include "glib.h"
28
29 typedef struct _GRealThreadPool GRealThreadPool;
30
31 struct _GRealThreadPool
32 {
33   GThreadPool pool;
34   GAsyncQueue* queue;
35   gint max_threads;
36   gint num_threads;
37   gboolean running;
38   gboolean immediate;
39   gboolean waiting;
40 };
41
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;
46
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);
52
53 static GMutex *inform_mutex = NULL;
54 static GCond *inform_cond = NULL;
55
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);
60
61 #define g_thread_should_run(pool, len) \
62   ((pool)->running || (!(pool)->immediate && (len) > 0))
63
64 static void 
65 g_thread_pool_thread_proxy (gpointer data)
66 {
67   GRealThreadPool *pool = data;
68   GThread* self = g_thread_self ();
69
70   g_async_queue_lock (pool->queue);
71   while (TRUE)
72     {
73       gpointer task; 
74       gboolean goto_global_pool = !pool->pool.exclusive;
75       gint len = g_async_queue_length_unlocked (pool->queue);
76       
77       if (g_thread_should_run (pool, len))
78         {
79           task = g_async_queue_pop_unlocked (pool->queue);
80
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 */
85             {
86               g_async_queue_push_unlocked (pool->queue, task);
87               goto_global_pool = TRUE;
88             }
89           else if (pool->running || !pool->immediate)
90             {
91               g_async_queue_unlock (pool->queue);
92               pool->pool.thread_func (task, pool->pool.user_data);
93               g_async_queue_lock (pool->queue);
94             }
95
96           len = g_async_queue_length_unlocked (pool->queue);
97         }
98
99       if (!g_thread_should_run (pool, len))
100         g_cond_broadcast (inform_cond);
101       
102       if (!pool->running && (pool->immediate || len <= 0))
103         goto_global_pool = TRUE;
104       else if (len >= 0)
105         /* At this pool there is no thread waiting */
106         goto_global_pool = FALSE; 
107       
108       if (goto_global_pool)
109         {
110           GThreadPriority priority = pool->pool.priority;
111           pool->num_threads--; 
112
113           if (!pool->running && !pool->waiting)
114             {
115               if (pool->num_threads == 0)
116                 {
117                   g_async_queue_unlock (pool->queue);
118                   g_thread_pool_free_internal (pool);
119                 }               
120               else if (len == - pool->num_threads)
121                 g_thread_pool_wakeup_and_stop_all (pool);
122             }
123           else
124             g_async_queue_unlock (pool->queue);
125           
126           g_async_queue_lock (unused_thread_queue[priority]);
127
128           G_LOCK (unused_threads);
129           if (unused_threads >= max_unused_threads)
130             {
131               G_UNLOCK (unused_threads);
132               g_async_queue_unlock (unused_thread_queue[priority]);
133               /* Stop this thread */
134               return;      
135             }
136           unused_threads++;
137           G_UNLOCK (unused_threads);
138
139           pool = 
140             g_async_queue_pop_unlocked (unused_thread_queue[priority]);
141
142           G_LOCK (unused_threads);
143           unused_threads--;
144           G_UNLOCK (unused_threads);
145
146           g_async_queue_unlock (unused_thread_queue[priority]);
147           
148           if (pool == stop_this_thread_marker)
149             /* Stop this thread */
150             return;
151           
152           g_async_queue_lock (pool->queue);
153
154           if (pool->pool.priority != self->priority)
155             g_thread_set_priority (self, pool->pool.priority);
156
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. */
160         }
161     }
162 }
163
164 static void
165 g_thread_pool_start_thread (GRealThreadPool  *pool, 
166                             GError          **error)
167 {
168   gboolean success = FALSE;
169   GThreadPriority priority = pool->pool.priority;
170   GAsyncQueue *queue = unused_thread_queue[priority];
171   
172   if (pool->num_threads >= pool->max_threads && pool->max_threads != -1)
173     /* Enough threads are already running */
174     return;
175
176   g_async_queue_lock (queue);
177
178   if (g_async_queue_length_unlocked (queue) < 0)
179     {
180       /* First we try a thread with the right priority */
181       g_async_queue_push_unlocked (queue, pool);
182       success = TRUE;
183     }
184
185   g_async_queue_unlock (queue);
186
187   if (!success)
188     {
189       /* Now we search for threads with other priorities too, but
190        * only, when there is more than one unused thread with that
191        * priority. */
192       GThreadPriority priority;
193       for (priority = G_THREAD_PRIORITY_LOW; 
194            priority < G_THREAD_PRIORITY_URGENT + 1; priority++)
195         {
196           queue = unused_thread_queue[priority];
197           g_async_queue_lock (queue);
198           
199           if (g_async_queue_length_unlocked (queue) < -1)
200             {
201               g_async_queue_push_unlocked (queue, pool);
202               success = TRUE;
203             }
204           
205           g_async_queue_unlock (queue);
206         }      
207     }
208
209   if (!success)
210     {
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);
216       
217       if (local_error)
218         {
219           g_propagate_error (error, local_error);
220           return;
221         }
222     }
223
224   /* See comment in g_thread_pool_thread_proxy as to why this is done
225    * here and not there */
226   pool->num_threads++;
227 }
228
229 GThreadPool* 
230 g_thread_pool_new (GFunc            thread_func,
231                    gint             max_threads,
232                    gulong           stack_size,
233                    gboolean         bound,
234                    GThreadPriority  priority,
235                    gboolean         exclusive,
236                    gpointer         user_data,
237                    GError         **error)
238 {
239   GRealThreadPool *retval;
240
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);
245
246   retval = g_new (GRealThreadPool, 1);
247
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;
254
255   retval->queue = g_async_queue_new ();
256   retval->max_threads = max_threads;
257   retval->num_threads = 0;
258   retval->running = TRUE;
259
260   if (!inform_mutex)
261     {
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 ();
267     }
268
269   if (retval->pool.exclusive)
270     {
271       g_async_queue_lock (retval->queue);
272   
273       while (retval->num_threads < retval->max_threads)
274         {
275           GError *local_error = NULL;
276           g_thread_pool_start_thread (retval, &local_error);
277           if (local_error)
278             {
279               g_propagate_error (error, local_error);
280               break;
281             }
282         }
283
284       g_async_queue_unlock (retval->queue);
285     }
286
287   return (GThreadPool*) retval;
288 }
289
290 void 
291 g_thread_pool_push (GThreadPool     *pool,
292                     gpointer         data,
293                     GError         **error)
294 {
295   GRealThreadPool *real = (GRealThreadPool*) pool;
296
297   g_return_if_fail (real);
298
299   g_async_queue_lock (real->queue);
300   
301   if (!real->running)
302     {
303       g_async_queue_unlock (real->queue);
304       g_return_if_fail (real->running);
305     }
306
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);
310
311   g_async_queue_push_unlocked (real->queue, data);
312   g_async_queue_unlock (real->queue);
313 }
314
315 void
316 g_thread_pool_set_max_threads (GThreadPool     *pool,
317                                gint             max_threads,
318                                GError         **error)
319 {
320   GRealThreadPool *real = (GRealThreadPool*) pool;
321   gint to_start;
322
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);
327
328   g_async_queue_lock (real->queue);
329
330   real->max_threads = max_threads;
331   
332   if (pool->exclusive)
333     to_start = real->max_threads - real->num_threads;
334   else
335     to_start = g_async_queue_length_unlocked (real->queue);
336   
337   for ( ; to_start > 0; to_start--)
338     {
339       GError *local_error = NULL;
340       g_thread_pool_start_thread (real, &local_error);
341       if (local_error)
342         {
343           g_propagate_error (error, local_error);
344           break;
345         }
346     }
347    
348   g_async_queue_unlock (real->queue);
349 }
350
351 gint
352 g_thread_pool_get_max_threads (GThreadPool     *pool)
353 {
354   GRealThreadPool *real = (GRealThreadPool*) pool;
355   gint retval;
356
357   g_return_val_if_fail (real, 0);
358   g_return_val_if_fail (real->running, 0);
359
360   g_async_queue_lock (real->queue);
361
362   retval = real->max_threads;
363     
364   g_async_queue_unlock (real->queue);
365
366   return retval;
367 }
368
369 guint
370 g_thread_pool_get_num_threads (GThreadPool     *pool)
371 {
372   GRealThreadPool *real = (GRealThreadPool*) pool;
373   guint retval;
374
375   g_return_val_if_fail (real, 0);
376   g_return_val_if_fail (real->running, 0);
377
378   g_async_queue_lock (real->queue);
379
380   retval = real->num_threads;
381     
382   g_async_queue_unlock (real->queue);
383
384   return retval;
385 }
386
387 guint
388 g_thread_pool_unprocessed (GThreadPool     *pool)
389 {
390   GRealThreadPool *real = (GRealThreadPool*) pool;
391   gint unprocessed;
392
393   g_return_val_if_fail (real, 0);
394   g_return_val_if_fail (real->running, 0);
395
396   unprocessed = g_async_queue_length (real->queue);
397
398   return MAX (unprocessed, 0);
399 }
400
401 void
402 g_thread_pool_free (GThreadPool     *pool,
403                     gboolean         immediate,
404                     gboolean         wait)
405 {
406   GRealThreadPool *real = (GRealThreadPool*) pool;
407
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);
414
415   g_async_queue_lock (real->queue);
416
417   real->running = FALSE;
418   real->immediate = immediate;
419   real->waiting = wait;
420
421   if (wait)
422     {
423       g_mutex_lock (inform_mutex);
424       while (g_async_queue_length_unlocked (real->queue) != -real->num_threads)
425         {
426           g_async_queue_unlock (real->queue); 
427           g_cond_wait (inform_cond, inform_mutex); 
428           g_async_queue_lock (real->queue); 
429         }
430       g_mutex_unlock (inform_mutex); 
431     }
432
433   if (g_async_queue_length_unlocked (real->queue) == -real->num_threads)
434     {
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 */
438         {
439           g_async_queue_unlock (real->queue);
440           g_thread_pool_free_internal (real);
441           return;
442         }
443
444       g_thread_pool_wakeup_and_stop_all (real);
445     }
446   
447   real->waiting = FALSE; /* The last thread should cleanup the pool */
448   g_async_queue_unlock (real->queue);
449 }
450
451 static void
452 g_thread_pool_free_internal (GRealThreadPool* pool)
453 {
454   g_return_if_fail (pool);
455   g_return_if_fail (!pool->running);
456   g_return_if_fail (pool->num_threads == 0);
457
458   g_async_queue_unref (pool->queue);
459
460   g_free (pool);
461 }
462
463 static void
464 g_thread_pool_wakeup_and_stop_all (GRealThreadPool* pool)
465 {
466   guint i;
467   
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) == 
472                     -pool->num_threads);
473
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));
477 }
478
479 void
480 g_thread_pool_set_max_unused_threads (gint max_threads)
481 {
482   g_return_if_fail (max_threads >= -1);  
483
484   G_LOCK (unused_threads);
485   
486   max_unused_threads = max_threads;
487
488   if (max_unused_threads < unused_threads && max_unused_threads != -1)
489     {
490       guint close_down_num = unused_threads - max_unused_threads;
491       GThreadPriority priority;
492
493       while (close_down_num > 0)
494         {
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; 
498                priority++)
499             {
500               GAsyncQueue *queue = unused_thread_queue[priority];
501               g_async_queue_lock (queue);
502               
503               if (g_async_queue_length_unlocked (queue) < 0)
504                 {
505                   g_async_queue_push_unlocked (queue, 
506                                                stop_this_thread_marker);
507                   close_down_num--;
508                 }
509               
510               g_async_queue_unlock (queue);
511             }
512
513           /* Just to make sure, there are no counting problems */
514           g_assert (old_close_down_num != close_down_num);
515         }
516     }
517     
518   G_UNLOCK (unused_threads);
519 }
520
521 gint
522 g_thread_pool_get_max_unused_threads (void)
523 {
524   gint retval;
525   
526   G_LOCK (unused_threads);
527   retval = max_unused_threads;
528   G_UNLOCK (unused_threads);
529
530   return retval;
531 }
532
533 guint g_thread_pool_get_num_unused_threads (void)
534 {
535   guint retval;
536   
537   G_LOCK (unused_threads);
538   retval = unused_threads;
539   G_UNLOCK (unused_threads);
540
541   return retval;
542 }
543
544 void g_thread_pool_stop_unused_threads (void)
545
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);
549 }