fsautoproc
Basic file transformation automation management utility
Loading...
Searching...
No Matches
tp.c File Reference

Thread pool implementation for executing work requests. More...

#include "tp.h"
#include <assert.h>
#include <pthread.h>
#include <stdatomic.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "fd.h"
#include "fs.h"
#include "index.h"
#include "lcmd.h"
#include "log.h"
Include dependency graph for tp.c:

Data Structures

struct  thrd_s
 Initialized worker thread in the thread pool. More...
 

Functions

static void * tpentrypoint (void *arg)
 Thread pool worker thread entry point. The thread will spin lock while waiting to be reserved. Once reserved, it spin locks while waiting for the main thread to finalize the work request. Once ready, the work request is executed and the thread is released back to the pool.
 
static void tpinitthrd (struct thrd_s *t, const int flags)
 Configures an initialized thread with the specified options. If the TPOPT_LOGFILES flag is set, the file descriptor set is initialized. If the file descriptor set initialization fails, or the flag is not set, the file descriptors will default to STDOUT_FILENO and STDERR_FILENO.
 
int tpinit (const int size, const int flags)
 Initializes a global worker thread pool of the given size.
 
int tpqueue (const struct tpreq_s *req)
 Allocates a work request to the first available thread in the global pool. If no threads are available, the request will block until a thread becomes available.
 
void tpwait (void)
 Waits for all threads in the global pool to finish executing their work requests.
 
void tpshutdown (void)
 Waits for all threads in the global pool to finish executing their work requests, and then shuts down the pool and exits its threads. This function should be followed by a call to tpfree().
 
void tpfree (void)
 Frees all memory allocated by tpinit(). This function should be called at the end of the program. It is safe to call this function even if tpinit() was not called. This function should be called directly after tpshutdown().
 

Variables

static struct thrd_s ** thrds
 Thread pool worker threads array.
 
static _Atomic bool haltthrds
 Thread pool halt flag.
 
static _Atomic int thrdrc
 Thread pool thread count.
 

Detailed Description

Thread pool implementation for executing work requests.

Function Documentation

◆ tpentrypoint()

static void * tpentrypoint ( void * arg)
static

Thread pool worker thread entry point. The thread will spin lock while waiting to be reserved. Once reserved, it spin locks while waiting for the main thread to finalize the work request. Once ready, the work request is executed and the thread is released back to the pool.

Parameters
argThe thread self context
Returns
NULL in all cases
Here is the call graph for this function:
Here is the caller graph for this function:

◆ tpfree()

void tpfree ( void )

Frees all memory allocated by tpinit(). This function should be called at the end of the program. It is safe to call this function even if tpinit() was not called. This function should be called directly after tpshutdown().

Here is the caller graph for this function:

◆ tpinit()

int tpinit ( int size,
int flags )

Initializes a global worker thread pool of the given size.

Parameters
sizeThe number of threads to create, must be greater than 0.
flagsThe flags to use when creating the thread pool.
Returns
0 on success, -1 on failure.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ tpinitthrd()

static void tpinitthrd ( struct thrd_s * t,
const int flags )
static

Configures an initialized thread with the specified options. If the TPOPT_LOGFILES flag is set, the file descriptor set is initialized. If the file descriptor set initialization fails, or the flag is not set, the file descriptors will default to STDOUT_FILENO and STDERR_FILENO.

Parameters
tThe thread to configure
flagsThe configuration flags
Here is the call graph for this function:
Here is the caller graph for this function:

◆ tpqueue()

int tpqueue ( const struct tpreq_s * req)

Allocates a work request to the first available thread in the global pool. If no threads are available, the request will block until a thread becomes available.

Parameters
reqThe work request to allocate.
Returns
0 on success, -1 on failure.
Here is the caller graph for this function:

◆ tpshutdown()

void tpshutdown ( void )

Waits for all threads in the global pool to finish executing their work requests, and then shuts down the pool and exits its threads. This function should be followed by a call to tpfree().

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

◆ tpwait()

void tpwait ( void )

Waits for all threads in the global pool to finish executing their work requests.

Here is the caller graph for this function:

Variable Documentation

◆ haltthrds

_Atomic bool haltthrds
static

Thread pool halt flag.

◆ thrdrc

_Atomic int thrdrc
static

Thread pool thread count.

◆ thrds

struct thrd_s** thrds
static

Thread pool worker threads array.