Look for nanosleep function.
[platform/upstream/glib.git] / glib / 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 = (gpointer) &g_thread_pool_new;
46
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);
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 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);
61
62 #define g_thread_should_run(pool, len) \
63   ((pool)->running || (!(pool)->immediate && (len) > 0))
64
65 static gpointer 
66 g_thread_pool_thread_proxy (gpointer data)
67 {
68   GRealThreadPool *pool = data;
69   gboolean watcher = FALSE;
70
71   g_async_queue_lock (pool->queue);
72   while (TRUE)
73     {
74       gpointer task; 
75       gboolean goto_global_pool = !pool->pool.exclusive;
76       gint len = g_async_queue_length_unlocked (pool->queue);
77       
78       if (g_thread_should_run (pool, len))
79         {
80           if (watcher)
81             {
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);
91             }
92           else
93             task = g_async_queue_pop_unlocked (pool->queue);
94           
95           if (task)
96             {
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 */
103                 {
104                   g_async_queue_push_unlocked (pool->queue, task);
105                   goto_global_pool = TRUE;
106                 }
107               else if (pool->running || !pool->immediate)
108                 {
109                   g_async_queue_unlock (pool->queue);
110                   pool->pool.func (task, pool->pool.user_data);
111                   g_async_queue_lock (pool->queue);
112                 }
113             }
114           len = g_async_queue_length_unlocked (pool->queue);
115         }
116
117       if (!g_thread_should_run (pool, len))
118         {
119           g_cond_broadcast (inform_cond);
120           goto_global_pool = TRUE;
121         }
122       else if (len > 0)
123         {
124           /* At this pool there are no threads waiting, but tasks are. */
125           goto_global_pool = FALSE; 
126         }
127       else if (len == 0 && !watcher && !pool->pool.exclusive)
128         {
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;
135         }
136
137       
138       if (goto_global_pool)
139         {
140           pool->num_threads--; 
141
142           if (!pool->running && !pool->waiting)
143             {
144               if (pool->num_threads == 0)
145                 {
146                   g_async_queue_unlock (pool->queue);
147                   g_thread_pool_free_internal (pool);
148                 }               
149               else if (len == - pool->num_threads)
150                 {
151                   g_thread_pool_wakeup_and_stop_all (pool);
152                   g_async_queue_unlock (pool->queue);
153                 }
154             }
155           else
156             g_async_queue_unlock (pool->queue);
157           
158           g_async_queue_lock (unused_thread_queue);
159
160           G_LOCK (unused_threads);
161           if ((unused_threads >= max_unused_threads && 
162                max_unused_threads != -1))
163             {
164               G_UNLOCK (unused_threads);
165               g_async_queue_unlock (unused_thread_queue);
166               /* Stop this thread */
167               return NULL;      
168             }
169           unused_threads++;
170           G_UNLOCK (unused_threads);
171
172           pool = g_async_queue_pop_unlocked (unused_thread_queue);
173
174           G_LOCK (unused_threads);
175           unused_threads--;
176           G_UNLOCK (unused_threads);
177
178           g_async_queue_unlock (unused_thread_queue);
179           
180           if (pool == stop_this_thread_marker)
181             /* Stop this thread */
182             return NULL;
183           
184           g_async_queue_lock (pool->queue);
185
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. */
189         }
190     }
191   return NULL;
192 }
193
194 static void
195 g_thread_pool_start_thread (GRealThreadPool  *pool, 
196                             GError          **error)
197 {
198   gboolean success = FALSE;
199   
200   if (pool->num_threads >= pool->max_threads && pool->max_threads != -1)
201     /* Enough threads are already running */
202     return;
203
204   g_async_queue_lock (unused_thread_queue);
205
206   if (g_async_queue_length_unlocked (unused_thread_queue) < 0)
207     {
208       g_async_queue_push_unlocked (unused_thread_queue, pool);
209       success = TRUE;
210     }
211
212   g_async_queue_unlock (unused_thread_queue);
213
214   if (!success)
215     {
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);
219       
220       if (local_error)
221         {
222           g_propagate_error (error, local_error);
223           return;
224         }
225     }
226
227   /* See comment in g_thread_pool_thread_proxy as to why this is done
228    * here and not there */
229   pool->num_threads++;
230 }
231
232 /**
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
241  *
242  * This function creates a new thread pool.
243  *
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.
251  *
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.
260  *
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.
264  *
265  * Return value: the new #GThreadPool
266  **/
267 GThreadPool* 
268 g_thread_pool_new (GFunc            func,
269                    gpointer         user_data,
270                    gint             max_threads,
271                    gboolean         exclusive,
272                    GError         **error)
273 {
274   GRealThreadPool *retval;
275   G_LOCK_DEFINE_STATIC (init);
276
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);
281
282   retval = g_new (GRealThreadPool, 1);
283
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;
291
292   G_LOCK (init);
293   
294   if (!inform_mutex)
295     {
296       inform_mutex = g_mutex_new ();
297       inform_cond = g_cond_new ();
298       unused_thread_queue = g_async_queue_new ();
299     }
300
301   G_UNLOCK (init);
302
303   if (retval->pool.exclusive)
304     {
305       g_async_queue_lock (retval->queue);
306   
307       while (retval->num_threads < retval->max_threads)
308         {
309           GError *local_error = NULL;
310           g_thread_pool_start_thread (retval, &local_error);
311           if (local_error)
312             {
313               g_propagate_error (error, local_error);
314               break;
315             }
316         }
317
318       g_async_queue_unlock (retval->queue);
319     }
320
321   return (GThreadPool*) retval;
322 }
323
324 /**
325  * g_thread_pool_push:
326  * @pool: a #GThreadPool
327  * @data: a new task for @pool
328  * @error: return location for error
329  * 
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. 
336  *
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.  
341  **/
342 void 
343 g_thread_pool_push (GThreadPool     *pool,
344                     gpointer         data,
345                     GError         **error)
346 {
347   GRealThreadPool *real = (GRealThreadPool*) pool;
348
349   g_return_if_fail (real);
350
351   g_async_queue_lock (real->queue);
352   
353   if (!real->running)
354     {
355       g_async_queue_unlock (real->queue);
356       g_return_if_fail (real->running);
357     }
358
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);
362
363   g_async_queue_push_unlocked (real->queue, data);
364   g_async_queue_unlock (real->queue);
365 }
366
367 /**
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
372  * 
373  * Sets the maximal allowed number of threads for @pool. A value of -1
374  * means, that the maximal number of threads is unlimited.
375  *
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.
379  * 
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.
385  *
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. 
389  **/
390 void
391 g_thread_pool_set_max_threads (GThreadPool     *pool,
392                                gint             max_threads,
393                                GError         **error)
394 {
395   GRealThreadPool *real = (GRealThreadPool*) pool;
396   gint to_start;
397
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);
402
403   g_async_queue_lock (real->queue);
404
405   real->max_threads = max_threads;
406   
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);
411   
412   for ( ; to_start > 0; to_start--)
413     {
414       GError *local_error = NULL;
415       g_thread_pool_start_thread (real, &local_error);
416       if (local_error)
417         {
418           g_propagate_error (error, local_error);
419           break;
420         }
421     }
422    
423   g_async_queue_unlock (real->queue);
424 }
425
426 /**
427  * g_thread_pool_get_max_threads:
428  * @pool: a #GThreadPool
429  *
430  * Returns the maximal number of threads for @pool.
431  *
432  * Return value: the maximal number of threads
433  **/
434 gint
435 g_thread_pool_get_max_threads (GThreadPool     *pool)
436 {
437   GRealThreadPool *real = (GRealThreadPool*) pool;
438   gint retval;
439
440   g_return_val_if_fail (real, 0);
441   g_return_val_if_fail (real->running, 0);
442
443   g_async_queue_lock (real->queue);
444
445   retval = real->max_threads;
446     
447   g_async_queue_unlock (real->queue);
448
449   return retval;
450 }
451
452 /**
453  * g_thread_pool_get_num_threads:
454  * @pool: a #GThreadPool
455  *
456  * Returns the number of threads currently running in @pool.
457  *
458  * Return value: the number of threads currently running
459  **/
460 guint
461 g_thread_pool_get_num_threads (GThreadPool     *pool)
462 {
463   GRealThreadPool *real = (GRealThreadPool*) pool;
464   guint retval;
465
466   g_return_val_if_fail (real, 0);
467   g_return_val_if_fail (real->running, 0);
468
469   g_async_queue_lock (real->queue);
470
471   retval = real->num_threads;
472     
473   g_async_queue_unlock (real->queue);
474
475   return retval;
476 }
477
478 /**
479  * g_thread_pool_unprocessed:
480  * @pool: a #GThreadPool
481  *
482  * Returns the number of tasks still unprocessed in @pool.
483  *
484  * Return value: the number of unprocessed tasks
485  **/
486 guint
487 g_thread_pool_unprocessed (GThreadPool     *pool)
488 {
489   GRealThreadPool *real = (GRealThreadPool*) pool;
490   gint unprocessed;
491
492   g_return_val_if_fail (real, 0);
493   g_return_val_if_fail (real->running, 0);
494
495   unprocessed = g_async_queue_length (real->queue);
496
497   return MAX (unprocessed, 0);
498 }
499
500 /**
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?
505  *
506  * Frees all resources allocated for @pool.
507  *
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.
513  *
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.
517  *
518  * After calling this function @pool must not be used anymore. 
519  **/
520 void
521 g_thread_pool_free (GThreadPool     *pool,
522                     gboolean         immediate,
523                     gboolean         wait)
524 {
525   GRealThreadPool *real = (GRealThreadPool*) pool;
526
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);
533
534   g_async_queue_lock (real->queue);
535
536   real->running = FALSE;
537   real->immediate = immediate;
538   real->waiting = wait;
539
540   if (wait)
541     {
542       g_mutex_lock (inform_mutex);
543       while (g_async_queue_length_unlocked (real->queue) != -real->num_threads)
544         {
545           g_async_queue_unlock (real->queue); 
546           g_cond_wait (inform_cond, inform_mutex); 
547           g_async_queue_lock (real->queue); 
548         }
549       g_mutex_unlock (inform_mutex); 
550     }
551
552   if (g_async_queue_length_unlocked (real->queue) == -real->num_threads)
553     {
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 */
557         {
558           g_async_queue_unlock (real->queue);
559           g_thread_pool_free_internal (real);
560           return;
561         }
562
563       g_thread_pool_wakeup_and_stop_all (real);
564     }
565   
566   real->waiting = FALSE; /* The last thread should cleanup the pool */
567   g_async_queue_unlock (real->queue);
568 }
569
570 static void
571 g_thread_pool_free_internal (GRealThreadPool* pool)
572 {
573   g_return_if_fail (pool);
574   g_return_if_fail (!pool->running);
575   g_return_if_fail (pool->num_threads == 0);
576
577   g_async_queue_unref (pool->queue);
578
579   g_free (pool);
580 }
581
582 static void
583 g_thread_pool_wakeup_and_stop_all (GRealThreadPool* pool)
584 {
585   guint i;
586   
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);
592
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));
596 }
597
598 /**
599  * g_thread_pool_set_max_unused_threads:
600  * @max_threads: maximal number of unused threads
601  *
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.
605  **/
606 void
607 g_thread_pool_set_max_unused_threads (gint max_threads)
608 {
609   g_return_if_fail (max_threads >= -1);  
610
611   G_LOCK (unused_threads);
612   
613   max_unused_threads = max_threads;
614
615   if (max_unused_threads < unused_threads && max_unused_threads != -1)
616     {
617       guint i;
618
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);
624     }
625     
626   G_UNLOCK (unused_threads);
627 }
628
629 /**
630  * g_thread_pool_get_max_unused_threads:
631  * 
632  * Returns the maximal allowed number of unused threads.
633  *
634  * Return value: the maximal number of unused threads
635  **/
636 gint
637 g_thread_pool_get_max_unused_threads (void)
638 {
639   gint retval;
640   
641   G_LOCK (unused_threads);
642   retval = max_unused_threads;
643   G_UNLOCK (unused_threads);
644
645   return retval;
646 }
647
648 /**
649  * g_thread_pool_get_num_unused_threads:
650  * 
651  * Returns the number of currently unused threads.
652  *
653  * Return value: the number of currently unused threads
654  **/
655 guint g_thread_pool_get_num_unused_threads (void)
656 {
657   guint retval;
658   
659   G_LOCK (unused_threads);
660   retval = unused_threads;
661   G_UNLOCK (unused_threads);
662
663   return retval;
664 }
665
666 /**
667  * g_thread_pool_stop_unused_threads:
668  * 
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().
672  **/
673 void g_thread_pool_stop_unused_threads (void)
674
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);
678 }