Nilorea Library
C utilities for networking, threading, graphics
THREADS: tools to create and manage a thread pool

Data Structures

struct  THREAD_POOL
 Structure of a trhead pool. More...
 
struct  THREAD_POOL_NODE
 A thread pool node. More...
 
struct  THREAD_WAITING_PROC
 Structure of a waiting process item. More...
 

Macros

#define DIRECT_PROC   2
 processing mode for added func, direct start More...
 
#define EXITED_THREAD   256
 indicate that the pool is off, all jobs have been consumed More...
 
#define EXITING_THREAD   128
 indicate that the pool is exiting, unfinished jobs will finish and the pool will exit the threads and enter the EXITED state More...
 
#define IDLE_PROC   8
 status of a thread which is waiting for some proc More...
 
#define NO_LOCK   512
 if passed to add_threaded_process, skip main table lock in case we are in a func which is already locking it More...
 
#define NOQUEUE   4
 processing mode for waiting_list: do not readd the work in queue More...
 
#define RUNNING_PROC   32
 status of a thread which proc is currently running More...
 
#define RUNNING_THREAD   64
 indicate that the pool is running and ready to use More...
 
#define SYNCED_PROC   1
 processing mode for added func, synced start More...
 
#define WAITING_PROC   16
 status of a thread who have proc waiting to be processed
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 More...
 
int destroy_threaded_pool (THREAD_POOL **thread_pool, int delay)
 delete a thread_pool, exit the threads and free the structs More...
 
int get_nb_cpu_cores ()
 get number of core of current system More...
 
THREAD_POOLnew_thread_pool (int nbmaxthr, int nb_max_waiting)
 Create a new pool of nbmaxthr threads. More...
 
int refresh_thread_pool (THREAD_POOL *thread_pool)
 try to add some waiting DIRECT_PROCs on some free thread slots, else do nothing More...
 
int start_threaded_pool (THREAD_POOL *thread_pool)
 Launch the process waiting for exectution in the thread pool. More...
 
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 More...
 
int wait_for_threaded_pool (THREAD_POOL *thread_pool, int delay)
 Wait for all the launched process in the thread pool to terminate. More...
 

Detailed Description


Data Structure Documentation

◆ THREAD_POOL

struct THREAD_POOL

Structure of a trhead pool.

Examples
ex_fluid.c, ex_network.c, and ex_threads.c.

Definition at line 80 of file n_thread_pool.h.

+ Collaboration diagram for THREAD_POOL:
Data Fields
pthread_mutex_t lock mutex to prevent mutual access of waiting_list parameters
int max_threads Maximum number of running threads in the list.
int nb_actives number of threads actually doing a proc
int nb_max_waiting Maximum number of waiting procedures int the list, 0 or -1 for unlimited.
sem_t nb_tasks semaphore to store the number of tasks
THREAD_POOL_NODE ** thread_list Dynamically allocated but fixed size thread array.
LIST * waiting_list Waiting list handling.

◆ THREAD_POOL_NODE

struct THREAD_POOL_NODE

A thread pool node.

Definition at line 48 of file n_thread_pool.h.

+ Collaboration diagram for THREAD_POOL_NODE:

Data Fields

void *(* func )(void *param)
 function to call in the thread More...
 
pthread_mutex_t lock
 mutex to prevent mutual access of node parameters More...
 
void * param
 if not NULL , passed as argument More...
 
int state
 state of the proc , RUNNING_PROC when it is busy processing func( param) , IDLE_PROC when it waits for some func and param to process , WAITING_PROC when it has things waiting to be processed More...
 
sem_t th_end
 thread ending semaphore More...
 
sem_t th_start
 thread starting semaphore More...
 
pthread_t thr
 thread id More...
 
struct THREAD_POOLthread_pool
 pointer to assigned thread pool More...
 
int thread_state
 state of the managing thread , RUNNING_THREAD, EXITING_THREAD, EXITED_THREAD More...
 
int type
 SYNCED or DIRECT process start. More...
 

Field Documentation

◆ func

void *(* THREAD_POOL_NODE::func) (void *param)

function to call in the thread

Definition at line 51 of file n_thread_pool.h.

◆ lock

pthread_mutex_t THREAD_POOL_NODE::lock

mutex to prevent mutual access of node parameters

Definition at line 71 of file n_thread_pool.h.

◆ param

void* THREAD_POOL_NODE::param

if not NULL , passed as argument

Definition at line 54 of file n_thread_pool.h.

◆ state

int THREAD_POOL_NODE::state

state of the proc , RUNNING_PROC when it is busy processing func( param) , IDLE_PROC when it waits for some func and param to process , WAITING_PROC when it has things waiting to be processed

Definition at line 59 of file n_thread_pool.h.

◆ th_end

sem_t THREAD_POOL_NODE::th_end

thread ending semaphore

Definition at line 68 of file n_thread_pool.h.

◆ th_start

sem_t THREAD_POOL_NODE::th_start

thread starting semaphore

Definition at line 66 of file n_thread_pool.h.

◆ thr

pthread_t THREAD_POOL_NODE::thr

thread id

Definition at line 63 of file n_thread_pool.h.

◆ thread_pool

struct THREAD_POOL* THREAD_POOL_NODE::thread_pool

pointer to assigned thread pool

Definition at line 74 of file n_thread_pool.h.

◆ thread_state

int THREAD_POOL_NODE::thread_state

state of the managing thread , RUNNING_THREAD, EXITING_THREAD, EXITED_THREAD

Definition at line 61 of file n_thread_pool.h.

◆ type

int THREAD_POOL_NODE::type

SYNCED or DIRECT process start.

Definition at line 57 of file n_thread_pool.h.

◆ THREAD_WAITING_PROC

struct THREAD_WAITING_PROC

Structure of a waiting process item.

Definition at line 106 of file n_thread_pool.h.

+ Collaboration diagram for THREAD_WAITING_PROC:

Data Fields

void *(* func )(void *param)
 function to call in the thread More...
 
void * param
 if not NULL , passed as argument More...
 

Field Documentation

◆ func

void *(* THREAD_WAITING_PROC::func) (void *param)

function to call in the thread

Definition at line 109 of file n_thread_pool.h.

◆ param

void* THREAD_WAITING_PROC::param

if not NULL , passed as argument

Definition at line 111 of file n_thread_pool.h.

Macro Definition Documentation

◆ DIRECT_PROC

#define DIRECT_PROC   2

processing mode for added func, direct start

Examples
ex_network.c, and ex_threads.c.

Definition at line 29 of file n_thread_pool.h.

◆ EXITED_THREAD

#define EXITED_THREAD   256

indicate that the pool is off, all jobs have been consumed

Definition at line 43 of file n_thread_pool.h.

◆ EXITING_THREAD

#define EXITING_THREAD   128

indicate that the pool is exiting, unfinished jobs will finish and the pool will exit the threads and enter the EXITED state

Definition at line 41 of file n_thread_pool.h.

◆ IDLE_PROC

#define IDLE_PROC   8

status of a thread which is waiting for some proc

Definition at line 33 of file n_thread_pool.h.

◆ NO_LOCK

#define NO_LOCK   512

if passed to add_threaded_process, skip main table lock in case we are in a func which is already locking it

Definition at line 45 of file n_thread_pool.h.

◆ NOQUEUE

#define NOQUEUE   4

processing mode for waiting_list: do not readd the work in queue

Definition at line 31 of file n_thread_pool.h.

◆ RUNNING_PROC

#define RUNNING_PROC   32

status of a thread which proc is currently running

Definition at line 37 of file n_thread_pool.h.

◆ RUNNING_THREAD

#define RUNNING_THREAD   64

indicate that the pool is running and ready to use

Definition at line 39 of file n_thread_pool.h.

◆ SYNCED_PROC

#define SYNCED_PROC   1

processing mode for added func, synced start

Definition at line 27 of file n_thread_pool.h.

◆ WAITING_PROC

#define WAITING_PROC   16

status of a thread who have proc waiting to be processed

Definition at line 35 of file n_thread_pool.h.

Function Documentation

◆ add_threaded_process()

int add_threaded_process ( THREAD_POOL thread_pool,
void *(*)(void *param)  func_ptr,
void *  param,
int  mode 
)

add a function and params to a thread pool

Parameters
thread_poolThe target thread pool
func_ptrThe function pointer to launch
paramEventual parameter struct to pass to the function
modeSYNCED_PROC: added to pool, but wait for a start call with other waiting procs. DIRECT_PROC: the function actually starts as quick as possible
Returns
TRUE or FALSE

Definition at line 205 of file n_thread_pool.c.

References DIRECT_PROC, IDLE_PROC, list_push(), LOG_DEBUG, LOG_ERR, Malloc, n_log, NO_LOCK, NOQUEUE, RUNNING_THREAD, SYNCED_PROC, and WAITING_PROC.

Referenced by n_fluid_simulate_threaded(), n_vigenere_cypher(), and refresh_thread_pool().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ destroy_threaded_pool()

int destroy_threaded_pool ( THREAD_POOL **  pool,
int  delay 
)

delete a thread_pool, exit the threads and free the structs

Parameters
poolThe THREAD_POOL *object to kill
delayThe THREAD_POOL *object to kill
Returns
TRUE or FALSE

Definition at line 403 of file n_thread_pool.c.

References __n_assert, EXITING_THREAD, Free, IDLE_PROC, list_destroy(), and u_sleep().

Referenced by n_vigenere_cypher().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ get_nb_cpu_cores()

int get_nb_cpu_cores ( )

get number of core of current system

Returns
The number of cores or -1 if the system command is not supported

Definition at line 27 of file n_thread_pool.c.

Referenced by new_n_fluid().

+ Here is the caller graph for this function:

◆ new_thread_pool()

THREAD_POOL * new_thread_pool ( int  nbmaxthr,
int  nb_max_waiting 
)

Create a new pool of nbmaxthr threads.

Parameters
nbmaxthrnumber of active threads in the pool
nb_max_waitingmax number of waiting procs in the pool. Negative or zero value for no limit
Returns
NULL or a new trhead pool object

Definition at line 125 of file n_thread_pool.c.

References Free, IDLE_PROC, LOG_ERR, Malloc, n_log, new_generic_list(), RUNNING_THREAD, and thread_pool_processing_function().

Referenced by n_vigenere_cypher().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ refresh_thread_pool()

int refresh_thread_pool ( THREAD_POOL thread_pool)

try to add some waiting DIRECT_PROCs on some free thread slots, else do nothing

Parameters
thread_poolThe thread pool to refresh
Returns
TRUE or FALSE

Definition at line 469 of file n_thread_pool.c.

References __n_assert, add_threaded_process(), DIRECT_PROC, Free, NO_LOCK, NOQUEUE, remove_list_node, and RUNNING_PROC.

Referenced by n_fluid_simulate_threaded(), n_vigenere_cypher(), thread_pool_processing_function(), and wait_for_threaded_pool().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ start_threaded_pool()

int start_threaded_pool ( THREAD_POOL thread_pool)

Launch the process waiting for exectution in the thread pool.

Parameters
thread_poolThe thread pool to launche
Returns
TRUE or FALSE

Definition at line 286 of file n_thread_pool.c.

References LOG_ERR, n_log, SYNCED_PROC, and WAITING_PROC.

Referenced by n_fluid_simulate_threaded().

+ Here is the caller graph for this function:

◆ wait_for_synced_threaded_pool()

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

Parameters
thread_poolThe thread pool to wait
Returns
TRUE or FALSE

Definition at line 326 of file n_thread_pool.c.

References __n_assert, LOG_ERR, and n_log.

Referenced by n_fluid_simulate_threaded().

+ Here is the caller graph for this function:

◆ wait_for_threaded_pool()

int wait_for_threaded_pool ( THREAD_POOL thread_pool,
int  delay 
)

Wait for all the launched process in the thread pool to terminate.

Parameters
thread_poolThe thread pool to wait
delaytime between each check
Returns
TRUE or FALSE

Definition at line 352 of file n_thread_pool.c.

References IDLE_PROC, refresh_thread_pool(), and u_sleep().

Referenced by n_vigenere_cypher().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: