Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_thread_pool.h File Reference

Thread pool declaration. More...

#include "n_list.h"
#include <pthread.h>
#include <semaphore.h>
Include dependency graph for n_thread_pool.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  THREAD_POOL
 Structure of a thread pool. More...
struct  THREAD_POOL_NODE
struct  THREAD_WAITING_PROC
 Structure of a waiting process item. More...

Macros

#define DIRECT_PROC   4
 processing mode for added func, direct start, not queued
#define EXITED_THREAD   512
 indicate that the pool is off, all jobs have been consumed
#define EXITING_THREAD   256
 indicate that the pool is exiting, unfinished jobs will finish and the pool will exit the threads and enter the EXITED state
#define IDLE_PROC   16
 status of a thread which is waiting for some proc
#define N_THREAD_POOL_WAIT_POLL_US   1000
 A thread pool node.
#define NO_LOCK   1024
 if passed to add_threaded_process, skip main table lock in case we are in a func which is already locking it
#define NO_QUEUE   8
 special processing mode for waiting_list: do not add the work in queue since it' coming from the queue
#define NORMAL_PROC   1
 processing mode for added func, synced start, can be queued
#define RUNNING_PROC   64
 status of a thread which proc is currently running
#define RUNNING_THREAD   128
 indicate that the pool is running and ready to use
#define SYNCED_PROC   2
 processing mode for added func, synced start, not queued
#define WAITING_PROC   32
 status of a thread who have proc waiting to be processed

Typedefs

typedef 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

Functions

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 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 discarded, see set_thread_pool_discard()
long int get_nb_cpu_cores ()
 get number of core of current system
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.
THREAD_POOL * new_thread_pool (size_t nbmaxthr, size_t nb_max_waiting)
 allocate a new thread pool
int refresh_thread_pool (THREAD_POOL *thread_pool)
 try to add some waiting process on some free thread slots, else do nothing
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
int start_threaded_pool (THREAD_POOL *thread_pool)
 tell all the waiting threads to start their associated process
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 is no polling
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

Detailed Description

Thread pool declaration.

Author
Castagnier Mickael
Version
1.0
Date
30/04/2014

Definition in file n_thread_pool.h.