ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
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 "common.h"
77#include <libplatform/cond.h>
78#include <libplatform/mutex.h>
79#include <libplatform/thread.h>
80#include <stdbool.h>
81#include <stdint.h>
82
83#ifdef __cplusplus
84extern "C" {
85#endif
86
87// Forward declaration
96typedef struct thread_pool thread_pool_t;
98
107 void *(*work_func)(void *);
108 void *work_arg;
111
120 platform_thread_t thread;
122 void *(*thread_func)(void *);
126};
127
152
165thread_pool_t *thread_pool_create(const char *pool_name);
166
184 size_t num_workers);
185
196
215 void *(*work_func)(void *),
216 void *work_arg);
217
247 void *(*thread_func)(void *),
248 void *thread_arg, int stop_id,
249 const char *thread_name);
250
270
279size_t thread_pool_get_count(const thread_pool_t *pool);
280
289bool thread_pool_has_threads(const thread_pool_t *pool);
290
313
314#ifdef __cplusplus
315}
316#endif
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
⚙️ Common definitions, error codes, macros, and types shared throughout the application
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.
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:97
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:96
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.