#include "config.h"
#include "glib.h"
typedef struct _GRealThreadPool GRealThreadPool;
struct _GRealThreadPool
{
GThreadPool pool;
GAsyncQueue* queue;
gint max_threads;
gint num_threads;
gboolean running;
gboolean immediate;
gboolean waiting;
};
static const gpointer stop_this_thread_marker = (gpointer) &g_thread_pool_new;
static GAsyncQueue *unused_thread_queue;
static gint unused_threads = 0;
static gint max_unused_threads = 0;
G_LOCK_DEFINE_STATIC (unused_threads);
static GMutex *inform_mutex = NULL;
static GCond *inform_cond = NULL;
static void g_thread_pool_free_internal (GRealThreadPool* pool);
static gpointer g_thread_pool_thread_proxy (gpointer data);
static void g_thread_pool_start_thread (GRealThreadPool* pool,
GError **error);
static void g_thread_pool_wakeup_and_stop_all (GRealThreadPool* pool);
#define g_thread_should_run(pool, len) \
((pool)->running || (!(pool)->immediate && (len) > 0))
static gpointer
g_thread_pool_thread_proxy (gpointer data)
{
GRealThreadPool *pool = data;
gboolean watcher = FALSE;
g_async_queue_lock (pool->queue);
while (TRUE)
{
gpointer task;
gboolean goto_global_pool = !pool->pool.exclusive;
gint len = g_async_queue_length_unlocked (pool->queue);
if (g_thread_should_run (pool, len))
{
if (watcher)
{
GTimeVal end_time;
g_get_current_time (&end_time);
g_time_val_add (&end_time, G_USEC_PER_SEC / 2);
task = g_async_queue_timed_pop_unlocked (pool->queue, &end_time);
}
else
task = g_async_queue_pop_unlocked (pool->queue);
if (task)
{
watcher = FALSE;
if (pool->num_threads > pool->max_threads &&
pool->max_threads != -1)
{
g_async_queue_push_unlocked (pool->queue, task);
goto_global_pool = TRUE;
}
else if (pool->running || !pool->immediate)
{
g_async_queue_unlock (pool->queue);
pool->pool.func (task, pool->pool.user_data);
g_async_queue_lock (pool->queue);
}
}
len = g_async_queue_length_unlocked (pool->queue);
}
if (!g_thread_should_run (pool, len))
{
g_cond_broadcast (inform_cond);
goto_global_pool = TRUE;
}
else if (len > 0)
{
goto_global_pool = FALSE;
}
else if (len == 0 && !watcher && !pool->pool.exclusive)
{
goto_global_pool = FALSE;
watcher = TRUE;
}
if (goto_global_pool)
{
pool->num_threads--;
if (!pool->running && !pool->waiting)
{
if (pool->num_threads == 0)
{
g_async_queue_unlock (pool->queue);
g_thread_pool_free_internal (pool);
}
else
{
if (len == - pool->num_threads)
g_thread_pool_wakeup_and_stop_all (pool);
g_async_queue_unlock (pool->queue);
}
}
else
g_async_queue_unlock (pool->queue);
g_async_queue_lock (unused_thread_queue);
G_LOCK (unused_threads);
if ((unused_threads >= max_unused_threads &&
max_unused_threads != -1))
{
G_UNLOCK (unused_threads);
g_async_queue_unlock (unused_thread_queue);
return NULL;
}
unused_threads++;
G_UNLOCK (unused_threads);
pool = g_async_queue_pop_unlocked (unused_thread_queue);
G_LOCK (unused_threads);
unused_threads--;
G_UNLOCK (unused_threads);
g_async_queue_unlock (unused_thread_queue);
if (pool == stop_this_thread_marker)
return NULL;
g_async_queue_lock (pool->queue);
}
}
return NULL;
}
static void
g_thread_pool_start_thread (GRealThreadPool *pool,
GError **error)
{
gboolean success = FALSE;
if (pool->num_threads >= pool->max_threads && pool->max_threads != -1)
return;
g_async_queue_lock (unused_thread_queue);
if (g_async_queue_length_unlocked (unused_thread_queue) < 0)
{
g_async_queue_push_unlocked (unused_thread_queue, pool);
success = TRUE;
}
g_async_queue_unlock (unused_thread_queue);
if (!success)
{
GError *local_error = NULL;
g_thread_create (g_thread_pool_thread_proxy, pool, FALSE, &local_error);
if (local_error)
{
g_propagate_error (error, local_error);
return;
}
}
pool->num_threads++;
}
GThreadPool*
g_thread_pool_new (GFunc func,
gpointer user_data,
gint max_threads,
gboolean exclusive,
GError **error)
{
GRealThreadPool *retval;
G_LOCK_DEFINE_STATIC (init);
g_return_val_if_fail (func, NULL);
g_return_val_if_fail (!exclusive || max_threads != -1, NULL);
g_return_val_if_fail (max_threads >= -1, NULL);
g_return_val_if_fail (g_thread_supported (), NULL);
retval = g_new (GRealThreadPool, 1);
retval->pool.func = func;
retval->pool.user_data = user_data;
retval->pool.exclusive = exclusive;
retval->queue = g_async_queue_new ();
retval->max_threads = max_threads;
retval->num_threads = 0;
retval->running = TRUE;
G_LOCK (init);
if (!inform_mutex)
{
inform_mutex = g_mutex_new ();
inform_cond = g_cond_new ();
unused_thread_queue = g_async_queue_new ();
}
G_UNLOCK (init);
if (retval->pool.exclusive)
{
g_async_queue_lock (retval->queue);
while (retval->num_threads < retval->max_threads)
{
GError *local_error = NULL;
g_thread_pool_start_thread (retval, &local_error);
if (local_error)
{
g_propagate_error (error, local_error);
break;
}
}
g_async_queue_unlock (retval->queue);
}
return (GThreadPool*) retval;
}
void
g_thread_pool_push (GThreadPool *pool,
gpointer data,
GError **error)
{
GRealThreadPool *real = (GRealThreadPool*) pool;
g_return_if_fail (real);
g_async_queue_lock (real->queue);
if (!real->running)
{
g_async_queue_unlock (real->queue);
g_return_if_fail (real->running);
}
if (g_async_queue_length_unlocked (real->queue) >= 0)
g_thread_pool_start_thread (real, error);
g_async_queue_push_unlocked (real->queue, data);
g_async_queue_unlock (real->queue);
}
void
g_thread_pool_set_max_threads (GThreadPool *pool,
gint max_threads,
GError **error)
{
GRealThreadPool *real = (GRealThreadPool*) pool;
gint to_start;
g_return_if_fail (real);
g_return_if_fail (real->running);
g_return_if_fail (!real->pool.exclusive || max_threads != -1);
g_return_if_fail (max_threads >= -1);
g_async_queue_lock (real->queue);
real->max_threads = max_threads;
if (pool->exclusive)
to_start = real->max_threads - real->num_threads;
else
to_start = g_async_queue_length_unlocked (real->queue);
for ( ; to_start > 0; to_start--)
{
GError *local_error = NULL;
g_thread_pool_start_thread (real, &local_error);
if (local_error)
{
g_propagate_error (error, local_error);
break;
}
}
g_async_queue_unlock (real->queue);
}
gint
g_thread_pool_get_max_threads (GThreadPool *pool)
{
GRealThreadPool *real = (GRealThreadPool*) pool;
gint retval;
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;
}
guint
g_thread_pool_get_num_threads (GThreadPool *pool)
{
GRealThreadPool *real = (GRealThreadPool*) pool;
guint retval;
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;
}
guint
g_thread_pool_unprocessed (GThreadPool *pool)
{
GRealThreadPool *real = (GRealThreadPool*) pool;
gint unprocessed;
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);
}
void
g_thread_pool_free (GThreadPool *pool,
gboolean immediate,
gboolean wait)
{
GRealThreadPool *real = (GRealThreadPool*) pool;
g_return_if_fail (real);
g_return_if_fail (real->running);
g_return_if_fail (immediate || real->max_threads != 0 ||
g_async_queue_length (real->queue) == 0);
g_async_queue_lock (real->queue);
real->running = FALSE;
real->immediate = immediate;
real->waiting = wait;
if (wait)
{
g_mutex_lock (inform_mutex);
while (g_async_queue_length_unlocked (real->queue) != -real->num_threads)
{
g_async_queue_unlock (real->queue);
g_cond_wait (inform_cond, inform_mutex);
g_async_queue_lock (real->queue);
}
g_mutex_unlock (inform_mutex);
}
if (g_async_queue_length_unlocked (real->queue) == -real->num_threads)
{
if (real->num_threads == 0)
{
g_async_queue_unlock (real->queue);
g_thread_pool_free_internal (real);
return;
}
g_thread_pool_wakeup_and_stop_all (real);
}
real->waiting = FALSE;
g_async_queue_unlock (real->queue);
}
static void
g_thread_pool_free_internal (GRealThreadPool* pool)
{
g_return_if_fail (pool);
g_return_if_fail (!pool->running);
g_return_if_fail (pool->num_threads == 0);
g_async_queue_unref (pool->queue);
g_free (pool);
}
static void
g_thread_pool_wakeup_and_stop_all (GRealThreadPool* pool)
{
guint i;
g_return_if_fail (pool);
g_return_if_fail (!pool->running);
g_return_if_fail (pool->num_threads != 0);
g_return_if_fail (g_async_queue_length_unlocked (pool->queue) ==
-pool->num_threads);
pool->immediate = TRUE;
for (i = 0; i < pool->num_threads; i++)
g_async_queue_push_unlocked (pool->queue, GUINT_TO_POINTER (1));
}
void
g_thread_pool_set_max_unused_threads (gint max_threads)
{
g_return_if_fail (max_threads >= -1);
G_LOCK (unused_threads);
max_unused_threads = max_threads;
if (max_unused_threads < unused_threads && max_unused_threads != -1)
{
guint i;
g_async_queue_lock (unused_thread_queue);
for (i = unused_threads - max_unused_threads; i > 0; i--)
g_async_queue_push_unlocked (unused_thread_queue,
stop_this_thread_marker);
g_async_queue_unlock (unused_thread_queue);
}
G_UNLOCK (unused_threads);
}
gint
g_thread_pool_get_max_unused_threads (void)
{
gint retval;
G_LOCK (unused_threads);
retval = max_unused_threads;
G_UNLOCK (unused_threads);
return retval;
}
guint g_thread_pool_get_num_unused_threads (void)
{
guint retval;
G_LOCK (unused_threads);
retval = unused_threads;
G_UNLOCK (unused_threads);
return retval;
}
void g_thread_pool_stop_unused_threads (void)
{
guint oldval = g_thread_pool_get_max_unused_threads ();
g_thread_pool_set_max_unused_threads (0);
g_thread_pool_set_max_unused_threads (oldval);
}