fix docs; they said we validated the UTF-8, but we can't possibly detect
[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   gulong stack_size;
35   GAsyncQueue* queue;
36   gint max_threads;
37   gint num_threads;
38   gboolean running;
39   gboolean immediate;
40   gboolean waiting;
41 };
42
43 /* The following is just an address to mark the stop order for a
44  * thread, it could be any address (as long, as it isn't a valid
45  * GThreadPool address) */
46 static const gpointer stop_this_thread_marker = (gpointer) &g_thread_pool_new;
47
48 /* Here all unused threads are waiting, depending on their priority */
49 static GAsyncQueue *unused_thread_queue[G_THREAD_PRIORITY_URGENT + 1][2];
50 static gint unused_threads = 0;
51 static gint max_unused_threads = 0;
52 G_LOCK_DEFINE_STATIC (unused_threads);
53
54 static GMutex *inform_mutex = NULL;
55 static GCond *inform_cond = NULL;
56
57 static void     g_thread_pool_free_internal (GRealThreadPool* pool);
58 static gpointer g_thread_pool_thread_proxy (gpointer data);
59 static void     g_thread_pool_start_thread (GRealThreadPool* pool, 
60                                             GError **error);
61 static void     g_thread_pool_wakeup_and_stop_all (GRealThreadPool* pool);
62
63 #define g_thread_should_run(pool, len) \
64   ((pool)->running || (!(pool)->immediate && (len) > 0))
65
66 static gpointer 
67 g_thread_pool_thread_proxy (gpointer data)
68 {
69   GRealThreadPool *pool = data;
70   gboolean watcher = FALSE;
71
72   g_async_queue_lock (pool->queue);
73   while (TRUE)
74     {
75       gpointer task; 
76       gboolean goto_global_pool = 
77         !pool->pool.exclusive && pool->stack_size == 0;
78       gint len = g_async_queue_length_unlocked (pool->queue);
79       
80       if (g_thread_should_run (pool, len))
81         {
82           if (watcher)
83             {
84               /* This thread is actually not needed here, but it waits
85                * for some time anyway. If during that time a new
86                * request arrives, this saves process
87                * swicthes. Otherwise the thread will go to the global
88                * pool afterwards */
89               GTimeVal end_time;
90               g_get_current_time (&end_time);
91               end_time.tv_usec += G_USEC_PER_SEC / 2; /* Halv a second */
92               if (end_time.tv_usec >= G_USEC_PER_SEC)
93                 {
94                   end_time.tv_usec -= G_USEC_PER_SEC;
95                   end_time.tv_sec += 1;
96                 }
97          
98               task = g_async_queue_timed_pop_unlocked (pool->queue, &end_time);
99             }
100           else
101             {
102               task = g_async_queue_pop_unlocked (pool->queue);
103             }
104
105           if (task)
106             {
107               watcher = FALSE;
108               if (pool->num_threads > pool->max_threads && 
109                   pool->max_threads != -1)
110                 /* We are in fact a superfluous threads, so we go to
111                  * the global pool and just hand the data further to
112                  * the next one waiting in the queue */
113                 {
114                   g_async_queue_push_unlocked (pool->queue, task);
115                   goto_global_pool = TRUE;
116                 }
117               else if (pool->running || !pool->immediate)
118                 {
119                   g_async_queue_unlock (pool->queue);
120                   pool->pool.func (task, pool->pool.user_data);
121                   g_async_queue_lock (pool->queue);
122                 }
123             }
124           len = g_async_queue_length_unlocked (pool->queue);
125         }
126
127       if (!g_thread_should_run (pool, len))
128         {
129           g_cond_broadcast (inform_cond);
130           goto_global_pool = TRUE;
131         }
132       else if (len > 0)
133         {
134           /* At this pool there are no threads waiting, but tasks are. */
135           goto_global_pool = FALSE; 
136         }
137       else if (len == 0 && !watcher && !pool->pool.exclusive)
138         {
139           /* Here neither threads nor tasks are queued and we didn't
140            * just return from a timed wait. We now wait for a limited
141            * time at this pool for new tasks to avoid costly context
142            * switches. */
143           goto_global_pool = FALSE;
144           watcher = TRUE;
145         }
146
147       
148       if (goto_global_pool)
149         {
150           GAsyncQueue *unused_queue = 
151             unused_thread_queue[pool->pool.priority][pool->pool.bound ? 1 : 0];
152           pool->num_threads--; 
153
154           if (!pool->running && !pool->waiting)
155             {
156               if (pool->num_threads == 0)
157                 {
158                   g_async_queue_unlock (pool->queue);
159                   g_thread_pool_free_internal (pool);
160                 }               
161               else if (len == - pool->num_threads)
162                 {
163                   g_thread_pool_wakeup_and_stop_all (pool);
164                   g_async_queue_unlock (pool->queue);
165                 }
166             }
167           else
168             g_async_queue_unlock (pool->queue);
169           
170           g_async_queue_lock (unused_queue);
171
172           G_LOCK (unused_threads);
173           if ((unused_threads >= max_unused_threads && 
174                max_unused_threads != -1) || pool->stack_size != 0)
175             {
176               G_UNLOCK (unused_threads);
177               g_async_queue_unlock (unused_queue);
178               /* Stop this thread */
179               return NULL;      
180             }
181           unused_threads++;
182           G_UNLOCK (unused_threads);
183
184           pool = g_async_queue_pop_unlocked (unused_queue);
185
186           G_LOCK (unused_threads);
187           unused_threads--;
188           G_UNLOCK (unused_threads);
189
190           g_async_queue_unlock (unused_queue);
191           
192           if (pool == stop_this_thread_marker)
193             /* Stop this thread */
194             return NULL;
195           
196           g_async_queue_lock (pool->queue);
197
198           /* pool->num_threads++ is not done here, but in
199            * g_thread_pool_start_thread to make the new started thread
200            * known to the pool, before itself can do it. */
201         }
202     }
203   return NULL;
204 }
205
206 static void
207 g_thread_pool_start_thread (GRealThreadPool  *pool, 
208                             GError          **error)
209 {
210   gboolean success = FALSE;
211   GThreadPriority priority = pool->pool.priority;
212   guint bound = pool->pool.bound ? 1 : 0;
213   GAsyncQueue *queue = unused_thread_queue[priority][bound];
214   
215   if (pool->num_threads >= pool->max_threads && pool->max_threads != -1)
216     /* Enough threads are already running */
217     return;
218
219   g_async_queue_lock (queue);
220
221   if (g_async_queue_length_unlocked (queue) < 0)
222     {
223       /* First we try a thread with the right priority */
224       g_async_queue_push_unlocked (queue, pool);
225       success = TRUE;
226     }
227
228   g_async_queue_unlock (queue);
229
230   /* We will not search for threads with other priorities, because changing
231    * priority is quite unportable */
232   
233   if (!success)
234     {
235       GError *local_error = NULL;
236       /* No thread was found, we have to start a new one */
237       g_thread_create (g_thread_pool_thread_proxy, pool, 
238                        pool->stack_size, FALSE, 
239                        bound, priority, &local_error);
240       
241       if (local_error)
242         {
243           g_propagate_error (error, local_error);
244           return;
245         }
246     }
247
248   /* See comment in g_thread_pool_thread_proxy as to why this is done
249    * here and not there */
250   pool->num_threads++;
251 }
252
253 /**
254  * g_thread_pool_new: 
255  * @func: a function to execute in the threads of the new thread pool
256  * @user_data: user data that is handed over to @func every time it 
257  *   is called
258  * @max_threads: the maximal number of threads to execute concurrently in 
259  *   the new thread pool, -1 means no limit
260  * @stack_size: the stack size for the threads of the new thread pool,
261  *   0 means using the standard
262  * @bound: should the threads of the new thread pool be bound?
263  * @priority: a priority for the threads of the new thread pool
264  * @exclusive: should this thread pool be exclusive?
265  * @error: return location for error
266  *
267  * This function creates a new thread pool. All threads created within
268  * this thread pool will have the priority @priority and the stack
269  * size @stack_size and will be bound if and only if @bound is
270  * true. 
271  *
272  * Whenever you call g_thread_pool_push(), either a new thread is
273  * created or an unused one is reused. At most @max_threads threads
274  * are running concurrently for this thread pool. @max_threads = -1
275  * allows unlimited threads to be created for this thread pool. The
276  * newly created or reused thread now executes the function @func with
277  * the two arguments. The first one is the parameter to
278  * g_thread_pool_push() and the second one is @user_data.
279  *
280  * The parameter @exclusive determines, whether the thread pool owns
281  * all threads exclusive or whether the threads are shared
282  * globally. If @exclusive is @TRUE, @max_threads threads are started
283  * immediately and they will run exclusively for this thread pool until
284  * it is destroyed by g_thread_pool_free(). If @exclusive is @FALSE,
285  * threads are created, when needed and shared between all
286  * non-exclusive thread pools. This implies that @max_threads may not
287  * be -1 for exclusive thread pools.
288  *
289  * Note, that only threads from a thread pool with a @stack_size of 0
290  * (which means using the standard stack size) will be globally
291  * reused. Threads from a thread pool with a non-zero stack size will
292  * stay only in this thread pool until it is freed and can thus not be
293  * controlled by the g_thread_pool_set_unused_threads() function.
294  *
295  * @error can be NULL to ignore errors, or non-NULL to report
296  * errors. An error can only occur, when @exclusive is set to @TRUE and
297  * not all @max_threads threads could be created.
298  *
299  * Return value: the new #GThreadPool
300  **/
301 GThreadPool* 
302 g_thread_pool_new (GFunc            func,
303                    gpointer         user_data,
304                    gint             max_threads,
305                    gulong           stack_size,
306                    gboolean         bound,
307                    GThreadPriority  priority,
308                    gboolean         exclusive,
309                    GError         **error)
310 {
311   GRealThreadPool *retval;
312   G_LOCK_DEFINE_STATIC (init);
313
314   g_return_val_if_fail (func, NULL);
315   g_return_val_if_fail (!exclusive || max_threads != -1, NULL);
316   g_return_val_if_fail (max_threads >= -1, NULL);
317   g_return_val_if_fail (g_thread_supported (), NULL);
318
319   retval = g_new (GRealThreadPool, 1);
320
321   retval->pool.func = func;
322   retval->pool.user_data = user_data;
323   retval->pool.bound = bound;
324   retval->pool.priority = priority;
325   retval->pool.exclusive = exclusive;
326   retval->stack_size = stack_size;
327   retval->queue = g_async_queue_new ();
328   retval->max_threads = max_threads;
329   retval->num_threads = 0;
330   retval->running = TRUE;
331
332   G_LOCK (init);
333   
334   if (!inform_mutex)
335     {
336       inform_mutex = g_mutex_new ();
337       inform_cond = g_cond_new ();
338       for (priority = G_THREAD_PRIORITY_LOW; 
339            priority < G_THREAD_PRIORITY_URGENT + 1; priority++)
340         {
341           unused_thread_queue[priority][0] = g_async_queue_new ();
342           unused_thread_queue[priority][1] = g_async_queue_new ();
343         }
344     }
345
346   G_UNLOCK (init);
347
348   if (retval->pool.exclusive)
349     {
350       g_async_queue_lock (retval->queue);
351   
352       while (retval->num_threads < retval->max_threads)
353         {
354           GError *local_error = NULL;
355           g_thread_pool_start_thread (retval, &local_error);
356           if (local_error)
357             {
358               g_propagate_error (error, local_error);
359               break;
360             }
361         }
362
363       g_async_queue_unlock (retval->queue);
364     }
365
366   return (GThreadPool*) retval;
367 }
368
369 /**
370  * g_thread_pool_push:
371  * @pool: a #GThreadPool
372  * @data: a new task for @pool
373  * @error: return location for error
374  * 
375  * Inserts @data into the list of tasks to be executed by @pool. When
376  * the number of currently running threads is lower than the maximal
377  * allowed number of threads, a new thread is started (or reused) with
378  * the properties given to g_thread_pool_new (). Otherwise @data stays
379  * in the queue until a thread in this pool finishes its previous task
380  * and processes @data. 
381  *
382  * @error can be NULL to ignore errors, or non-NULL to report
383  * errors. An error can only occur, when a new thread couldn't be
384  * created. In that case @data is simply appended to the queue of work
385  * to do.  
386  **/
387 void 
388 g_thread_pool_push (GThreadPool     *pool,
389                     gpointer         data,
390                     GError         **error)
391 {
392   GRealThreadPool *real = (GRealThreadPool*) pool;
393
394   g_return_if_fail (real);
395
396   g_async_queue_lock (real->queue);
397   
398   if (!real->running)
399     {
400       g_async_queue_unlock (real->queue);
401       g_return_if_fail (real->running);
402     }
403
404   if (g_async_queue_length_unlocked (real->queue) >= 0)
405     /* No thread is waiting in the queue */
406     g_thread_pool_start_thread (real, error);
407
408   g_async_queue_push_unlocked (real->queue, data);
409   g_async_queue_unlock (real->queue);
410 }
411
412 /**
413  * g_thread_pool_set_max_threads:
414  * @pool: a #GThreadPool
415  * @max_threads: a new maximal number of threads for @pool
416  * @error: return location for error
417  * 
418  * Sets the maximal allowed number of threads for @pool. A value of -1
419  * means, that the maximal number of threads is unlimited.
420  *
421  * Setting @max_threads to 0 means stopping all work for @pool. It is
422  * effectively frozen until @max_threads is set to a non-zero value
423  * again.
424  * 
425  * A thread is never terminated while calling @func, as supplied by
426  * g_thread_pool_new (). Instead the maximal number of threads only
427  * has effect for the allocation of new threads in g_thread_pool_push
428  * (). A new thread is allocated, whenever the number of currently
429  * running threads in @pool is smaller than the maximal number.
430  *
431  * @error can be NULL to ignore errors, or non-NULL to report
432  * errors. An error can only occur, when a new thread couldn't be
433  * created. 
434  **/
435 void
436 g_thread_pool_set_max_threads (GThreadPool     *pool,
437                                gint             max_threads,
438                                GError         **error)
439 {
440   GRealThreadPool *real = (GRealThreadPool*) pool;
441   gint to_start;
442
443   g_return_if_fail (real);
444   g_return_if_fail (real->running);
445   g_return_if_fail (!real->pool.exclusive || max_threads != -1);
446   g_return_if_fail (max_threads >= -1);
447
448   g_async_queue_lock (real->queue);
449
450   real->max_threads = max_threads;
451   
452   if (pool->exclusive)
453     to_start = real->max_threads - real->num_threads;
454   else
455     to_start = g_async_queue_length_unlocked (real->queue);
456   
457   for ( ; to_start > 0; to_start--)
458     {
459       GError *local_error = NULL;
460       g_thread_pool_start_thread (real, &local_error);
461       if (local_error)
462         {
463           g_propagate_error (error, local_error);
464           break;
465         }
466     }
467    
468   g_async_queue_unlock (real->queue);
469 }
470
471 /**
472  * g_thread_pool_get_max_threads:
473  * @pool: a #GThreadPool
474  *
475  * Returns the maximal number of threads for @pool.
476  *
477  * Return value: the maximal number of threads
478  **/
479 gint
480 g_thread_pool_get_max_threads (GThreadPool     *pool)
481 {
482   GRealThreadPool *real = (GRealThreadPool*) pool;
483   gint retval;
484
485   g_return_val_if_fail (real, 0);
486   g_return_val_if_fail (real->running, 0);
487
488   g_async_queue_lock (real->queue);
489
490   retval = real->max_threads;
491     
492   g_async_queue_unlock (real->queue);
493
494   return retval;
495 }
496
497 /**
498  * g_thread_pool_get_num_threads:
499  * @pool: a #GThreadPool
500  *
501  * Returns the number of threads currently running in @pool.
502  *
503  * Return value: the number of threads currently running
504  **/
505 guint
506 g_thread_pool_get_num_threads (GThreadPool     *pool)
507 {
508   GRealThreadPool *real = (GRealThreadPool*) pool;
509   guint retval;
510
511   g_return_val_if_fail (real, 0);
512   g_return_val_if_fail (real->running, 0);
513
514   g_async_queue_lock (real->queue);
515
516   retval = real->num_threads;
517     
518   g_async_queue_unlock (real->queue);
519
520   return retval;
521 }
522
523 /**
524  * g_thread_pool_unprocessed:
525  * @pool: a #GThreadPool
526  *
527  * Returns the number of tasks still unprocessed in @pool.
528  *
529  * Return value: the number of unprocessed tasks
530  **/
531 guint
532 g_thread_pool_unprocessed (GThreadPool     *pool)
533 {
534   GRealThreadPool *real = (GRealThreadPool*) pool;
535   gint unprocessed;
536
537   g_return_val_if_fail (real, 0);
538   g_return_val_if_fail (real->running, 0);
539
540   unprocessed = g_async_queue_length (real->queue);
541
542   return MAX (unprocessed, 0);
543 }
544
545 /**
546  * g_thread_pool_free:
547  * @pool: a #GThreadPool
548  * @immediate: should @pool shut down immediately?
549  * @wait: should the function wait for all tasks to be finished?
550  *
551  * Frees all resources allocated for @pool.
552  *
553  * If @immediate is #TRUE, no new task is processed for
554  * @pool. Otherwise @pool is not freed before the last task is
555  * processed. Note however, that no thread of this pool is
556  * interrupted, while processing a task. Instead at least all still
557  * running threads can finish their tasks before the @pool is freed.
558  *
559  * If @wait is #TRUE, the functions does not return before all tasks
560  * to be processed (dependent on @immediate, whether all or only the
561  * currently running) are ready. Otherwise the function returns immediately.
562  *
563  * After calling this function @pool must not be used anymore. 
564  **/
565 void
566 g_thread_pool_free (GThreadPool     *pool,
567                     gboolean         immediate,
568                     gboolean         wait)
569 {
570   GRealThreadPool *real = (GRealThreadPool*) pool;
571
572   g_return_if_fail (real);
573   g_return_if_fail (real->running);
574   /* It there's no thread allowed here, there is not much sense in
575    * not stopping this pool immediately, when it's not empty */
576   g_return_if_fail (immediate || real->max_threads != 0 || 
577                     g_async_queue_length (real->queue) == 0);
578
579   g_async_queue_lock (real->queue);
580
581   real->running = FALSE;
582   real->immediate = immediate;
583   real->waiting = wait;
584
585   if (wait)
586     {
587       g_mutex_lock (inform_mutex);
588       while (g_async_queue_length_unlocked (real->queue) != -real->num_threads)
589         {
590           g_async_queue_unlock (real->queue); 
591           g_cond_wait (inform_cond, inform_mutex); 
592           g_async_queue_lock (real->queue); 
593         }
594       g_mutex_unlock (inform_mutex); 
595     }
596
597   if (g_async_queue_length_unlocked (real->queue) == -real->num_threads)
598     {
599       /* No thread is currently doing something (and nothing is left
600        * to process in the queue) */
601       if (real->num_threads == 0) /* No threads left, we clean up */
602         {
603           g_async_queue_unlock (real->queue);
604           g_thread_pool_free_internal (real);
605           return;
606         }
607
608       g_thread_pool_wakeup_and_stop_all (real);
609     }
610   
611   real->waiting = FALSE; /* The last thread should cleanup the pool */
612   g_async_queue_unlock (real->queue);
613 }
614
615 static void
616 g_thread_pool_free_internal (GRealThreadPool* pool)
617 {
618   g_return_if_fail (pool);
619   g_return_if_fail (!pool->running);
620   g_return_if_fail (pool->num_threads == 0);
621
622   g_async_queue_unref (pool->queue);
623
624   g_free (pool);
625 }
626
627 static void
628 g_thread_pool_wakeup_and_stop_all (GRealThreadPool* pool)
629 {
630   guint i;
631   
632   g_return_if_fail (pool);
633   g_return_if_fail (!pool->running);
634   g_return_if_fail (pool->num_threads != 0);
635   g_return_if_fail (g_async_queue_length_unlocked (pool->queue) == 
636                     -pool->num_threads);
637
638   pool->immediate = TRUE; 
639   for (i = 0; i < pool->num_threads; i++)
640     g_async_queue_push_unlocked (pool->queue, GUINT_TO_POINTER (1));
641 }
642
643 /**
644  * g_thread_pool_set_max_unused_threads:
645  * @max_threads: maximal number of unused threads
646  *
647  * Sets the maximal number of unused threads to @max_threads. If
648  * @max_threads is -1, no limit is imposed on the number of unused
649  * threads.
650  **/
651 void
652 g_thread_pool_set_max_unused_threads (gint max_threads)
653 {
654   g_return_if_fail (max_threads >= -1);  
655
656   G_LOCK (unused_threads);
657   
658   max_unused_threads = max_threads;
659
660   if (max_unused_threads < unused_threads && max_unused_threads != -1)
661     {
662       guint close_down_num = unused_threads - max_unused_threads;
663
664       while (close_down_num > 0)
665         {
666           GThreadPriority priority;
667           guint bound;
668
669           guint old_close_down_num = close_down_num;
670           for (priority = G_THREAD_PRIORITY_LOW; 
671                priority < G_THREAD_PRIORITY_URGENT + 1 && close_down_num > 0; 
672                priority++)
673             {
674               for (bound = 0; bound < 2; bound++)
675                 {
676                   GAsyncQueue *queue = unused_thread_queue[priority][bound];
677                   g_async_queue_lock (queue);
678                   
679                   if (g_async_queue_length_unlocked (queue) < 0)
680                     {
681                       g_async_queue_push_unlocked (queue, 
682                                                    stop_this_thread_marker);
683                       close_down_num--;
684                     }
685                   
686                   g_async_queue_unlock (queue);
687                 }
688             }
689
690           /* Just to make sure, there are no counting problems */
691           g_assert (old_close_down_num != close_down_num);
692         }
693     }
694     
695   G_UNLOCK (unused_threads);
696 }
697
698 /**
699  * g_thread_pool_get_max_unused_threads:
700  * 
701  * Returns the maximal allowed number of unused threads.
702  *
703  * Return value: the maximal number of unused threads
704  **/
705 gint
706 g_thread_pool_get_max_unused_threads (void)
707 {
708   gint retval;
709   
710   G_LOCK (unused_threads);
711   retval = max_unused_threads;
712   G_UNLOCK (unused_threads);
713
714   return retval;
715 }
716
717 /**
718  * g_thread_pool_get_num_unused_threads:
719  * 
720  * Returns the number of currently unused threads.
721  *
722  * Return value: the number of currently unused threads
723  **/
724 guint g_thread_pool_get_num_unused_threads (void)
725 {
726   guint retval;
727   
728   G_LOCK (unused_threads);
729   retval = unused_threads;
730   G_UNLOCK (unused_threads);
731
732   return retval;
733 }
734
735 /**
736  * g_thread_pool_stop_unused_threads:
737  * 
738  * Stops all currently unused threads. This does not change the
739  * maximal number of unused threads. This function can be used to
740  * regularly stop all unused threads e.g. from g_timeout_add().
741  **/
742 void g_thread_pool_stop_unused_threads (void)
743
744   guint oldval = g_thread_pool_get_max_unused_threads ();
745   g_thread_pool_set_max_unused_threads (0);
746   g_thread_pool_set_max_unused_threads (oldval);
747 }