-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpool.h
576 lines (466 loc) · 14.2 KB
/
pool.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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#ifdef ENABLE_TRACING
#include "spall_native_auto.h"
#endif
// cross-platform thread wrappers, because microsoft couldn't be arsed to take 5 seconds and
// do this and save all the junior devs and codebases everywhere from this pile of nonsense.
#if defined(__linux__) || defined(__APPLE__)
#include <stdatomic.h>
#include <pthread.h>
#include <unistd.h>
#include <errno.h>
typedef pthread_t TPool_ThreadHandle;
#define tpool_thread_start(t) pthread_create(&(t)->thread, NULL, _tpool_worker, (void *) (t))
#define tpool_thread_end(t) pthread_join((t)->thread, NULL)
#elif defined(_WIN32)
#include <windows.h>
#include <process.h>
typedef ptrdiff_t ssize_t;
typedef HANDLE TPool_ThreadHandle;
#define tpool_thread_start(t) ((t)->thread = (HANDLE) _beginthread(_tpool_worker, 0, t))
#define tpool_thread_end(t) WaitForSingleObject((t)->thread, INFINITE)
#endif
// MSVC only took 11 years to put C11 atomics in, (despite the fact that MSVC/C++11 has them).
// This is the pain we suffer because microsoft got lazy
#if defined(_MSC_VER)
#define TPool_Thread_Local __declspec(thread)
#define TPool_Atomic volatile
#define TPOOL_LOAD(val) val
#define TPOOL_CAS(addr, expected, desired) (InterlockedCompareExchange64(addr, desired, expected) == expected)
#define TPOOL_ATOMIC_FUTEX_INC(val) (_InterlockedIncrement64(&(val)))
#define TPOOL_ATOMIC_FUTEX_DEC(val) (_InterlockedDecrement64(&(val)))
#else
#include <stdatomic.h>
#define TPool_Thread_Local _Thread_local
#define TPool_Atomic _Atomic
#define TPOOL_LOAD(val) atomic_load(&val)
#define TPOOL_CAS(addr, expected, desired) atomic_compare_exchange_weak(addr, &expected, desired)
#define TPOOL_ATOMIC_FUTEX_INC(val) (atomic_fetch_add_explicit(&val, 1, memory_order_acquire))
#define TPOOL_ATOMIC_FUTEX_DEC(val) (atomic_fetch_sub_explicit(&val, 1, memory_order_acquire))
#define __debugbreak() __builtin_trap()
#endif
// cross-platform futex, because we can't just have nice things. All the popular platforms have them under the hood,
// but giving them to users? NO! Users are too stupid to have nice things, save them for the fedora-wearing elite.
#if defined(__linux__)
#include <linux/futex.h>
#include <sys/syscall.h>
typedef TPool_Atomic int32_t TPool_Futex;
void _tpool_signal(TPool_Futex *addr) {
int ret = syscall(SYS_futex, addr, FUTEX_WAKE | FUTEX_PRIVATE_FLAG, 1, NULL, NULL, 0);
if (ret == -1) {
perror("Futex wake");
__debugbreak();
}
}
void _tpool_broadcast(TPool_Futex *addr) {
int ret = syscall(SYS_futex, addr, FUTEX_WAKE | FUTEX_PRIVATE_FLAG, INT32_MAX, NULL, NULL, 0);
if (ret == -1) {
perror("Futex wake");
__debugbreak();
}
}
void _tpool_wait(TPool_Futex *addr, TPool_Futex val) {
for (;;) {
int ret = syscall(SYS_futex, addr, FUTEX_WAIT | FUTEX_PRIVATE_FLAG, val, NULL, NULL, 0);
if (ret == -1) {
if (errno != EAGAIN) {
perror("Futex wait");
__debugbreak();
} else {
return;
}
} else if (ret == 0) {
if (*addr != val) {
return;
}
}
}
}
#elif defined(__APPLE__)
typedef TPool_Atomic int64_t TPool_Futex;
#define UL_COMPARE_AND_WAIT 0x00000001
#define ULF_WAKE_ALL 0x00000100
#define ULF_NO_ERRNO 0x01000000
/* timeout is specified in microseconds */
int __ulock_wait(uint32_t operation, void *addr, uint64_t value, uint32_t timeout);
int __ulock_wake(uint32_t operation, void *addr, uint64_t wake_value);
void _tpool_signal(TPool_Futex *addr) {
for (;;) {
int ret = __ulock_wake(UL_COMPARE_AND_WAIT | ULF_NO_ERRNO, addr, 0);
if (ret >= 0) {
return;
}
ret = -ret;
if (ret == EINTR || ret == EFAULT) {
continue;
}
if (ret == ENOENT) {
return;
}
printf("futex wake fail?\n");
__debugbreak();
}
}
void _tpool_broadcast(TPool_Futex *addr) {
for (;;) {
int ret = __ulock_wake(UL_COMPARE_AND_WAIT | ULF_NO_ERRNO | ULF_WAKE_ALL, addr, 0);
if (ret >= 0) {
return;
}
ret = -ret;
if (ret == EINTR || ret == EFAULT) {
continue;
}
if (ret == ENOENT) {
return;
}
printf("futex wake fail?\n");
__debugbreak();
}
}
void _tpool_wait(TPool_Futex *addr, TPool_Futex val) {
for (;;) {
int ret = __ulock_wait(UL_COMPARE_AND_WAIT | ULF_NO_ERRNO, addr, val, 0);
if (ret >= 0) {
if (*addr != val) {
return;
}
continue;
}
ret = -ret;
if (ret == EINTR || ret == EFAULT) {
continue;
}
if (ret == ENOENT) {
return;
}
printf("futex wait fail?\n");
__debugbreak();
}
}
#elif defined(_WIN32)
typedef TPool_Atomic int64_t TPool_Futex;
void _tpool_signal(TPool_Futex *addr) {
WakeByAddressSingle((void *)addr);
}
void _tpool_broadcast(TPool_Futex *addr) {
WakeByAddressAll((void *)addr);
}
void _tpool_wait(TPool_Futex *addr, TPool_Futex val) {
for (;;) {
int ret = WaitOnAddress(addr, (void *)&val, sizeof(val), INFINITE);
if (*addr != val) break;
}
}
#elif defined(__FreeBSD__)
#include <sys/types.h>
#include <sys/umtx.h>
typedef TPool_Atomic int32_t TPool_Futex;
void _tpool_signal(TPool_Futex *addr) {
_umtx_op(addr, UMTX_OP_WAKE, 1, 0, 0);
}
void _tpool_broadcast(TPool_Futex *addr) {
_umtx_op(addr, UMTX_OP_WAKE, INT32_MAX, 0, 0);
}
void _tpool_wait(TPool_Futex *addr, TPool_Futex val) {
for (;;) {
int ret = _umtx_op(addr, UMTX_OP_WAIT_UINT, val, 0, NULL);
if (ret == 0) {
if (errno == ETIMEDOUT || errno == EINTR) {
continue;
}
perror("Futex wait");
__debugbreak();
} else if (ret == 0) {
if (*addr != val) {
return;
}
}
}
}
#elif defined(__OpenBSD__)
#include <sys/futex.h>
typedef TPool_Atomic int32_t TPool_Futex;
void _tpool_signal(TPool_Futex *addr) {
for (;;) {
int ret = futex(addr, FUTEX_WAKE | FUTEX_PRIVATE_FLAG, 1, NULL, NULL);
if (ret == -1) {
if (errno == ETIMEDOUT || errno == EINTR) {
continue;
}
perror("Futex wake");
__debugbreak();
} else if (ret == 1) {
return;
}
}
}
void _tpool_broadcast(TPool_Futex *addr) {
for (;;) {
int ret = futex(addr, FUTEX_WAKE | FUTEX_PRIVATE_FLAG, INT32_MAX, NULL, NULL);
if (ret == -1) {
if (errno == ETIMEDOUT || errno == EINTR) {
continue;
}
perror("Futex wake");
__debugbreak();
} else if (ret == 1) {
return;
}
}
}
void _tpool_wait(TPool_Futex *addr, TPool_Futex val) {
for (;;) {
int ret = futex(addr, FUTEX_WAIT | FUTEX_PRIVATE_FLAG, val, NULL, NULL);
if (ret == -1) {
if (*addr != val) {
return;
}
if (errno == ETIMEDOUT || errno == EINTR) {
continue;
}
perror("Futex wait");
__debugbreak();
}
}
}
#endif
struct TPool;
typedef void tpool_task_proc(struct TPool *pool, void *data);
TPool_Thread_Local int tpool_current_thread_idx;
#define GRAB_SUCCESS 0
#define GRAB_EMPTY 1
#define GRAB_FAILED 2
typedef struct TPool_Task {
tpool_task_proc *do_work;
void *args;
} TPool_Task;
typedef struct {
TPool_Atomic ssize_t size;
TPool_Task *buffer;
} TPool_RingBuffer;
typedef struct {
TPool_Atomic ssize_t top;
TPool_Atomic ssize_t bottom;
TPool_Atomic(TPool_RingBuffer *) ring;
} TPool_Queue;
typedef struct TPool_Thread {
TPool_ThreadHandle thread;
int idx;
TPool_Queue queue;
struct TPool *pool;
} TPool_Thread;
typedef struct TPool {
struct TPool_Thread *threads;
int thread_count;
TPool_Atomic bool running;
TPool_Futex tasks_available;
TPool_Futex tasks_left;
} TPool;
TPool_RingBuffer *tpool_ring_make(ssize_t size) {
TPool_RingBuffer *ring = malloc(sizeof(TPool_RingBuffer));
ring->size = size;
ring->buffer = calloc(ring->size, sizeof(TPool_Task));
return ring;
}
TPool_Queue tpool_queue_make(ssize_t size) {
TPool_Queue d = {};
TPool_RingBuffer *ring = tpool_ring_make(size);
atomic_store(&d.ring, ring);
return d;
}
void tpool_queue_delete(TPool_Queue *q) {
free(q->ring->buffer);
free(q->ring);
}
TPool_RingBuffer *tpool_ring_grow(TPool_RingBuffer *ring, ssize_t bottom, ssize_t top) {
TPool_RingBuffer *new_ring = tpool_ring_make(ring->size * 2);
for (ssize_t i = top; i < bottom; i++) {
new_ring->buffer[i % new_ring->size] = ring->buffer[i % ring->size];
}
return new_ring;
}
void _thread_init(TPool *pool, TPool_Thread *thread, int idx) {
thread->queue = tpool_queue_make(1 << 1);
thread->pool = pool;
thread->idx = idx;
}
void _tpool_queue_push(TPool_Thread *thread, TPool_Task task) {
ssize_t bot = atomic_load_explicit(&thread->queue.bottom, memory_order_relaxed);
ssize_t top = atomic_load_explicit(&thread->queue.top, memory_order_acquire);
TPool_RingBuffer *cur_ring = atomic_load_explicit(&thread->queue.ring, memory_order_relaxed);
ssize_t size = bot - top;
if (size > (cur_ring->size - 1)) {
// Queue is full
thread->queue.ring = tpool_ring_grow(thread->queue.ring, bot, top);
cur_ring = atomic_load_explicit(&thread->queue.ring, memory_order_relaxed);
}
cur_ring->buffer[bot % cur_ring->size] = task;
atomic_thread_fence(memory_order_release);
atomic_store_explicit(&thread->queue.bottom, bot + 1, memory_order_relaxed);
TPOOL_ATOMIC_FUTEX_INC(thread->pool->tasks_left);
TPOOL_ATOMIC_FUTEX_INC(thread->pool->tasks_available);
_tpool_broadcast(&thread->pool->tasks_available);
}
int _tpool_queue_take(TPool_Thread *thread, TPool_Task *task) {
ssize_t bot = atomic_load_explicit(&thread->queue.bottom, memory_order_relaxed) - 1;
TPool_RingBuffer *cur_ring = atomic_load_explicit(&thread->queue.ring, memory_order_relaxed);
atomic_store_explicit(&thread->queue.bottom, bot, memory_order_relaxed);
atomic_thread_fence(memory_order_seq_cst);
ssize_t top = atomic_load_explicit(&thread->queue.top, memory_order_relaxed);
if (top <= bot) {
// Queue is not empty
*task = cur_ring->buffer[bot % cur_ring->size];
if (top == bot) {
// Only one entry left in queue
if (!atomic_compare_exchange_strong_explicit(&thread->queue.top, &top, top + 1, memory_order_seq_cst, memory_order_relaxed)) {
// Race failed
atomic_store_explicit(&thread->queue.bottom, bot + 1, memory_order_relaxed);
return GRAB_EMPTY;
}
atomic_store_explicit(&thread->queue.bottom, bot + 1, memory_order_relaxed);
return GRAB_SUCCESS;
}
// We got a task without hitting a race
return GRAB_SUCCESS;
} else {
// Queue is empty
atomic_store_explicit(&thread->queue.bottom, bot + 1, memory_order_relaxed);
return GRAB_EMPTY;
}
}
int _tpool_queue_steal(TPool_Thread *thread, TPool_Task *task) {
ssize_t top = atomic_load_explicit(&thread->queue.top, memory_order_acquire);
atomic_thread_fence(memory_order_seq_cst);
ssize_t bot = atomic_load_explicit(&thread->queue.bottom, memory_order_acquire);
int ret = GRAB_EMPTY;
if (top < bot) {
// Queue is not empty
TPool_RingBuffer *cur_ring = atomic_load_explicit(&thread->queue.ring, memory_order_consume);
*task = cur_ring->buffer[top % cur_ring->size];
if (!atomic_compare_exchange_strong_explicit(&thread->queue.top, &top, top + 1, memory_order_seq_cst, memory_order_relaxed)) {
// Race failed
ret = GRAB_FAILED;
} else {
ret = GRAB_SUCCESS;
}
}
return ret;
}
#ifndef _WIN32
void *_tpool_worker(void *ptr)
#else
void _tpool_worker(void *ptr)
#endif
{
TPool_Task task;
TPool_Thread *current_thread = (TPool_Thread *)ptr;
tpool_current_thread_idx = current_thread->idx;
TPool *pool = current_thread->pool;
#ifdef ENABLE_TRACING
spall_auto_thread_init(tpool_current_thread_idx, SPALL_DEFAULT_BUFFER_SIZE);
#endif
for (;;) {
work_start:
if (!pool->running) {
break;
}
// If we've got tasks to process, work through them
size_t finished_tasks = 0;
while (!_tpool_queue_take(current_thread, &task)) {
task.do_work(pool, task.args);
TPOOL_ATOMIC_FUTEX_DEC(pool->tasks_left);
finished_tasks += 1;
}
if (finished_tasks > 0 && !TPOOL_LOAD(pool->tasks_left)) {
_tpool_signal(&pool->tasks_left);
}
// If there's still work somewhere and we don't have it, steal it
if (TPOOL_LOAD(pool->tasks_left)) {
int idx = current_thread->idx;
for (int i = 0; i < pool->thread_count; i++) {
if (!TPOOL_LOAD(pool->tasks_left)) {
break;
}
idx = (idx + 1) % pool->thread_count;
TPool_Thread *thread = &pool->threads[idx];
TPool_Task task;
int ret = _tpool_queue_steal(thread, &task);
if (ret == GRAB_FAILED) {
goto work_start;
} else if (ret == GRAB_EMPTY) {
continue;
}
task.do_work(pool, task.args);
TPOOL_ATOMIC_FUTEX_DEC(pool->tasks_left);
if (!TPOOL_LOAD(pool->tasks_left)) {
_tpool_signal(&pool->tasks_left);
}
goto work_start;
}
}
// if we've done all our work, and there's nothing to steal, go to sleep
int32_t state = TPOOL_LOAD(pool->tasks_available);
if (!pool->running) { break; }
_tpool_wait(&pool->tasks_available, state);
}
#ifdef ENABLE_TRACING
spall_auto_thread_quit();
#endif
#ifndef _WIN32
return NULL;
#endif
}
void tpool_add_task(TPool *pool, TPool_Task task) {
TPool_Thread *current_thread = &pool->threads[tpool_current_thread_idx];
_tpool_queue_push(current_thread, task);
}
void tpool_wait(TPool *pool) {
TPool_Task task;
TPool_Thread *current_thread = &pool->threads[tpool_current_thread_idx];
while (TPOOL_LOAD(pool->tasks_left)) {
// if we've got tasks on our queue, run them
while (!_tpool_queue_take(current_thread, &task)) {
task.do_work(pool, task.args);
TPOOL_ATOMIC_FUTEX_DEC(pool->tasks_left);
}
// is this mem-barriered enough?
// This *must* be executed in this order, so the futex wakes immediately
// if rem_tasks has changed since we checked last, otherwise the program
// will permanently sleep
TPool_Futex rem_tasks = TPOOL_LOAD(pool->tasks_left);
if (!rem_tasks) {
break;
}
_tpool_wait(&pool->tasks_left, rem_tasks);
}
}
void tpool_init(TPool *pool, int child_thread_count) {
int thread_count = child_thread_count + 1;
pool->thread_count = thread_count;
pool->threads = malloc(sizeof(TPool_Thread) * pool->thread_count);
pool->running = true;
// setup the main thread
_thread_init(pool, &pool->threads[0], 0);
tpool_current_thread_idx = 0;
for (int i = 1; i < pool->thread_count; i++) {
_thread_init(pool, &pool->threads[i], i);
tpool_thread_start(&pool->threads[i]);
}
}
void tpool_destroy(TPool *pool) {
pool->running = false;
for (int i = 1; i < pool->thread_count; i++) {
TPOOL_ATOMIC_FUTEX_INC(pool->tasks_available);
_tpool_broadcast(&pool->tasks_available);
tpool_thread_end(&pool->threads[i]);
}
for (int i = 0; i < pool->thread_count; i++) {
tpool_queue_delete(&pool->threads[i].queue);
}
free(pool->threads);
}