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 NORMAL_PROC 1
27#define SYNCED_PROC 2
29#define DIRECT_PROC 4
31#define NO_QUEUE 8
33#define IDLE_PROC 16
35#define WAITING_PROC 32
37#define RUNNING_PROC 64
39#define RUNNING_THREAD 128
41#define EXITING_THREAD 256
43#define EXITED_THREAD 512
45#define NO_LOCK 1024
46
48typedef struct THREAD_POOL_NODE {
50 void* (*func)(void* param);
51
53 void* param;
54
56 int type;
58 int state;
62 pthread_t thr;
63
65 sem_t th_start,
68
70 pthread_mutex_t lock;
71
74
76
78typedef struct THREAD_POOL {
81
87 size_t nb_actives;
88
90 pthread_mutex_t lock;
91
93 sem_t nb_tasks;
94
97
99
101typedef struct THREAD_WAITING_PROC {
103 void* (*func)(void* param);
105 void* param;
106
108
109/* get number of core of current system */
110long int get_nb_cpu_cores();
111/* allocate a new thread pool */
112THREAD_POOL* new_thread_pool(size_t nbmaxthr, size_t nb_max_waiting);
113/* add a function to run in an available thread inside a pool */
114int add_threaded_process(THREAD_POOL* thread_pool, void* (*func_ptr)(void* param), void* param, int mode);
115/* tell all the waiting threads to start their associated process */
117/* wait for all the threads in the pool to terminate processing, blocking but light on the CPU as there is no polling */
119/* wait for all running threads to finish */
120int wait_for_threaded_pool(THREAD_POOL* thread_pool, unsigned int delay);
121/* destroy all running threads */
122int destroy_threaded_pool(THREAD_POOL** thread_pool, unsigned int delay);
123/* try to add some waiting process on some free thread slots, else do nothing */
125
130#ifdef __cplusplus
131}
132#endif
133
134#endif
THREAD_POOL * thread_pool
Definition ex_fluid.c:59
int mode
Network for managing conenctions.
Definition ex_network.c:22
Structure of a generic LIST container.
Definition n_list.h:40
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.