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 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 * implied. See the License for the specific language governing
15 * permissions and limitations under the License.
16 *
17 * SPDX-License-Identifier: Apache-2.0
18 */
19
26
27#ifndef NILOREA_THREAD_POOL_LIBRARY
28#define NILOREA_THREAD_POOL_LIBRARY
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
38
39#include "n_list.h"
40#include <pthread.h>
41#include <semaphore.h>
42
44#define NORMAL_PROC 1
46#define SYNCED_PROC 2
48#define DIRECT_PROC 4
50#define NO_QUEUE 8
52#define IDLE_PROC 16
54#define WAITING_PROC 32
56#define RUNNING_PROC 64
58#define RUNNING_THREAD 128
60#define EXITING_THREAD 256
62#define EXITED_THREAD 512
64#define NO_LOCK 1024
65
68#define N_THREAD_POOL_WAIT_POLL_US 1000
69
70typedef struct THREAD_POOL_NODE {
72 void* (*func)(void* param);
73
75 void* param;
76
78 int type;
80 int state;
84 pthread_t thr;
85
87 sem_t th_start,
90
92 pthread_mutex_t lock;
93
96
98
100typedef struct THREAD_POOL {
103
110
112 pthread_mutex_t lock;
113
115 sem_t nb_tasks;
116
119
124 void (*discard)(void* param);
125
127
129typedef struct THREAD_WAITING_PROC {
131 void* (*func)(void* param);
133 void* param;
134
136
138long int get_nb_cpu_cores();
140THREAD_POOL* new_thread_pool(size_t nbmaxthr, size_t nb_max_waiting);
142int add_threaded_process(THREAD_POOL* thread_pool, void* (*func_ptr)(void* param), void* param, int mode);
150int destroy_threaded_pool(THREAD_POOL** thread_pool, unsigned int delay);
152int set_thread_pool_discard(THREAD_POOL* thread_pool, void (*discard)(void* param));
155
157typedef void (*N_PARALLEL_FN)(size_t begin, size_t end, void* user);
158
179int n_parallel_for(size_t begin, size_t end, size_t grain, N_PARALLEL_FN fn, void* user);
180
184
185#ifdef __cplusplus
186}
187#endif
188
189#endif
THREAD_POOL * thread_pool
Definition ex_fluid.c:76
static int mode
Structure of a generic LIST container.
Definition n_list.h:59
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
void(* discard)(void *param)
Called by destroy_threaded_pool() with the param of every queued job it discards without running,...
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 n_parallel_for(size_t begin, size_t end, size_t grain, N_PARALLEL_FN fn, void *user)
Run a loop over a range on several cores, and return when all of it is done.
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
void(* N_PARALLEL_FN)(size_t begin, size_t end, void *user)
work done by n_parallel_for() on one slice [begin, end) of a range
int destroy_threaded_pool(THREAD_POOL **thread_pool, unsigned int delay)
stop every thread and free the pool; queued jobs still waiting when the retry budget runs out are dis...
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
int set_thread_pool_discard(THREAD_POOL *thread_pool, void(*discard)(void *param))
set the function destroy_threaded_pool() hands the param of each discarded queued job to
Structure of a thread pool.
Structure of a waiting process item.
List structures and definitions.