Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
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#endif
14
20#include "n_list.h"
21#include <pthread.h>
22#include <semaphore.h>
23
25#define SYNCED_PROC 1
27#define DIRECT_PROC 2
29#define NOQUEUE 4
31#define IDLE_PROC 8
33#define WAITING_PROC 16
35#define RUNNING_PROC 32
37#define RUNNING_THREAD 64
39#define EXITING_THREAD 128
41#define EXITED_THREAD 256
43#define NO_LOCK 512
44
46typedef struct THREAD_POOL_NODE {
48 void* (*func)(void* param);
49
51 void* param;
52
54 int type;
56 int state;
60 pthread_t thr;
61
63 sem_t th_start,
66
68 pthread_mutex_t lock;
69
72
74
76typedef struct THREAD_POOL {
79
85 size_t nb_actives;
86
88 pthread_mutex_t lock;
89
91 sem_t nb_tasks;
92
95
97
99typedef struct THREAD_WAITING_PROC {
101 void* (*func)(void* param);
103 void* param;
104
106
107/* get number of core of current system */
108long int get_nb_cpu_cores();
109/* allocate a new thread pool */
110THREAD_POOL* new_thread_pool(size_t nbmaxthr, size_t nb_max_waiting);
111/* add a function to run in an available thread inside a pool */
112int add_threaded_process(THREAD_POOL* thread_pool, void* (*func_ptr)(void* param), void* param, int mode);
113/* tell all the waiting threads to start their associated process */
114int start_threaded_pool(THREAD_POOL* thread_pool);
115/* wait for all the threads in the pool to terminate processing, blocking but light on the CPU as there is no polling */
117/* wait for all running threads to finish */
118int wait_for_threaded_pool(THREAD_POOL* thread_pool, unsigned int delay);
119/* destroy all running threads */
120int destroy_threaded_pool(THREAD_POOL** thread_pool, unsigned int delay);
121/* try to add some waiting process on some free thread slots, else do nothing */
122int refresh_thread_pool(THREAD_POOL* thread_pool);
123
128#ifdef __cplusplus
129}
130#endif
131
132#endif
Structure of a generic LIST container.
Definition n_list.h:39
size_t nb_max_waiting
Maximum number of waiting procedures int the list, 0 or -1 for unlimited.
int state
state of the proc , RUNNING_PROC when it is busy processing func( param) , IDLE_PROC when it waits fo...
int type
SYNCED or DIRECT process start.
size_t max_threads
Maximum number of running threads in the list.
void * param
if not NULL , passed as argument
sem_t th_end
thread ending semaphore
pthread_mutex_t lock
mutex to prevent mutual access of node parameters
struct THREAD_POOL * thread_pool
pointer to assigned thread pool
sem_t th_start
thread starting semaphore
pthread_t thr
thread id
THREAD_POOL_NODE ** thread_list
Dynamically allocated but fixed size thread array.
LIST * waiting_list
Waiting list handling.
size_t nb_actives
number of threads actually doing a proc
sem_t nb_tasks
semaphore to store the number of tasks
int thread_state
state of the managing thread , RUNNING_THREAD, EXITING_THREAD, EXITED_THREAD
pthread_mutex_t lock
mutex to prevent mutual access of waiting_list parameters
void * param
if not NULL , passed as argument
int start_threaded_pool(THREAD_POOL *thread_pool)
Launch the process waiting for execution in the thread pool.
THREAD_POOL * new_thread_pool(size_t nbmaxthr, size_t nb_max_waiting)
Create a new pool of nbmaxthr threads.
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 destroy_threaded_pool(THREAD_POOL **thread_pool, unsigned int delay)
delete a thread_pool, exit the threads and free the structs
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
long int get_nb_cpu_cores()
get number of core of current system
int wait_for_threaded_pool(THREAD_POOL *thread_pool, unsigned int delay)
Wait for all the launched process in the thread pool to terminate.
Structure of a trhead pool.
A thread pool node.
Structure of a waiting process item.
List structures and definitions.