-
Notifications
You must be signed in to change notification settings - Fork 0
/
threadpool.h
85 lines (72 loc) · 2.24 KB
/
threadpool.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#ifndef THREADPOOL_H
#define THREADPOOL_H
#include <pthread.h>
#include <semaphore.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "queue.h"
/**
* @file threadpool.h
* @brief Threadpool Header File
*/
typedef enum {
success = 0,
pthread_error = -1,
memory_error = -2,
invalid_argument_error = -3,
semaphore_error = -4,
user_error = -6,
queue_error = -5
} thread_pool_error_t;
#define THREAD_SEMAPHORE 0
typedef struct runnable {
void (*function)(void *, size_t);
void *arg;
size_t argsz;
} runnable_t;
/**
* @struct thread_pool_t
* @brief The threadpool struct
* @var *threads Array of all of the threads.
* @var size Amount of threads.
* @var *runnables Queue of the tasks.
* @var end Flag, that forbids to add new tasks after destroy is called.
* @var attr Initializer for threads.
* @var runnables_semaphore Semaphore for the queue.
* @var pool_mutex Semaphore for the pool
*/
typedef struct thread_pool {
pthread_t *threads;
size_t size;
Queue *runnables;
bool end;
pthread_attr_t attr;
sem_t runnables_semaphore;
sem_t pool_mutex;
} thread_pool_t;
/**
* @function thread_pool_init
* @brief Creates a thread_pool_t object at argument pool.
* @param pool Where to initialize threadpool.
* @param pool_size Number of threads.
* @return 0 if all goes correctly, negative values in case of an error(@see thread_pool_error_t for codes).
*/
int thread_pool_init(thread_pool_t *pool, size_t pool_size);
/**
* @function thread_pool_destroy
* @brief Finishes all of the tasks, stops and removes pool.
* @param pool Which threadpool to destroy.
*/
void thread_pool_destroy(thread_pool_t *pool);
/**
* @function defer
* @brief Adds a new task to the pool.
* @param pool Thread pool to execute the task.
* @param runnable Task to run on the pool.
* @return 0 if all goes correctly, negative values in case of an error(@see thread_pool_error_t for codes).
*/
int defer(thread_pool_t *pool, runnable_t runnable);
#endif