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

Thread pool functions. More...

#include <unistd.h>
#include "nilorea/n_common.h"
#include "nilorea/n_log.h"
#include "nilorea/n_thread_pool.h"
#include "nilorea/n_time.h"
#include <pthread.h>
#include <string.h>
#include <errno.h>
Include dependency graph for n_thread_pool.c:

Go to the source code of this file.

Data Structures

struct  N_PARALLEL_SLICE
 One slice of a parallel loop, as handed to its thread. More...

Functions

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 destroy_threaded_pool (THREAD_POOL **pool, unsigned int delay)
 Stop every thread of a pool and free it.
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.
static void * n_parallel_slice (void *arg)
 Thread entry of one slice of a parallel loop.
THREAD_POOL * new_thread_pool (size_t nbmaxthr, size_t nb_max_waiting)
 Create a new pool of nbmaxthr threads.
int refresh_thread_pool (THREAD_POOL *thread_pool)
 try to add some waiting DIRECT_PROCs on some free thread slots, else do nothing
int set_thread_pool_discard (THREAD_POOL *thread_pool, void(*discard)(void *param))
 Set what destroy_threaded_pool() does with the param of a queued job it discards.
int start_threaded_pool (THREAD_POOL *thread_pool)
 Launch the process waiting for execution in the thread pool.
void * thread_pool_processing_function (void *param)
 Internal thread pool processing function.
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
int wait_for_threaded_pool (THREAD_POOL *thread_pool)
 Wait for the thread pool to become idle (no active threads, empty waiting list), blocking without polling.

Variables

static long parallel_budget = -1
 get number of core of current system
static pthread_mutex_t parallel_lock = PTHREAD_MUTEX_INITIALIZER
 Guards parallel_budget.

Detailed Description

Thread pool functions.

Author
Castagnier Mickael
Version
1.0
Date
30/04/2014

Definition in file n_thread_pool.c.


Data Structure Documentation

◆ N_PARALLEL_SLICE

struct N_PARALLEL_SLICE

One slice of a parallel loop, as handed to its thread.

Definition at line 53 of file n_thread_pool.c.

Collaboration diagram for N_PARALLEL_SLICE:
Data Fields
size_t begin first index of the slice
size_t end one past its last
N_PARALLEL_FN fn the work
void * user passed to the work

Function Documentation

◆ n_parallel_slice()

void * n_parallel_slice ( void * arg)
static

Thread entry of one slice of a parallel loop.

Parameters
argthe N_PARALLEL_SLICE
Returns
NULL

Definition at line 69 of file n_thread_pool.c.

References N_PARALLEL_SLICE::begin, N_PARALLEL_SLICE::end, N_PARALLEL_SLICE::fn, and N_PARALLEL_SLICE::user.

Referenced by n_parallel_for().

Here is the caller graph for this function:

◆ thread_pool_processing_function()

void * thread_pool_processing_function ( void * param)

Internal thread pool processing function.

Parameters
paramparams of a threaded process
Returns
NULL when exiting

Definition at line 156 of file n_thread_pool.c.

References EXITED_THREAD, EXITING_THREAD, THREAD_POOL_NODE::func, IDLE_PROC, THREAD_POOL_NODE::lock, LOG_DEBUG, LOG_ERR, n_log, THREAD_POOL_NODE::param, refresh_thread_pool(), RUNNING_PROC, RUNNING_THREAD, THREAD_POOL_NODE::state, SYNCED_PROC, THREAD_POOL_NODE::th_end, THREAD_POOL_NODE::th_start, THREAD_POOL_NODE::thr, THREAD_POOL_NODE::thread_pool, THREAD_POOL_NODE::thread_state, THREAD_POOL_NODE::type, and type().

Referenced by new_thread_pool().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ parallel_budget

long parallel_budget = -1
static

get number of core of current system

Returns
The number of cores or 0 if the system command is not supported

Extra threads n_parallel_for() may have running at once, across every caller; -1 until the first call sizes it from the core count.

Definition at line 47 of file n_thread_pool.c.

Referenced by n_parallel_for().

◆ parallel_lock

pthread_mutex_t parallel_lock = PTHREAD_MUTEX_INITIALIZER
static

Guards parallel_budget.

Definition at line 50 of file n_thread_pool.c.

Referenced by n_parallel_for().