Misc doc formatting fixes
[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  * GThreadPool: 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 "config.h"
28
29 #include "gthreadpool.h"
30
31 #include "gasyncqueue.h"
32 #include "gasyncqueueprivate.h"
33 #include "gmain.h"
34 #include "gtestutils.h"
35 #include "gtimer.h"
36
37 /**
38  * SECTION:thread_pools
39  * @title: Thread Pools
40  * @short_description: pools of threads to execute work concurrently
41  * @see_also: #GThread
42  *
43  * Sometimes you wish to asynchronously fork out the execution of work
44  * and continue working in your own thread. If that will happen often,
45  * the overhead of starting and destroying a thread each time might be
46  * too high. In such cases reusing already started threads seems like a
47  * good idea. And it indeed is, but implementing this can be tedious
48  * and error-prone.
49  *
50  * Therefore GLib provides thread pools for your convenience. An added
51  * advantage is, that the threads can be shared between the different
52  * subsystems of your program, when they are using GLib.
53  *
54  * To create a new thread pool, you use g_thread_pool_new().
55  * It is destroyed by g_thread_pool_free().
56  *
57  * If you want to execute a certain task within a thread pool,
58  * you call g_thread_pool_push().
59  *
60  * To get the current number of running threads you call
61  * g_thread_pool_get_num_threads(). To get the number of still
62  * unprocessed tasks you call g_thread_pool_unprocessed(). To control
63  * the maximal number of threads for a thread pool, you use
64  * g_thread_pool_get_max_threads() and g_thread_pool_set_max_threads().
65  *
66  * Finally you can control the number of unused threads, that are kept
67  * alive by GLib for future use. The current number can be fetched with
68  * g_thread_pool_get_num_unused_threads(). The maximal number can be
69  * controlled by g_thread_pool_get_max_unused_threads() and
70  * g_thread_pool_set_max_unused_threads(). All currently unused threads
71  * can be stopped by calling g_thread_pool_stop_unused_threads().
72  */
73
74 #define DEBUG_MSG(x)
75 /* #define DEBUG_MSG(args) g_printerr args ; g_printerr ("\n");    */
76
77 typedef struct _GRealThreadPool GRealThreadPool;
78
79 /**
80  * GThreadPool:
81  * @func: the function to execute in the threads of this pool
82  * @user_data: the user data for the threads of this pool
83  * @exclusive: are all threads exclusive to this pool
84  *
85  * The #GThreadPool struct represents a thread pool. It has three
86  * public read-only members, but the underlying struct is bigger,
87  * so you must not copy this struct.
88  */
89 struct _GRealThreadPool
90 {
91   GThreadPool pool;
92   GAsyncQueue *queue;
93   GCond cond;
94   gint max_threads;
95   gint num_threads;
96   gboolean running;
97   gboolean immediate;
98   gboolean waiting;
99   GCompareDataFunc sort_func;
100   gpointer sort_user_data;
101 };
102
103 /* The following is just an address to mark the wakeup order for a
104  * thread, it could be any address (as long, as it isn't a valid
105  * GThreadPool address)
106  */
107 static const gpointer wakeup_thread_marker = (gpointer) &g_thread_pool_new;
108 static gint wakeup_thread_serial = 0;
109
110 /* Here all unused threads are waiting  */
111 static GAsyncQueue *unused_thread_queue = NULL;
112 static gint unused_threads = 0;
113 static gint max_unused_threads = 0;
114 static gint kill_unused_threads = 0;
115 static guint max_idle_time = 0;
116
117 static void             g_thread_pool_queue_push_unlocked (GRealThreadPool  *pool,
118                                                            gpointer          data);
119 static void             g_thread_pool_free_internal       (GRealThreadPool  *pool);
120 static gpointer         g_thread_pool_thread_proxy        (gpointer          data);
121 static gboolean         g_thread_pool_start_thread        (GRealThreadPool  *pool,
122                                                            GError          **error);
123 static void             g_thread_pool_wakeup_and_stop_all (GRealThreadPool  *pool);
124 static GRealThreadPool* g_thread_pool_wait_for_new_pool   (void);
125 static gpointer         g_thread_pool_wait_for_new_task   (GRealThreadPool  *pool);
126
127 static void
128 g_thread_pool_queue_push_unlocked (GRealThreadPool *pool,
129                                    gpointer         data)
130 {
131   if (pool->sort_func)
132     g_async_queue_push_sorted_unlocked (pool->queue,
133                                         data,
134                                         pool->sort_func,
135                                         pool->sort_user_data);
136   else
137     g_async_queue_push_unlocked (pool->queue, data);
138 }
139
140 static GRealThreadPool*
141 g_thread_pool_wait_for_new_pool (void)
142 {
143   GRealThreadPool *pool;
144   gint local_wakeup_thread_serial;
145   guint local_max_unused_threads;
146   gint local_max_idle_time;
147   gint last_wakeup_thread_serial;
148   gboolean have_relayed_thread_marker = FALSE;
149
150   local_max_unused_threads = g_atomic_int_get (&max_unused_threads);
151   local_max_idle_time = g_atomic_int_get (&max_idle_time);
152   last_wakeup_thread_serial = g_atomic_int_get (&wakeup_thread_serial);
153
154   g_atomic_int_inc (&unused_threads);
155
156   do
157     {
158       if (g_atomic_int_get (&unused_threads) >= local_max_unused_threads)
159         {
160           /* If this is a superfluous thread, stop it. */
161           pool = NULL;
162         }
163       else if (local_max_idle_time > 0)
164         {
165           /* If a maximal idle time is given, wait for the given time. */
166           GTimeVal end_time;
167
168           g_get_current_time (&end_time);
169           g_time_val_add (&end_time, local_max_idle_time * 1000);
170
171           DEBUG_MSG (("thread %p waiting in global pool for %f seconds.",
172                       g_thread_self (), local_max_idle_time / 1000.0));
173
174           pool = g_async_queue_timed_pop (unused_thread_queue, &end_time);
175         }
176       else
177         {
178           /* If no maximal idle time is given, wait indefinitely. */
179           DEBUG_MSG (("thread %p waiting in global pool.", g_thread_self ()));
180           pool = g_async_queue_pop (unused_thread_queue);
181         }
182
183       if (pool == wakeup_thread_marker)
184         {
185           local_wakeup_thread_serial = g_atomic_int_get (&wakeup_thread_serial);
186           if (last_wakeup_thread_serial == local_wakeup_thread_serial)
187             {
188               if (!have_relayed_thread_marker)
189               {
190                 /* If this wakeup marker has been received for
191                  * the second time, relay it.
192                  */
193                 DEBUG_MSG (("thread %p relaying wakeup message to "
194                             "waiting thread with lower serial.",
195                             g_thread_self ()));
196
197                 g_async_queue_push (unused_thread_queue, wakeup_thread_marker);
198                 have_relayed_thread_marker = TRUE;
199
200                 /* If a wakeup marker has been relayed, this thread
201                  * will get out of the way for 100 microseconds to
202                  * avoid receiving this marker again.
203                  */
204                 g_usleep (100);
205               }
206             }
207           else
208             {
209               if (g_atomic_int_add (&kill_unused_threads, -1) > 0)
210                 {
211                   pool = NULL;
212                   break;
213                 }
214
215               DEBUG_MSG (("thread %p updating to new limits.",
216                           g_thread_self ()));
217
218               local_max_unused_threads = g_atomic_int_get (&max_unused_threads);
219               local_max_idle_time = g_atomic_int_get (&max_idle_time);
220               last_wakeup_thread_serial = local_wakeup_thread_serial;
221
222               have_relayed_thread_marker = FALSE;
223             }
224         }
225     }
226   while (pool == wakeup_thread_marker);
227
228   g_atomic_int_add (&unused_threads, -1);
229
230   return pool;
231 }
232
233 static gpointer
234 g_thread_pool_wait_for_new_task (GRealThreadPool *pool)
235 {
236   gpointer task = NULL;
237
238   if (pool->running || (!pool->immediate &&
239                         g_async_queue_length_unlocked (pool->queue) > 0))
240     {
241       /* This thread pool is still active. */
242       if (pool->num_threads > pool->max_threads && pool->max_threads != -1)
243         {
244           /* This is a superfluous thread, so it goes to the global pool. */
245           DEBUG_MSG (("superfluous thread %p in pool %p.",
246                       g_thread_self (), pool));
247         }
248       else if (pool->pool.exclusive)
249         {
250           /* Exclusive threads stay attached to the pool. */
251           task = g_async_queue_pop_unlocked (pool->queue);
252
253           DEBUG_MSG (("thread %p in exclusive pool %p waits for task "
254                       "(%d running, %d unprocessed).",
255                       g_thread_self (), pool, pool->num_threads,
256                       g_async_queue_length_unlocked (pool->queue)));
257         }
258       else
259         {
260           /* A thread will wait for new tasks for at most 1/2
261            * second before going to the global pool.
262            */
263           GTimeVal end_time;
264
265           g_get_current_time (&end_time);
266           g_time_val_add (&end_time, G_USEC_PER_SEC / 2);    /* 1/2 second */
267
268           DEBUG_MSG (("thread %p in pool %p waits for up to a 1/2 second for task "
269                       "(%d running, %d unprocessed).",
270                       g_thread_self (), pool, pool->num_threads,
271                       g_async_queue_length_unlocked (pool->queue)));
272
273           task = g_async_queue_timed_pop_unlocked (pool->queue, &end_time);
274         }
275     }
276   else
277     {
278       /* This thread pool is inactive, it will no longer process tasks. */
279       DEBUG_MSG (("pool %p not active, thread %p will go to global pool "
280                   "(running: %s, immediate: %s, len: %d).",
281                   pool, g_thread_self (),
282                   pool->running ? "true" : "false",
283                   pool->immediate ? "true" : "false",
284                   g_async_queue_length_unlocked (pool->queue)));
285     }
286
287   return task;
288 }
289
290
291 static gpointer
292 g_thread_pool_thread_proxy (gpointer data)
293 {
294   GRealThreadPool *pool;
295
296   pool = data;
297
298   DEBUG_MSG (("thread %p started for pool %p.", g_thread_self (), pool));
299
300   g_async_queue_lock (pool->queue);
301
302   while (TRUE)
303     {
304       gpointer task;
305
306       task = g_thread_pool_wait_for_new_task (pool);
307       if (task)
308         {
309           if (pool->running || !pool->immediate)
310             {
311               /* A task was received and the thread pool is active,
312                * so execute the function.
313                */
314               g_async_queue_unlock (pool->queue);
315               DEBUG_MSG (("thread %p in pool %p calling func.",
316                           g_thread_self (), pool));
317               pool->pool.func (task, pool->pool.user_data);
318               g_async_queue_lock (pool->queue);
319             }
320         }
321       else
322         {
323           /* No task was received, so this thread goes to the global pool. */
324           gboolean free_pool = FALSE;
325
326           DEBUG_MSG (("thread %p leaving pool %p for global pool.",
327                       g_thread_self (), pool));
328           pool->num_threads--;
329
330           if (!pool->running)
331             {
332               if (!pool->waiting)
333                 {
334                   if (pool->num_threads == 0)
335                     {
336                       /* If the pool is not running and no other
337                        * thread is waiting for this thread pool to
338                        * finish and this is the last thread of this
339                        * pool, free the pool.
340                        */
341                       free_pool = TRUE;
342                     }
343                   else
344                     {
345                       /* If the pool is not running and no other
346                        * thread is waiting for this thread pool to
347                        * finish and this is not the last thread of
348                        * this pool and there are no tasks left in the
349                        * queue, wakeup the remaining threads.
350                        */
351                       if (g_async_queue_length_unlocked (pool->queue) ==
352                           - pool->num_threads)
353                         g_thread_pool_wakeup_and_stop_all (pool);
354                     }
355                 }
356               else if (pool->immediate ||
357                        g_async_queue_length_unlocked (pool->queue) <= 0)
358                 {
359                   /* If the pool is not running and another thread is
360                    * waiting for this thread pool to finish and there
361                    * are either no tasks left or the pool shall stop
362                    * immediately, inform the waiting thread of a change
363                    * of the thread pool state.
364                    */
365                   g_cond_broadcast (&pool->cond);
366                 }
367             }
368
369           g_async_queue_unlock (pool->queue);
370
371           if (free_pool)
372             g_thread_pool_free_internal (pool);
373
374           if ((pool = g_thread_pool_wait_for_new_pool ()) == NULL)
375             break;
376
377           g_async_queue_lock (pool->queue);
378
379           DEBUG_MSG (("thread %p entering pool %p from global pool.",
380                       g_thread_self (), pool));
381
382           /* pool->num_threads++ is not done here, but in
383            * g_thread_pool_start_thread to make the new started
384            * thread known to the pool before itself can do it.
385            */
386         }
387     }
388
389   return NULL;
390 }
391
392 static gboolean
393 g_thread_pool_start_thread (GRealThreadPool  *pool,
394                             GError          **error)
395 {
396   gboolean success = FALSE;
397
398   if (pool->num_threads >= pool->max_threads && pool->max_threads != -1)
399     /* Enough threads are already running */
400     return TRUE;
401
402   g_async_queue_lock (unused_thread_queue);
403
404   if (g_async_queue_length_unlocked (unused_thread_queue) < 0)
405     {
406       g_async_queue_push_unlocked (unused_thread_queue, pool);
407       success = TRUE;
408     }
409
410   g_async_queue_unlock (unused_thread_queue);
411
412   if (!success)
413     {
414       GThread *thread;
415
416       /* No thread was found, we have to start a new one */
417       thread = g_thread_try_new ("pool", g_thread_pool_thread_proxy, pool, error);
418
419       if (thread == NULL)
420         return FALSE;
421
422       g_thread_unref (thread);
423     }
424
425   /* See comment in g_thread_pool_thread_proxy as to why this is done
426    * here and not there
427    */
428   pool->num_threads++;
429
430   return TRUE;
431 }
432
433 /**
434  * g_thread_pool_new:
435  * @func: a function to execute in the threads of the new thread pool
436  * @user_data: user data that is handed over to @func every time it
437  *     is called
438  * @max_threads: the maximal number of threads to execute concurrently
439  *     in  the new thread pool, -1 means no limit
440  * @exclusive: should this thread pool be exclusive?
441  * @error: return location for error, or %NULL
442  *
443  * This function creates a new thread pool.
444  *
445  * Whenever you call g_thread_pool_push(), either a new thread is
446  * created or an unused one is reused. At most @max_threads threads
447  * are running concurrently for this thread pool. @max_threads = -1
448  * allows unlimited threads to be created for this thread pool. The
449  * newly created or reused thread now executes the function @func
450  * with the two arguments. The first one is the parameter to
451  * g_thread_pool_push() and the second one is @user_data.
452  *
453  * The parameter @exclusive determines whether the thread pool owns
454  * all threads exclusive or shares them with other thread pools.
455  * If @exclusive is %TRUE, @max_threads threads are started
456  * immediately and they will run exclusively for this thread pool
457  * until it is destroyed by g_thread_pool_free(). If @exclusive is
458  * %FALSE, threads are created when needed and shared between all
459  * non-exclusive thread pools. This implies that @max_threads may
460  * not be -1 for exclusive thread pools.
461  *
462  * @error can be %NULL to ignore errors, or non-%NULL to report
463  * errors. An error can only occur when @exclusive is set to %TRUE
464  * and not all @max_threads threads could be created.
465  *
466  * Return value: the new #GThreadPool
467  */
468 GThreadPool *
469 g_thread_pool_new (GFunc      func,
470                    gpointer   user_data,
471                    gint       max_threads,
472                    gboolean   exclusive,
473                    GError   **error)
474 {
475   GRealThreadPool *retval;
476   G_LOCK_DEFINE_STATIC (init);
477
478   g_return_val_if_fail (func, NULL);
479   g_return_val_if_fail (!exclusive || max_threads != -1, NULL);
480   g_return_val_if_fail (max_threads >= -1, NULL);
481
482   retval = g_new (GRealThreadPool, 1);
483
484   retval->pool.func = func;
485   retval->pool.user_data = user_data;
486   retval->pool.exclusive = exclusive;
487   retval->queue = g_async_queue_new ();
488   g_cond_init (&retval->cond);
489   retval->max_threads = max_threads;
490   retval->num_threads = 0;
491   retval->running = TRUE;
492   retval->immediate = FALSE;
493   retval->waiting = FALSE;
494   retval->sort_func = NULL;
495   retval->sort_user_data = NULL;
496
497   G_LOCK (init);
498   if (!unused_thread_queue)
499       unused_thread_queue = g_async_queue_new ();
500   G_UNLOCK (init);
501
502   if (retval->pool.exclusive)
503     {
504       g_async_queue_lock (retval->queue);
505
506       while (retval->num_threads < retval->max_threads)
507         {
508           GError *local_error = NULL;
509
510           if (!g_thread_pool_start_thread (retval, &local_error))
511             {
512               g_propagate_error (error, local_error);
513               break;
514             }
515         }
516
517       g_async_queue_unlock (retval->queue);
518     }
519
520   return (GThreadPool*) retval;
521 }
522
523 /**
524  * g_thread_pool_push:
525  * @pool: a #GThreadPool
526  * @data: a new task for @pool
527  * @error: return location for error, or %NULL
528  *
529  * Inserts @data into the list of tasks to be executed by @pool.
530  *
531  * When the number of currently running threads is lower than the
532  * maximal allowed number of threads, a new thread is started (or
533  * reused) with the properties given to g_thread_pool_new().
534  * Otherwise, @data stays in the queue until a thread in this pool
535  * finishes its previous task and processes @data.
536  *
537  * @error can be %NULL to ignore errors, or non-%NULL to report
538  * errors. An error can only occur when a new thread couldn't be
539  * created. In that case @data is simply appended to the queue of
540  * work to do.
541  *
542  * Before version 2.32, this function did not return a success status.
543  *
544  * Return value: %TRUE on success, %FALSE if an error occurred
545  */
546 gboolean
547 g_thread_pool_push (GThreadPool  *pool,
548                     gpointer      data,
549                     GError      **error)
550 {
551   GRealThreadPool *real;
552   gboolean result;
553
554   real = (GRealThreadPool*) pool;
555
556   g_return_val_if_fail (real, FALSE);
557   g_return_val_if_fail (real->running, FALSE);
558
559   result = TRUE;
560
561   g_async_queue_lock (real->queue);
562
563   if (g_async_queue_length_unlocked (real->queue) >= 0)
564     {
565       /* No thread is waiting in the queue */
566       GError *local_error = NULL;
567
568       if (!g_thread_pool_start_thread (real, &local_error))
569         {
570           g_propagate_error (error, local_error);
571           result = FALSE;
572         }
573     }
574
575   g_thread_pool_queue_push_unlocked (real, data);
576   g_async_queue_unlock (real->queue);
577
578   return result;
579 }
580
581 /**
582  * g_thread_pool_set_max_threads:
583  * @pool: a #GThreadPool
584  * @max_threads: a new maximal number of threads for @pool,
585  *     or -1 for unlimited
586  * @error: return location for error, or %NULL
587  *
588  * Sets the maximal allowed number of threads for @pool.
589  * A value of -1 means that the maximal number of threads
590  * is unlimited. If @pool is an exclusive thread pool, setting
591  * the maximal number of threads to -1 is not allowed.
592  *
593  * Setting @max_threads to 0 means stopping all work for @pool.
594  * It is effectively frozen until @max_threads is set to a non-zero
595  * value again.
596  *
597  * A thread is never terminated while calling @func, as supplied by
598  * g_thread_pool_new(). Instead the maximal number of threads only
599  * has effect for the allocation of new threads in g_thread_pool_push().
600  * A new thread is allocated, whenever the number of currently
601  * running threads in @pool is smaller than the maximal number.
602  *
603  * @error can be %NULL to ignore errors, or non-%NULL to report
604  * errors. An error can only occur when a new thread couldn't be
605  * created.
606  *
607  * Before version 2.32, this function did not return a success status.
608  *
609  * Return value: %TRUE on success, %FALSE if an error occurred
610  */
611 gboolean
612 g_thread_pool_set_max_threads (GThreadPool  *pool,
613                                gint          max_threads,
614                                GError      **error)
615 {
616   GRealThreadPool *real;
617   gint to_start;
618   gboolean result;
619
620   real = (GRealThreadPool*) pool;
621
622   g_return_val_if_fail (real, FALSE);
623   g_return_val_if_fail (real->running, FALSE);
624   g_return_val_if_fail (!real->pool.exclusive || max_threads != -1, FALSE);
625   g_return_val_if_fail (max_threads >= -1, FALSE);
626
627   result = TRUE;
628
629   g_async_queue_lock (real->queue);
630
631   real->max_threads = max_threads;
632
633   if (pool->exclusive)
634     to_start = real->max_threads - real->num_threads;
635   else
636     to_start = g_async_queue_length_unlocked (real->queue);
637
638   for ( ; to_start > 0; to_start--)
639     {
640       GError *local_error = NULL;
641
642       if (!g_thread_pool_start_thread (real, &local_error))
643         {
644           g_propagate_error (error, local_error);
645           result = FALSE;
646           break;
647         }
648     }
649
650   g_async_queue_unlock (real->queue);
651
652   return result;
653 }
654
655 /**
656  * g_thread_pool_get_max_threads:
657  * @pool: a #GThreadPool
658  *
659  * Returns the maximal number of threads for @pool.
660  *
661  * Return value: the maximal number of threads
662  */
663 gint
664 g_thread_pool_get_max_threads (GThreadPool *pool)
665 {
666   GRealThreadPool *real;
667   gint retval;
668
669   real = (GRealThreadPool*) pool;
670
671   g_return_val_if_fail (real, 0);
672   g_return_val_if_fail (real->running, 0);
673
674   g_async_queue_lock (real->queue);
675   retval = real->max_threads;
676   g_async_queue_unlock (real->queue);
677
678   return retval;
679 }
680
681 /**
682  * g_thread_pool_get_num_threads:
683  * @pool: a #GThreadPool
684  *
685  * Returns the number of threads currently running in @pool.
686  *
687  * Return value: the number of threads currently running
688  */
689 guint
690 g_thread_pool_get_num_threads (GThreadPool *pool)
691 {
692   GRealThreadPool *real;
693   guint retval;
694
695   real = (GRealThreadPool*) pool;
696
697   g_return_val_if_fail (real, 0);
698   g_return_val_if_fail (real->running, 0);
699
700   g_async_queue_lock (real->queue);
701   retval = real->num_threads;
702   g_async_queue_unlock (real->queue);
703
704   return retval;
705 }
706
707 /**
708  * g_thread_pool_unprocessed:
709  * @pool: a #GThreadPool
710  *
711  * Returns the number of tasks still unprocessed in @pool.
712  *
713  * Return value: the number of unprocessed tasks
714  */
715 guint
716 g_thread_pool_unprocessed (GThreadPool *pool)
717 {
718   GRealThreadPool *real;
719   gint unprocessed;
720
721   real = (GRealThreadPool*) pool;
722
723   g_return_val_if_fail (real, 0);
724   g_return_val_if_fail (real->running, 0);
725
726   unprocessed = g_async_queue_length (real->queue);
727
728   return MAX (unprocessed, 0);
729 }
730
731 /**
732  * g_thread_pool_free:
733  * @pool: a #GThreadPool
734  * @immediate: should @pool shut down immediately?
735  * @wait_: should the function wait for all tasks to be finished?
736  *
737  * Frees all resources allocated for @pool.
738  *
739  * If @immediate is %TRUE, no new task is processed for @pool.
740  * Otherwise @pool is not freed before the last task is processed.
741  * Note however, that no thread of this pool is interrupted while
742  * processing a task. Instead at least all still running threads
743  * can finish their tasks before the @pool is freed.
744  *
745  * If @wait_ is %TRUE, the functions does not return before all
746  * tasks to be processed (dependent on @immediate, whether all
747  * or only the currently running) are ready.
748  * Otherwise the function returns immediately.
749  *
750  * After calling this function @pool must not be used anymore.
751  */
752 void
753 g_thread_pool_free (GThreadPool *pool,
754                     gboolean     immediate,
755                     gboolean     wait_)
756 {
757   GRealThreadPool *real;
758
759   real = (GRealThreadPool*) pool;
760
761   g_return_if_fail (real);
762   g_return_if_fail (real->running);
763
764   /* If there's no thread allowed here, there is not much sense in
765    * not stopping this pool immediately, when it's not empty
766    */
767   g_return_if_fail (immediate ||
768                     real->max_threads != 0 ||
769                     g_async_queue_length (real->queue) == 0);
770
771   g_async_queue_lock (real->queue);
772
773   real->running = FALSE;
774   real->immediate = immediate;
775   real->waiting = wait_;
776
777   if (wait_)
778     {
779       while (g_async_queue_length_unlocked (real->queue) != -real->num_threads &&
780              !(immediate && real->num_threads == 0))
781         g_cond_wait (&real->cond, _g_async_queue_get_mutex (real->queue));
782     }
783
784   if (immediate || g_async_queue_length_unlocked (real->queue) == -real->num_threads)
785     {
786       /* No thread is currently doing something (and nothing is left
787        * to process in the queue)
788        */
789       if (real->num_threads == 0)
790         {
791           /* No threads left, we clean up */
792           g_async_queue_unlock (real->queue);
793           g_thread_pool_free_internal (real);
794           return;
795         }
796
797       g_thread_pool_wakeup_and_stop_all (real);
798     }
799
800   /* The last thread should cleanup the pool */
801   real->waiting = FALSE;
802   g_async_queue_unlock (real->queue);
803 }
804
805 static void
806 g_thread_pool_free_internal (GRealThreadPool* pool)
807 {
808   g_return_if_fail (pool);
809   g_return_if_fail (pool->running == FALSE);
810   g_return_if_fail (pool->num_threads == 0);
811
812   g_async_queue_unref (pool->queue);
813   g_cond_clear (&pool->cond);
814
815   g_free (pool);
816 }
817
818 static void
819 g_thread_pool_wakeup_and_stop_all (GRealThreadPool *pool)
820 {
821   guint i;
822
823   g_return_if_fail (pool);
824   g_return_if_fail (pool->running == FALSE);
825   g_return_if_fail (pool->num_threads != 0);
826
827   pool->immediate = TRUE;
828
829   for (i = 0; i < pool->num_threads; i++)
830     g_thread_pool_queue_push_unlocked (pool, GUINT_TO_POINTER (1));
831 }
832
833 /**
834  * g_thread_pool_set_max_unused_threads:
835  * @max_threads: maximal number of unused threads
836  *
837  * Sets the maximal number of unused threads to @max_threads.
838  * If @max_threads is -1, no limit is imposed on the number
839  * of unused threads.
840  */
841 void
842 g_thread_pool_set_max_unused_threads (gint max_threads)
843 {
844   g_return_if_fail (max_threads >= -1);
845
846   g_atomic_int_set (&max_unused_threads, max_threads);
847
848   if (max_threads != -1)
849     {
850       max_threads -= g_atomic_int_get (&unused_threads);
851       if (max_threads < 0)
852         {
853           g_atomic_int_set (&kill_unused_threads, -max_threads);
854           g_atomic_int_inc (&wakeup_thread_serial);
855
856           g_async_queue_lock (unused_thread_queue);
857
858           do
859             {
860               g_async_queue_push_unlocked (unused_thread_queue,
861                                            wakeup_thread_marker);
862             }
863           while (++max_threads);
864
865           g_async_queue_unlock (unused_thread_queue);
866         }
867     }
868 }
869
870 /**
871  * g_thread_pool_get_max_unused_threads:
872  *
873  * Returns the maximal allowed number of unused threads.
874  *
875  * Return value: the maximal number of unused threads
876  */
877 gint
878 g_thread_pool_get_max_unused_threads (void)
879 {
880   return g_atomic_int_get (&max_unused_threads);
881 }
882
883 /**
884  * g_thread_pool_get_num_unused_threads:
885  *
886  * Returns the number of currently unused threads.
887  *
888  * Return value: the number of currently unused threads
889  */
890 guint
891 g_thread_pool_get_num_unused_threads (void)
892 {
893   return g_atomic_int_get (&unused_threads);
894 }
895
896 /**
897  * g_thread_pool_stop_unused_threads:
898  *
899  * Stops all currently unused threads. This does not change the
900  * maximal number of unused threads. This function can be used to
901  * regularly stop all unused threads e.g. from g_timeout_add().
902  */
903 void
904 g_thread_pool_stop_unused_threads (void)
905 {
906   guint oldval;
907
908   oldval = g_thread_pool_get_max_unused_threads ();
909
910   g_thread_pool_set_max_unused_threads (0);
911   g_thread_pool_set_max_unused_threads (oldval);
912 }
913
914 /**
915  * g_thread_pool_set_sort_function:
916  * @pool: a #GThreadPool
917  * @func: the #GCompareDataFunc used to sort the list of tasks.
918  *     This function is passed two tasks. It should return
919  *     0 if the order in which they are handled does not matter,
920  *     a negative value if the first task should be processed before
921  *     the second or a positive value if the second task should be
922  *     processed first.
923  * @user_data: user data passed to @func
924  *
925  * Sets the function used to sort the list of tasks. This allows the
926  * tasks to be processed by a priority determined by @func, and not
927  * just in the order in which they were added to the pool.
928  *
929  * Note, if the maximum number of threads is more than 1, the order
930  * that threads are executed cannot be guaranteed 100%. Threads are
931  * scheduled by the operating system and are executed at random. It
932  * cannot be assumed that threads are executed in the order they are
933  * created.
934  *
935  * Since: 2.10
936  */
937 void
938 g_thread_pool_set_sort_function (GThreadPool      *pool,
939                                  GCompareDataFunc  func,
940                                  gpointer          user_data)
941 {
942   GRealThreadPool *real;
943
944   real = (GRealThreadPool*) pool;
945
946   g_return_if_fail (real);
947   g_return_if_fail (real->running);
948
949   g_async_queue_lock (real->queue);
950
951   real->sort_func = func;
952   real->sort_user_data = user_data;
953
954   if (func)
955     g_async_queue_sort_unlocked (real->queue,
956                                  real->sort_func,
957                                  real->sort_user_data);
958
959   g_async_queue_unlock (real->queue);
960 }
961
962 /**
963  * g_thread_pool_set_max_idle_time:
964  * @interval: the maximum @interval (in milliseconds)
965  *     a thread can be idle
966  *
967  * This function will set the maximum @interval that a thread
968  * waiting in the pool for new tasks can be idle for before
969  * being stopped. This function is similar to calling
970  * g_thread_pool_stop_unused_threads() on a regular timeout,
971  * except this is done on a per thread basis.
972  *
973  * By setting @interval to 0, idle threads will not be stopped.
974  *
975  * This function makes use of g_async_queue_timed_pop () using
976  * @interval.
977  *
978  * Since: 2.10
979  */
980 void
981 g_thread_pool_set_max_idle_time (guint interval)
982 {
983   guint i;
984
985   g_atomic_int_set (&max_idle_time, interval);
986
987   i = g_atomic_int_get (&unused_threads);
988   if (i > 0)
989     {
990       g_atomic_int_inc (&wakeup_thread_serial);
991       g_async_queue_lock (unused_thread_queue);
992
993       do
994         {
995           g_async_queue_push_unlocked (unused_thread_queue,
996                                        wakeup_thread_marker);
997         }
998       while (--i);
999
1000       g_async_queue_unlock (unused_thread_queue);
1001     }
1002 }
1003
1004 /**
1005  * g_thread_pool_get_max_idle_time:
1006  *
1007  * This function will return the maximum @interval that a
1008  * thread will wait in the thread pool for new tasks before
1009  * being stopped.
1010  *
1011  * If this function returns 0, threads waiting in the thread
1012  * pool for new work are not stopped.
1013  *
1014  * Return value: the maximum @interval (milliseconds) to wait
1015  *     for new tasks in the thread pool before stopping the
1016  *     thread
1017  *
1018  * Since: 2.10
1019  */
1020 guint
1021 g_thread_pool_get_max_idle_time (void)
1022 {
1023   return g_atomic_int_get (&max_idle_time);
1024 }