Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_thread_pool.h
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
26#ifndef NILOREA_THREAD_POOL_LIBRARY
27#define NILOREA_THREAD_POOL_LIBRARY
28
29#ifdef __cplusplus
30extern "C" {
31#endif
32
38#include "n_list.h"
39#include <pthread.h>
40#include <semaphore.h>
41
43#define NORMAL_PROC 1
45#define SYNCED_PROC 2
47#define DIRECT_PROC 4
49#define NO_QUEUE 8
51#define IDLE_PROC 16
53#define WAITING_PROC 32
55#define RUNNING_PROC 64
57#define RUNNING_THREAD 128
59#define EXITING_THREAD 256
61#define EXITED_THREAD 512
63#define NO_LOCK 1024
64
66typedef struct THREAD_POOL_NODE {
68 void* (*func)(void* param);
69
71 void* param;
72
74 int type;
76 int state;
80 pthread_t thr;
81
83 sem_t th_start,
86
88 pthread_mutex_t lock;
89
92
94
96typedef struct THREAD_POOL {
99
106
108 pthread_mutex_t lock;
109
111 sem_t nb_tasks;
112
115
117
119typedef struct THREAD_WAITING_PROC {
121 void* (*func)(void* param);
123 void* param;
124
126
128long int get_nb_cpu_cores();
130THREAD_POOL* new_thread_pool(size_t nbmaxthr, size_t nb_max_waiting);
132int add_threaded_process(THREAD_POOL* thread_pool, void* (*func_ptr)(void* param), void* param, int mode);
140int destroy_threaded_pool(THREAD_POOL** thread_pool, unsigned int delay);
143
148#ifdef __cplusplus
149}
150#endif
151
152#endif
THREAD_POOL * thread_pool
Definition ex_fluid.c:77
static int mode
Structure of a generic LIST container.
Definition n_list.h:58
size_t nb_max_waiting
Maximum number of waiting procedures in the list, 0 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 signaling pool idle state: value 0 = work in progress, value 1 = pool is idle (no active th...
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)
tell all the waiting threads to start their associated process
THREAD_POOL * new_thread_pool(size_t nbmaxthr, size_t nb_max_waiting)
allocate a new thread pool
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 refresh_thread_pool(THREAD_POOL *thread_pool)
try to add some waiting process on some free thread slots, else do nothing
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
int destroy_threaded_pool(THREAD_POOL **thread_pool, unsigned int delay)
destroy all running threads
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 ...
long int get_nb_cpu_cores()
get number of core of current system
Structure of a thread pool.
A thread pool node.
Structure of a waiting process item.
List structures and definitions.