Crinit -- Configurable Rootfs Init
thrpool.c File Reference

(2023-08-02, commit: 9434b31)

Implementations of a generic worker thread pool. Used by the notification/service interface to handle socket communication. More...

#include "thrpool.h"
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "logio.h"
Include dependency graph for thrpool.c:

Macros

#define crinitDryPoolThreshold(poolSize)   ((poolSize) / 10)
 

Functions

static int crinitThreadPoolGrow (crinitThreadPool_t *ctx, size_t newSize)
 
static void * crinitDryPoolWatchdog (void *thrpool)
 
int crinitThreadPoolInit (crinitThreadPool_t *ctx, size_t initialSize, void *(*threadFunc)(void *), const void *thrArgs, size_t thrArgsSize)
 
int crinitThreadPoolThreadBusyCallback (crinitThreadPool_t *ctx)
 
int crinitThreadPoolThreadAvailCallback (crinitThreadPool_t *ctx)
 

Detailed Description

Implementations of a generic worker thread pool. Used by the notification/service interface to handle socket communication.

Macro Definition Documentation

◆ crinitDryPoolThreshold

#define crinitDryPoolThreshold (   poolSize)    ((poolSize) / 10)

Function macro to calculate threshold value at and below which point the pool is considerd "dry".

Parameters
poolSizeThe current total size of the worker thread pool.
Returns
The threshold value.

Function Documentation

◆ crinitDryPoolWatchdog()

static void * crinitDryPoolWatchdog ( void *  thrpool)
static

Thread function to monitor the current load of the thread pool and grow its size if needed.

To be started as a pthread running alongside the worker threads. Will monitor the given thread pool via crinitThreadPool_t::threadAvail and crinitThreadPool_t::threadAvailChanged.

A "dry pool" condition is detected and the thread pool grown if 90% or more of the pool's worker threads are currently unavailable.

Parameters
thrpoolThe crinitThreadPool_t to monitor.

◆ crinitThreadPoolGrow()

static int crinitThreadPoolGrow ( crinitThreadPool_t ctx,
size_t  newSize 
)
static

Grow the given thread pool to the given new size.

Gets called by the dryPoolWatchdog() if not enough threads are idle/available.

Parameters
ctxThe thread pool to grow.
newSizeThe new size of the thread pool, must be larger than the old size.
Returns
0 on success, -1 on error

◆ crinitThreadPoolInit()

int crinitThreadPoolInit ( crinitThreadPool_t ctx,
size_t  initialSize,
void *(*)(void *)  threadFunc,
const void *  thrArgs,
size_t  thrArgsSize 
)

Initialize an crinitThreadPool_t.

Parameters
ctxThe crinitThreadPool_t to initialize.
initialSizeInitial size (in number of threads) of the pool.
threadFuncWorker thread function to use.
thrArgsArguments to the worker thread function. Will be copied and saved in case more threads need to be started by dryPoolWatchdog.
thrArgsSizeSize (Bytes) of arguments to the worker thread function.
Returns
0 on success, -1 otherwise

◆ crinitThreadPoolThreadAvailCallback()

int crinitThreadPoolThreadAvailCallback ( crinitThreadPool_t ctx)

Callback to be used by the worker thread function signalling it is idle/available.

Parameters
ctxThe crinitThreadPool_t context.
Returns
0 on success, -1 otherwise

◆ crinitThreadPoolThreadBusyCallback()

int crinitThreadPoolThreadBusyCallback ( crinitThreadPool_t ctx)

Callback to be used by the worker thread function signalling it is busy/unavailable.

Parameters
ctxThe crinitThreadPool_t context.
Returns
0 on success, -1 otherwise