Nilorea Library
C utilities for networking, threading, graphics
n_thread_pool.h
Go to the documentation of this file.
1
8#ifndef NILOREA_THREAD_POOL_LIBRARY
9#define NILOREA_THREAD_POOL_LIBRARY
10
11#ifdef __cplusplus
12extern "C"
13{
14#endif
15
21#include "n_list.h"
22#include <pthread.h>
23#include <semaphore.h>
24
25
27#define SYNCED_PROC 1
29#define DIRECT_PROC 2
31#define NOQUEUE 4
33#define IDLE_PROC 8
35#define WAITING_PROC 16
37#define RUNNING_PROC 32
39#define RUNNING_THREAD 64
41#define EXITING_THREAD 128
43#define EXITED_THREAD 256
45#define NO_LOCK 512
46
48typedef struct THREAD_POOL_NODE
49{
51 void *(*func)(void *param);
52
54 void *param ;
55
57 int type ;
59 int state ;
63 pthread_t thr ;
64
66 sem_t th_start,
69
71 pthread_mutex_t lock ;
72
75
77
78
80typedef struct THREAD_POOL
81{
84
91
93 pthread_mutex_t lock ;
94
96 sem_t nb_tasks ;
97
100
101} THREAD_POOL ;
102
103
104
107{
109 void *(*func)(void *param);
111 void *param ;
112
114
115/* get number of core of current system */
116int get_nb_cpu_cores();
117/* allocate a new thread pool */
118THREAD_POOL *new_thread_pool( int nbmaxthr, int nb_max_waiting );
119/* add a function to run in an available thread inside a pool */
120int add_threaded_process( THREAD_POOL *thread_pool, void *(*func_ptr)(void *param), void *param, int mode );
121/* tell all the waiting threads to start their associated process */
122int start_threaded_pool( THREAD_POOL *thread_pool );
123/* wait for all the threads in the pool to terminate processing, blocking but light on the CPU as there is no polling */
125/* wait for all running threads to finish */
126int wait_for_threaded_pool( THREAD_POOL *thread_pool, int delay );
127/* destroy all running threads */
128int destroy_threaded_pool( THREAD_POOL **thread_pool, int delay );
129/* try to add some waiting process on some free thread slots, else do nothing */
130int refresh_thread_pool( THREAD_POOL *thread_pool );
131
136#ifdef __cplusplus
137}
138#endif
139
140#endif
141
Structure of a generic LIST container.
Definition: n_list.h:45
int state
state of the proc , RUNNING_PROC when it is busy processing func( param) , IDLE_PROC when it waits fo...
Definition: n_thread_pool.h:59
int nb_actives
number of threads actually doing a proc
Definition: n_thread_pool.h:90
int type
SYNCED or DIRECT process start.
Definition: n_thread_pool.h:57
void * param
if not NULL , passed as argument
sem_t th_end
thread ending semaphore
Definition: n_thread_pool.h:68
pthread_mutex_t lock
mutex to prevent mutual access of node parameters
Definition: n_thread_pool.h:71
struct THREAD_POOL * thread_pool
pointer to assigned thread pool
Definition: n_thread_pool.h:74
sem_t th_start
thread starting semaphore
Definition: n_thread_pool.h:66
pthread_t thr
thread id
Definition: n_thread_pool.h:63
THREAD_POOL_NODE ** thread_list
Dynamically allocated but fixed size thread array.
Definition: n_thread_pool.h:83
LIST * waiting_list
Waiting list handling.
Definition: n_thread_pool.h:99
sem_t nb_tasks
semaphore to store the number of tasks
Definition: n_thread_pool.h:96
int nb_max_waiting
Maximum number of waiting procedures int the list, 0 or -1 for unlimited.
Definition: n_thread_pool.h:88
int thread_state
state of the managing thread , RUNNING_THREAD, EXITING_THREAD, EXITED_THREAD
Definition: n_thread_pool.h:61
int max_threads
Maximum number of running threads in the list.
Definition: n_thread_pool.h:86
pthread_mutex_t lock
mutex to prevent mutual access of waiting_list parameters
Definition: n_thread_pool.h:93
void * param
if not NULL , passed as argument
Definition: n_thread_pool.h:54
int start_threaded_pool(THREAD_POOL *thread_pool)
Launch the process waiting for exectution in the thread pool.
int get_nb_cpu_cores()
get number of core of current system
Definition: n_thread_pool.c:27
int destroy_threaded_pool(THREAD_POOL **thread_pool, int delay)
delete a thread_pool, exit the threads and free the structs
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 refresh_thread_pool(THREAD_POOL *thread_pool)
try to add some waiting DIRECT_PROCs on some free thread slots, else do nothing
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, int delay)
Wait for all the launched process in the thread pool to terminate.
THREAD_POOL * new_thread_pool(int nbmaxthr, int nb_max_waiting)
Create a new pool of nbmaxthr threads.
Structure of a trhead pool.
Definition: n_thread_pool.h:81
A thread pool node.
Definition: n_thread_pool.h:49
Structure of a waiting process item.
List structures and definitions.