![]() |
Nilorea Library
C utilities for networking, threading, graphics
|
Thread pool functions. More...
#include <unistd.h>#include "nilorea/n_common.h"#include "nilorea/n_log.h"#include "nilorea/n_thread_pool.h"#include "nilorea/n_time.h"#include <pthread.h>#include <string.h>#include <errno.h>Go to the source code of this file.
Data Structures | |
| struct | N_PARALLEL_SLICE |
| One slice of a parallel loop, as handed to its thread. More... | |
Functions | |
| int | add_threaded_process (THREAD_POOL *thread_pool, void *(*func_ptr)(void *param), void *param, int mode) |
| add a function and params to a thread pool | |
| int | destroy_threaded_pool (THREAD_POOL **pool, unsigned int delay) |
| Stop every thread of a pool and free it. | |
| long int | get_nb_cpu_cores () |
| get number of core of current system | |
| int | n_parallel_for (size_t begin, size_t end, size_t grain, N_PARALLEL_FN fn, void *user) |
| Run a loop over a range on several cores, and return when all of it is done. | |
| static void * | n_parallel_slice (void *arg) |
| Thread entry of one slice of a parallel loop. | |
| THREAD_POOL * | new_thread_pool (size_t nbmaxthr, size_t nb_max_waiting) |
| Create a new pool of nbmaxthr threads. | |
| int | refresh_thread_pool (THREAD_POOL *thread_pool) |
| try to add some waiting DIRECT_PROCs on some free thread slots, else do nothing | |
| int | set_thread_pool_discard (THREAD_POOL *thread_pool, void(*discard)(void *param)) |
| Set what destroy_threaded_pool() does with the param of a queued job it discards. | |
| int | start_threaded_pool (THREAD_POOL *thread_pool) |
| Launch the process waiting for execution in the thread pool. | |
| void * | thread_pool_processing_function (void *param) |
| Internal thread pool processing function. | |
| int | wait_for_synced_threaded_pool (THREAD_POOL *thread_pool) |
| wait for all the launched process, blocking but light on the CPU as there is no polling | |
| int | wait_for_threaded_pool (THREAD_POOL *thread_pool) |
| Wait for the thread pool to become idle (no active threads, empty waiting list), blocking without polling. | |
Variables | |
| static long | parallel_budget = -1 |
| get number of core of current system | |
| static pthread_mutex_t | parallel_lock = PTHREAD_MUTEX_INITIALIZER |
| Guards parallel_budget. | |
| struct N_PARALLEL_SLICE |
One slice of a parallel loop, as handed to its thread.
Definition at line 53 of file n_thread_pool.c.
| Data Fields | ||
|---|---|---|
| size_t | begin | first index of the slice |
| size_t | end | one past its last |
| N_PARALLEL_FN | fn | the work |
| void * | user | passed to the work |
|
static |
Thread entry of one slice of a parallel loop.
| arg | the N_PARALLEL_SLICE |
Definition at line 69 of file n_thread_pool.c.
References N_PARALLEL_SLICE::begin, N_PARALLEL_SLICE::end, N_PARALLEL_SLICE::fn, and N_PARALLEL_SLICE::user.
Referenced by n_parallel_for().
| void * thread_pool_processing_function | ( | void * | param | ) |
Internal thread pool processing function.
| param | params of a threaded process |
Definition at line 156 of file n_thread_pool.c.
References EXITED_THREAD, EXITING_THREAD, THREAD_POOL_NODE::func, IDLE_PROC, THREAD_POOL_NODE::lock, LOG_DEBUG, LOG_ERR, n_log, THREAD_POOL_NODE::param, refresh_thread_pool(), RUNNING_PROC, RUNNING_THREAD, THREAD_POOL_NODE::state, SYNCED_PROC, THREAD_POOL_NODE::th_end, THREAD_POOL_NODE::th_start, THREAD_POOL_NODE::thr, THREAD_POOL_NODE::thread_pool, THREAD_POOL_NODE::thread_state, THREAD_POOL_NODE::type, and type().
Referenced by new_thread_pool().
|
static |
get number of core of current system
Extra threads n_parallel_for() may have running at once, across every caller; -1 until the first call sizes it from the core count.
Definition at line 47 of file n_thread_pool.c.
Referenced by n_parallel_for().
|
static |
Guards parallel_budget.
Definition at line 50 of file n_thread_pool.c.
Referenced by n_parallel_for().