fsautoproc
Basic file transformation automation management utility
|
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"
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. | |
Thread pool implementation for executing work requests.
|
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.
arg | The thread self context |
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()
.
int tpinit | ( | int | size, |
int | flags ) |
Initializes a global worker thread pool of the given size.
size | The number of threads to create, must be greater than 0. |
flags | The flags to use when creating the thread pool. |
|
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
.
t | The thread to configure |
flags | The configuration flags |
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.
req | The work request to allocate. |
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 tpwait | ( | void | ) |
Waits for all threads in the global pool to finish executing their work requests.
|
static |
Thread pool halt flag.
|
static |
Thread pool thread count.
|
static |
Thread pool worker threads array.