-
Notifications
You must be signed in to change notification settings - Fork 2
/
threadpool_winxp.c
301 lines (242 loc) · 8.61 KB
/
threadpool_winxp.c
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <inttypes.h>
#include <windows.h>
#include <process.h>
#include "event.h"
#include "log.h"
#include "mutex.h"
#include "threadpool.h"
typedef struct threadpool_job_s {
void *user_data;
threadpool_job_cb callback;
struct threadpool_job_s *next;
} threadpool_job_s;
struct threadpool_s;
typedef struct threadpool_thread_s {
uintptr_t handle;
struct threadpool_thread_s *next;
} threadpool_thread_s;
typedef struct threadpool_s {
bool stop;
int32_t min_threads;
int32_t num_threads;
int32_t max_threads;
int32_t busy_threads;
void *wakeup_cond;
void *lazy_cond;
int32_t queue_count;
void *queue_lock;
threadpool_job_s *queue_first;
threadpool_job_s *queue_last;
threadpool_thread_s *threads;
} threadpool_s;
static threadpool_job_s *threadpool_job_create(void *user_data, threadpool_job_cb callback) {
threadpool_job_s *job = (threadpool_job_s *)calloc(1, sizeof(threadpool_job_s));
if (!job)
return NULL;
job->user_data = user_data;
job->callback = callback;
job->next = NULL;
return job;
}
static bool threadpool_job_delete(threadpool_job_s **job) {
if (!job)
return false;
free(*job);
*job = NULL;
return true;
}
static void threadpool_enqueue_job(threadpool_s *threadpool, threadpool_job_s *job) {
LOG_DEBUG("threadpool - job 0x%" PRIxPTR " - enqueue\n", (intptr_t)job);
// Add job to the end of the queue
if (!threadpool->queue_last) {
threadpool->queue_first = job;
threadpool->queue_last = job;
} else {
threadpool->queue_last->next = job;
threadpool->queue_last = job;
}
threadpool->queue_count++;
}
static threadpool_job_s *threadpool_dequeue_job(threadpool_s *threadpool) {
if (!threadpool->queue_first)
return NULL;
// Remove the first job from the queue
threadpool_job_s *job = threadpool->queue_first;
threadpool->queue_first = threadpool->queue_first->next;
if (!threadpool->queue_first)
threadpool->queue_last = NULL;
threadpool->queue_count--;
LOG_DEBUG("threadpool - job 0x%" PRIxPTR " - dequeue\n", (intptr_t)job);
return job;
}
static void __cdecl threadpool_do_work(void *arg) {
threadpool_s *threadpool = arg;
LOG_DEBUG("threadpool - worker 0x%" PRIx32 " - started\n", GetCurrentThreadId());
while (true) {
mutex_lock(threadpool->queue_lock);
LOG_DEBUG("threadpool - worker 0x%" PRIx32 " - waiting for job\n", GetCurrentThreadId());
// Sleep until there is work to do
while (!threadpool->stop && !threadpool->queue_first) {
mutex_unlock(threadpool->queue_lock);
// queue_lock will be unlocked during sleep and locked during awake
bool wakeup = event_wait(threadpool->wakeup_cond, 250);
mutex_lock(threadpool->queue_lock);
if (!wakeup)
continue;
}
if (threadpool->stop)
break;
// Get the next job to do
threadpool_job_s *job = threadpool_dequeue_job(threadpool);
// Increment count of busy threads
threadpool->busy_threads++;
mutex_unlock(threadpool->queue_lock);
// Do the job
if (job) {
LOG_DEBUG("threadpool - worker 0x%" PRIx32 " - processing job 0x%" PRIxPTR "\n", GetCurrentThreadId(),
(intptr_t)job);
job->callback(job->user_data);
LOG_DEBUG("threadpool - worker 0x%" PRIx32 " - job complete 0x%" PRIxPTR "\n", GetCurrentThreadId(),
(intptr_t)job);
threadpool_job_delete(&job);
}
mutex_lock(threadpool->queue_lock);
// Decrement count of busy threads
threadpool->busy_threads--;
// If no busy threads then signal threadpool_wait that we are lazy
if (threadpool->busy_threads == 0 && !threadpool->queue_first)
event_set(threadpool->lazy_cond);
mutex_unlock(threadpool->queue_lock);
}
LOG_DEBUG("threadpool - worker 0x%" PRIx32 " - stopped\n", GetCurrentThreadId());
event_set(threadpool->lazy_cond);
mutex_unlock(threadpool->queue_lock);
}
static void threadpool_create_thread_on_demand(threadpool_s *threadpool) {
// Create new thread and add it to the list of threads
uintptr_t handle = _beginthread(threadpool_do_work, 0, threadpool);
if (handle == (uintptr_t)-1)
return;
threadpool_thread_s *thread = calloc(1, sizeof(threadpool_thread_s));
thread->handle = handle;
thread->next = threadpool->threads;
threadpool->threads = thread;
threadpool->num_threads++;
}
bool threadpool_enqueue(void *ctx, void *user_data, threadpool_job_cb callback) {
threadpool_s *threadpool = (threadpool_s *)ctx;
// Create new job
threadpool_job_s *job = threadpool_job_create(user_data, callback);
if (!job)
return false;
mutex_lock(threadpool->queue_lock);
// Add job to the job queue
threadpool_enqueue_job(threadpool, job);
// Create new thread if all threads are busy
if (threadpool->busy_threads == threadpool->num_threads && threadpool->num_threads < threadpool->max_threads)
threadpool_create_thread_on_demand(threadpool);
mutex_unlock(threadpool->queue_lock);
// Wake up waiting threads
event_set(threadpool->wakeup_cond);
return true;
}
static void threadpool_stop_threads(threadpool_s *threadpool) {
mutex_lock(threadpool->queue_lock);
// Stop threads from doing anymore work
threadpool->stop = true;
mutex_unlock(threadpool->queue_lock);
// Wake up all threads to check stop flag
event_set(threadpool->wakeup_cond);
}
static void threadpool_delete_threads(threadpool_s *threadpool) {
threadpool_thread_s *thread = NULL;
// Delete threads from list of threads
while (threadpool->threads) {
thread = threadpool->threads;
threadpool->threads = threadpool->threads->next;
// Wait for thread to exit
while (true) {
// Signal wake up condition to wake up threads to stop
event_set(threadpool->wakeup_cond);
DWORD wait = WaitForSingleObject((HANDLE)thread->handle, 250);
if (wait != WAIT_TIMEOUT)
break;
}
free(thread);
}
threadpool->num_threads = 0;
}
static void threadpool_delete_jobs(threadpool_s *threadpool) {
threadpool_job_s *job = NULL;
// Delete job from the queue
while (threadpool->queue_first) {
job = threadpool->queue_first;
threadpool->queue_first = threadpool->queue_first->next;
threadpool_job_delete(&job);
}
}
void threadpool_wait(void *ctx) {
threadpool_s *threadpool = (threadpool_s *)ctx;
if (!threadpool)
return;
mutex_lock(threadpool->queue_lock);
while (true) {
if ((!threadpool->stop && (threadpool->busy_threads != 0 || threadpool->queue_count != 0)) ||
(threadpool->stop && threadpool->num_threads != 0)) {
mutex_unlock(threadpool->queue_lock);
// Wait for signal that indicates there is no more work to do
event_wait(threadpool->lazy_cond, 250);
mutex_lock(threadpool->queue_lock);
} else {
break;
}
}
mutex_unlock(threadpool->queue_lock);
}
void *threadpool_create(int32_t min_threads, int32_t max_threads) {
threadpool_s *threadpool = (threadpool_s *)calloc(1, sizeof(threadpool_s));
if (!threadpool)
return NULL;
threadpool->queue_lock = mutex_create();
if (!threadpool->queue_lock) {
free(threadpool);
return NULL;
}
threadpool->wakeup_cond = event_create();
if (!threadpool->wakeup_cond) {
mutex_delete(&threadpool->queue_lock);
free(threadpool);
return NULL;
}
threadpool->lazy_cond = event_create();
if (!threadpool->lazy_cond) {
event_delete(&threadpool->wakeup_cond);
mutex_delete(&threadpool->queue_lock);
free(threadpool);
return NULL;
}
threadpool->min_threads = min_threads;
threadpool->max_threads = max_threads;
return threadpool;
}
bool threadpool_delete(void **ctx) {
if (!ctx)
return false;
threadpool_s *threadpool = (threadpool_s *)*ctx;
if (!threadpool)
return false;
threadpool_stop_threads(threadpool);
threadpool_delete_threads(threadpool);
threadpool_delete_jobs(threadpool);
mutex_delete(&threadpool->queue_lock);
event_delete(&threadpool->wakeup_cond);
event_delete(&threadpool->lazy_cond);
free(threadpool);
*ctx = NULL;
return true;
}