Thread pool
threadpool.h
Go to the documentation of this file.
1 
9 #ifndef __THREADPOOL_H__
10 #define __THREADPOOL_H__
11 
12 #include <semaphore.h>
13 #include <signal.h>
14 #include <stdbool.h>
15 #include <stddef.h>
16 
20 typedef struct runnable {
21  void (*function)(void *, size_t);
22  void *arg;
23  size_t argsz;
24 } runnable_t;
25 
29 typedef struct node {
31  struct node* next;
32 } node_t;
33 
37 typedef struct queue {
40  size_t size;
41 } queue_t;
42 
46 typedef struct thread_pool {
47  size_t pool_size;
48  sem_t mutex;
51  pthread_t* threads;
52  bool finished;
53  pthread_attr_t attr;
55 
64 int thread_pool_init(thread_pool_t *pool, size_t pool_size);
65 
71 
80 
81 #endif // __THREADPOOL_H__
thread_pool::queue
queue_t * queue
pointer to the queue of tasks;
Definition: threadpool.h:50
thread_pool::finished
bool finished
information about finishing all tasks;
Definition: threadpool.h:52
threadpool.h
runnable
Runnable function.
Definition: threadpool.h:20
old_action
struct sigaction old_action
old sigaction;
Definition: threadpool.c:86
thread_pool_init
int thread_pool_init(thread_pool_t *pool, size_t num_threads)
Initialize the thread-pool.
Definition: threadpool.c:185
pools_size
size_t pools_size
size of the known_pools array;
Definition: threadpool.c:88
queue::last
node_t * last
pointer to the last element;
Definition: threadpool.h:39
node
Single queue node.
Definition: threadpool.h:29
thread_pool::threads
pthread_t * threads
array of threads;
Definition: threadpool.h:51
thread_pool
Thread-pool.
Definition: threadpool.h:46
queue::size
size_t size
size of the queue;
Definition: threadpool.h:40
handler
struct @0 handler
Signal handler.
node::runnable
runnable_t runnable
runnable function;
Definition: threadpool.h:30
free_queue
static void free_queue(queue_t *queue)
Deallocate the queue.
Definition: threadpool.c:72
thread_pool::pool_size
size_t pool_size
number of threads;
Definition: threadpool.h:47
queue
Standard FIFO queue.
Definition: threadpool.h:37
thread_pool::waiting_threads
sem_t waiting_threads
semaphore for free threads;
Definition: threadpool.h:49
node_t
struct node node_t
Single queue node.
sigint_destroy
static void sigint_destroy()
Destroy all pools after receiving SIGINT.
Definition: threadpool.c:97
__attribute__
__attribute__((constructor))
Constructor of the exception handler.
Definition: threadpool.c:115
queue::first
node_t * first
pointer to the first element;
Definition: threadpool.h:38
thread_pool_init
int thread_pool_init(thread_pool_t *pool, size_t pool_size)
Initialize the thread-pool.
Definition: threadpool.c:185
thread_pool_destroy
void thread_pool_destroy(thread_pool_t *pool)
Destroy the thread-pool.
Definition: threadpool.c:256
runnable::function
void(* function)(void *, size_t)
f(arg, argsz);
Definition: threadpool.h:21
thread_function
static void * thread_function(void *arg)
Function run by every thread.
Definition: threadpool.c:145
thread_pool_destroy
void thread_pool_destroy(thread_pool_t *pool)
Destroy the thread-pool.
Definition: threadpool.c:256
push
static void push(queue_t *queue, runnable_t runnable)
Add a new element to the queue.
Definition: threadpool.c:21
defer
int defer(thread_pool_t *pool, runnable_t runnable)
Add a new task to the pool.
Definition: threadpool.c:318
action
struct sigaction action
new sigaction;
Definition: threadpool.c:85
thread_pool_t
struct thread_pool thread_pool_t
Thread-pool.
thread_pool::mutex
sem_t mutex
thread-pool mutex;
Definition: threadpool.h:48
known_pools
thread_pool_t ** known_pools
array of pointers to all known pools;
Definition: threadpool.c:87
runnable::argsz
size_t argsz
number of arguments;
Definition: threadpool.h:23
queue_t
struct queue queue_t
Standard FIFO queue.
runnable::arg
void * arg
array of arguments;
Definition: threadpool.h:22
block_mask
sigset_t block_mask
mask with blocked signals;
Definition: threadpool.c:90
last
unsigned last
number of initialized pools;
Definition: threadpool.c:89
pop
static runnable_t pop(queue_t *queue)
Read and pop the first element in queue.
Definition: threadpool.c:48
runnable_t
struct runnable runnable_t
Runnable function.
thread_pool::attr
pthread_attr_t attr
standard pthread attribute;
Definition: threadpool.h:53
node::next
struct node * next
pointer to the next node;
Definition: threadpool.h:31
defer
int defer(struct thread_pool *pool, runnable_t runnable)
Add a new task to the pool.
Definition: threadpool.c:318