Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
THREADS: tools to create and manage a thread pool

Data Structures

struct  THREAD_POOL
 Structure of a thread 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   4
 processing mode for added func, direct start, not queued
 
#define EXITED_THREAD   512
 indicate that the pool is off, all jobs have been consumed
 
#define EXITING_THREAD   256
 indicate that the pool is exiting, unfinished jobs will finish and the pool will exit the threads and enter the EXITED state
 
#define IDLE_PROC   16
 status of a thread which is waiting for some proc
 
#define NO_LOCK   1024
 if passed to add_threaded_process, skip main table lock in case we are in a func which is already locking it
 
#define NO_QUEUE   8
 special processing mode for waiting_list: do not add the work in queue since it' coming from the queue
 
#define NORMAL_PROC   1
 processing mode for added func, synced start, can be queued
 
#define RUNNING_PROC   64
 status of a thread which proc is currently running
 
#define RUNNING_THREAD   128
 indicate that the pool is running and ready to use
 
#define SYNCED_PROC   2
 processing mode for added func, synced start, not queued
 
#define WAITING_PROC   32
 status of a thread who have proc waiting to be processed

 

Functions

int add_threaded_process (THREAD_POOL *thread_pool, void *(*func_ptr)(void *param), void *param, int mode)
 add a function to run in an available thread inside a pool
 
int destroy_threaded_pool (THREAD_POOL **thread_pool, unsigned int delay)
 destroy all running threads
 
long int get_nb_cpu_cores ()
 get number of core of current system
 
THREAD_POOLnew_thread_pool (size_t nbmaxthr, size_t nb_max_waiting)
 allocate a new thread pool
 
int refresh_thread_pool (THREAD_POOL *thread_pool)
 try to add some waiting process on some free thread slots, else do nothing
 
int start_threaded_pool (THREAD_POOL *thread_pool)
 tell all the waiting threads to start their associated process
 
int wait_for_synced_threaded_pool (THREAD_POOL *thread_pool)
 wait for all the threads in the pool to terminate processing, blocking but light on the CPU as there is no polling
 
int wait_for_threaded_pool (THREAD_POOL *thread_pool)
 wait for the pool to become idle (no active threads, empty waiting list), blocking without polling
 

Detailed Description


Data Structure Documentation

◆ THREAD_POOL

struct THREAD_POOL
+ Collaboration diagram for THREAD_POOL:
Data Fields
pthread_mutex_t lock mutex to prevent mutual access of waiting_list parameters
size_t max_threads Maximum number of running threads in the list.
size_t nb_actives number of threads actually doing a proc
size_t nb_max_waiting Maximum number of waiting procedures in the list, 0 for unlimited.
sem_t nb_tasks semaphore signaling pool idle state: value 0 = work in progress, value 1 = pool is idle (no active threads, empty waiting list).

Used by wait_for_threaded_pool() to block until idle.

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 66 of file n_thread_pool.h.

+ Collaboration diagram for THREAD_POOL_NODE:

Data Fields

void *(* func )(void *param)
 function to call in the thread
 
pthread_mutex_t lock
 mutex to prevent mutual access of node parameters
 
void * param
 if not NULL , passed as argument
 
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
 
sem_t th_end
 thread ending semaphore
 
sem_t th_start
 thread starting semaphore
 
pthread_t thr
 thread id
 
struct THREAD_POOLthread_pool
 pointer to assigned thread pool
 
int thread_state
 state of the managing thread , RUNNING_THREAD, EXITING_THREAD, EXITED_THREAD
 
int type
 SYNCED or DIRECT process start.
 

Field Documentation

◆ func

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

function to call in the thread

Definition at line 68 of file n_thread_pool.h.

Referenced by add_threaded_process(), new_thread_pool(), and thread_pool_processing_function().

◆ lock

pthread_mutex_t THREAD_POOL_NODE::lock

mutex to prevent mutual access of node parameters

Definition at line 88 of file n_thread_pool.h.

Referenced by add_threaded_process(), new_thread_pool(), refresh_thread_pool(), start_threaded_pool(), thread_pool_processing_function(), and wait_for_synced_threaded_pool().

◆ param

void* THREAD_POOL_NODE::param

if not NULL , passed as argument

Definition at line 71 of file n_thread_pool.h.

Referenced by add_threaded_process(), new_thread_pool(), and thread_pool_processing_function().

◆ 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 76 of file n_thread_pool.h.

Referenced by add_threaded_process(), new_thread_pool(), refresh_thread_pool(), start_threaded_pool(), and thread_pool_processing_function().

◆ th_end

sem_t THREAD_POOL_NODE::th_end

thread ending semaphore

Definition at line 85 of file n_thread_pool.h.

Referenced by new_thread_pool(), thread_pool_processing_function(), and wait_for_synced_threaded_pool().

◆ th_start

sem_t THREAD_POOL_NODE::th_start

thread starting semaphore

Definition at line 83 of file n_thread_pool.h.

Referenced by add_threaded_process(), new_thread_pool(), start_threaded_pool(), and thread_pool_processing_function().

◆ thr

pthread_t THREAD_POOL_NODE::thr

thread id

Definition at line 80 of file n_thread_pool.h.

Referenced by new_thread_pool(), and thread_pool_processing_function().

◆ thread_pool

struct THREAD_POOL* THREAD_POOL_NODE::thread_pool

pointer to assigned thread pool

Definition at line 91 of file n_thread_pool.h.

Referenced by new_thread_pool(), and thread_pool_processing_function().

◆ thread_state

int THREAD_POOL_NODE::thread_state

state of the managing thread , RUNNING_THREAD, EXITING_THREAD, EXITED_THREAD

Definition at line 78 of file n_thread_pool.h.

Referenced by add_threaded_process(), new_thread_pool(), and thread_pool_processing_function().

◆ type

int THREAD_POOL_NODE::type

◆ THREAD_WAITING_PROC

struct THREAD_WAITING_PROC

Structure of a waiting process item.

Definition at line 119 of file n_thread_pool.h.

+ Collaboration diagram for THREAD_WAITING_PROC:

Data Fields

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

Field Documentation

◆ func

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

function to call in the thread

Definition at line 121 of file n_thread_pool.h.

Referenced by add_threaded_process(), and refresh_thread_pool().

◆ param

void* THREAD_WAITING_PROC::param

if not NULL , passed as argument

Definition at line 123 of file n_thread_pool.h.

Referenced by add_threaded_process(), and refresh_thread_pool().

Macro Definition Documentation

◆ DIRECT_PROC

#define DIRECT_PROC   4

processing mode for added func, direct start, not queued

Examples
ex_network.c, ex_network_ssl.c, and ex_network_ssl_hardened.c.

Definition at line 47 of file n_thread_pool.h.

◆ EXITED_THREAD

#define EXITED_THREAD   512

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

Definition at line 61 of file n_thread_pool.h.

◆ EXITING_THREAD

#define EXITING_THREAD   256

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 59 of file n_thread_pool.h.

◆ IDLE_PROC

#define IDLE_PROC   16

status of a thread which is waiting for some proc

Definition at line 51 of file n_thread_pool.h.

◆ NO_LOCK

#define NO_LOCK   1024

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 63 of file n_thread_pool.h.

◆ NO_QUEUE

#define NO_QUEUE   8

special processing mode for waiting_list: do not add the work in queue since it' coming from the queue

Definition at line 49 of file n_thread_pool.h.

◆ NORMAL_PROC

#define NORMAL_PROC   1

processing mode for added func, synced start, can be queued

Examples
ex_accept_pool_client.c, ex_accept_pool_server.c, and ex_threads.c.

Definition at line 43 of file n_thread_pool.h.

◆ RUNNING_PROC

#define RUNNING_PROC   64

status of a thread which proc is currently running

Definition at line 55 of file n_thread_pool.h.

◆ RUNNING_THREAD

#define RUNNING_THREAD   128

indicate that the pool is running and ready to use

Definition at line 57 of file n_thread_pool.h.

◆ SYNCED_PROC

#define SYNCED_PROC   2

processing mode for added func, synced start, not queued

Examples
ex_threads.c.

Definition at line 45 of file n_thread_pool.h.

◆ WAITING_PROC

#define WAITING_PROC   32

status of a thread who have proc waiting to be processed

Definition at line 53 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 to run in an available thread inside a pool

add a function to run in an available thread inside a pool

Parameters
thread_poolThe target thread pool
func_ptrThe function pointer to launch
paramEventual parameter struct to pass to the function
modeNORMAL_PROC: add to an available active thread or queue in the waiting list. SYNCED_PROC: add to an available thread and wait for a start call, no waiting list. DIRECT_PROC: add to an available thread, no waiting list.
Returns
TRUE or FALSE

Definition at line 235 of file n_thread_pool.c.

References DIRECT_PROC, THREAD_POOL_NODE::func, THREAD_WAITING_PROC::func, IDLE_PROC, list_push(), THREAD_POOL_NODE::lock, THREAD_POOL::lock, LOG_DEBUG, LOG_ERR, Malloc, mode, n_log, LIST::nb_items, THREAD_POOL::nb_max_waiting, THREAD_POOL::nb_tasks, NO_LOCK, NO_QUEUE, NORMAL_PROC, THREAD_POOL_NODE::param, THREAD_WAITING_PROC::param, RUNNING_THREAD, THREAD_POOL_NODE::state, SYNCED_PROC, THREAD_POOL_NODE::th_start, THREAD_POOL::thread_list, thread_pool, THREAD_POOL_NODE::thread_state, THREAD_POOL_NODE::type, THREAD_POOL::waiting_list, and WAITING_PROC.

Referenced by dispatch_to_pool(), main(), main(), 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,
unsigned int  delay 
)

destroy all running threads

destroy all running threads

Parameters
poolThe THREAD_POOL *object to kill
delaytime in usec between each check
Returns
TRUE or FALSE

Definition at line 431 of file n_thread_pool.c.

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

Referenced by main(), main(), and n_vigenere_cypher().

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

◆ get_nb_cpu_cores()

long int get_nb_cpu_cores ( )

get number of core of current system

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

Definition at line 44 of file n_thread_pool.c.

Referenced by main(), main(), and new_n_fluid().

+ Here is the caller graph for this function:

◆ new_thread_pool()

THREAD_POOL * new_thread_pool ( size_t  nbmaxthr,
size_t  nb_max_waiting 
)

allocate a new thread pool

allocate a new thread pool

Parameters
nbmaxthrnumber of active threads in the pool
nb_max_waitingmax number of waiting procs in the pool. Zero for no limit
Returns
NULL or a new thread pool object

Definition at line 132 of file n_thread_pool.c.

References EXITING_THREAD, Free, THREAD_POOL_NODE::func, IDLE_PROC, list_destroy(), THREAD_POOL_NODE::lock, THREAD_POOL::lock, LOG_ERR, Malloc, MAX_LIST_ITEMS, THREAD_POOL::max_threads, n_log, THREAD_POOL::nb_actives, THREAD_POOL::nb_max_waiting, THREAD_POOL::nb_tasks, new_generic_list(), THREAD_POOL_NODE::param, RUNNING_THREAD, THREAD_POOL_NODE::state, THREAD_POOL_NODE::th_end, THREAD_POOL_NODE::th_start, THREAD_POOL_NODE::thr, THREAD_POOL::thread_list, thread_pool, THREAD_POOL_NODE::thread_pool, thread_pool_processing_function(), THREAD_POOL_NODE::thread_state, THREAD_POOL_NODE::type, and THREAD_POOL::waiting_list.

Referenced by main(), main(), and 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 process on some free thread slots, else do nothing

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

Parameters
thread_poolThe thread pool to refresh
Returns
TRUE or FALSE

Definition at line 512 of file n_thread_pool.c.

References __n_assert, add_threaded_process(), Free, THREAD_WAITING_PROC::func, THREAD_POOL_NODE::lock, THREAD_POOL::lock, LOG_DEBUG, LOG_ERR, THREAD_POOL::max_threads, n_log, THREAD_POOL::nb_actives, LIST::nb_items, THREAD_POOL::nb_tasks, LIST_NODE::next, NO_LOCK, NO_QUEUE, NORMAL_PROC, THREAD_WAITING_PROC::param, LIST_NODE::ptr, remove_list_node, RUNNING_PROC, LIST::start, THREAD_POOL_NODE::state, THREAD_POOL::thread_list, thread_pool, THREAD_POOL::waiting_list, and WAITING_PROC.

Referenced by main(), 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)

tell all the waiting threads to start their associated process

tell all the waiting threads to start their associated process

Parameters
thread_poolThe thread pool to launch
Returns
TRUE or FALSE

Definition at line 341 of file n_thread_pool.c.

References THREAD_POOL_NODE::lock, THREAD_POOL::lock, LOG_ERR, THREAD_POOL::max_threads, n_log, THREAD_POOL_NODE::state, SYNCED_PROC, THREAD_POOL_NODE::th_start, THREAD_POOL::thread_list, thread_pool, THREAD_POOL_NODE::type, and WAITING_PROC.

Referenced by main(), and 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 threads in the pool to terminate processing, blocking but light on the CPU as there is no polling

wait for all the threads in the pool to terminate processing, 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 376 of file n_thread_pool.c.

References __n_assert, THREAD_POOL_NODE::lock, LOG_ERR, THREAD_POOL::max_threads, n_log, SYNCED_PROC, THREAD_POOL_NODE::th_end, THREAD_POOL::thread_list, thread_pool, and THREAD_POOL_NODE::type.

Referenced by main(), and 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)

wait for the pool to become idle (no active threads, empty waiting list), blocking without polling

wait for the pool to become idle (no active threads, empty waiting list), blocking without polling

Parameters
thread_poolThe thread pool to wait on
Returns
TRUE or FALSE

Definition at line 405 of file n_thread_pool.c.

References __n_assert, LOG_ERR, n_log, THREAD_POOL::nb_tasks, refresh_thread_pool(), THREAD_POOL::thread_list, and thread_pool.

Referenced by main(), main(), and n_vigenere_cypher().

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