• Home
  • Raw
  • Download

Lines Matching full:pool

4  * GThreadPool: thread pool implementation.
54 * To create a new thread pool, you use g_thread_pool_new().
57 * If you want to execute a certain task within a thread pool,
63 * the maximal number of threads for a thread pool, you use
81 * @func: the function to execute in the threads of this pool
82 * @user_data: the user data for the threads of this pool
83 * @exclusive: are all threads exclusive to this pool
85 * The #GThreadPool struct represents a thread pool. It has three
91 GThreadPool pool; member
123 GThreadPool *pool; member
131 static void g_thread_pool_queue_push_unlocked (GRealThreadPool *pool,
133 static void g_thread_pool_free_internal (GRealThreadPool *pool);
135 static gboolean g_thread_pool_start_thread (GRealThreadPool *pool,
137 static void g_thread_pool_wakeup_and_stop_all (GRealThreadPool *pool);
139 static gpointer g_thread_pool_wait_for_new_task (GRealThreadPool *pool);
142 g_thread_pool_queue_push_unlocked (GRealThreadPool *pool, in g_thread_pool_queue_push_unlocked() argument
145 if (pool->sort_func) in g_thread_pool_queue_push_unlocked()
146 g_async_queue_push_sorted_unlocked (pool->queue, in g_thread_pool_queue_push_unlocked()
148 pool->sort_func, in g_thread_pool_queue_push_unlocked()
149 pool->sort_user_data); in g_thread_pool_queue_push_unlocked()
151 g_async_queue_push_unlocked (pool->queue, data); in g_thread_pool_queue_push_unlocked()
157 GRealThreadPool *pool; in g_thread_pool_wait_for_new_pool() local
175 pool = NULL; in g_thread_pool_wait_for_new_pool()
180 DEBUG_MSG (("thread %p waiting in global pool for %f seconds.", in g_thread_pool_wait_for_new_pool()
183 pool = g_async_queue_timeout_pop (unused_thread_queue, in g_thread_pool_wait_for_new_pool()
189 DEBUG_MSG (("thread %p waiting in global pool.", g_thread_self ())); in g_thread_pool_wait_for_new_pool()
190 pool = g_async_queue_pop (unused_thread_queue); in g_thread_pool_wait_for_new_pool()
193 if (pool == wakeup_thread_marker) in g_thread_pool_wait_for_new_pool()
221 pool = NULL; in g_thread_pool_wait_for_new_pool()
236 while (pool == wakeup_thread_marker); in g_thread_pool_wait_for_new_pool()
240 return pool; in g_thread_pool_wait_for_new_pool()
244 g_thread_pool_wait_for_new_task (GRealThreadPool *pool) in g_thread_pool_wait_for_new_task() argument
248 if (pool->running || (!pool->immediate && in g_thread_pool_wait_for_new_task()
249 g_async_queue_length_unlocked (pool->queue) > 0)) in g_thread_pool_wait_for_new_task()
251 /* This thread pool is still active. */ in g_thread_pool_wait_for_new_task()
252 if (pool->max_threads != -1 && pool->num_threads > (guint) pool->max_threads) in g_thread_pool_wait_for_new_task()
254 /* This is a superfluous thread, so it goes to the global pool. */ in g_thread_pool_wait_for_new_task()
255 DEBUG_MSG (("superfluous thread %p in pool %p.", in g_thread_pool_wait_for_new_task()
256 g_thread_self (), pool)); in g_thread_pool_wait_for_new_task()
258 else if (pool->pool.exclusive) in g_thread_pool_wait_for_new_task()
260 /* Exclusive threads stay attached to the pool. */ in g_thread_pool_wait_for_new_task()
261 task = g_async_queue_pop_unlocked (pool->queue); in g_thread_pool_wait_for_new_task()
263 DEBUG_MSG (("thread %p in exclusive pool %p waits for task " in g_thread_pool_wait_for_new_task()
265 g_thread_self (), pool, pool->num_threads, in g_thread_pool_wait_for_new_task()
266 g_async_queue_length_unlocked (pool->queue))); in g_thread_pool_wait_for_new_task()
271 * second before going to the global pool. in g_thread_pool_wait_for_new_task()
273 DEBUG_MSG (("thread %p in pool %p waits for up to a 1/2 second for task " in g_thread_pool_wait_for_new_task()
275 g_thread_self (), pool, pool->num_threads, in g_thread_pool_wait_for_new_task()
276 g_async_queue_length_unlocked (pool->queue))); in g_thread_pool_wait_for_new_task()
278 task = g_async_queue_timeout_pop_unlocked (pool->queue, in g_thread_pool_wait_for_new_task()
284 /* This thread pool is inactive, it will no longer process tasks. */ in g_thread_pool_wait_for_new_task()
285 DEBUG_MSG (("pool %p not active, thread %p will go to global pool " in g_thread_pool_wait_for_new_task()
287 pool, g_thread_self (), in g_thread_pool_wait_for_new_task()
288 pool->running ? "true" : "false", in g_thread_pool_wait_for_new_task()
289 pool->immediate ? "true" : "false", in g_thread_pool_wait_for_new_task()
290 g_async_queue_length_unlocked (pool->queue))); in g_thread_pool_wait_for_new_task()
305 gchar name[16] = "pool"; in g_thread_pool_spawn_thread()
308 g_snprintf (name, sizeof (name), "pool-%s", prgname); in g_thread_pool_spawn_thread()
311 /* Spawn a new thread for the given pool and wake the requesting thread in g_thread_pool_spawn_thread()
314 * that created the first non-exclusive thread-pool. */ in g_thread_pool_spawn_thread()
316 thread = g_thread_try_new (name, g_thread_pool_thread_proxy, spawn_thread_data->pool, &error); in g_thread_pool_spawn_thread()
331 GRealThreadPool *pool; in g_thread_pool_thread_proxy() local
333 pool = data; in g_thread_pool_thread_proxy()
335 DEBUG_MSG (("thread %p started for pool %p.", g_thread_self (), pool)); in g_thread_pool_thread_proxy()
337 g_async_queue_lock (pool->queue); in g_thread_pool_thread_proxy()
343 task = g_thread_pool_wait_for_new_task (pool); in g_thread_pool_thread_proxy()
346 if (pool->running || !pool->immediate) in g_thread_pool_thread_proxy()
348 /* A task was received and the thread pool is active, in g_thread_pool_thread_proxy()
351 g_async_queue_unlock (pool->queue); in g_thread_pool_thread_proxy()
352 DEBUG_MSG (("thread %p in pool %p calling func.", in g_thread_pool_thread_proxy()
353 g_thread_self (), pool)); in g_thread_pool_thread_proxy()
354 pool->pool.func (task, pool->pool.user_data); in g_thread_pool_thread_proxy()
355 g_async_queue_lock (pool->queue); in g_thread_pool_thread_proxy()
360 /* No task was received, so this thread goes to the global pool. */ in g_thread_pool_thread_proxy()
363 DEBUG_MSG (("thread %p leaving pool %p for global pool.", in g_thread_pool_thread_proxy()
364 g_thread_self (), pool)); in g_thread_pool_thread_proxy()
365 pool->num_threads--; in g_thread_pool_thread_proxy()
367 if (!pool->running) in g_thread_pool_thread_proxy()
369 if (!pool->waiting) in g_thread_pool_thread_proxy()
371 if (pool->num_threads == 0) in g_thread_pool_thread_proxy()
373 /* If the pool is not running and no other in g_thread_pool_thread_proxy()
374 * thread is waiting for this thread pool to in g_thread_pool_thread_proxy()
376 * pool, free the pool. in g_thread_pool_thread_proxy()
382 /* If the pool is not running and no other in g_thread_pool_thread_proxy()
383 * thread is waiting for this thread pool to in g_thread_pool_thread_proxy()
385 * this pool and there are no tasks left in the in g_thread_pool_thread_proxy()
388 if (g_async_queue_length_unlocked (pool->queue) == in g_thread_pool_thread_proxy()
389 (gint) -pool->num_threads) in g_thread_pool_thread_proxy()
390 g_thread_pool_wakeup_and_stop_all (pool); in g_thread_pool_thread_proxy()
393 else if (pool->immediate || in g_thread_pool_thread_proxy()
394 g_async_queue_length_unlocked (pool->queue) <= 0) in g_thread_pool_thread_proxy()
396 /* If the pool is not running and another thread is in g_thread_pool_thread_proxy()
397 * waiting for this thread pool to finish and there in g_thread_pool_thread_proxy()
398 * are either no tasks left or the pool shall stop in g_thread_pool_thread_proxy()
400 * of the thread pool state. in g_thread_pool_thread_proxy()
402 g_cond_broadcast (&pool->cond); in g_thread_pool_thread_proxy()
406 g_async_queue_unlock (pool->queue); in g_thread_pool_thread_proxy()
409 g_thread_pool_free_internal (pool); in g_thread_pool_thread_proxy()
411 if ((pool = g_thread_pool_wait_for_new_pool ()) == NULL) in g_thread_pool_thread_proxy()
414 g_async_queue_lock (pool->queue); in g_thread_pool_thread_proxy()
416 DEBUG_MSG (("thread %p entering pool %p from global pool.", in g_thread_pool_thread_proxy()
417 g_thread_self (), pool)); in g_thread_pool_thread_proxy()
419 /* pool->num_threads++ is not done here, but in in g_thread_pool_thread_proxy()
421 * thread known to the pool before itself can do it. in g_thread_pool_thread_proxy()
430 g_thread_pool_start_thread (GRealThreadPool *pool, in g_thread_pool_start_thread() argument
435 if (pool->max_threads != -1 && pool->num_threads >= (guint) pool->max_threads) in g_thread_pool_start_thread()
443 g_async_queue_push_unlocked (unused_thread_queue, pool); in g_thread_pool_start_thread()
452 gchar name[16] = "pool"; in g_thread_pool_start_thread()
456 g_snprintf (name, sizeof (name), "pool-%s", prgname); in g_thread_pool_start_thread()
459 if (pool->pool.exclusive) in g_thread_pool_start_thread()
465 thread = g_thread_try_new (name, g_thread_pool_thread_proxy, pool, error); in g_thread_pool_start_thread()
477 … g_thread_new_internal (name, g_thread_proxy, g_thread_pool_thread_proxy, pool, 0, &shared_thread_… in g_thread_pool_start_thread()
481 SpawnThreadData spawn_thread_data = { (GThreadPool *) pool, NULL, NULL }; in g_thread_pool_start_thread()
506 pool->num_threads++; in g_thread_pool_start_thread()
513 * @func: a function to execute in the threads of the new thread pool
517 * in the new thread pool, -1 means no limit
518 * @exclusive: should this thread pool be exclusive?
521 * This function creates a new thread pool.
525 * are running concurrently for this thread pool. @max_threads = -1
526 * allows unlimited threads to be created for this thread pool. The
535 * The parameter @exclusive determines whether the thread pool owns
538 * immediately and they will run exclusively for this thread pool
545 * global pool.
571 retval->pool.func = func; in g_thread_pool_new()
572 retval->pool.user_data = user_data; in g_thread_pool_new()
573 retval->pool.exclusive = exclusive; in g_thread_pool_new()
588 /* For the very first non-exclusive thread-pool we remember the thread in g_thread_pool_new()
589 * scheduler settings of the thread creating the pool, if supported by in g_thread_pool_new()
591 * all threads created on the non-exclusive thread-pool have the same in g_thread_pool_new()
620 g_thread_new ("pool-spawner", g_thread_pool_spawn_thread, NULL); in g_thread_pool_new()
625 if (retval->pool.exclusive) in g_thread_pool_new()
648 * @pool: a #GThreadPool
649 * @data: a new task for @pool
652 * Inserts @data into the list of tasks to be executed by @pool.
657 * Otherwise, @data stays in the queue until a thread in this pool
670 g_thread_pool_push (GThreadPool *pool, in g_thread_pool_push() argument
677 real = (GRealThreadPool*) pool; in g_thread_pool_push()
706 * @pool: a #GThreadPool
707 * @max_threads: a new maximal number of threads for @pool,
711 * Sets the maximal allowed number of threads for @pool.
713 * is unlimited. If @pool is an exclusive thread pool, setting
716 * Setting @max_threads to 0 means stopping all work for @pool.
724 * running threads in @pool is smaller than the maximal number.
735 g_thread_pool_set_max_threads (GThreadPool *pool, in g_thread_pool_set_max_threads() argument
743 real = (GRealThreadPool*) pool; in g_thread_pool_set_max_threads()
747 g_return_val_if_fail (!real->pool.exclusive || max_threads != -1, FALSE); in g_thread_pool_set_max_threads()
756 if (pool->exclusive) in g_thread_pool_set_max_threads()
780 * @pool: a #GThreadPool
782 * Returns the maximal number of threads for @pool.
787 g_thread_pool_get_max_threads (GThreadPool *pool) in g_thread_pool_get_max_threads() argument
792 real = (GRealThreadPool*) pool; in g_thread_pool_get_max_threads()
806 * @pool: a #GThreadPool
808 * Returns the number of threads currently running in @pool.
813 g_thread_pool_get_num_threads (GThreadPool *pool) in g_thread_pool_get_num_threads() argument
818 real = (GRealThreadPool*) pool; in g_thread_pool_get_num_threads()
832 * @pool: a #GThreadPool
834 * Returns the number of tasks still unprocessed in @pool.
839 g_thread_pool_unprocessed (GThreadPool *pool) in g_thread_pool_unprocessed() argument
844 real = (GRealThreadPool*) pool; in g_thread_pool_unprocessed()
856 * @pool: a #GThreadPool
857 * @immediate: should @pool shut down immediately?
860 * Frees all resources allocated for @pool.
862 * If @immediate is %TRUE, no new task is processed for @pool.
863 * Otherwise @pool is not freed before the last task is processed.
864 * Note however, that no thread of this pool is interrupted while
866 * can finish their tasks before the @pool is freed.
873 * After calling this function @pool must not be used anymore.
876 g_thread_pool_free (GThreadPool *pool, in g_thread_pool_free() argument
882 real = (GRealThreadPool*) pool; in g_thread_pool_free()
888 * not stopping this pool immediately, when it's not empty in g_thread_pool_free()
923 /* The last thread should cleanup the pool */ in g_thread_pool_free()
929 g_thread_pool_free_internal (GRealThreadPool* pool) in g_thread_pool_free_internal() argument
931 g_return_if_fail (pool); in g_thread_pool_free_internal()
932 g_return_if_fail (pool->running == FALSE); in g_thread_pool_free_internal()
933 g_return_if_fail (pool->num_threads == 0); in g_thread_pool_free_internal()
935 g_async_queue_unref (pool->queue); in g_thread_pool_free_internal()
936 g_cond_clear (&pool->cond); in g_thread_pool_free_internal()
938 g_free (pool); in g_thread_pool_free_internal()
942 g_thread_pool_wakeup_and_stop_all (GRealThreadPool *pool) in g_thread_pool_wakeup_and_stop_all() argument
946 g_return_if_fail (pool); in g_thread_pool_wakeup_and_stop_all()
947 g_return_if_fail (pool->running == FALSE); in g_thread_pool_wakeup_and_stop_all()
948 g_return_if_fail (pool->num_threads != 0); in g_thread_pool_wakeup_and_stop_all()
950 pool->immediate = TRUE; in g_thread_pool_wakeup_and_stop_all()
953 * So here we're sending bogus data to the pool threads, which in g_thread_pool_wakeup_and_stop_all()
955 * pool->immediate condition. However we don't want that in g_thread_pool_wakeup_and_stop_all()
958 for (i = 0; i < pool->num_threads; i++) in g_thread_pool_wakeup_and_stop_all()
959 g_async_queue_push_unlocked (pool->queue, GUINT_TO_POINTER (1)); in g_thread_pool_wakeup_and_stop_all()
1047 * @pool: a #GThreadPool
1058 * just in the order in which they were added to the pool.
1069 g_thread_pool_set_sort_function (GThreadPool *pool, in g_thread_pool_set_sort_function() argument
1075 real = (GRealThreadPool*) pool; in g_thread_pool_set_sort_function()
1095 * @pool: a #GThreadPool
1096 * @data: an unprocessed item in the pool
1106 g_thread_pool_move_to_front (GThreadPool *pool, in g_thread_pool_move_to_front() argument
1109 GRealThreadPool *real = (GRealThreadPool*) pool; in g_thread_pool_move_to_front()
1129 * waiting in the pool for new tasks can be idle for before
1168 * thread will wait in the thread pool for new tasks before
1172 * pool for new work are not stopped.
1175 * for new tasks in the thread pool before stopping the