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
27#ifndef NILOREA_THREAD_POOL_LIBRARY
28#define NILOREA_THREAD_POOL_LIBRARY
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
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
67typedef struct THREAD_POOL_NODE {
69 void* (*func)(void* param);
70
72 void* param;
73
75 int type;
77 int state;
81 pthread_t thr;
82
84 sem_t th_start,
87
89 pthread_mutex_t lock;
90
93
95
97typedef struct THREAD_POOL {
100
107
109 pthread_mutex_t lock;
110
112 sem_t nb_tasks;
113
116
118
120typedef struct THREAD_WAITING_PROC {
122 void* (*func)(void* param);
124 void* param;
125
127
129long int get_nb_cpu_cores();
131THREAD_POOL* new_thread_pool(size_t nbmaxthr, size_t nb_max_waiting);
133int add_threaded_process(THREAD_POOL* thread_pool, void* (*func_ptr)(void* param), void* param, int mode);
141int destroy_threaded_pool(THREAD_POOL** thread_pool, unsigned int delay);
144
149#ifdef __cplusplus
150}
151#endif
152
153#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
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.