Blame glib/gthreadpool.c

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