2000-04-28 14:24:53 +02:00
|
|
|
|
/* GLIB - Library of useful routines for C programming
|
|
|
|
|
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
|
|
|
|
|
*
|
2011-10-05 17:45:51 +02:00
|
|
|
|
* GThreadPool: thread pool implementation.
|
2000-04-28 14:24:53 +02:00
|
|
|
|
* Copyright (C) 2000 Sebastian Wilhelmi; University of Karlsruhe
|
|
|
|
|
*
|
2022-05-18 10:15:38 +02:00
|
|
|
|
* SPDX-License-Identifier: LGPL-2.1-or-later
|
|
|
|
|
*
|
2000-04-28 14:24:53 +02:00
|
|
|
|
* This library is free software; you can redistribute it and/or
|
2000-07-26 13:02:02 +02:00
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
2000-04-28 14:24:53 +02:00
|
|
|
|
* License as published by the Free Software Foundation; either
|
2017-01-05 12:47:07 +01:00
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2000-04-28 14:24:53 +02:00
|
|
|
|
*
|
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2000-07-26 13:02:02 +02:00
|
|
|
|
* Lesser General Public License for more details.
|
2000-04-28 14:24:53 +02:00
|
|
|
|
*
|
2000-07-26 13:02:02 +02:00
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2014-01-23 12:58:29 +01:00
|
|
|
|
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
2000-04-28 14:24:53 +02:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* MT safe
|
|
|
|
|
*/
|
|
|
|
|
|
2002-12-04 02:27:44 +01:00
|
|
|
|
#include "config.h"
|
|
|
|
|
|
2010-09-04 05:03:14 +02:00
|
|
|
|
#include "gthreadpool.h"
|
|
|
|
|
|
|
|
|
|
#include "gasyncqueue.h"
|
2011-10-02 00:42:48 +02:00
|
|
|
|
#include "gasyncqueueprivate.h"
|
2010-09-04 05:03:14 +02:00
|
|
|
|
#include "gmain.h"
|
|
|
|
|
#include "gtestutils.h"
|
2019-12-24 14:33:30 +01:00
|
|
|
|
#include "gthreadprivate.h"
|
2010-09-04 05:03:14 +02:00
|
|
|
|
#include "gtimer.h"
|
2018-10-04 16:39:52 +02:00
|
|
|
|
#include "gutils.h"
|
2000-04-28 14:24:53 +02:00
|
|
|
|
|
2010-01-31 06:18:09 +01:00
|
|
|
|
/**
|
2011-02-01 19:17:23 +01:00
|
|
|
|
* SECTION:thread_pools
|
2010-01-31 06:18:09 +01:00
|
|
|
|
* @title: Thread Pools
|
|
|
|
|
* @short_description: pools of threads to execute work concurrently
|
2011-10-02 00:20:27 +02:00
|
|
|
|
* @see_also: #GThread
|
2010-01-31 06:18:09 +01:00
|
|
|
|
*
|
|
|
|
|
* Sometimes you wish to asynchronously fork out the execution of work
|
|
|
|
|
* and continue working in your own thread. If that will happen often,
|
|
|
|
|
* the overhead of starting and destroying a thread each time might be
|
|
|
|
|
* too high. In such cases reusing already started threads seems like a
|
|
|
|
|
* good idea. And it indeed is, but implementing this can be tedious
|
|
|
|
|
* and error-prone.
|
|
|
|
|
*
|
|
|
|
|
* Therefore GLib provides thread pools for your convenience. An added
|
|
|
|
|
* advantage is, that the threads can be shared between the different
|
|
|
|
|
* subsystems of your program, when they are using GLib.
|
|
|
|
|
*
|
2011-10-02 00:20:27 +02:00
|
|
|
|
* To create a new thread pool, you use g_thread_pool_new().
|
|
|
|
|
* It is destroyed by g_thread_pool_free().
|
2010-01-31 06:18:09 +01:00
|
|
|
|
*
|
2011-10-02 00:20:27 +02:00
|
|
|
|
* If you want to execute a certain task within a thread pool,
|
|
|
|
|
* you call g_thread_pool_push().
|
2010-01-31 06:18:09 +01:00
|
|
|
|
*
|
|
|
|
|
* To get the current number of running threads you call
|
|
|
|
|
* g_thread_pool_get_num_threads(). To get the number of still
|
|
|
|
|
* unprocessed tasks you call g_thread_pool_unprocessed(). To control
|
|
|
|
|
* the maximal number of threads for a thread pool, you use
|
|
|
|
|
* g_thread_pool_get_max_threads() and g_thread_pool_set_max_threads().
|
|
|
|
|
*
|
|
|
|
|
* Finally you can control the number of unused threads, that are kept
|
|
|
|
|
* alive by GLib for future use. The current number can be fetched with
|
|
|
|
|
* g_thread_pool_get_num_unused_threads(). The maximal number can be
|
|
|
|
|
* controlled by g_thread_pool_get_max_unused_threads() and
|
|
|
|
|
* g_thread_pool_set_max_unused_threads(). All currently unused threads
|
|
|
|
|
* can be stopped by calling g_thread_pool_stop_unused_threads().
|
2011-10-02 00:20:27 +02:00
|
|
|
|
*/
|
2010-01-31 06:18:09 +01:00
|
|
|
|
|
2011-10-02 00:20:27 +02:00
|
|
|
|
#define DEBUG_MSG(x)
|
2006-04-07 11:23:42 +02:00
|
|
|
|
/* #define DEBUG_MSG(args) g_printerr args ; g_printerr ("\n"); */
|
2002-12-04 02:27:44 +01:00
|
|
|
|
|
2000-04-28 14:24:53 +02:00
|
|
|
|
typedef struct _GRealThreadPool GRealThreadPool;
|
|
|
|
|
|
2010-01-31 06:18:09 +01:00
|
|
|
|
/**
|
|
|
|
|
* GThreadPool:
|
|
|
|
|
* @func: the function to execute in the threads of this pool
|
|
|
|
|
* @user_data: the user data for the threads of this pool
|
|
|
|
|
* @exclusive: are all threads exclusive to this pool
|
|
|
|
|
*
|
|
|
|
|
* The #GThreadPool struct represents a thread pool. It has three
|
2011-10-02 00:20:27 +02:00
|
|
|
|
* public read-only members, but the underlying struct is bigger,
|
|
|
|
|
* so you must not copy this struct.
|
|
|
|
|
*/
|
2000-04-28 14:24:53 +02:00
|
|
|
|
struct _GRealThreadPool
|
|
|
|
|
{
|
|
|
|
|
GThreadPool pool;
|
2011-10-02 00:42:48 +02:00
|
|
|
|
GAsyncQueue *queue;
|
2011-10-04 05:52:13 +02:00
|
|
|
|
GCond cond;
|
2000-04-28 14:24:53 +02:00
|
|
|
|
gint max_threads;
|
2019-02-04 10:34:13 +01:00
|
|
|
|
guint num_threads;
|
2000-04-28 14:24:53 +02:00
|
|
|
|
gboolean running;
|
|
|
|
|
gboolean immediate;
|
|
|
|
|
gboolean waiting;
|
2005-12-21 05:45:56 +01:00
|
|
|
|
GCompareDataFunc sort_func;
|
|
|
|
|
gpointer sort_user_data;
|
2000-04-28 14:24:53 +02:00
|
|
|
|
};
|
|
|
|
|
|
2006-01-17 21:06:27 +01:00
|
|
|
|
/* The following is just an address to mark the wakeup order for a
|
2001-04-03 14:42:54 +02:00
|
|
|
|
* thread, it could be any address (as long, as it isn't a valid
|
2011-10-02 00:20:27 +02:00
|
|
|
|
* GThreadPool address)
|
|
|
|
|
*/
|
2008-09-26 18:00:45 +02:00
|
|
|
|
static const gpointer wakeup_thread_marker = (gpointer) &g_thread_pool_new;
|
2006-01-17 21:06:27 +01:00
|
|
|
|
static gint wakeup_thread_serial = 0;
|
2000-04-28 14:24:53 +02:00
|
|
|
|
|
2001-05-18 10:44:57 +02:00
|
|
|
|
/* Here all unused threads are waiting */
|
2006-04-07 11:23:42 +02:00
|
|
|
|
static GAsyncQueue *unused_thread_queue = NULL;
|
2000-04-28 14:24:53 +02:00
|
|
|
|
static gint unused_threads = 0;
|
2012-07-25 20:41:04 +02:00
|
|
|
|
static gint max_unused_threads = 2;
|
2006-04-07 11:23:42 +02:00
|
|
|
|
static gint kill_unused_threads = 0;
|
2012-07-25 20:41:04 +02:00
|
|
|
|
static guint max_idle_time = 15 * 1000;
|
2000-04-28 14:24:53 +02:00
|
|
|
|
|
2019-12-24 14:33:30 +01:00
|
|
|
|
static GThreadSchedulerSettings shared_thread_scheduler_settings;
|
2020-01-18 12:23:30 +01:00
|
|
|
|
static gboolean have_shared_thread_scheduler_settings = FALSE;
|
|
|
|
|
|
2019-12-24 14:33:30 +01:00
|
|
|
|
typedef struct
|
|
|
|
|
{
|
|
|
|
|
/* Either thread or error are set in the end. Both transfer-full. */
|
|
|
|
|
GThreadPool *pool;
|
|
|
|
|
GThread *thread;
|
|
|
|
|
GError *error;
|
|
|
|
|
} SpawnThreadData;
|
|
|
|
|
|
|
|
|
|
static GCond spawn_thread_cond;
|
|
|
|
|
static GAsyncQueue *spawn_thread_queue;
|
|
|
|
|
|
2006-04-07 11:23:42 +02:00
|
|
|
|
static void g_thread_pool_queue_push_unlocked (GRealThreadPool *pool,
|
2011-10-02 00:20:27 +02:00
|
|
|
|
gpointer data);
|
2006-04-07 11:23:42 +02:00
|
|
|
|
static void g_thread_pool_free_internal (GRealThreadPool *pool);
|
|
|
|
|
static gpointer g_thread_pool_thread_proxy (gpointer data);
|
2011-10-02 00:20:27 +02:00
|
|
|
|
static gboolean g_thread_pool_start_thread (GRealThreadPool *pool,
|
|
|
|
|
GError **error);
|
2006-04-07 11:23:42 +02:00
|
|
|
|
static void g_thread_pool_wakeup_and_stop_all (GRealThreadPool *pool);
|
|
|
|
|
static GRealThreadPool* g_thread_pool_wait_for_new_pool (void);
|
|
|
|
|
static gpointer g_thread_pool_wait_for_new_task (GRealThreadPool *pool);
|
2000-04-28 14:24:53 +02:00
|
|
|
|
|
2005-12-21 05:45:56 +01:00
|
|
|
|
static void
|
|
|
|
|
g_thread_pool_queue_push_unlocked (GRealThreadPool *pool,
|
2011-10-02 00:20:27 +02:00
|
|
|
|
gpointer data)
|
2005-12-21 05:45:56 +01:00
|
|
|
|
{
|
2011-10-02 00:20:27 +02:00
|
|
|
|
if (pool->sort_func)
|
|
|
|
|
g_async_queue_push_sorted_unlocked (pool->queue,
|
|
|
|
|
data,
|
|
|
|
|
pool->sort_func,
|
|
|
|
|
pool->sort_user_data);
|
2005-12-21 05:45:56 +01:00
|
|
|
|
else
|
|
|
|
|
g_async_queue_push_unlocked (pool->queue, data);
|
|
|
|
|
}
|
|
|
|
|
|
2006-04-07 11:23:42 +02:00
|
|
|
|
static GRealThreadPool*
|
|
|
|
|
g_thread_pool_wait_for_new_pool (void)
|
2000-04-28 14:24:53 +02:00
|
|
|
|
{
|
2006-04-07 11:23:42 +02:00
|
|
|
|
GRealThreadPool *pool;
|
|
|
|
|
gint local_wakeup_thread_serial;
|
|
|
|
|
guint local_max_unused_threads;
|
|
|
|
|
gint local_max_idle_time;
|
|
|
|
|
gint last_wakeup_thread_serial;
|
|
|
|
|
gboolean have_relayed_thread_marker = FALSE;
|
2000-04-28 14:24:53 +02:00
|
|
|
|
|
2019-09-21 10:45:36 +02:00
|
|
|
|
local_max_unused_threads = (guint) g_atomic_int_get (&max_unused_threads);
|
2006-04-07 11:23:42 +02:00
|
|
|
|
local_max_idle_time = g_atomic_int_get (&max_idle_time);
|
|
|
|
|
last_wakeup_thread_serial = g_atomic_int_get (&wakeup_thread_serial);
|
2006-01-03 16:09:52 +01:00
|
|
|
|
|
2006-04-07 11:23:42 +02:00
|
|
|
|
g_atomic_int_inc (&unused_threads);
|
|
|
|
|
|
|
|
|
|
do
|
2000-04-28 14:24:53 +02:00
|
|
|
|
{
|
2019-02-04 10:34:13 +01:00
|
|
|
|
if ((guint) g_atomic_int_get (&unused_threads) >= local_max_unused_threads)
|
2011-10-02 00:20:27 +02:00
|
|
|
|
{
|
|
|
|
|
/* If this is a superfluous thread, stop it. */
|
|
|
|
|
pool = NULL;
|
|
|
|
|
}
|
2006-04-07 11:23:42 +02:00
|
|
|
|
else if (local_max_idle_time > 0)
|
2011-10-02 00:20:27 +02:00
|
|
|
|
{
|
|
|
|
|
/* If a maximal idle time is given, wait for the given time. */
|
|
|
|
|
DEBUG_MSG (("thread %p waiting in global pool for %f seconds.",
|
|
|
|
|
g_thread_self (), local_max_idle_time / 1000.0));
|
2006-01-03 16:09:52 +01:00
|
|
|
|
|
2012-02-08 14:26:36 +01:00
|
|
|
|
pool = g_async_queue_timeout_pop (unused_thread_queue,
|
|
|
|
|
local_max_idle_time * 1000);
|
2011-10-02 00:20:27 +02:00
|
|
|
|
}
|
2006-04-07 11:23:42 +02:00
|
|
|
|
else
|
2011-10-02 00:20:27 +02:00
|
|
|
|
{
|
|
|
|
|
/* If no maximal idle time is given, wait indefinitely. */
|
2011-10-02 00:42:48 +02:00
|
|
|
|
DEBUG_MSG (("thread %p waiting in global pool.", g_thread_self ()));
|
2011-10-02 00:20:27 +02:00
|
|
|
|
pool = g_async_queue_pop (unused_thread_queue);
|
|
|
|
|
}
|
2006-04-07 11:23:42 +02:00
|
|
|
|
|
|
|
|
|
if (pool == wakeup_thread_marker)
|
2011-10-02 00:20:27 +02:00
|
|
|
|
{
|
|
|
|
|
local_wakeup_thread_serial = g_atomic_int_get (&wakeup_thread_serial);
|
|
|
|
|
if (last_wakeup_thread_serial == local_wakeup_thread_serial)
|
|
|
|
|
{
|
|
|
|
|
if (!have_relayed_thread_marker)
|
|
|
|
|
{
|
|
|
|
|
/* If this wakeup marker has been received for
|
|
|
|
|
* the second time, relay it.
|
|
|
|
|
*/
|
|
|
|
|
DEBUG_MSG (("thread %p relaying wakeup message to "
|
|
|
|
|
"waiting thread with lower serial.",
|
|
|
|
|
g_thread_self ()));
|
|
|
|
|
|
|
|
|
|
g_async_queue_push (unused_thread_queue, wakeup_thread_marker);
|
|
|
|
|
have_relayed_thread_marker = TRUE;
|
|
|
|
|
|
|
|
|
|
/* If a wakeup marker has been relayed, this thread
|
|
|
|
|
* will get out of the way for 100 microseconds to
|
|
|
|
|
* avoid receiving this marker again.
|
|
|
|
|
*/
|
|
|
|
|
g_usleep (100);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (g_atomic_int_add (&kill_unused_threads, -1) > 0)
|
|
|
|
|
{
|
|
|
|
|
pool = NULL;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DEBUG_MSG (("thread %p updating to new limits.",
|
|
|
|
|
g_thread_self ()));
|
|
|
|
|
|
2019-09-21 10:45:36 +02:00
|
|
|
|
local_max_unused_threads = (guint) g_atomic_int_get (&max_unused_threads);
|
2011-10-02 00:20:27 +02:00
|
|
|
|
local_max_idle_time = g_atomic_int_get (&max_idle_time);
|
|
|
|
|
last_wakeup_thread_serial = local_wakeup_thread_serial;
|
|
|
|
|
|
|
|
|
|
have_relayed_thread_marker = FALSE;
|
|
|
|
|
}
|
|
|
|
|
}
|
2006-04-07 11:23:42 +02:00
|
|
|
|
}
|
|
|
|
|
while (pool == wakeup_thread_marker);
|
|
|
|
|
|
|
|
|
|
g_atomic_int_add (&unused_threads, -1);
|
|
|
|
|
|
|
|
|
|
return pool;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gpointer
|
|
|
|
|
g_thread_pool_wait_for_new_task (GRealThreadPool *pool)
|
|
|
|
|
{
|
|
|
|
|
gpointer task = NULL;
|
|
|
|
|
|
|
|
|
|
if (pool->running || (!pool->immediate &&
|
2011-10-02 00:20:27 +02:00
|
|
|
|
g_async_queue_length_unlocked (pool->queue) > 0))
|
2006-04-07 11:23:42 +02:00
|
|
|
|
{
|
|
|
|
|
/* This thread pool is still active. */
|
2019-02-04 10:34:13 +01:00
|
|
|
|
if (pool->max_threads != -1 && pool->num_threads > (guint) pool->max_threads)
|
2011-10-02 00:20:27 +02:00
|
|
|
|
{
|
|
|
|
|
/* This is a superfluous thread, so it goes to the global pool. */
|
|
|
|
|
DEBUG_MSG (("superfluous thread %p in pool %p.",
|
|
|
|
|
g_thread_self (), pool));
|
|
|
|
|
}
|
2006-04-07 11:23:42 +02:00
|
|
|
|
else if (pool->pool.exclusive)
|
2011-10-02 00:20:27 +02:00
|
|
|
|
{
|
|
|
|
|
/* Exclusive threads stay attached to the pool. */
|
|
|
|
|
task = g_async_queue_pop_unlocked (pool->queue);
|
|
|
|
|
|
|
|
|
|
DEBUG_MSG (("thread %p in exclusive pool %p waits for task "
|
|
|
|
|
"(%d running, %d unprocessed).",
|
|
|
|
|
g_thread_self (), pool, pool->num_threads,
|
|
|
|
|
g_async_queue_length_unlocked (pool->queue)));
|
|
|
|
|
}
|
2006-01-17 21:06:27 +01:00
|
|
|
|
else
|
2011-10-02 00:20:27 +02:00
|
|
|
|
{
|
|
|
|
|
/* A thread will wait for new tasks for at most 1/2
|
|
|
|
|
* second before going to the global pool.
|
|
|
|
|
*/
|
|
|
|
|
DEBUG_MSG (("thread %p in pool %p waits for up to a 1/2 second for task "
|
|
|
|
|
"(%d running, %d unprocessed).",
|
|
|
|
|
g_thread_self (), pool, pool->num_threads,
|
|
|
|
|
g_async_queue_length_unlocked (pool->queue)));
|
|
|
|
|
|
2012-02-08 14:26:36 +01:00
|
|
|
|
task = g_async_queue_timeout_pop_unlocked (pool->queue,
|
|
|
|
|
G_USEC_PER_SEC / 2);
|
2011-10-02 00:20:27 +02:00
|
|
|
|
}
|
2006-04-07 11:23:42 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/* This thread pool is inactive, it will no longer process tasks. */
|
|
|
|
|
DEBUG_MSG (("pool %p not active, thread %p will go to global pool "
|
2011-10-02 00:20:27 +02:00
|
|
|
|
"(running: %s, immediate: %s, len: %d).",
|
|
|
|
|
pool, g_thread_self (),
|
|
|
|
|
pool->running ? "true" : "false",
|
|
|
|
|
pool->immediate ? "true" : "false",
|
|
|
|
|
g_async_queue_length_unlocked (pool->queue)));
|
2006-04-07 11:23:42 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return task;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-24 14:33:30 +01:00
|
|
|
|
static gpointer
|
|
|
|
|
g_thread_pool_spawn_thread (gpointer data)
|
|
|
|
|
{
|
|
|
|
|
while (TRUE)
|
|
|
|
|
{
|
|
|
|
|
SpawnThreadData *spawn_thread_data;
|
|
|
|
|
GThread *thread = NULL;
|
|
|
|
|
GError *error = NULL;
|
|
|
|
|
const gchar *prgname = g_get_prgname ();
|
|
|
|
|
gchar name[16] = "pool";
|
|
|
|
|
|
|
|
|
|
if (prgname)
|
|
|
|
|
g_snprintf (name, sizeof (name), "pool-%s", prgname);
|
|
|
|
|
|
|
|
|
|
g_async_queue_lock (spawn_thread_queue);
|
|
|
|
|
/* Spawn a new thread for the given pool and wake the requesting thread
|
|
|
|
|
* up again with the result. This new thread will have the scheduler
|
|
|
|
|
* settings inherited from this thread and in extension of the thread
|
|
|
|
|
* that created the first non-exclusive thread-pool. */
|
|
|
|
|
spawn_thread_data = g_async_queue_pop_unlocked (spawn_thread_queue);
|
|
|
|
|
thread = g_thread_try_new (name, g_thread_pool_thread_proxy, spawn_thread_data->pool, &error);
|
|
|
|
|
|
|
|
|
|
spawn_thread_data->thread = g_steal_pointer (&thread);
|
|
|
|
|
spawn_thread_data->error = g_steal_pointer (&error);
|
|
|
|
|
|
|
|
|
|
g_cond_broadcast (&spawn_thread_cond);
|
|
|
|
|
g_async_queue_unlock (spawn_thread_queue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2006-04-07 11:23:42 +02:00
|
|
|
|
|
2011-10-02 00:20:27 +02:00
|
|
|
|
static gpointer
|
2006-04-07 11:23:42 +02:00
|
|
|
|
g_thread_pool_thread_proxy (gpointer data)
|
|
|
|
|
{
|
|
|
|
|
GRealThreadPool *pool;
|
|
|
|
|
|
|
|
|
|
pool = data;
|
|
|
|
|
|
2011-10-02 00:20:27 +02:00
|
|
|
|
DEBUG_MSG (("thread %p started for pool %p.", g_thread_self (), pool));
|
2006-04-07 11:23:42 +02:00
|
|
|
|
|
|
|
|
|
g_async_queue_lock (pool->queue);
|
|
|
|
|
|
|
|
|
|
while (TRUE)
|
|
|
|
|
{
|
|
|
|
|
gpointer task;
|
|
|
|
|
|
|
|
|
|
task = g_thread_pool_wait_for_new_task (pool);
|
2006-01-17 21:06:27 +01:00
|
|
|
|
if (task)
|
2011-10-02 00:20:27 +02:00
|
|
|
|
{
|
|
|
|
|
if (pool->running || !pool->immediate)
|
|
|
|
|
{
|
|
|
|
|
/* A task was received and the thread pool is active,
|
|
|
|
|
* so execute the function.
|
|
|
|
|
*/
|
|
|
|
|
g_async_queue_unlock (pool->queue);
|
|
|
|
|
DEBUG_MSG (("thread %p in pool %p calling func.",
|
|
|
|
|
g_thread_self (), pool));
|
|
|
|
|
pool->pool.func (task, pool->pool.user_data);
|
|
|
|
|
g_async_queue_lock (pool->queue);
|
|
|
|
|
}
|
|
|
|
|
}
|
2006-01-17 21:06:27 +01:00
|
|
|
|
else
|
2011-10-02 00:20:27 +02:00
|
|
|
|
{
|
|
|
|
|
/* No task was received, so this thread goes to the global pool. */
|
|
|
|
|
gboolean free_pool = FALSE;
|
|
|
|
|
|
|
|
|
|
DEBUG_MSG (("thread %p leaving pool %p for global pool.",
|
|
|
|
|
g_thread_self (), pool));
|
|
|
|
|
pool->num_threads--;
|
|
|
|
|
|
|
|
|
|
if (!pool->running)
|
|
|
|
|
{
|
|
|
|
|
if (!pool->waiting)
|
|
|
|
|
{
|
|
|
|
|
if (pool->num_threads == 0)
|
|
|
|
|
{
|
|
|
|
|
/* If the pool is not running and no other
|
|
|
|
|
* thread is waiting for this thread pool to
|
|
|
|
|
* finish and this is the last thread of this
|
|
|
|
|
* pool, free the pool.
|
|
|
|
|
*/
|
|
|
|
|
free_pool = TRUE;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/* If the pool is not running and no other
|
|
|
|
|
* thread is waiting for this thread pool to
|
|
|
|
|
* finish and this is not the last thread of
|
|
|
|
|
* this pool and there are no tasks left in the
|
|
|
|
|
* queue, wakeup the remaining threads.
|
|
|
|
|
*/
|
|
|
|
|
if (g_async_queue_length_unlocked (pool->queue) ==
|
2019-02-04 10:34:13 +01:00
|
|
|
|
(gint) -pool->num_threads)
|
2011-10-02 00:20:27 +02:00
|
|
|
|
g_thread_pool_wakeup_and_stop_all (pool);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (pool->immediate ||
|
|
|
|
|
g_async_queue_length_unlocked (pool->queue) <= 0)
|
|
|
|
|
{
|
|
|
|
|
/* If the pool is not running and another thread is
|
|
|
|
|
* waiting for this thread pool to finish and there
|
|
|
|
|
* are either no tasks left or the pool shall stop
|
|
|
|
|
* immediately, inform the waiting thread of a change
|
|
|
|
|
* of the thread pool state.
|
|
|
|
|
*/
|
2011-10-04 05:52:13 +02:00
|
|
|
|
g_cond_broadcast (&pool->cond);
|
2011-10-02 00:20:27 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g_async_queue_unlock (pool->queue);
|
|
|
|
|
|
|
|
|
|
if (free_pool)
|
|
|
|
|
g_thread_pool_free_internal (pool);
|
|
|
|
|
|
|
|
|
|
if ((pool = g_thread_pool_wait_for_new_pool ()) == NULL)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
g_async_queue_lock (pool->queue);
|
|
|
|
|
|
|
|
|
|
DEBUG_MSG (("thread %p entering pool %p from global pool.",
|
|
|
|
|
g_thread_self (), pool));
|
|
|
|
|
|
|
|
|
|
/* pool->num_threads++ is not done here, but in
|
|
|
|
|
* g_thread_pool_start_thread to make the new started
|
|
|
|
|
* thread known to the pool before itself can do it.
|
|
|
|
|
*/
|
|
|
|
|
}
|
2000-04-28 14:24:53 +02:00
|
|
|
|
}
|
2006-04-07 11:23:42 +02:00
|
|
|
|
|
2001-05-08 10:23:18 +02:00
|
|
|
|
return NULL;
|
2000-04-28 14:24:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
2011-10-02 00:20:27 +02:00
|
|
|
|
static gboolean
|
|
|
|
|
g_thread_pool_start_thread (GRealThreadPool *pool,
|
|
|
|
|
GError **error)
|
2000-04-28 14:24:53 +02:00
|
|
|
|
{
|
|
|
|
|
gboolean success = FALSE;
|
2011-10-02 00:20:27 +02:00
|
|
|
|
|
2019-02-04 10:34:13 +01:00
|
|
|
|
if (pool->max_threads != -1 && pool->num_threads >= (guint) pool->max_threads)
|
2000-04-28 14:24:53 +02:00
|
|
|
|
/* Enough threads are already running */
|
2011-10-02 00:20:27 +02:00
|
|
|
|
return TRUE;
|
2000-04-28 14:24:53 +02:00
|
|
|
|
|
2001-05-18 10:44:57 +02:00
|
|
|
|
g_async_queue_lock (unused_thread_queue);
|
2000-04-28 14:24:53 +02:00
|
|
|
|
|
2001-05-18 10:44:57 +02:00
|
|
|
|
if (g_async_queue_length_unlocked (unused_thread_queue) < 0)
|
2000-04-28 14:24:53 +02:00
|
|
|
|
{
|
2001-05-18 10:44:57 +02:00
|
|
|
|
g_async_queue_push_unlocked (unused_thread_queue, pool);
|
2000-04-28 14:24:53 +02:00
|
|
|
|
success = TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
2001-05-18 10:44:57 +02:00
|
|
|
|
g_async_queue_unlock (unused_thread_queue);
|
2000-04-28 14:24:53 +02:00
|
|
|
|
|
|
|
|
|
if (!success)
|
2000-09-01 15:03:23 +02:00
|
|
|
|
{
|
2018-10-04 16:39:52 +02:00
|
|
|
|
const gchar *prgname = g_get_prgname ();
|
|
|
|
|
gchar name[16] = "pool";
|
2011-10-13 06:43:33 +02:00
|
|
|
|
GThread *thread;
|
2011-10-02 00:20:27 +02:00
|
|
|
|
|
2018-10-04 16:39:52 +02:00
|
|
|
|
if (prgname)
|
|
|
|
|
g_snprintf (name, sizeof (name), "pool-%s", prgname);
|
|
|
|
|
|
2001-04-03 14:42:54 +02:00
|
|
|
|
/* No thread was found, we have to start a new one */
|
2019-12-24 14:33:30 +01:00
|
|
|
|
if (pool->pool.exclusive)
|
|
|
|
|
{
|
|
|
|
|
/* For exclusive thread-pools this is directly called from new() and
|
|
|
|
|
* we simply start new threads that inherit the scheduler settings
|
|
|
|
|
* from the current thread.
|
|
|
|
|
*/
|
|
|
|
|
thread = g_thread_try_new (name, g_thread_pool_thread_proxy, pool, error);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/* For non-exclusive thread-pools this can be called at any time
|
|
|
|
|
* when a new thread is needed. We make sure to create a new thread
|
|
|
|
|
* here with the correct scheduler settings: either by directly
|
|
|
|
|
* providing them if supported by the GThread implementation or by
|
|
|
|
|
* going via our helper thread.
|
|
|
|
|
*/
|
2020-01-18 12:23:30 +01:00
|
|
|
|
if (have_shared_thread_scheduler_settings)
|
|
|
|
|
{
|
|
|
|
|
thread = g_thread_new_internal (name, g_thread_proxy, g_thread_pool_thread_proxy, pool, 0, &shared_thread_scheduler_settings, error);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
SpawnThreadData spawn_thread_data = { (GThreadPool *) pool, NULL, NULL };
|
2019-12-24 14:33:30 +01:00
|
|
|
|
|
2020-01-18 12:23:30 +01:00
|
|
|
|
g_async_queue_lock (spawn_thread_queue);
|
2019-12-24 14:33:30 +01:00
|
|
|
|
|
2020-01-18 12:23:30 +01:00
|
|
|
|
g_async_queue_push_unlocked (spawn_thread_queue, &spawn_thread_data);
|
2019-12-24 14:33:30 +01:00
|
|
|
|
|
2020-01-18 12:23:30 +01:00
|
|
|
|
while (!spawn_thread_data.thread && !spawn_thread_data.error)
|
|
|
|
|
g_cond_wait (&spawn_thread_cond, _g_async_queue_get_mutex (spawn_thread_queue));
|
2019-12-24 14:33:30 +01:00
|
|
|
|
|
2020-01-18 12:23:30 +01:00
|
|
|
|
thread = spawn_thread_data.thread;
|
|
|
|
|
if (!thread)
|
|
|
|
|
g_propagate_error (error, g_steal_pointer (&spawn_thread_data.error));
|
|
|
|
|
g_async_queue_unlock (spawn_thread_queue);
|
|
|
|
|
}
|
2019-12-24 14:33:30 +01:00
|
|
|
|
}
|
2011-10-13 06:43:33 +02:00
|
|
|
|
|
|
|
|
|
if (thread == NULL)
|
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
|
|
g_thread_unref (thread);
|
2000-09-01 15:03:23 +02:00
|
|
|
|
}
|
2000-04-28 14:24:53 +02:00
|
|
|
|
|
|
|
|
|
/* See comment in g_thread_pool_thread_proxy as to why this is done
|
2006-04-07 11:23:42 +02:00
|
|
|
|
* here and not there
|
|
|
|
|
*/
|
2000-04-28 14:24:53 +02:00
|
|
|
|
pool->num_threads++;
|
2011-10-02 00:20:27 +02:00
|
|
|
|
|
|
|
|
|
return TRUE;
|
2000-04-28 14:24:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
2001-04-03 14:42:54 +02:00
|
|
|
|
/**
|
2011-10-02 00:20:27 +02:00
|
|
|
|
* g_thread_pool_new:
|
2001-05-09 14:51:21 +02:00
|
|
|
|
* @func: a function to execute in the threads of the new thread pool
|
2011-10-02 00:20:27 +02:00
|
|
|
|
* @user_data: user data that is handed over to @func every time it
|
|
|
|
|
* is called
|
|
|
|
|
* @max_threads: the maximal number of threads to execute concurrently
|
|
|
|
|
* in the new thread pool, -1 means no limit
|
2001-04-03 14:42:54 +02:00
|
|
|
|
* @exclusive: should this thread pool be exclusive?
|
2011-10-02 00:20:27 +02:00
|
|
|
|
* @error: return location for error, or %NULL
|
2001-04-03 14:42:54 +02:00
|
|
|
|
*
|
2001-05-18 10:44:57 +02:00
|
|
|
|
* This function creates a new thread pool.
|
2001-04-03 14:42:54 +02:00
|
|
|
|
*
|
|
|
|
|
* Whenever you call g_thread_pool_push(), either a new thread is
|
|
|
|
|
* created or an unused one is reused. At most @max_threads threads
|
|
|
|
|
* are running concurrently for this thread pool. @max_threads = -1
|
|
|
|
|
* allows unlimited threads to be created for this thread pool. The
|
2011-10-02 00:20:27 +02:00
|
|
|
|
* newly created or reused thread now executes the function @func
|
|
|
|
|
* with the two arguments. The first one is the parameter to
|
2001-05-09 14:51:21 +02:00
|
|
|
|
* g_thread_pool_push() and the second one is @user_data.
|
2001-04-03 14:42:54 +02:00
|
|
|
|
*
|
2020-07-27 03:17:37 +02:00
|
|
|
|
* Pass g_get_num_processors() to @max_threads to create as many threads as
|
|
|
|
|
* there are logical processors on the system. This will not pin each thread to
|
|
|
|
|
* a specific processor.
|
|
|
|
|
*
|
2011-10-02 00:20:27 +02:00
|
|
|
|
* The parameter @exclusive determines whether the thread pool owns
|
|
|
|
|
* all threads exclusive or shares them with other thread pools.
|
|
|
|
|
* If @exclusive is %TRUE, @max_threads threads are started
|
|
|
|
|
* immediately and they will run exclusively for this thread pool
|
|
|
|
|
* until it is destroyed by g_thread_pool_free(). If @exclusive is
|
|
|
|
|
* %FALSE, threads are created when needed and shared between all
|
|
|
|
|
* non-exclusive thread pools. This implies that @max_threads may
|
2014-09-03 08:12:01 +02:00
|
|
|
|
* not be -1 for exclusive thread pools. Besides, exclusive thread
|
|
|
|
|
* pools are not affected by g_thread_pool_set_max_idle_time()
|
|
|
|
|
* since their threads are never considered idle and returned to the
|
|
|
|
|
* global pool.
|
2001-04-03 14:42:54 +02:00
|
|
|
|
*
|
Documentation fixes.
* glib/gconvert.c, glib/grand.c, glib/ghash.c,
glib/gthreadpool.c, glib/gtree.c: Documentation fixes.
* glib/tmpl/allocators.sgml, glib/tmpl/arrays.sgml,
glib/tmpl/arrays_byte.sgml, glib/tmpl/arrays_pointer.sgml,
glib/tmpl/caches.sgml, glib/tmpl/completion.sgml,
glib/tmpl/conversions.sgml,
glib/tmpl/datalist.sgml, glib/tmpl/date.sgml,
glib/tmpl/error_reporting.sgml, glib/tmpl/fileutils.sgml,
glib/tmpl/hash_tables.sgml,
glib/tmpl/hooks.sgml, glib/tmpl/macros.sgml,
glib/tmpl/macros_misc.sgml, glib/tmpl/main.sgml, glib/tmpl/markup.sgml,
glib/tmpl/memory.sgml, glib/tmpl/memory_chunks.sgml,
glib/tmpl/messages.sgml, glib/tmpl/misc_utils.sgml,
glib/tmpl/modules.sgml, glib/tmpl/numerical.sgml,
glib/tmpl/patterns.sgml, glib/tmpl/queue.sgml,
glib/tmpl/shell.sgml, glib/tmpl/spawn.sgml,
glib/tmpl/string_utils.sgml, glib/tmpl/thread_pools.sgml,
glib/tmpl/threads.sgml, glib/tmpl/timers.sgml,
glib/tmpl/trees-binary.sgml, glib/tmpl/trees-nary.sgml,
glib/tmpl/type_conversion.sgml, glib/tmpl/unicode.sgml,
glib/tmpl/warnings.sgml, glib/tmpl/windows.sgml:
Improve markup of examples, general consistency improvements.
2001-12-12 21:32:07 +01:00
|
|
|
|
* @error can be %NULL to ignore errors, or non-%NULL to report
|
2011-10-02 00:20:27 +02:00
|
|
|
|
* errors. An error can only occur when @exclusive is set to %TRUE
|
|
|
|
|
* and not all @max_threads threads could be created.
|
2014-10-05 19:11:08 +02:00
|
|
|
|
* See #GThreadError for possible errors that may occur.
|
2014-09-03 08:12:01 +02:00
|
|
|
|
* Note, even in case of error a valid #GThreadPool is returned.
|
2001-04-03 14:42:54 +02:00
|
|
|
|
*
|
2014-02-20 01:35:23 +01:00
|
|
|
|
* Returns: the new #GThreadPool
|
2011-10-02 00:20:27 +02:00
|
|
|
|
*/
|
|
|
|
|
GThreadPool *
|
|
|
|
|
g_thread_pool_new (GFunc func,
|
|
|
|
|
gpointer user_data,
|
|
|
|
|
gint max_threads,
|
|
|
|
|
gboolean exclusive,
|
|
|
|
|
GError **error)
|
2021-06-26 12:38:42 +02:00
|
|
|
|
{
|
|
|
|
|
return g_thread_pool_new_full (func, user_data, NULL, max_threads, exclusive, error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_thread_pool_new_full:
|
|
|
|
|
* @func: a function to execute in the threads of the new thread pool
|
|
|
|
|
* @user_data: user data that is handed over to @func every time it
|
|
|
|
|
* is called
|
|
|
|
|
* @item_free_func: (nullable): used to pass as a free function to
|
|
|
|
|
* g_async_queue_new_full()
|
|
|
|
|
* @max_threads: the maximal number of threads to execute concurrently
|
|
|
|
|
* in the new thread pool, `-1` means no limit
|
|
|
|
|
* @exclusive: should this thread pool be exclusive?
|
|
|
|
|
* @error: return location for error, or %NULL
|
|
|
|
|
*
|
|
|
|
|
* This function creates a new thread pool similar to g_thread_pool_new()
|
|
|
|
|
* but allowing @item_free_func to be specified to free the data passed
|
|
|
|
|
* to g_thread_pool_push() in the case that the #GThreadPool is stopped
|
|
|
|
|
* and freed before all tasks have been executed.
|
|
|
|
|
*
|
|
|
|
|
* Returns: (transfer full): the new #GThreadPool
|
|
|
|
|
*
|
|
|
|
|
* Since: 2.70
|
|
|
|
|
*/
|
|
|
|
|
GThreadPool *
|
|
|
|
|
g_thread_pool_new_full (GFunc func,
|
|
|
|
|
gpointer user_data,
|
|
|
|
|
GDestroyNotify item_free_func,
|
|
|
|
|
gint max_threads,
|
|
|
|
|
gboolean exclusive,
|
|
|
|
|
GError **error)
|
2000-04-28 14:24:53 +02:00
|
|
|
|
{
|
|
|
|
|
GRealThreadPool *retval;
|
2001-04-03 14:42:54 +02:00
|
|
|
|
G_LOCK_DEFINE_STATIC (init);
|
2000-04-28 14:24:53 +02:00
|
|
|
|
|
2001-05-09 14:51:21 +02:00
|
|
|
|
g_return_val_if_fail (func, NULL);
|
2000-04-28 14:24:53 +02:00
|
|
|
|
g_return_val_if_fail (!exclusive || max_threads != -1, NULL);
|
|
|
|
|
g_return_val_if_fail (max_threads >= -1, NULL);
|
|
|
|
|
|
|
|
|
|
retval = g_new (GRealThreadPool, 1);
|
|
|
|
|
|
2001-05-09 14:51:21 +02:00
|
|
|
|
retval->pool.func = func;
|
|
|
|
|
retval->pool.user_data = user_data;
|
2000-04-28 14:24:53 +02:00
|
|
|
|
retval->pool.exclusive = exclusive;
|
2021-06-26 12:38:42 +02:00
|
|
|
|
retval->queue = g_async_queue_new_full (item_free_func);
|
2011-10-04 05:52:13 +02:00
|
|
|
|
g_cond_init (&retval->cond);
|
2000-04-28 14:24:53 +02:00
|
|
|
|
retval->max_threads = max_threads;
|
|
|
|
|
retval->num_threads = 0;
|
|
|
|
|
retval->running = TRUE;
|
2011-10-02 00:42:48 +02:00
|
|
|
|
retval->immediate = FALSE;
|
|
|
|
|
retval->waiting = FALSE;
|
2005-12-21 05:45:56 +01:00
|
|
|
|
retval->sort_func = NULL;
|
|
|
|
|
retval->sort_user_data = NULL;
|
2000-04-28 14:24:53 +02:00
|
|
|
|
|
2001-04-03 14:42:54 +02:00
|
|
|
|
G_LOCK (init);
|
2006-04-07 11:23:42 +02:00
|
|
|
|
if (!unused_thread_queue)
|
2001-05-18 10:44:57 +02:00
|
|
|
|
unused_thread_queue = g_async_queue_new ();
|
2020-01-25 10:08:22 +01:00
|
|
|
|
|
|
|
|
|
/* For the very first non-exclusive thread-pool we remember the thread
|
|
|
|
|
* scheduler settings of the thread creating the pool, if supported by
|
|
|
|
|
* the GThread implementation. This is then used for making sure that
|
|
|
|
|
* all threads created on the non-exclusive thread-pool have the same
|
|
|
|
|
* scheduler settings, and more importantly don't just inherit them
|
|
|
|
|
* from the thread that just happened to push a new task and caused
|
|
|
|
|
* a new thread to be created.
|
|
|
|
|
*
|
|
|
|
|
* Not doing so could cause real-time priority threads or otherwise
|
|
|
|
|
* threads with problematic scheduler settings to be part of the
|
|
|
|
|
* non-exclusive thread-pools.
|
|
|
|
|
*
|
|
|
|
|
* If this is not supported by the GThread implementation then we here
|
|
|
|
|
* start a thread that will inherit the scheduler settings from this
|
|
|
|
|
* very thread and whose only purpose is to spawn new threads with the
|
|
|
|
|
* same settings for use by the non-exclusive thread-pools.
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* For non-exclusive thread-pools this is not required as all threads
|
|
|
|
|
* are created immediately below and are running forever, so they will
|
|
|
|
|
* automatically inherit the scheduler settings from this very thread.
|
|
|
|
|
*/
|
|
|
|
|
if (!exclusive && !have_shared_thread_scheduler_settings && !spawn_thread_queue)
|
|
|
|
|
{
|
|
|
|
|
if (g_thread_get_scheduler_settings (&shared_thread_scheduler_settings))
|
2019-12-24 14:33:30 +01:00
|
|
|
|
{
|
2020-01-25 10:08:22 +01:00
|
|
|
|
have_shared_thread_scheduler_settings = TRUE;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
spawn_thread_queue = g_async_queue_new ();
|
|
|
|
|
g_cond_init (&spawn_thread_cond);
|
|
|
|
|
g_thread_new ("pool-spawner", g_thread_pool_spawn_thread, NULL);
|
2019-12-24 14:33:30 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2001-04-03 14:42:54 +02:00
|
|
|
|
G_UNLOCK (init);
|
|
|
|
|
|
2000-04-28 14:24:53 +02:00
|
|
|
|
if (retval->pool.exclusive)
|
|
|
|
|
{
|
|
|
|
|
g_async_queue_lock (retval->queue);
|
2011-10-02 00:20:27 +02:00
|
|
|
|
|
2019-02-04 10:34:13 +01:00
|
|
|
|
while (retval->num_threads < (guint) retval->max_threads)
|
2011-10-02 00:20:27 +02:00
|
|
|
|
{
|
|
|
|
|
GError *local_error = NULL;
|
|
|
|
|
|
|
|
|
|
if (!g_thread_pool_start_thread (retval, &local_error))
|
|
|
|
|
{
|
|
|
|
|
g_propagate_error (error, local_error);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2000-04-28 14:24:53 +02:00
|
|
|
|
|
|
|
|
|
g_async_queue_unlock (retval->queue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (GThreadPool*) retval;
|
|
|
|
|
}
|
|
|
|
|
|
2001-04-03 14:42:54 +02:00
|
|
|
|
/**
|
|
|
|
|
* g_thread_pool_push:
|
|
|
|
|
* @pool: a #GThreadPool
|
|
|
|
|
* @data: a new task for @pool
|
2011-10-02 00:20:27 +02:00
|
|
|
|
* @error: return location for error, or %NULL
|
|
|
|
|
*
|
2011-10-02 00:42:48 +02:00
|
|
|
|
* Inserts @data into the list of tasks to be executed by @pool.
|
|
|
|
|
*
|
|
|
|
|
* When the number of currently running threads is lower than the
|
|
|
|
|
* maximal allowed number of threads, a new thread is started (or
|
|
|
|
|
* reused) with the properties given to g_thread_pool_new().
|
|
|
|
|
* Otherwise, @data stays in the queue until a thread in this pool
|
|
|
|
|
* finishes its previous task and processes @data.
|
2001-04-03 14:42:54 +02:00
|
|
|
|
*
|
Documentation fixes.
* glib/gconvert.c, glib/grand.c, glib/ghash.c,
glib/gthreadpool.c, glib/gtree.c: Documentation fixes.
* glib/tmpl/allocators.sgml, glib/tmpl/arrays.sgml,
glib/tmpl/arrays_byte.sgml, glib/tmpl/arrays_pointer.sgml,
glib/tmpl/caches.sgml, glib/tmpl/completion.sgml,
glib/tmpl/conversions.sgml,
glib/tmpl/datalist.sgml, glib/tmpl/date.sgml,
glib/tmpl/error_reporting.sgml, glib/tmpl/fileutils.sgml,
glib/tmpl/hash_tables.sgml,
glib/tmpl/hooks.sgml, glib/tmpl/macros.sgml,
glib/tmpl/macros_misc.sgml, glib/tmpl/main.sgml, glib/tmpl/markup.sgml,
glib/tmpl/memory.sgml, glib/tmpl/memory_chunks.sgml,
glib/tmpl/messages.sgml, glib/tmpl/misc_utils.sgml,
glib/tmpl/modules.sgml, glib/tmpl/numerical.sgml,
glib/tmpl/patterns.sgml, glib/tmpl/queue.sgml,
glib/tmpl/shell.sgml, glib/tmpl/spawn.sgml,
glib/tmpl/string_utils.sgml, glib/tmpl/thread_pools.sgml,
glib/tmpl/threads.sgml, glib/tmpl/timers.sgml,
glib/tmpl/trees-binary.sgml, glib/tmpl/trees-nary.sgml,
glib/tmpl/type_conversion.sgml, glib/tmpl/unicode.sgml,
glib/tmpl/warnings.sgml, glib/tmpl/windows.sgml:
Improve markup of examples, general consistency improvements.
2001-12-12 21:32:07 +01:00
|
|
|
|
* @error can be %NULL to ignore errors, or non-%NULL to report
|
|
|
|
|
* errors. An error can only occur when a new thread couldn't be
|
2011-10-02 00:20:27 +02:00
|
|
|
|
* created. In that case @data is simply appended to the queue of
|
|
|
|
|
* work to do.
|
|
|
|
|
*
|
|
|
|
|
* Before version 2.32, this function did not return a success status.
|
|
|
|
|
*
|
2014-02-20 01:35:23 +01:00
|
|
|
|
* Returns: %TRUE on success, %FALSE if an error occurred
|
2011-10-02 00:20:27 +02:00
|
|
|
|
*/
|
|
|
|
|
gboolean
|
2006-04-07 11:23:42 +02:00
|
|
|
|
g_thread_pool_push (GThreadPool *pool,
|
2011-10-02 00:20:27 +02:00
|
|
|
|
gpointer data,
|
|
|
|
|
GError **error)
|
2000-04-28 14:24:53 +02:00
|
|
|
|
{
|
2006-04-07 11:23:42 +02:00
|
|
|
|
GRealThreadPool *real;
|
2011-10-02 00:20:27 +02:00
|
|
|
|
gboolean result;
|
2006-04-07 11:23:42 +02:00
|
|
|
|
|
|
|
|
|
real = (GRealThreadPool*) pool;
|
2000-04-28 14:24:53 +02:00
|
|
|
|
|
2011-10-02 00:20:27 +02:00
|
|
|
|
g_return_val_if_fail (real, FALSE);
|
|
|
|
|
g_return_val_if_fail (real->running, FALSE);
|
|
|
|
|
|
|
|
|
|
result = TRUE;
|
2000-04-28 14:24:53 +02:00
|
|
|
|
|
|
|
|
|
g_async_queue_lock (real->queue);
|
|
|
|
|
|
2001-04-03 14:42:54 +02:00
|
|
|
|
if (g_async_queue_length_unlocked (real->queue) >= 0)
|
2011-10-02 00:20:27 +02:00
|
|
|
|
{
|
|
|
|
|
/* No thread is waiting in the queue */
|
|
|
|
|
GError *local_error = NULL;
|
|
|
|
|
|
|
|
|
|
if (!g_thread_pool_start_thread (real, &local_error))
|
|
|
|
|
{
|
|
|
|
|
g_propagate_error (error, local_error);
|
|
|
|
|
result = FALSE;
|
|
|
|
|
}
|
|
|
|
|
}
|
2000-09-01 15:03:23 +02:00
|
|
|
|
|
2005-12-21 05:45:56 +01:00
|
|
|
|
g_thread_pool_queue_push_unlocked (real, data);
|
2000-04-28 14:24:53 +02:00
|
|
|
|
g_async_queue_unlock (real->queue);
|
2011-10-02 00:20:27 +02:00
|
|
|
|
|
|
|
|
|
return result;
|
2000-04-28 14:24:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
2001-04-03 14:42:54 +02:00
|
|
|
|
/**
|
|
|
|
|
* g_thread_pool_set_max_threads:
|
|
|
|
|
* @pool: a #GThreadPool
|
2011-10-02 00:20:27 +02:00
|
|
|
|
* @max_threads: a new maximal number of threads for @pool,
|
|
|
|
|
* or -1 for unlimited
|
|
|
|
|
* @error: return location for error, or %NULL
|
|
|
|
|
*
|
|
|
|
|
* Sets the maximal allowed number of threads for @pool.
|
|
|
|
|
* A value of -1 means that the maximal number of threads
|
|
|
|
|
* is unlimited. If @pool is an exclusive thread pool, setting
|
|
|
|
|
* the maximal number of threads to -1 is not allowed.
|
|
|
|
|
*
|
|
|
|
|
* Setting @max_threads to 0 means stopping all work for @pool.
|
|
|
|
|
* It is effectively frozen until @max_threads is set to a non-zero
|
|
|
|
|
* value again.
|
|
|
|
|
*
|
2001-05-09 14:51:21 +02:00
|
|
|
|
* A thread is never terminated while calling @func, as supplied by
|
2011-10-02 00:20:27 +02:00
|
|
|
|
* g_thread_pool_new(). Instead the maximal number of threads only
|
|
|
|
|
* has effect for the allocation of new threads in g_thread_pool_push().
|
Documentation fixes.
* glib/gconvert.c, glib/grand.c, glib/ghash.c,
glib/gthreadpool.c, glib/gtree.c: Documentation fixes.
* glib/tmpl/allocators.sgml, glib/tmpl/arrays.sgml,
glib/tmpl/arrays_byte.sgml, glib/tmpl/arrays_pointer.sgml,
glib/tmpl/caches.sgml, glib/tmpl/completion.sgml,
glib/tmpl/conversions.sgml,
glib/tmpl/datalist.sgml, glib/tmpl/date.sgml,
glib/tmpl/error_reporting.sgml, glib/tmpl/fileutils.sgml,
glib/tmpl/hash_tables.sgml,
glib/tmpl/hooks.sgml, glib/tmpl/macros.sgml,
glib/tmpl/macros_misc.sgml, glib/tmpl/main.sgml, glib/tmpl/markup.sgml,
glib/tmpl/memory.sgml, glib/tmpl/memory_chunks.sgml,
glib/tmpl/messages.sgml, glib/tmpl/misc_utils.sgml,
glib/tmpl/modules.sgml, glib/tmpl/numerical.sgml,
glib/tmpl/patterns.sgml, glib/tmpl/queue.sgml,
glib/tmpl/shell.sgml, glib/tmpl/spawn.sgml,
glib/tmpl/string_utils.sgml, glib/tmpl/thread_pools.sgml,
glib/tmpl/threads.sgml, glib/tmpl/timers.sgml,
glib/tmpl/trees-binary.sgml, glib/tmpl/trees-nary.sgml,
glib/tmpl/type_conversion.sgml, glib/tmpl/unicode.sgml,
glib/tmpl/warnings.sgml, glib/tmpl/windows.sgml:
Improve markup of examples, general consistency improvements.
2001-12-12 21:32:07 +01:00
|
|
|
|
* A new thread is allocated, whenever the number of currently
|
2001-05-09 14:51:21 +02:00
|
|
|
|
* running threads in @pool is smaller than the maximal number.
|
2001-04-03 14:42:54 +02:00
|
|
|
|
*
|
Documentation fixes.
* glib/gconvert.c, glib/grand.c, glib/ghash.c,
glib/gthreadpool.c, glib/gtree.c: Documentation fixes.
* glib/tmpl/allocators.sgml, glib/tmpl/arrays.sgml,
glib/tmpl/arrays_byte.sgml, glib/tmpl/arrays_pointer.sgml,
glib/tmpl/caches.sgml, glib/tmpl/completion.sgml,
glib/tmpl/conversions.sgml,
glib/tmpl/datalist.sgml, glib/tmpl/date.sgml,
glib/tmpl/error_reporting.sgml, glib/tmpl/fileutils.sgml,
glib/tmpl/hash_tables.sgml,
glib/tmpl/hooks.sgml, glib/tmpl/macros.sgml,
glib/tmpl/macros_misc.sgml, glib/tmpl/main.sgml, glib/tmpl/markup.sgml,
glib/tmpl/memory.sgml, glib/tmpl/memory_chunks.sgml,
glib/tmpl/messages.sgml, glib/tmpl/misc_utils.sgml,
glib/tmpl/modules.sgml, glib/tmpl/numerical.sgml,
glib/tmpl/patterns.sgml, glib/tmpl/queue.sgml,
glib/tmpl/shell.sgml, glib/tmpl/spawn.sgml,
glib/tmpl/string_utils.sgml, glib/tmpl/thread_pools.sgml,
glib/tmpl/threads.sgml, glib/tmpl/timers.sgml,
glib/tmpl/trees-binary.sgml, glib/tmpl/trees-nary.sgml,
glib/tmpl/type_conversion.sgml, glib/tmpl/unicode.sgml,
glib/tmpl/warnings.sgml, glib/tmpl/windows.sgml:
Improve markup of examples, general consistency improvements.
2001-12-12 21:32:07 +01:00
|
|
|
|
* @error can be %NULL to ignore errors, or non-%NULL to report
|
|
|
|
|
* errors. An error can only occur when a new thread couldn't be
|
2011-10-02 00:20:27 +02:00
|
|
|
|
* created.
|
|
|
|
|
*
|
|
|
|
|
* Before version 2.32, this function did not return a success status.
|
|
|
|
|
*
|
2014-02-20 01:35:23 +01:00
|
|
|
|
* Returns: %TRUE on success, %FALSE if an error occurred
|
2011-10-02 00:20:27 +02:00
|
|
|
|
*/
|
|
|
|
|
gboolean
|
2006-04-07 11:23:42 +02:00
|
|
|
|
g_thread_pool_set_max_threads (GThreadPool *pool,
|
2011-10-02 00:20:27 +02:00
|
|
|
|
gint max_threads,
|
|
|
|
|
GError **error)
|
2000-04-28 14:24:53 +02:00
|
|
|
|
{
|
2006-04-07 11:23:42 +02:00
|
|
|
|
GRealThreadPool *real;
|
2000-04-28 14:24:53 +02:00
|
|
|
|
gint to_start;
|
2011-10-02 00:20:27 +02:00
|
|
|
|
gboolean result;
|
2000-04-28 14:24:53 +02:00
|
|
|
|
|
2006-04-07 11:23:42 +02:00
|
|
|
|
real = (GRealThreadPool*) pool;
|
|
|
|
|
|
2011-10-02 00:20:27 +02:00
|
|
|
|
g_return_val_if_fail (real, FALSE);
|
|
|
|
|
g_return_val_if_fail (real->running, FALSE);
|
|
|
|
|
g_return_val_if_fail (!real->pool.exclusive || max_threads != -1, FALSE);
|
|
|
|
|
g_return_val_if_fail (max_threads >= -1, FALSE);
|
|
|
|
|
|
|
|
|
|
result = TRUE;
|
2000-04-28 14:24:53 +02:00
|
|
|
|
|
|
|
|
|
g_async_queue_lock (real->queue);
|
|
|
|
|
|
|
|
|
|
real->max_threads = max_threads;
|
2011-10-02 00:20:27 +02:00
|
|
|
|
|
2000-04-28 14:24:53 +02:00
|
|
|
|
if (pool->exclusive)
|
|
|
|
|
to_start = real->max_threads - real->num_threads;
|
|
|
|
|
else
|
|
|
|
|
to_start = g_async_queue_length_unlocked (real->queue);
|
2011-10-02 00:20:27 +02:00
|
|
|
|
|
2000-04-28 14:24:53 +02:00
|
|
|
|
for ( ; to_start > 0; to_start--)
|
2000-09-01 15:03:23 +02:00
|
|
|
|
{
|
|
|
|
|
GError *local_error = NULL;
|
2006-04-07 11:23:42 +02:00
|
|
|
|
|
2011-10-02 00:20:27 +02:00
|
|
|
|
if (!g_thread_pool_start_thread (real, &local_error))
|
|
|
|
|
{
|
|
|
|
|
g_propagate_error (error, local_error);
|
|
|
|
|
result = FALSE;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2000-09-01 15:03:23 +02:00
|
|
|
|
}
|
2011-10-02 00:20:27 +02:00
|
|
|
|
|
2000-04-28 14:24:53 +02:00
|
|
|
|
g_async_queue_unlock (real->queue);
|
2011-10-02 00:20:27 +02:00
|
|
|
|
|
|
|
|
|
return result;
|
2000-04-28 14:24:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
2001-04-03 14:42:54 +02:00
|
|
|
|
/**
|
|
|
|
|
* g_thread_pool_get_max_threads:
|
|
|
|
|
* @pool: a #GThreadPool
|
|
|
|
|
*
|
|
|
|
|
* Returns the maximal number of threads for @pool.
|
|
|
|
|
*
|
2014-02-20 01:35:23 +01:00
|
|
|
|
* Returns: the maximal number of threads
|
2011-10-02 00:20:27 +02:00
|
|
|
|
*/
|
2000-04-28 14:24:53 +02:00
|
|
|
|
gint
|
2006-04-07 11:23:42 +02:00
|
|
|
|
g_thread_pool_get_max_threads (GThreadPool *pool)
|
2000-04-28 14:24:53 +02:00
|
|
|
|
{
|
2006-04-07 11:23:42 +02:00
|
|
|
|
GRealThreadPool *real;
|
2000-04-28 14:24:53 +02:00
|
|
|
|
gint retval;
|
|
|
|
|
|
2006-04-07 11:23:42 +02:00
|
|
|
|
real = (GRealThreadPool*) pool;
|
|
|
|
|
|
2000-04-28 14:24:53 +02:00
|
|
|
|
g_return_val_if_fail (real, 0);
|
|
|
|
|
g_return_val_if_fail (real->running, 0);
|
|
|
|
|
|
|
|
|
|
g_async_queue_lock (real->queue);
|
|
|
|
|
retval = real->max_threads;
|
|
|
|
|
g_async_queue_unlock (real->queue);
|
|
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
|
}
|
|
|
|
|
|
2001-04-03 14:42:54 +02:00
|
|
|
|
/**
|
|
|
|
|
* g_thread_pool_get_num_threads:
|
|
|
|
|
* @pool: a #GThreadPool
|
|
|
|
|
*
|
|
|
|
|
* Returns the number of threads currently running in @pool.
|
|
|
|
|
*
|
2014-02-20 01:35:23 +01:00
|
|
|
|
* Returns: the number of threads currently running
|
2011-10-02 00:20:27 +02:00
|
|
|
|
*/
|
2000-04-28 14:24:53 +02:00
|
|
|
|
guint
|
2006-04-07 11:23:42 +02:00
|
|
|
|
g_thread_pool_get_num_threads (GThreadPool *pool)
|
2000-04-28 14:24:53 +02:00
|
|
|
|
{
|
2006-04-07 11:23:42 +02:00
|
|
|
|
GRealThreadPool *real;
|
2000-04-28 14:24:53 +02:00
|
|
|
|
guint retval;
|
|
|
|
|
|
2006-04-07 11:23:42 +02:00
|
|
|
|
real = (GRealThreadPool*) pool;
|
|
|
|
|
|
2000-04-28 14:24:53 +02:00
|
|
|
|
g_return_val_if_fail (real, 0);
|
|
|
|
|
g_return_val_if_fail (real->running, 0);
|
|
|
|
|
|
|
|
|
|
g_async_queue_lock (real->queue);
|
|
|
|
|
retval = real->num_threads;
|
|
|
|
|
g_async_queue_unlock (real->queue);
|
|
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
|
}
|
|
|
|
|
|
2001-04-03 14:42:54 +02:00
|
|
|
|
/**
|
|
|
|
|
* g_thread_pool_unprocessed:
|
|
|
|
|
* @pool: a #GThreadPool
|
|
|
|
|
*
|
|
|
|
|
* Returns the number of tasks still unprocessed in @pool.
|
|
|
|
|
*
|
2014-02-20 01:35:23 +01:00
|
|
|
|
* Returns: the number of unprocessed tasks
|
2011-10-02 00:20:27 +02:00
|
|
|
|
*/
|
2000-04-28 14:24:53 +02:00
|
|
|
|
guint
|
2006-04-07 11:23:42 +02:00
|
|
|
|
g_thread_pool_unprocessed (GThreadPool *pool)
|
2000-04-28 14:24:53 +02:00
|
|
|
|
{
|
2006-04-07 11:23:42 +02:00
|
|
|
|
GRealThreadPool *real;
|
2000-04-28 14:24:53 +02:00
|
|
|
|
gint unprocessed;
|
|
|
|
|
|
2006-04-07 11:23:42 +02:00
|
|
|
|
real = (GRealThreadPool*) pool;
|
|
|
|
|
|
2000-04-28 14:24:53 +02:00
|
|
|
|
g_return_val_if_fail (real, 0);
|
|
|
|
|
g_return_val_if_fail (real->running, 0);
|
|
|
|
|
|
|
|
|
|
unprocessed = g_async_queue_length (real->queue);
|
|
|
|
|
|
|
|
|
|
return MAX (unprocessed, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2001-04-03 14:42:54 +02:00
|
|
|
|
/**
|
|
|
|
|
* g_thread_pool_free:
|
|
|
|
|
* @pool: a #GThreadPool
|
|
|
|
|
* @immediate: should @pool shut down immediately?
|
2006-12-17 21:27:02 +01:00
|
|
|
|
* @wait_: should the function wait for all tasks to be finished?
|
2001-04-03 14:42:54 +02:00
|
|
|
|
*
|
|
|
|
|
* Frees all resources allocated for @pool.
|
|
|
|
|
*
|
2011-10-02 00:20:27 +02:00
|
|
|
|
* If @immediate is %TRUE, no new task is processed for @pool.
|
|
|
|
|
* Otherwise @pool is not freed before the last task is processed.
|
2011-10-02 00:42:48 +02:00
|
|
|
|
* Note however, that no thread of this pool is interrupted while
|
2011-10-02 00:20:27 +02:00
|
|
|
|
* processing a task. Instead at least all still running threads
|
|
|
|
|
* can finish their tasks before the @pool is freed.
|
2001-04-03 14:42:54 +02:00
|
|
|
|
*
|
2020-06-17 12:46:52 +02:00
|
|
|
|
* If @wait_ is %TRUE, this function does not return before all
|
2011-10-02 00:20:27 +02:00
|
|
|
|
* tasks to be processed (dependent on @immediate, whether all
|
2011-10-15 22:50:23 +02:00
|
|
|
|
* or only the currently running) are ready.
|
2020-06-17 12:46:52 +02:00
|
|
|
|
* Otherwise this function returns immediately.
|
2001-04-03 14:42:54 +02:00
|
|
|
|
*
|
2011-10-02 00:20:27 +02:00
|
|
|
|
* After calling this function @pool must not be used anymore.
|
|
|
|
|
*/
|
2000-04-28 14:24:53 +02:00
|
|
|
|
void
|
2006-04-07 11:23:42 +02:00
|
|
|
|
g_thread_pool_free (GThreadPool *pool,
|
2011-10-02 00:20:27 +02:00
|
|
|
|
gboolean immediate,
|
|
|
|
|
gboolean wait_)
|
2000-04-28 14:24:53 +02:00
|
|
|
|
{
|
2006-04-07 11:23:42 +02:00
|
|
|
|
GRealThreadPool *real;
|
|
|
|
|
|
|
|
|
|
real = (GRealThreadPool*) pool;
|
2000-04-28 14:24:53 +02:00
|
|
|
|
|
|
|
|
|
g_return_if_fail (real);
|
|
|
|
|
g_return_if_fail (real->running);
|
2006-04-07 11:23:42 +02:00
|
|
|
|
|
2006-02-15 23:10:49 +01:00
|
|
|
|
/* If there's no thread allowed here, there is not much sense in
|
2011-10-02 00:20:27 +02:00
|
|
|
|
* not stopping this pool immediately, when it's not empty
|
2006-04-07 11:23:42 +02:00
|
|
|
|
*/
|
2011-10-02 00:20:27 +02:00
|
|
|
|
g_return_if_fail (immediate ||
|
|
|
|
|
real->max_threads != 0 ||
|
|
|
|
|
g_async_queue_length (real->queue) == 0);
|
2000-04-28 14:24:53 +02:00
|
|
|
|
|
|
|
|
|
g_async_queue_lock (real->queue);
|
|
|
|
|
|
|
|
|
|
real->running = FALSE;
|
|
|
|
|
real->immediate = immediate;
|
2006-12-17 21:27:02 +01:00
|
|
|
|
real->waiting = wait_;
|
2000-04-28 14:24:53 +02:00
|
|
|
|
|
2006-12-17 21:27:02 +01:00
|
|
|
|
if (wait_)
|
2000-04-28 14:24:53 +02:00
|
|
|
|
{
|
2019-02-04 10:34:13 +01:00
|
|
|
|
while (g_async_queue_length_unlocked (real->queue) != (gint) -real->num_threads &&
|
2011-10-02 00:20:27 +02:00
|
|
|
|
!(immediate && real->num_threads == 0))
|
2011-10-04 05:52:13 +02:00
|
|
|
|
g_cond_wait (&real->cond, _g_async_queue_get_mutex (real->queue));
|
2000-04-28 14:24:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-04 10:34:13 +01:00
|
|
|
|
if (immediate || g_async_queue_length_unlocked (real->queue) == (gint) -real->num_threads)
|
2000-04-28 14:24:53 +02:00
|
|
|
|
{
|
|
|
|
|
/* No thread is currently doing something (and nothing is left
|
2011-10-02 00:20:27 +02:00
|
|
|
|
* to process in the queue)
|
2006-04-07 11:23:42 +02:00
|
|
|
|
*/
|
2011-10-02 00:20:27 +02:00
|
|
|
|
if (real->num_threads == 0)
|
|
|
|
|
{
|
|
|
|
|
/* No threads left, we clean up */
|
|
|
|
|
g_async_queue_unlock (real->queue);
|
|
|
|
|
g_thread_pool_free_internal (real);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2000-04-28 14:24:53 +02:00
|
|
|
|
|
|
|
|
|
g_thread_pool_wakeup_and_stop_all (real);
|
|
|
|
|
}
|
2011-10-02 00:20:27 +02:00
|
|
|
|
|
2006-04-07 11:23:42 +02:00
|
|
|
|
/* The last thread should cleanup the pool */
|
2011-10-02 00:20:27 +02:00
|
|
|
|
real->waiting = FALSE;
|
2000-04-28 14:24:53 +02:00
|
|
|
|
g_async_queue_unlock (real->queue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
g_thread_pool_free_internal (GRealThreadPool* pool)
|
|
|
|
|
{
|
|
|
|
|
g_return_if_fail (pool);
|
2006-04-07 11:23:42 +02:00
|
|
|
|
g_return_if_fail (pool->running == FALSE);
|
2000-04-28 14:24:53 +02:00
|
|
|
|
g_return_if_fail (pool->num_threads == 0);
|
|
|
|
|
|
2021-08-19 15:15:37 +02:00
|
|
|
|
/* Ensure the dummy item pushed on by g_thread_pool_wakeup_and_stop_all() is
|
|
|
|
|
* removed, before it’s potentially passed to the user-provided
|
|
|
|
|
* @item_free_func. */
|
|
|
|
|
g_async_queue_remove (pool->queue, GUINT_TO_POINTER (1));
|
|
|
|
|
|
2000-04-28 14:24:53 +02:00
|
|
|
|
g_async_queue_unref (pool->queue);
|
2011-10-04 05:52:13 +02:00
|
|
|
|
g_cond_clear (&pool->cond);
|
2006-04-07 11:23:42 +02:00
|
|
|
|
|
2000-04-28 14:24:53 +02:00
|
|
|
|
g_free (pool);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2011-10-02 00:20:27 +02:00
|
|
|
|
g_thread_pool_wakeup_and_stop_all (GRealThreadPool *pool)
|
2000-04-28 14:24:53 +02:00
|
|
|
|
{
|
|
|
|
|
guint i;
|
2011-10-02 00:20:27 +02:00
|
|
|
|
|
2000-04-28 14:24:53 +02:00
|
|
|
|
g_return_if_fail (pool);
|
2006-04-07 11:23:42 +02:00
|
|
|
|
g_return_if_fail (pool->running == FALSE);
|
2000-04-28 14:24:53 +02:00
|
|
|
|
g_return_if_fail (pool->num_threads != 0);
|
|
|
|
|
|
2011-10-02 00:20:27 +02:00
|
|
|
|
pool->immediate = TRUE;
|
2006-04-07 11:23:42 +02:00
|
|
|
|
|
2013-11-10 00:15:30 +01:00
|
|
|
|
/*
|
|
|
|
|
* So here we're sending bogus data to the pool threads, which
|
|
|
|
|
* should cause them each to wake up, and check the above
|
|
|
|
|
* pool->immediate condition. However we don't want that
|
|
|
|
|
* data to be sorted (since it'll crash the sorter).
|
|
|
|
|
*/
|
2000-04-28 14:24:53 +02:00
|
|
|
|
for (i = 0; i < pool->num_threads; i++)
|
2013-11-10 00:15:30 +01:00
|
|
|
|
g_async_queue_push_unlocked (pool->queue, GUINT_TO_POINTER (1));
|
2000-04-28 14:24:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
2001-04-03 14:42:54 +02:00
|
|
|
|
/**
|
|
|
|
|
* g_thread_pool_set_max_unused_threads:
|
|
|
|
|
* @max_threads: maximal number of unused threads
|
|
|
|
|
*
|
2011-10-02 00:20:27 +02:00
|
|
|
|
* Sets the maximal number of unused threads to @max_threads.
|
|
|
|
|
* If @max_threads is -1, no limit is imposed on the number
|
|
|
|
|
* of unused threads.
|
2012-07-25 20:41:04 +02:00
|
|
|
|
*
|
|
|
|
|
* The default value is 2.
|
2011-10-02 00:20:27 +02:00
|
|
|
|
*/
|
2000-04-28 14:24:53 +02:00
|
|
|
|
void
|
|
|
|
|
g_thread_pool_set_max_unused_threads (gint max_threads)
|
|
|
|
|
{
|
2011-10-02 00:20:27 +02:00
|
|
|
|
g_return_if_fail (max_threads >= -1);
|
2000-04-28 14:24:53 +02:00
|
|
|
|
|
2006-04-07 11:23:42 +02:00
|
|
|
|
g_atomic_int_set (&max_unused_threads, max_threads);
|
2000-04-28 14:24:53 +02:00
|
|
|
|
|
2006-04-07 11:23:42 +02:00
|
|
|
|
if (max_threads != -1)
|
2000-04-28 14:24:53 +02:00
|
|
|
|
{
|
2006-04-07 11:23:42 +02:00
|
|
|
|
max_threads -= g_atomic_int_get (&unused_threads);
|
|
|
|
|
if (max_threads < 0)
|
2011-10-02 00:20:27 +02:00
|
|
|
|
{
|
|
|
|
|
g_atomic_int_set (&kill_unused_threads, -max_threads);
|
|
|
|
|
g_atomic_int_inc (&wakeup_thread_serial);
|
2001-04-03 14:42:54 +02:00
|
|
|
|
|
2011-10-02 00:20:27 +02:00
|
|
|
|
g_async_queue_lock (unused_thread_queue);
|
2006-04-07 11:23:42 +02:00
|
|
|
|
|
2011-10-02 00:20:27 +02:00
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
g_async_queue_push_unlocked (unused_thread_queue,
|
|
|
|
|
wakeup_thread_marker);
|
|
|
|
|
}
|
|
|
|
|
while (++max_threads);
|
2006-04-07 11:23:42 +02:00
|
|
|
|
|
2011-10-02 00:20:27 +02:00
|
|
|
|
g_async_queue_unlock (unused_thread_queue);
|
|
|
|
|
}
|
2000-04-28 14:24:53 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2001-04-03 14:42:54 +02:00
|
|
|
|
/**
|
|
|
|
|
* g_thread_pool_get_max_unused_threads:
|
2011-10-02 00:20:27 +02:00
|
|
|
|
*
|
2001-04-03 14:42:54 +02:00
|
|
|
|
* Returns the maximal allowed number of unused threads.
|
|
|
|
|
*
|
2014-02-20 01:35:23 +01:00
|
|
|
|
* Returns: the maximal number of unused threads
|
2011-10-02 00:20:27 +02:00
|
|
|
|
*/
|
2000-04-28 14:24:53 +02:00
|
|
|
|
gint
|
|
|
|
|
g_thread_pool_get_max_unused_threads (void)
|
|
|
|
|
{
|
2006-04-07 11:23:42 +02:00
|
|
|
|
return g_atomic_int_get (&max_unused_threads);
|
2000-04-28 14:24:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
2001-04-03 14:42:54 +02:00
|
|
|
|
/**
|
|
|
|
|
* g_thread_pool_get_num_unused_threads:
|
2011-10-02 00:20:27 +02:00
|
|
|
|
*
|
2001-04-03 14:42:54 +02:00
|
|
|
|
* Returns the number of currently unused threads.
|
|
|
|
|
*
|
2014-02-20 01:35:23 +01:00
|
|
|
|
* Returns: the number of currently unused threads
|
2011-10-02 00:20:27 +02:00
|
|
|
|
*/
|
|
|
|
|
guint
|
2006-01-03 16:09:52 +01:00
|
|
|
|
g_thread_pool_get_num_unused_threads (void)
|
2000-04-28 14:24:53 +02:00
|
|
|
|
{
|
2019-09-21 10:45:36 +02:00
|
|
|
|
return (guint) g_atomic_int_get (&unused_threads);
|
2000-04-28 14:24:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
2001-04-03 14:42:54 +02:00
|
|
|
|
/**
|
|
|
|
|
* g_thread_pool_stop_unused_threads:
|
2011-10-02 00:20:27 +02:00
|
|
|
|
*
|
2001-04-03 14:42:54 +02:00
|
|
|
|
* Stops all currently unused threads. This does not change the
|
|
|
|
|
* maximal number of unused threads. This function can be used to
|
|
|
|
|
* regularly stop all unused threads e.g. from g_timeout_add().
|
2011-10-02 00:20:27 +02:00
|
|
|
|
*/
|
2006-01-03 16:09:52 +01:00
|
|
|
|
void
|
|
|
|
|
g_thread_pool_stop_unused_threads (void)
|
2011-10-02 00:20:27 +02:00
|
|
|
|
{
|
2006-04-07 11:23:42 +02:00
|
|
|
|
guint oldval;
|
|
|
|
|
|
|
|
|
|
oldval = g_thread_pool_get_max_unused_threads ();
|
|
|
|
|
|
2000-04-28 14:24:53 +02:00
|
|
|
|
g_thread_pool_set_max_unused_threads (0);
|
|
|
|
|
g_thread_pool_set_max_unused_threads (oldval);
|
|
|
|
|
}
|
2005-03-14 05:26:57 +01:00
|
|
|
|
|
2005-12-21 05:45:56 +01:00
|
|
|
|
/**
|
|
|
|
|
* g_thread_pool_set_sort_function:
|
|
|
|
|
* @pool: a #GThreadPool
|
2011-10-02 00:20:27 +02:00
|
|
|
|
* @func: the #GCompareDataFunc used to sort the list of tasks.
|
2005-12-21 05:45:56 +01:00
|
|
|
|
* This function is passed two tasks. It should return
|
2011-10-02 00:20:27 +02:00
|
|
|
|
* 0 if the order in which they are handled does not matter,
|
2005-12-21 05:45:56 +01:00
|
|
|
|
* a negative value if the first task should be processed before
|
2011-10-02 00:20:27 +02:00
|
|
|
|
* the second or a positive value if the second task should be
|
2005-12-21 05:45:56 +01:00
|
|
|
|
* processed first.
|
2011-10-02 00:20:27 +02:00
|
|
|
|
* @user_data: user data passed to @func
|
2005-12-21 05:45:56 +01:00
|
|
|
|
*
|
|
|
|
|
* Sets the function used to sort the list of tasks. This allows the
|
|
|
|
|
* tasks to be processed by a priority determined by @func, and not
|
|
|
|
|
* just in the order in which they were added to the pool.
|
|
|
|
|
*
|
2006-03-24 16:21:28 +01:00
|
|
|
|
* Note, if the maximum number of threads is more than 1, the order
|
2011-08-29 20:49:32 +02:00
|
|
|
|
* that threads are executed cannot be guaranteed 100%. Threads are
|
2006-03-24 16:21:28 +01:00
|
|
|
|
* scheduled by the operating system and are executed at random. It
|
|
|
|
|
* cannot be assumed that threads are executed in the order they are
|
2011-10-02 00:20:27 +02:00
|
|
|
|
* created.
|
2006-03-24 16:21:28 +01:00
|
|
|
|
*
|
2005-12-21 05:45:56 +01:00
|
|
|
|
* Since: 2.10
|
2011-10-02 00:20:27 +02:00
|
|
|
|
*/
|
|
|
|
|
void
|
2006-01-03 16:09:52 +01:00
|
|
|
|
g_thread_pool_set_sort_function (GThreadPool *pool,
|
2011-10-02 00:20:27 +02:00
|
|
|
|
GCompareDataFunc func,
|
|
|
|
|
gpointer user_data)
|
|
|
|
|
{
|
2006-04-07 11:23:42 +02:00
|
|
|
|
GRealThreadPool *real;
|
|
|
|
|
|
|
|
|
|
real = (GRealThreadPool*) pool;
|
2005-12-21 05:45:56 +01:00
|
|
|
|
|
|
|
|
|
g_return_if_fail (real);
|
|
|
|
|
g_return_if_fail (real->running);
|
|
|
|
|
|
|
|
|
|
g_async_queue_lock (real->queue);
|
|
|
|
|
|
|
|
|
|
real->sort_func = func;
|
|
|
|
|
real->sort_user_data = user_data;
|
2011-10-02 00:20:27 +02:00
|
|
|
|
|
|
|
|
|
if (func)
|
|
|
|
|
g_async_queue_sort_unlocked (real->queue,
|
|
|
|
|
real->sort_func,
|
|
|
|
|
real->sort_user_data);
|
2005-12-21 05:45:56 +01:00
|
|
|
|
|
|
|
|
|
g_async_queue_unlock (real->queue);
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-29 17:19:31 +02:00
|
|
|
|
/**
|
|
|
|
|
* g_thread_pool_move_to_front:
|
|
|
|
|
* @pool: a #GThreadPool
|
|
|
|
|
* @data: an unprocessed item in the pool
|
|
|
|
|
*
|
|
|
|
|
* Moves the item to the front of the queue of unprocessed
|
|
|
|
|
* items, so that it will be processed next.
|
|
|
|
|
*
|
|
|
|
|
* Returns: %TRUE if the item was found and moved
|
|
|
|
|
*
|
|
|
|
|
* Since: 2.46
|
|
|
|
|
*/
|
|
|
|
|
gboolean
|
|
|
|
|
g_thread_pool_move_to_front (GThreadPool *pool,
|
|
|
|
|
gpointer data)
|
|
|
|
|
{
|
|
|
|
|
GRealThreadPool *real = (GRealThreadPool*) pool;
|
|
|
|
|
gboolean found;
|
|
|
|
|
|
|
|
|
|
g_async_queue_lock (real->queue);
|
|
|
|
|
|
|
|
|
|
found = g_async_queue_remove_unlocked (real->queue, data);
|
|
|
|
|
if (found)
|
|
|
|
|
g_async_queue_push_front_unlocked (real->queue, data);
|
|
|
|
|
|
|
|
|
|
g_async_queue_unlock (real->queue);
|
|
|
|
|
|
|
|
|
|
return found;
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-03 16:09:52 +01:00
|
|
|
|
/**
|
|
|
|
|
* g_thread_pool_set_max_idle_time:
|
2011-10-02 00:20:27 +02:00
|
|
|
|
* @interval: the maximum @interval (in milliseconds)
|
|
|
|
|
* a thread can be idle
|
2006-01-03 16:09:52 +01:00
|
|
|
|
*
|
2011-10-02 00:20:27 +02:00
|
|
|
|
* This function will set the maximum @interval that a thread
|
|
|
|
|
* waiting in the pool for new tasks can be idle for before
|
|
|
|
|
* being stopped. This function is similar to calling
|
|
|
|
|
* g_thread_pool_stop_unused_threads() on a regular timeout,
|
|
|
|
|
* except this is done on a per thread basis.
|
2006-01-03 16:09:52 +01:00
|
|
|
|
*
|
|
|
|
|
* By setting @interval to 0, idle threads will not be stopped.
|
2011-10-02 00:20:27 +02:00
|
|
|
|
*
|
2012-07-25 20:41:04 +02:00
|
|
|
|
* The default value is 15000 (15 seconds).
|
2006-01-03 16:09:52 +01:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.10
|
2011-10-02 00:20:27 +02:00
|
|
|
|
*/
|
2006-01-03 16:09:52 +01:00
|
|
|
|
void
|
|
|
|
|
g_thread_pool_set_max_idle_time (guint interval)
|
2011-10-02 00:20:27 +02:00
|
|
|
|
{
|
2006-01-17 21:06:27 +01:00
|
|
|
|
guint i;
|
|
|
|
|
|
2006-04-07 11:23:42 +02:00
|
|
|
|
g_atomic_int_set (&max_idle_time, interval);
|
2006-01-17 21:06:27 +01:00
|
|
|
|
|
2019-09-21 10:45:36 +02:00
|
|
|
|
i = (guint) g_atomic_int_get (&unused_threads);
|
2006-04-07 11:23:42 +02:00
|
|
|
|
if (i > 0)
|
|
|
|
|
{
|
|
|
|
|
g_atomic_int_inc (&wakeup_thread_serial);
|
|
|
|
|
g_async_queue_lock (unused_thread_queue);
|
2006-01-17 21:06:27 +01:00
|
|
|
|
|
2006-04-07 11:23:42 +02:00
|
|
|
|
do
|
2011-10-02 00:20:27 +02:00
|
|
|
|
{
|
|
|
|
|
g_async_queue_push_unlocked (unused_thread_queue,
|
|
|
|
|
wakeup_thread_marker);
|
|
|
|
|
}
|
2006-04-07 11:23:42 +02:00
|
|
|
|
while (--i);
|
|
|
|
|
|
|
|
|
|
g_async_queue_unlock (unused_thread_queue);
|
|
|
|
|
}
|
2006-01-03 16:09:52 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* g_thread_pool_get_max_idle_time:
|
|
|
|
|
*
|
2011-10-02 00:20:27 +02:00
|
|
|
|
* This function will return the maximum @interval that a
|
|
|
|
|
* thread will wait in the thread pool for new tasks before
|
|
|
|
|
* being stopped.
|
|
|
|
|
*
|
|
|
|
|
* If this function returns 0, threads waiting in the thread
|
|
|
|
|
* pool for new work are not stopped.
|
|
|
|
|
*
|
2014-02-20 01:35:23 +01:00
|
|
|
|
* Returns: the maximum @interval (milliseconds) to wait
|
2011-10-02 00:20:27 +02:00
|
|
|
|
* for new tasks in the thread pool before stopping the
|
|
|
|
|
* thread
|
2006-01-03 16:09:52 +01:00
|
|
|
|
*
|
|
|
|
|
* Since: 2.10
|
2011-10-02 00:20:27 +02:00
|
|
|
|
*/
|
2006-01-03 16:09:52 +01:00
|
|
|
|
guint
|
|
|
|
|
g_thread_pool_get_max_idle_time (void)
|
2011-10-02 00:20:27 +02:00
|
|
|
|
{
|
2019-09-21 10:45:36 +02:00
|
|
|
|
return (guint) g_atomic_int_get (&max_idle_time);
|
2006-01-03 16:09:52 +01:00
|
|
|
|
}
|