ascii-chat
Binary v0.15.83-53-gb711d8720
Library lib/v0.3.2-701-gb711d8720
Video chat in your terminal
Loading...
Searching...
No Matches
thread_pool.h
Go to the documentation of this file.
1#pragma once
2
72
73/* Storage capacities in elements; text buffers include the terminating NUL. */
74#define THREAD_POOL_NAME_BUFFER_SIZE 64
75
76#include <stdint.h>
77#include <stdbool.h>
78#include "common.h"
79#include <libplatform/mutex.h>
80#include <libplatform/cond.h>
81#include <libplatform/thread.h>
82
83// Forward declaration
89typedef struct thread_pool thread_pool_t;
91
99typedef struct thread_pool_work_entry {
100 void *(*work_func)(void *);
101 void *work_arg;
104
113 platform_thread_t thread;
115 void *(*thread_func)(void *);
119};
120
145
158thread_pool_t *thread_pool_create(const char *pool_name);
159
176thread_pool_t *thread_pool_create_with_workers(const char *pool_name, size_t num_workers);
177
188
206asciichat_error_t thread_pool_queue_work(const char *name, thread_pool_t *pool, void *(*work_func)(void *),
207 void *work_arg);
208
236asciichat_error_t thread_pool_spawn(thread_pool_t *pool, void *(*thread_func)(void *), void *thread_arg, int stop_id,
237 const char *thread_name);
238
258
267size_t thread_pool_get_count(const thread_pool_t *pool);
268
277bool thread_pool_has_threads(const thread_pool_t *pool);
278
⚙️ Common definitions, error codes, macros, and types shared throughout the application
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:44
struct thread_pool thread_pool_t
One queued function call waiting for a thread-pool worker.
Thread handle and shutdown metadata for one pool-owned thread.
platform_thread_t thread
Thread handle.
void * thread_arg
Thread argument.
struct thread_pool_entry * next
Linked list next pointer.
int stop_id
Cleanup order (lower = stop first, -1 = unordered).
char name[64]
Thread name for debugging.
One queued function call waiting for a thread-pool worker.
Definition thread_pool.h:99
void * work_arg
Argument passed to work function.
struct thread_pool_work_entry * next
Linked list next pointer.
Thread pool structure.
mutex_t threads_mutex
Mutex protecting thread list.
bool is_work_queue_mode
True if using work queue mode.
thread_pool_entry_t * threads
Linked list of threads (sorted by stop_id).
cond_t work_available
Condition variable for workers to wait on.
bool shutdown_requested
Signal workers to exit.
thread_pool_work_entry_t * work_queue
Queue of pending work.
size_t thread_count
Number of threads in pool.
size_t num_workers
Number of pre-created worker threads.
mutex_t work_queue_mutex
Mutex protecting work queue.
char name[64]
Pool name for debugging.
void thread_pool_destroy(thread_pool_t *pool)
Destroy a thread pool.
struct thread_pool_entry thread_pool_entry_t
Definition thread_pool.h:90
asciichat_error_t thread_pool_queue_work(const char *name, thread_pool_t *pool, void *(*work_func)(void *), void *work_arg)
Queue a named work item to a work queue pool.
asciichat_error_t thread_pool_interrupt_all(thread_pool_t *pool, int sig)
Send a signal to all threads in the pool.
thread_pool_t * thread_pool_create(const char *pool_name)
Create a new thread pool (long-lived threads mode).
Definition thread_pool.c:92
struct thread_pool_work_entry thread_pool_work_entry_t
One queued function call waiting for a thread-pool worker.
size_t thread_pool_get_count(const thread_pool_t *pool)
Get thread count in the pool.
#define THREAD_POOL_NAME_BUFFER_SIZE
Definition thread_pool.h:74
bool thread_pool_has_threads(const thread_pool_t *pool)
Check if pool has any threads.
asciichat_error_t thread_pool_spawn(thread_pool_t *pool, void *(*thread_func)(void *), void *thread_arg, int stop_id, const char *thread_name)
Spawn a worker thread in the pool.
asciichat_error_t thread_pool_stop_all(thread_pool_t *pool)
Stop all threads in the pool in stop_id order.
thread_pool_t * thread_pool_create_with_workers(const char *pool_name, size_t num_workers)
Create a new thread pool in work queue mode.