-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy paththreadpool.c
More file actions
167 lines (118 loc) · 3.7 KB
/
threadpool.c
File metadata and controls
167 lines (118 loc) · 3.7 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include "threadpool.h"
#include "threads.h"
//#define DEBUG
#ifdef DEBUG
#define DEB(x) printf("%s\n", x)
#define DEBI(msg, i) printf("%s: %d\n", msg, i)
#else
#define DEB(...)
#define DEBI(...)
#endif
const int TASK_QUEUE_MAX = 1000;
struct task_data {
void *(*work_routine)(void *);
void *arg;
};
struct thread_pool {
// N worker threads.
pthread_t *worker_threads;
// A circular queue that holds tasks that are yet to be executed.
struct task_data* task_queue;
// Head and tail of the queue.
int queue_head, queue_tail;
// How many worker threads can we have.
int max_threads;
// How many tasks are scheduled for execution. We use this so that
// we can wait for completion.
int scheduled;
pthread_mutex_t mutex;
// A condition that's signaled on when we go from a state of no work
// to a state of work available.
pthread_cond_t work_available;
// A condition that's signaled on when we don't have any more tasks scheduled.
pthread_cond_t done;
};
int pool_get_max_threads(struct thread_pool *pool) {
return pool->max_threads;
}
struct task_thread_args {
struct thread_pool* pool;
struct task_data td;
};
void *worker_thread_func(void *pool_arg) {
DEB("[W] Starting work thread.");
struct thread_pool *pool = (struct thread_pool *)pool_arg;
while (1) {
struct task_data picked_task;
Pthread_mutex_lock(&pool->mutex);
while (pool->queue_head == pool->queue_tail) {
DEB("[W] Empty queue. Waiting...");
Pthread_cond_wait(&pool->work_available, &pool->mutex);
}
assert(pool->queue_head != pool->queue_tail);
DEBI("[W] Picked", pool->queue_head);
picked_task = pool->task_queue[pool->queue_head % TASK_QUEUE_MAX];
pool->queue_head++;
// The task is scheduled.
pool->scheduled++;
Pthread_mutex_unlock(&pool->mutex);
// Run the task.
picked_task.work_routine(picked_task.arg);
Pthread_mutex_lock(&pool->mutex);
pool->scheduled--;
if (pool->scheduled == 0) {
Pthread_cond_signal(&pool->done);
}
Pthread_mutex_unlock(&pool->mutex);
}
return NULL;
}
void pool_add_task(struct thread_pool *pool, void *(*work_routine)(void*), void *arg) {
Pthread_mutex_lock(&pool->mutex);
DEB("[Q] Queueing one item.");
if (pool->queue_head == pool->queue_tail) {
Pthread_cond_broadcast(&pool->work_available);
}
struct task_data task;
task.work_routine = work_routine;
task.arg = arg;
pool->task_queue[pool->queue_tail % TASK_QUEUE_MAX] = task;
pool->queue_tail++;
Pthread_mutex_unlock(&pool->mutex);
}
void pool_wait(struct thread_pool *pool) {
DEB("[POOL] Waiting for completion.");
Pthread_mutex_lock(&pool->mutex);
while (pool->scheduled > 0) {
Pthread_cond_wait(&pool->done, &pool->mutex);
}
Pthread_mutex_unlock(&pool->mutex);
DEB("[POOL] Waiting done.");
}
struct thread_pool* pool_init(int max_threads) {
struct thread_pool* pool = malloc(sizeof(struct thread_pool));
pool->queue_head = pool->queue_tail = 0;
pool->scheduled = 0;
pool->task_queue = malloc(sizeof(struct task_data) * TASK_QUEUE_MAX);
pool->max_threads = max_threads;
pool->worker_threads = malloc(sizeof(pthread_t) * max_threads);
Pthread_mutex_init(&pool->mutex);
Pthread_cond_init(&pool->work_available);
Pthread_cond_init(&pool->done);
for (int i = 0; i < max_threads; i++) {
Pthread_create(&pool->worker_threads[i], NULL, worker_thread_func, pool);
}
return pool;
}
void pool_destroy(struct thread_pool *pool) {
pool_wait(pool);
for (int i = 0; i < pool->max_threads; i++) {
Pthread_detach(pool->worker_threads[i]);
}
free(pool->worker_threads);
free(pool->task_queue);
free(pool);
}