Blame glib/gthreadpool.c

Packit ae235b
/* GLIB - Library of useful routines for C programming
Packit ae235b
 * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
Packit ae235b
 *
Packit ae235b
 * GThreadPool: thread pool implementation.
Packit ae235b
 * Copyright (C) 2000 Sebastian Wilhelmi; University of Karlsruhe
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General Public
Packit ae235b
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * MT safe
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include "gthreadpool.h"
Packit ae235b
Packit ae235b
#include "gasyncqueue.h"
Packit ae235b
#include "gasyncqueueprivate.h"
Packit ae235b
#include "gmain.h"
Packit ae235b
#include "gtestutils.h"
Packit ae235b
#include "gtimer.h"
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:thread_pools
Packit ae235b
 * @title: Thread Pools
Packit ae235b
 * @short_description: pools of threads to execute work concurrently
Packit ae235b
 * @see_also: #GThread
Packit ae235b
 *
Packit ae235b
 * Sometimes you wish to asynchronously fork out the execution of work
Packit ae235b
 * and continue working in your own thread. If that will happen often,
Packit ae235b
 * the overhead of starting and destroying a thread each time might be
Packit ae235b
 * too high. In such cases reusing already started threads seems like a
Packit ae235b
 * good idea. And it indeed is, but implementing this can be tedious
Packit ae235b
 * and error-prone.
Packit ae235b
 *
Packit ae235b
 * Therefore GLib provides thread pools for your convenience. An added
Packit ae235b
 * advantage is, that the threads can be shared between the different
Packit ae235b
 * subsystems of your program, when they are using GLib.
Packit ae235b
 *
Packit ae235b
 * To create a new thread pool, you use g_thread_pool_new().
Packit ae235b
 * It is destroyed by g_thread_pool_free().
Packit ae235b
 *
Packit ae235b
 * If you want to execute a certain task within a thread pool,
Packit ae235b
 * you call g_thread_pool_push().
Packit ae235b
 *
Packit ae235b
 * To get the current number of running threads you call
Packit ae235b
 * g_thread_pool_get_num_threads(). To get the number of still
Packit ae235b
 * unprocessed tasks you call g_thread_pool_unprocessed(). To control
Packit ae235b
 * the maximal number of threads for a thread pool, you use
Packit ae235b
 * g_thread_pool_get_max_threads() and g_thread_pool_set_max_threads().
Packit ae235b
 *
Packit ae235b
 * Finally you can control the number of unused threads, that are kept
Packit ae235b
 * alive by GLib for future use. The current number can be fetched with
Packit ae235b
 * g_thread_pool_get_num_unused_threads(). The maximal number can be
Packit ae235b
 * controlled by g_thread_pool_get_max_unused_threads() and
Packit ae235b
 * g_thread_pool_set_max_unused_threads(). All currently unused threads
Packit ae235b
 * can be stopped by calling g_thread_pool_stop_unused_threads().
Packit ae235b
 */
Packit ae235b
Packit ae235b
#define DEBUG_MSG(x)
Packit ae235b
/* #define DEBUG_MSG(args) g_printerr args ; g_printerr ("\n");    */
Packit ae235b
Packit ae235b
typedef struct _GRealThreadPool GRealThreadPool;
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GThreadPool:
Packit ae235b
 * @func: the function to execute in the threads of this pool
Packit ae235b
 * @user_data: the user data for the threads of this pool
Packit ae235b
 * @exclusive: are all threads exclusive to this pool
Packit ae235b
 *
Packit ae235b
 * The #GThreadPool struct represents a thread pool. It has three
Packit ae235b
 * public read-only members, but the underlying struct is bigger,
Packit ae235b
 * so you must not copy this struct.
Packit ae235b
 */
Packit ae235b
struct _GRealThreadPool
Packit ae235b
{
Packit ae235b
  GThreadPool pool;
Packit ae235b
  GAsyncQueue *queue;
Packit ae235b
  GCond cond;
Packit ae235b
  gint max_threads;
Packit ae235b
  gint num_threads;
Packit ae235b
  gboolean running;
Packit ae235b
  gboolean immediate;
Packit ae235b
  gboolean waiting;
Packit ae235b
  GCompareDataFunc sort_func;
Packit ae235b
  gpointer sort_user_data;
Packit ae235b
};
Packit ae235b
Packit ae235b
/* The following is just an address to mark the wakeup order for a
Packit ae235b
 * thread, it could be any address (as long, as it isn't a valid
Packit ae235b
 * GThreadPool address)
Packit ae235b
 */
Packit ae235b
static const gpointer wakeup_thread_marker = (gpointer) &g_thread_pool_new;
Packit ae235b
static gint wakeup_thread_serial = 0;
Packit ae235b
Packit ae235b
/* Here all unused threads are waiting  */
Packit ae235b
static GAsyncQueue *unused_thread_queue = NULL;
Packit ae235b
static gint unused_threads = 0;
Packit ae235b
static gint max_unused_threads = 2;
Packit ae235b
static gint kill_unused_threads = 0;
Packit ae235b
static guint max_idle_time = 15 * 1000;
Packit ae235b
Packit ae235b
static void             g_thread_pool_queue_push_unlocked (GRealThreadPool  *pool,
Packit ae235b
                                                           gpointer          data);
Packit ae235b
static void             g_thread_pool_free_internal       (GRealThreadPool  *pool);
Packit ae235b
static gpointer         g_thread_pool_thread_proxy        (gpointer          data);
Packit ae235b
static gboolean         g_thread_pool_start_thread        (GRealThreadPool  *pool,
Packit ae235b
                                                           GError          **error);
Packit ae235b
static void             g_thread_pool_wakeup_and_stop_all (GRealThreadPool  *pool);
Packit ae235b
static GRealThreadPool* g_thread_pool_wait_for_new_pool   (void);
Packit ae235b
static gpointer         g_thread_pool_wait_for_new_task   (GRealThreadPool  *pool);
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_thread_pool_queue_push_unlocked (GRealThreadPool *pool,
Packit ae235b
                                   gpointer         data)
Packit ae235b
{
Packit ae235b
  if (pool->sort_func)
Packit ae235b
    g_async_queue_push_sorted_unlocked (pool->queue,
Packit ae235b
                                        data,
Packit ae235b
                                        pool->sort_func,
Packit ae235b
                                        pool->sort_user_data);
Packit ae235b
  else
Packit ae235b
    g_async_queue_push_unlocked (pool->queue, data);
Packit ae235b
}
Packit ae235b
Packit ae235b
static GRealThreadPool*
Packit ae235b
g_thread_pool_wait_for_new_pool (void)
Packit ae235b
{
Packit ae235b
  GRealThreadPool *pool;
Packit ae235b
  gint local_wakeup_thread_serial;
Packit ae235b
  guint local_max_unused_threads;
Packit ae235b
  gint local_max_idle_time;
Packit ae235b
  gint last_wakeup_thread_serial;
Packit ae235b
  gboolean have_relayed_thread_marker = FALSE;
Packit ae235b
Packit ae235b
  local_max_unused_threads = g_atomic_int_get (&max_unused_threads);
Packit ae235b
  local_max_idle_time = g_atomic_int_get (&max_idle_time);
Packit ae235b
  last_wakeup_thread_serial = g_atomic_int_get (&wakeup_thread_serial);
Packit ae235b
Packit ae235b
  g_atomic_int_inc (&unused_threads);
Packit ae235b
Packit ae235b
  do
Packit ae235b
    {
Packit ae235b
      if (g_atomic_int_get (&unused_threads) >= local_max_unused_threads)
Packit ae235b
        {
Packit ae235b
          /* If this is a superfluous thread, stop it. */
Packit ae235b
          pool = NULL;
Packit ae235b
        }
Packit ae235b
      else if (local_max_idle_time > 0)
Packit ae235b
        {
Packit ae235b
          /* If a maximal idle time is given, wait for the given time. */
Packit ae235b
          DEBUG_MSG (("thread %p waiting in global pool for %f seconds.",
Packit ae235b
                      g_thread_self (), local_max_idle_time / 1000.0));
Packit ae235b
Packit ae235b
          pool = g_async_queue_timeout_pop (unused_thread_queue,
Packit ae235b
					    local_max_idle_time * 1000);
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        {
Packit ae235b
          /* If no maximal idle time is given, wait indefinitely. */
Packit ae235b
          DEBUG_MSG (("thread %p waiting in global pool.", g_thread_self ()));
Packit ae235b
          pool = g_async_queue_pop (unused_thread_queue);
Packit ae235b
        }
Packit ae235b
Packit ae235b
      if (pool == wakeup_thread_marker)
Packit ae235b
        {
Packit ae235b
          local_wakeup_thread_serial = g_atomic_int_get (&wakeup_thread_serial);
Packit ae235b
          if (last_wakeup_thread_serial == local_wakeup_thread_serial)
Packit ae235b
            {
Packit ae235b
              if (!have_relayed_thread_marker)
Packit ae235b
              {
Packit ae235b
                /* If this wakeup marker has been received for
Packit ae235b
                 * the second time, relay it.
Packit ae235b
                 */
Packit ae235b
                DEBUG_MSG (("thread %p relaying wakeup message to "
Packit ae235b
                            "waiting thread with lower serial.",
Packit ae235b
                            g_thread_self ()));
Packit ae235b
Packit ae235b
                g_async_queue_push (unused_thread_queue, wakeup_thread_marker);
Packit ae235b
                have_relayed_thread_marker = TRUE;
Packit ae235b
Packit ae235b
                /* If a wakeup marker has been relayed, this thread
Packit ae235b
                 * will get out of the way for 100 microseconds to
Packit ae235b
                 * avoid receiving this marker again.
Packit ae235b
                 */
Packit ae235b
                g_usleep (100);
Packit ae235b
              }
Packit ae235b
            }
Packit ae235b
          else
Packit ae235b
            {
Packit ae235b
              if (g_atomic_int_add (&kill_unused_threads, -1) > 0)
Packit ae235b
                {
Packit ae235b
                  pool = NULL;
Packit ae235b
                  break;
Packit ae235b
                }
Packit ae235b
Packit ae235b
              DEBUG_MSG (("thread %p updating to new limits.",
Packit ae235b
                          g_thread_self ()));
Packit ae235b
Packit ae235b
              local_max_unused_threads = g_atomic_int_get (&max_unused_threads);
Packit ae235b
              local_max_idle_time = g_atomic_int_get (&max_idle_time);
Packit ae235b
              last_wakeup_thread_serial = local_wakeup_thread_serial;
Packit ae235b
Packit ae235b
              have_relayed_thread_marker = FALSE;
Packit ae235b
            }
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
  while (pool == wakeup_thread_marker);
Packit ae235b
Packit ae235b
  g_atomic_int_add (&unused_threads, -1);
Packit ae235b
Packit ae235b
  return pool;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gpointer
Packit ae235b
g_thread_pool_wait_for_new_task (GRealThreadPool *pool)
Packit ae235b
{
Packit ae235b
  gpointer task = NULL;
Packit ae235b
Packit ae235b
  if (pool->running || (!pool->immediate &&
Packit ae235b
                        g_async_queue_length_unlocked (pool->queue) > 0))
Packit ae235b
    {
Packit ae235b
      /* This thread pool is still active. */
Packit ae235b
      if (pool->num_threads > pool->max_threads && pool->max_threads != -1)
Packit ae235b
        {
Packit ae235b
          /* This is a superfluous thread, so it goes to the global pool. */
Packit ae235b
          DEBUG_MSG (("superfluous thread %p in pool %p.",
Packit ae235b
                      g_thread_self (), pool));
Packit ae235b
        }
Packit ae235b
      else if (pool->pool.exclusive)
Packit ae235b
        {
Packit ae235b
          /* Exclusive threads stay attached to the pool. */
Packit ae235b
          task = g_async_queue_pop_unlocked (pool->queue);
Packit ae235b
Packit ae235b
          DEBUG_MSG (("thread %p in exclusive pool %p waits for task "
Packit ae235b
                      "(%d running, %d unprocessed).",
Packit ae235b
                      g_thread_self (), pool, pool->num_threads,
Packit ae235b
                      g_async_queue_length_unlocked (pool->queue)));
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        {
Packit ae235b
          /* A thread will wait for new tasks for at most 1/2
Packit ae235b
           * second before going to the global pool.
Packit ae235b
           */
Packit ae235b
          DEBUG_MSG (("thread %p in pool %p waits for up to a 1/2 second for task "
Packit ae235b
                      "(%d running, %d unprocessed).",
Packit ae235b
                      g_thread_self (), pool, pool->num_threads,
Packit ae235b
                      g_async_queue_length_unlocked (pool->queue)));
Packit ae235b
Packit ae235b
          task = g_async_queue_timeout_pop_unlocked (pool->queue,
Packit ae235b
						     G_USEC_PER_SEC / 2);
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      /* This thread pool is inactive, it will no longer process tasks. */
Packit ae235b
      DEBUG_MSG (("pool %p not active, thread %p will go to global pool "
Packit ae235b
                  "(running: %s, immediate: %s, len: %d).",
Packit ae235b
                  pool, g_thread_self (),
Packit ae235b
                  pool->running ? "true" : "false",
Packit ae235b
                  pool->immediate ? "true" : "false",
Packit ae235b
                  g_async_queue_length_unlocked (pool->queue)));
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return task;
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
static gpointer
Packit ae235b
g_thread_pool_thread_proxy (gpointer data)
Packit ae235b
{
Packit ae235b
  GRealThreadPool *pool;
Packit ae235b
Packit ae235b
  pool = data;
Packit ae235b
Packit ae235b
  DEBUG_MSG (("thread %p started for pool %p.", g_thread_self (), pool));
Packit ae235b
Packit ae235b
  g_async_queue_lock (pool->queue);
Packit ae235b
Packit ae235b
  while (TRUE)
Packit ae235b
    {
Packit ae235b
      gpointer task;
Packit ae235b
Packit ae235b
      task = g_thread_pool_wait_for_new_task (pool);
Packit ae235b
      if (task)
Packit ae235b
        {
Packit ae235b
          if (pool->running || !pool->immediate)
Packit ae235b
            {
Packit ae235b
              /* A task was received and the thread pool is active,
Packit ae235b
               * so execute the function.
Packit ae235b
               */
Packit ae235b
              g_async_queue_unlock (pool->queue);
Packit ae235b
              DEBUG_MSG (("thread %p in pool %p calling func.",
Packit ae235b
                          g_thread_self (), pool));
Packit ae235b
              pool->pool.func (task, pool->pool.user_data);
Packit ae235b
              g_async_queue_lock (pool->queue);
Packit ae235b
            }
Packit ae235b
        }
Packit ae235b
      else
Packit ae235b
        {
Packit ae235b
          /* No task was received, so this thread goes to the global pool. */
Packit ae235b
          gboolean free_pool = FALSE;
Packit ae235b
Packit ae235b
          DEBUG_MSG (("thread %p leaving pool %p for global pool.",
Packit ae235b
                      g_thread_self (), pool));
Packit ae235b
          pool->num_threads--;
Packit ae235b
Packit ae235b
          if (!pool->running)
Packit ae235b
            {
Packit ae235b
              if (!pool->waiting)
Packit ae235b
                {
Packit ae235b
                  if (pool->num_threads == 0)
Packit ae235b
                    {
Packit ae235b
                      /* If the pool is not running and no other
Packit ae235b
                       * thread is waiting for this thread pool to
Packit ae235b
                       * finish and this is the last thread of this
Packit ae235b
                       * pool, free the pool.
Packit ae235b
                       */
Packit ae235b
                      free_pool = TRUE;
Packit ae235b
                    }
Packit ae235b
                  else
Packit ae235b
                    {
Packit ae235b
                      /* If the pool is not running and no other
Packit ae235b
                       * thread is waiting for this thread pool to
Packit ae235b
                       * finish and this is not the last thread of
Packit ae235b
                       * this pool and there are no tasks left in the
Packit ae235b
                       * queue, wakeup the remaining threads.
Packit ae235b
                       */
Packit ae235b
                      if (g_async_queue_length_unlocked (pool->queue) ==
Packit ae235b
                          - pool->num_threads)
Packit ae235b
                        g_thread_pool_wakeup_and_stop_all (pool);
Packit ae235b
                    }
Packit ae235b
                }
Packit ae235b
              else if (pool->immediate ||
Packit ae235b
                       g_async_queue_length_unlocked (pool->queue) <= 0)
Packit ae235b
                {
Packit ae235b
                  /* If the pool is not running and another thread is
Packit ae235b
                   * waiting for this thread pool to finish and there
Packit ae235b
                   * are either no tasks left or the pool shall stop
Packit ae235b
                   * immediately, inform the waiting thread of a change
Packit ae235b
                   * of the thread pool state.
Packit ae235b
                   */
Packit ae235b
                  g_cond_broadcast (&pool->cond);
Packit ae235b
                }
Packit ae235b
            }
Packit ae235b
Packit ae235b
          g_async_queue_unlock (pool->queue);
Packit ae235b
Packit ae235b
          if (free_pool)
Packit ae235b
            g_thread_pool_free_internal (pool);
Packit ae235b
Packit ae235b
          if ((pool = g_thread_pool_wait_for_new_pool ()) == NULL)
Packit ae235b
            break;
Packit ae235b
Packit ae235b
          g_async_queue_lock (pool->queue);
Packit ae235b
Packit ae235b
          DEBUG_MSG (("thread %p entering pool %p from global pool.",
Packit ae235b
                      g_thread_self (), pool));
Packit ae235b
Packit ae235b
          /* pool->num_threads++ is not done here, but in
Packit ae235b
           * g_thread_pool_start_thread to make the new started
Packit ae235b
           * thread known to the pool before itself can do it.
Packit ae235b
           */
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_thread_pool_start_thread (GRealThreadPool  *pool,
Packit ae235b
                            GError          **error)
Packit ae235b
{
Packit ae235b
  gboolean success = FALSE;
Packit ae235b
Packit ae235b
  if (pool->num_threads >= pool->max_threads && pool->max_threads != -1)
Packit ae235b
    /* Enough threads are already running */
Packit ae235b
    return TRUE;
Packit ae235b
Packit ae235b
  g_async_queue_lock (unused_thread_queue);
Packit ae235b
Packit ae235b
  if (g_async_queue_length_unlocked (unused_thread_queue) < 0)
Packit ae235b
    {
Packit ae235b
      g_async_queue_push_unlocked (unused_thread_queue, pool);
Packit ae235b
      success = TRUE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_async_queue_unlock (unused_thread_queue);
Packit ae235b
Packit ae235b
  if (!success)
Packit ae235b
    {
Packit ae235b
      GThread *thread;
Packit ae235b
Packit ae235b
      /* No thread was found, we have to start a new one */
Packit ae235b
      thread = g_thread_try_new ("pool", g_thread_pool_thread_proxy, pool, error);
Packit ae235b
Packit ae235b
      if (thread == NULL)
Packit ae235b
        return FALSE;
Packit ae235b
Packit ae235b
      g_thread_unref (thread);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* See comment in g_thread_pool_thread_proxy as to why this is done
Packit ae235b
   * here and not there
Packit ae235b
   */
Packit ae235b
  pool->num_threads++;
Packit ae235b
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_thread_pool_new:
Packit ae235b
 * @func: a function to execute in the threads of the new thread pool
Packit ae235b
 * @user_data: user data that is handed over to @func every time it
Packit ae235b
 *     is called
Packit ae235b
 * @max_threads: the maximal number of threads to execute concurrently
Packit ae235b
 *     in  the new thread pool, -1 means no limit
Packit ae235b
 * @exclusive: should this thread pool be exclusive?
Packit ae235b
 * @error: return location for error, or %NULL
Packit ae235b
 *
Packit ae235b
 * This function creates a new thread pool.
Packit ae235b
 *
Packit ae235b
 * Whenever you call g_thread_pool_push(), either a new thread is
Packit ae235b
 * created or an unused one is reused. At most @max_threads threads
Packit ae235b
 * are running concurrently for this thread pool. @max_threads = -1
Packit ae235b
 * allows unlimited threads to be created for this thread pool. The
Packit ae235b
 * newly created or reused thread now executes the function @func
Packit ae235b
 * with the two arguments. The first one is the parameter to
Packit ae235b
 * g_thread_pool_push() and the second one is @user_data.
Packit ae235b
 *
Packit ae235b
 * The parameter @exclusive determines whether the thread pool owns
Packit ae235b
 * all threads exclusive or shares them with other thread pools.
Packit ae235b
 * If @exclusive is %TRUE, @max_threads threads are started
Packit ae235b
 * immediately and they will run exclusively for this thread pool
Packit ae235b
 * until it is destroyed by g_thread_pool_free(). If @exclusive is
Packit ae235b
 * %FALSE, threads are created when needed and shared between all
Packit ae235b
 * non-exclusive thread pools. This implies that @max_threads may
Packit ae235b
 * not be -1 for exclusive thread pools. Besides, exclusive thread
Packit ae235b
 * pools are not affected by g_thread_pool_set_max_idle_time()
Packit ae235b
 * since their threads are never considered idle and returned to the
Packit ae235b
 * global pool.
Packit ae235b
 *
Packit ae235b
 * @error can be %NULL to ignore errors, or non-%NULL to report
Packit ae235b
 * errors. An error can only occur when @exclusive is set to %TRUE
Packit ae235b
 * and not all @max_threads threads could be created.
Packit ae235b
 * See #GThreadError for possible errors that may occur.
Packit ae235b
 * Note, even in case of error a valid #GThreadPool is returned.
Packit ae235b
 *
Packit ae235b
 * Returns: the new #GThreadPool
Packit ae235b
 */
Packit ae235b
GThreadPool *
Packit ae235b
g_thread_pool_new (GFunc      func,
Packit ae235b
                   gpointer   user_data,
Packit ae235b
                   gint       max_threads,
Packit ae235b
                   gboolean   exclusive,
Packit ae235b
                   GError   **error)
Packit ae235b
{
Packit ae235b
  GRealThreadPool *retval;
Packit ae235b
  G_LOCK_DEFINE_STATIC (init);
Packit ae235b
Packit ae235b
  g_return_val_if_fail (func, NULL);
Packit ae235b
  g_return_val_if_fail (!exclusive || max_threads != -1, NULL);
Packit ae235b
  g_return_val_if_fail (max_threads >= -1, NULL);
Packit ae235b
Packit ae235b
  retval = g_new (GRealThreadPool, 1);
Packit ae235b
Packit ae235b
  retval->pool.func = func;
Packit ae235b
  retval->pool.user_data = user_data;
Packit ae235b
  retval->pool.exclusive = exclusive;
Packit ae235b
  retval->queue = g_async_queue_new ();
Packit ae235b
  g_cond_init (&retval->cond);
Packit ae235b
  retval->max_threads = max_threads;
Packit ae235b
  retval->num_threads = 0;
Packit ae235b
  retval->running = TRUE;
Packit ae235b
  retval->immediate = FALSE;
Packit ae235b
  retval->waiting = FALSE;
Packit ae235b
  retval->sort_func = NULL;
Packit ae235b
  retval->sort_user_data = NULL;
Packit ae235b
Packit ae235b
  G_LOCK (init);
Packit ae235b
  if (!unused_thread_queue)
Packit ae235b
      unused_thread_queue = g_async_queue_new ();
Packit ae235b
  G_UNLOCK (init);
Packit ae235b
Packit ae235b
  if (retval->pool.exclusive)
Packit ae235b
    {
Packit ae235b
      g_async_queue_lock (retval->queue);
Packit ae235b
Packit ae235b
      while (retval->num_threads < retval->max_threads)
Packit ae235b
        {
Packit ae235b
          GError *local_error = NULL;
Packit ae235b
Packit ae235b
          if (!g_thread_pool_start_thread (retval, &local_error))
Packit ae235b
            {
Packit ae235b
              g_propagate_error (error, local_error);
Packit ae235b
              break;
Packit ae235b
            }
Packit ae235b
        }
Packit ae235b
Packit ae235b
      g_async_queue_unlock (retval->queue);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return (GThreadPool*) retval;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_thread_pool_push:
Packit ae235b
 * @pool: a #GThreadPool
Packit ae235b
 * @data: a new task for @pool
Packit ae235b
 * @error: return location for error, or %NULL
Packit ae235b
 *
Packit ae235b
 * Inserts @data into the list of tasks to be executed by @pool.
Packit ae235b
 *
Packit ae235b
 * When the number of currently running threads is lower than the
Packit ae235b
 * maximal allowed number of threads, a new thread is started (or
Packit ae235b
 * reused) with the properties given to g_thread_pool_new().
Packit ae235b
 * Otherwise, @data stays in the queue until a thread in this pool
Packit ae235b
 * finishes its previous task and processes @data.
Packit ae235b
 *
Packit ae235b
 * @error can be %NULL to ignore errors, or non-%NULL to report
Packit ae235b
 * errors. An error can only occur when a new thread couldn't be
Packit ae235b
 * created. In that case @data is simply appended to the queue of
Packit ae235b
 * work to do.
Packit ae235b
 *
Packit ae235b
 * Before version 2.32, this function did not return a success status.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE on success, %FALSE if an error occurred
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_thread_pool_push (GThreadPool  *pool,
Packit ae235b
                    gpointer      data,
Packit ae235b
                    GError      **error)
Packit ae235b
{
Packit ae235b
  GRealThreadPool *real;
Packit ae235b
  gboolean result;
Packit ae235b
Packit ae235b
  real = (GRealThreadPool*) pool;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (real, FALSE);
Packit ae235b
  g_return_val_if_fail (real->running, FALSE);
Packit ae235b
Packit ae235b
  result = TRUE;
Packit ae235b
Packit ae235b
  g_async_queue_lock (real->queue);
Packit ae235b
Packit ae235b
  if (g_async_queue_length_unlocked (real->queue) >= 0)
Packit ae235b
    {
Packit ae235b
      /* No thread is waiting in the queue */
Packit ae235b
      GError *local_error = NULL;
Packit ae235b
Packit ae235b
      if (!g_thread_pool_start_thread (real, &local_error))
Packit ae235b
        {
Packit ae235b
          g_propagate_error (error, local_error);
Packit ae235b
          result = FALSE;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_thread_pool_queue_push_unlocked (real, data);
Packit ae235b
  g_async_queue_unlock (real->queue);
Packit ae235b
Packit ae235b
  return result;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_thread_pool_set_max_threads:
Packit ae235b
 * @pool: a #GThreadPool
Packit ae235b
 * @max_threads: a new maximal number of threads for @pool,
Packit ae235b
 *     or -1 for unlimited
Packit ae235b
 * @error: return location for error, or %NULL
Packit ae235b
 *
Packit ae235b
 * Sets the maximal allowed number of threads for @pool.
Packit ae235b
 * A value of -1 means that the maximal number of threads
Packit ae235b
 * is unlimited. If @pool is an exclusive thread pool, setting
Packit ae235b
 * the maximal number of threads to -1 is not allowed.
Packit ae235b
 *
Packit ae235b
 * Setting @max_threads to 0 means stopping all work for @pool.
Packit ae235b
 * It is effectively frozen until @max_threads is set to a non-zero
Packit ae235b
 * value again.
Packit ae235b
 *
Packit ae235b
 * A thread is never terminated while calling @func, as supplied by
Packit ae235b
 * g_thread_pool_new(). Instead the maximal number of threads only
Packit ae235b
 * has effect for the allocation of new threads in g_thread_pool_push().
Packit ae235b
 * A new thread is allocated, whenever the number of currently
Packit ae235b
 * running threads in @pool is smaller than the maximal number.
Packit ae235b
 *
Packit ae235b
 * @error can be %NULL to ignore errors, or non-%NULL to report
Packit ae235b
 * errors. An error can only occur when a new thread couldn't be
Packit ae235b
 * created.
Packit ae235b
 *
Packit ae235b
 * Before version 2.32, this function did not return a success status.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE on success, %FALSE if an error occurred
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_thread_pool_set_max_threads (GThreadPool  *pool,
Packit ae235b
                               gint          max_threads,
Packit ae235b
                               GError      **error)
Packit ae235b
{
Packit ae235b
  GRealThreadPool *real;
Packit ae235b
  gint to_start;
Packit ae235b
  gboolean result;
Packit ae235b
Packit ae235b
  real = (GRealThreadPool*) pool;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (real, FALSE);
Packit ae235b
  g_return_val_if_fail (real->running, FALSE);
Packit ae235b
  g_return_val_if_fail (!real->pool.exclusive || max_threads != -1, FALSE);
Packit ae235b
  g_return_val_if_fail (max_threads >= -1, FALSE);
Packit ae235b
Packit ae235b
  result = TRUE;
Packit ae235b
Packit ae235b
  g_async_queue_lock (real->queue);
Packit ae235b
Packit ae235b
  real->max_threads = max_threads;
Packit ae235b
Packit ae235b
  if (pool->exclusive)
Packit ae235b
    to_start = real->max_threads - real->num_threads;
Packit ae235b
  else
Packit ae235b
    to_start = g_async_queue_length_unlocked (real->queue);
Packit ae235b
Packit ae235b
  for ( ; to_start > 0; to_start--)
Packit ae235b
    {
Packit ae235b
      GError *local_error = NULL;
Packit ae235b
Packit ae235b
      if (!g_thread_pool_start_thread (real, &local_error))
Packit ae235b
        {
Packit ae235b
          g_propagate_error (error, local_error);
Packit ae235b
          result = FALSE;
Packit ae235b
          break;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_async_queue_unlock (real->queue);
Packit ae235b
Packit ae235b
  return result;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_thread_pool_get_max_threads:
Packit ae235b
 * @pool: a #GThreadPool
Packit ae235b
 *
Packit ae235b
 * Returns the maximal number of threads for @pool.
Packit ae235b
 *
Packit ae235b
 * Returns: the maximal number of threads
Packit ae235b
 */
Packit ae235b
gint
Packit ae235b
g_thread_pool_get_max_threads (GThreadPool *pool)
Packit ae235b
{
Packit ae235b
  GRealThreadPool *real;
Packit ae235b
  gint retval;
Packit ae235b
Packit ae235b
  real = (GRealThreadPool*) pool;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (real, 0);
Packit ae235b
  g_return_val_if_fail (real->running, 0);
Packit ae235b
Packit ae235b
  g_async_queue_lock (real->queue);
Packit ae235b
  retval = real->max_threads;
Packit ae235b
  g_async_queue_unlock (real->queue);
Packit ae235b
Packit ae235b
  return retval;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_thread_pool_get_num_threads:
Packit ae235b
 * @pool: a #GThreadPool
Packit ae235b
 *
Packit ae235b
 * Returns the number of threads currently running in @pool.
Packit ae235b
 *
Packit ae235b
 * Returns: the number of threads currently running
Packit ae235b
 */
Packit ae235b
guint
Packit ae235b
g_thread_pool_get_num_threads (GThreadPool *pool)
Packit ae235b
{
Packit ae235b
  GRealThreadPool *real;
Packit ae235b
  guint retval;
Packit ae235b
Packit ae235b
  real = (GRealThreadPool*) pool;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (real, 0);
Packit ae235b
  g_return_val_if_fail (real->running, 0);
Packit ae235b
Packit ae235b
  g_async_queue_lock (real->queue);
Packit ae235b
  retval = real->num_threads;
Packit ae235b
  g_async_queue_unlock (real->queue);
Packit ae235b
Packit ae235b
  return retval;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_thread_pool_unprocessed:
Packit ae235b
 * @pool: a #GThreadPool
Packit ae235b
 *
Packit ae235b
 * Returns the number of tasks still unprocessed in @pool.
Packit ae235b
 *
Packit ae235b
 * Returns: the number of unprocessed tasks
Packit ae235b
 */
Packit ae235b
guint
Packit ae235b
g_thread_pool_unprocessed (GThreadPool *pool)
Packit ae235b
{
Packit ae235b
  GRealThreadPool *real;
Packit ae235b
  gint unprocessed;
Packit ae235b
Packit ae235b
  real = (GRealThreadPool*) pool;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (real, 0);
Packit ae235b
  g_return_val_if_fail (real->running, 0);
Packit ae235b
Packit ae235b
  unprocessed = g_async_queue_length (real->queue);
Packit ae235b
Packit ae235b
  return MAX (unprocessed, 0);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_thread_pool_free:
Packit ae235b
 * @pool: a #GThreadPool
Packit ae235b
 * @immediate: should @pool shut down immediately?
Packit ae235b
 * @wait_: should the function wait for all tasks to be finished?
Packit ae235b
 *
Packit ae235b
 * Frees all resources allocated for @pool.
Packit ae235b
 *
Packit ae235b
 * If @immediate is %TRUE, no new task is processed for @pool.
Packit ae235b
 * Otherwise @pool is not freed before the last task is processed.
Packit ae235b
 * Note however, that no thread of this pool is interrupted while
Packit ae235b
 * processing a task. Instead at least all still running threads
Packit ae235b
 * can finish their tasks before the @pool is freed.
Packit ae235b
 *
Packit ae235b
 * If @wait_ is %TRUE, the functions does not return before all
Packit ae235b
 * tasks to be processed (dependent on @immediate, whether all
Packit ae235b
 * or only the currently running) are ready.
Packit ae235b
 * Otherwise the function returns immediately.
Packit ae235b
 *
Packit ae235b
 * After calling this function @pool must not be used anymore.
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_thread_pool_free (GThreadPool *pool,
Packit ae235b
                    gboolean     immediate,
Packit ae235b
                    gboolean     wait_)
Packit ae235b
{
Packit ae235b
  GRealThreadPool *real;
Packit ae235b
Packit ae235b
  real = (GRealThreadPool*) pool;
Packit ae235b
Packit ae235b
  g_return_if_fail (real);
Packit ae235b
  g_return_if_fail (real->running);
Packit ae235b
Packit ae235b
  /* If there's no thread allowed here, there is not much sense in
Packit ae235b
   * not stopping this pool immediately, when it's not empty
Packit ae235b
   */
Packit ae235b
  g_return_if_fail (immediate ||
Packit ae235b
                    real->max_threads != 0 ||
Packit ae235b
                    g_async_queue_length (real->queue) == 0);
Packit ae235b
Packit ae235b
  g_async_queue_lock (real->queue);
Packit ae235b
Packit ae235b
  real->running = FALSE;
Packit ae235b
  real->immediate = immediate;
Packit ae235b
  real->waiting = wait_;
Packit ae235b
Packit ae235b
  if (wait_)
Packit ae235b
    {
Packit ae235b
      while (g_async_queue_length_unlocked (real->queue) != -real->num_threads &&
Packit ae235b
             !(immediate && real->num_threads == 0))
Packit ae235b
        g_cond_wait (&real->cond, _g_async_queue_get_mutex (real->queue));
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (immediate || g_async_queue_length_unlocked (real->queue) == -real->num_threads)
Packit ae235b
    {
Packit ae235b
      /* No thread is currently doing something (and nothing is left
Packit ae235b
       * to process in the queue)
Packit ae235b
       */
Packit ae235b
      if (real->num_threads == 0)
Packit ae235b
        {
Packit ae235b
          /* No threads left, we clean up */
Packit ae235b
          g_async_queue_unlock (real->queue);
Packit ae235b
          g_thread_pool_free_internal (real);
Packit ae235b
          return;
Packit ae235b
        }
Packit ae235b
Packit ae235b
      g_thread_pool_wakeup_and_stop_all (real);
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* The last thread should cleanup the pool */
Packit ae235b
  real->waiting = FALSE;
Packit ae235b
  g_async_queue_unlock (real->queue);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_thread_pool_free_internal (GRealThreadPool* pool)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (pool);
Packit ae235b
  g_return_if_fail (pool->running == FALSE);
Packit ae235b
  g_return_if_fail (pool->num_threads == 0);
Packit ae235b
Packit ae235b
  g_async_queue_unref (pool->queue);
Packit ae235b
  g_cond_clear (&pool->cond);
Packit ae235b
Packit ae235b
  g_free (pool);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_thread_pool_wakeup_and_stop_all (GRealThreadPool *pool)
Packit ae235b
{
Packit ae235b
  guint i;
Packit ae235b
Packit ae235b
  g_return_if_fail (pool);
Packit ae235b
  g_return_if_fail (pool->running == FALSE);
Packit ae235b
  g_return_if_fail (pool->num_threads != 0);
Packit ae235b
Packit ae235b
  pool->immediate = TRUE;
Packit ae235b
Packit ae235b
  /*
Packit ae235b
   * So here we're sending bogus data to the pool threads, which
Packit ae235b
   * should cause them each to wake up, and check the above
Packit ae235b
   * pool->immediate condition. However we don't want that
Packit ae235b
   * data to be sorted (since it'll crash the sorter).
Packit ae235b
   */
Packit ae235b
  for (i = 0; i < pool->num_threads; i++)
Packit ae235b
    g_async_queue_push_unlocked (pool->queue, GUINT_TO_POINTER (1));
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_thread_pool_set_max_unused_threads:
Packit ae235b
 * @max_threads: maximal number of unused threads
Packit ae235b
 *
Packit ae235b
 * Sets the maximal number of unused threads to @max_threads.
Packit ae235b
 * If @max_threads is -1, no limit is imposed on the number
Packit ae235b
 * of unused threads.
Packit ae235b
 *
Packit ae235b
 * The default value is 2.
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_thread_pool_set_max_unused_threads (gint max_threads)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (max_threads >= -1);
Packit ae235b
Packit ae235b
  g_atomic_int_set (&max_unused_threads, max_threads);
Packit ae235b
Packit ae235b
  if (max_threads != -1)
Packit ae235b
    {
Packit ae235b
      max_threads -= g_atomic_int_get (&unused_threads);
Packit ae235b
      if (max_threads < 0)
Packit ae235b
        {
Packit ae235b
          g_atomic_int_set (&kill_unused_threads, -max_threads);
Packit ae235b
          g_atomic_int_inc (&wakeup_thread_serial);
Packit ae235b
Packit ae235b
          g_async_queue_lock (unused_thread_queue);
Packit ae235b
Packit ae235b
          do
Packit ae235b
            {
Packit ae235b
              g_async_queue_push_unlocked (unused_thread_queue,
Packit ae235b
                                           wakeup_thread_marker);
Packit ae235b
            }
Packit ae235b
          while (++max_threads);
Packit ae235b
Packit ae235b
          g_async_queue_unlock (unused_thread_queue);
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_thread_pool_get_max_unused_threads:
Packit ae235b
 *
Packit ae235b
 * Returns the maximal allowed number of unused threads.
Packit ae235b
 *
Packit ae235b
 * Returns: the maximal number of unused threads
Packit ae235b
 */
Packit ae235b
gint
Packit ae235b
g_thread_pool_get_max_unused_threads (void)
Packit ae235b
{
Packit ae235b
  return g_atomic_int_get (&max_unused_threads);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_thread_pool_get_num_unused_threads:
Packit ae235b
 *
Packit ae235b
 * Returns the number of currently unused threads.
Packit ae235b
 *
Packit ae235b
 * Returns: the number of currently unused threads
Packit ae235b
 */
Packit ae235b
guint
Packit ae235b
g_thread_pool_get_num_unused_threads (void)
Packit ae235b
{
Packit ae235b
  return g_atomic_int_get (&unused_threads);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_thread_pool_stop_unused_threads:
Packit ae235b
 *
Packit ae235b
 * Stops all currently unused threads. This does not change the
Packit ae235b
 * maximal number of unused threads. This function can be used to
Packit ae235b
 * regularly stop all unused threads e.g. from g_timeout_add().
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_thread_pool_stop_unused_threads (void)
Packit ae235b
{
Packit ae235b
  guint oldval;
Packit ae235b
Packit ae235b
  oldval = g_thread_pool_get_max_unused_threads ();
Packit ae235b
Packit ae235b
  g_thread_pool_set_max_unused_threads (0);
Packit ae235b
  g_thread_pool_set_max_unused_threads (oldval);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_thread_pool_set_sort_function:
Packit ae235b
 * @pool: a #GThreadPool
Packit ae235b
 * @func: the #GCompareDataFunc used to sort the list of tasks.
Packit ae235b
 *     This function is passed two tasks. It should return
Packit ae235b
 *     0 if the order in which they are handled does not matter,
Packit ae235b
 *     a negative value if the first task should be processed before
Packit ae235b
 *     the second or a positive value if the second task should be
Packit ae235b
 *     processed first.
Packit ae235b
 * @user_data: user data passed to @func
Packit ae235b
 *
Packit ae235b
 * Sets the function used to sort the list of tasks. This allows the
Packit ae235b
 * tasks to be processed by a priority determined by @func, and not
Packit ae235b
 * just in the order in which they were added to the pool.
Packit ae235b
 *
Packit ae235b
 * Note, if the maximum number of threads is more than 1, the order
Packit ae235b
 * that threads are executed cannot be guaranteed 100%. Threads are
Packit ae235b
 * scheduled by the operating system and are executed at random. It
Packit ae235b
 * cannot be assumed that threads are executed in the order they are
Packit ae235b
 * created.
Packit ae235b
 *
Packit ae235b
 * Since: 2.10
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_thread_pool_set_sort_function (GThreadPool      *pool,
Packit ae235b
                                 GCompareDataFunc  func,
Packit ae235b
                                 gpointer          user_data)
Packit ae235b
{
Packit ae235b
  GRealThreadPool *real;
Packit ae235b
Packit ae235b
  real = (GRealThreadPool*) pool;
Packit ae235b
Packit ae235b
  g_return_if_fail (real);
Packit ae235b
  g_return_if_fail (real->running);
Packit ae235b
Packit ae235b
  g_async_queue_lock (real->queue);
Packit ae235b
Packit ae235b
  real->sort_func = func;
Packit ae235b
  real->sort_user_data = user_data;
Packit ae235b
Packit ae235b
  if (func)
Packit ae235b
    g_async_queue_sort_unlocked (real->queue,
Packit ae235b
                                 real->sort_func,
Packit ae235b
                                 real->sort_user_data);
Packit ae235b
Packit ae235b
  g_async_queue_unlock (real->queue);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_thread_pool_move_to_front:
Packit ae235b
 * @pool: a #GThreadPool
Packit ae235b
 * @data: an unprocessed item in the pool
Packit ae235b
 *
Packit ae235b
 * Moves the item to the front of the queue of unprocessed
Packit ae235b
 * items, so that it will be processed next.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if the item was found and moved
Packit ae235b
 *
Packit ae235b
 * Since: 2.46
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_thread_pool_move_to_front (GThreadPool *pool,
Packit ae235b
                             gpointer     data)
Packit ae235b
{
Packit ae235b
  GRealThreadPool *real = (GRealThreadPool*) pool;
Packit ae235b
  gboolean found;
Packit ae235b
Packit ae235b
  g_async_queue_lock (real->queue);
Packit ae235b
Packit ae235b
  found = g_async_queue_remove_unlocked (real->queue, data);
Packit ae235b
  if (found)
Packit ae235b
    g_async_queue_push_front_unlocked (real->queue, data);
Packit ae235b
Packit ae235b
  g_async_queue_unlock (real->queue);
Packit ae235b
Packit ae235b
  return found;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_thread_pool_set_max_idle_time:
Packit ae235b
 * @interval: the maximum @interval (in milliseconds)
Packit ae235b
 *     a thread can be idle
Packit ae235b
 *
Packit ae235b
 * This function will set the maximum @interval that a thread
Packit ae235b
 * waiting in the pool for new tasks can be idle for before
Packit ae235b
 * being stopped. This function is similar to calling
Packit ae235b
 * g_thread_pool_stop_unused_threads() on a regular timeout,
Packit ae235b
 * except this is done on a per thread basis.
Packit ae235b
 *
Packit ae235b
 * By setting @interval to 0, idle threads will not be stopped.
Packit ae235b
 *
Packit ae235b
 * The default value is 15000 (15 seconds).
Packit ae235b
 *
Packit ae235b
 * Since: 2.10
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_thread_pool_set_max_idle_time (guint interval)
Packit ae235b
{
Packit ae235b
  guint i;
Packit ae235b
Packit ae235b
  g_atomic_int_set (&max_idle_time, interval);
Packit ae235b
Packit ae235b
  i = g_atomic_int_get (&unused_threads);
Packit ae235b
  if (i > 0)
Packit ae235b
    {
Packit ae235b
      g_atomic_int_inc (&wakeup_thread_serial);
Packit ae235b
      g_async_queue_lock (unused_thread_queue);
Packit ae235b
Packit ae235b
      do
Packit ae235b
        {
Packit ae235b
          g_async_queue_push_unlocked (unused_thread_queue,
Packit ae235b
                                       wakeup_thread_marker);
Packit ae235b
        }
Packit ae235b
      while (--i);
Packit ae235b
Packit ae235b
      g_async_queue_unlock (unused_thread_queue);
Packit ae235b
    }
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_thread_pool_get_max_idle_time:
Packit ae235b
 *
Packit ae235b
 * This function will return the maximum @interval that a
Packit ae235b
 * thread will wait in the thread pool for new tasks before
Packit ae235b
 * being stopped.
Packit ae235b
 *
Packit ae235b
 * If this function returns 0, threads waiting in the thread
Packit ae235b
 * pool for new work are not stopped.
Packit ae235b
 *
Packit ae235b
 * Returns: the maximum @interval (milliseconds) to wait
Packit ae235b
 *     for new tasks in the thread pool before stopping the
Packit ae235b
 *     thread
Packit ae235b
 *
Packit ae235b
 * Since: 2.10
Packit ae235b
 */
Packit ae235b
guint
Packit ae235b
g_thread_pool_get_max_idle_time (void)
Packit ae235b
{
Packit ae235b
  return g_atomic_int_get (&max_idle_time);
Packit ae235b
}