-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththread_pool.c
373 lines (336 loc) · 10.3 KB
/
thread_pool.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
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
#include <stdint.h>
#include <stdatomic.h>
typedef enum
{
Working,
Stealing,
} ThreadState;
typedef struct
{
uint32_t id;
ThreadState state;
Rng64 rng;
WorkStealingDeque task_pool;
BufferPool buffer_pool;
IoRequestPool io_request_pool;
AsyncIoContext io_context;
IoContinuationList io_continuation_list;
_Atomic bool terminate;
} Thread;
static _Thread_local Thread MyThread;
typedef struct
{
_Atomic int32_t threads_created;
_Atomic int32_t threads_destroyed;
Slab slab;
uint64_t total_memory;
uint32_t num_threads;
Thread **threads;
Error *errors;
} ThreadTeam;
static ThreadTeam Team;
static void WaitForCreation()
{
while(atomic_load_explicit(&Team.threads_created, memory_order_relaxed) < Team.num_threads)
Yield_PlatformSpecific();
}
static void WaitForTermination()
{
while(atomic_load_explicit(&Team.threads_destroyed, memory_order_relaxed) < Team.num_threads)
Yield_PlatformSpecific();
}
static bool InitThread_Threadpool(uint32_t id)
{
InitDeque(&MyThread.task_pool, &MyThread.buffer_pool);
MyThread.id = id;
MyThread.state = Stealing;
atomic_store_explicit(&MyThread.terminate, false, memory_order_relaxed);
Team.threads[id] = &MyThread;
atomic_fetch_add_explicit(&Team.threads_created, 1, memory_order_relaxed);
if(id != 0)
WaitForCreation();
return true;
}
static bool InitThread(uint32_t id);
static void *TryStealBuffer()
{
int32_t num_other_threads = (int32_t)(Team.num_threads - 1);
uint32_t permutation[num_other_threads];
for(int32_t i = 0; i < MyThread.id && i < num_other_threads; i++)
permutation[i] = i;
for(int32_t i = MyThread.id + 1; i <= num_other_threads; i++)
permutation[i - 1] = i;
for(int32_t i = 0; i < num_other_threads - 2; i++)
{
uint32_t remaining = num_other_threads - i - 1;
uint32_t j = (uint32_t)(RngNext(&MyThread.rng) % remaining) + i;
uint32_t tmp = permutation[i];
permutation[i] = permutation[j];
permutation[j] = tmp;
}
const int32_t num_tries = 2;
for(int i = 0; i < num_other_threads; i++)
{
uint32_t victim = permutation[i];
BufferPool *pool = &Team.threads[victim]->buffer_pool;
void *buffer = StealBuffer(pool);
if(buffer)
return buffer;
}
return NULL;
}
static bool PollTaskQueue(uint32_t id)
{
if(MyThread.state == Working)
{
Task t = PopTask(&MyThread.task_pool);
if(!t.fn)
MyThread.state = Stealing;
else
return t.fn(id, t.capture);
}
else
{
uint32_t victim = (uint32_t)(RngNext(&MyThread.rng) % Team.num_threads);
Task t = StealTask(&Team.threads[victim]->task_pool);
if(t.fn)
return t.fn(id, t.capture);
}
return true;
}
static bool PollAsyncIoSubmissions(bool *out_did_something, uint32_t id, uint32_t victim)
{
uint32_t submitted = IoSubmissionsInFlight_PlatformSpecific(&MyThread.io_context);
uint32_t completed = IoCompletionsInFlight_PlatformSpecific(&MyThread.io_context);
uint32_t max_submitted = MaxSubmissions_PlatformSpecific(&MyThread.io_context);
uint32_t max_completed = MaxCompletions_PlatformSpecific(&MyThread.io_context);
if(submitted + completed >= max_completed)
return true;
if(submitted >= max_submitted)
return true;
uint32_t can_submit = max_submitted - submitted;
for(uint32_t i = 0; i < can_submit; i++)
{
IoRequest request;
if(id == victim)
PopIoRequest(&request, &MyThread.io_request_pool);
else
StealIoRequest(&request, &Team.threads[victim]->io_request_pool);
if(request.op_code == IO_Null)
break;
CHECK(SubmitIoRequest(&MyThread.io_context, &MyThread.io_continuation_list, &request));
}
return true;
}
static bool PollAsyncIoCompletions(bool *out_did_something, uint32_t id, uint32_t victim)
{
uint64_t idx;
CHECK(PollAsyncIo_PlatformSpecific(&idx, &Team.threads[victim]->io_context));
if(idx == -1)
return true;
*out_did_something = true;
Task t = (id == victim) ? FreeContinuation(&MyThread.io_continuation_list, idx)
: FreeContinuation_Thief(&Team.threads[victim]->io_continuation_list, idx);
return t.fn(id, t.capture);
}
static bool PollAsyncIo(uint32_t id)
{
{
bool did_something = false;
CHECK(PollAsyncIoSubmissions(&did_something, id, id));
if(!did_something)
{
uint32_t victim = (uint32_t)(RngNext(&MyThread.rng) % Team.num_threads);
CHECK(PollAsyncIoSubmissions(&did_something, id, victim));
}
}
{
bool did_something = false;
CHECK(PollAsyncIoCompletions(&did_something, id, id));
if(!did_something)
{
uint32_t victim = (uint32_t)(RngNext(&MyThread.rng) % Team.num_threads);
CHECK(PollAsyncIoCompletions(&did_something, id, victim));
}
}
return true;
}
static void TerminateOthers()
{
for(uint32_t i = 0; i < Team.num_threads; i++)
atomic_store_explicit(&Team.threads[i]->terminate, true, memory_order_seq_cst);
}
static void CleanupThread()
{
CleanupAsyncIO_PlatformSpecific(&MyThread.io_context);
atomic_fetch_add_explicit(&Team.threads_destroyed, 1, memory_order_relaxed);
}
static void StopThreadTeam()
{
atomic_fetch_add_explicit(&Team.threads_destroyed, 1, memory_order_relaxed);
TerminateOthers();
WaitForTermination();
}
static ThreadReturnValue WaitForWork(void *arg)
{
uint32_t id = (uint32_t)(uintptr_t)arg;
if(!InitThread(id))
{
StopThreadTeam();
CleanupThread();
return 0;
}
WaitForCreation();
for(bool poll_io = false;
!atomic_load_explicit(&MyThread.terminate, memory_order_relaxed);
poll_io = !poll_io)
{
atomic_thread_fence(memory_order_seq_cst);
bool success = poll_io ? PollAsyncIo(id) : PollTaskQueue(id);
if(!success)
{
TerminateOthers();
break;
}
}
CleanupThread();
WaitForTermination();
return 0;
}
static bool CreateThread(uint32_t id)
{
if(id == 0)
{
CHECK(InitThread(id));
}
else
{
CHECK(CreateThread_PlatformSpecific(WaitForWork, (void *)(intptr_t)id));
}
return true;
}
static bool InitThreadTeam()
{
Team.threads_created = 0;
CHECK(MapVirtualMemory_PlatformSpecific(&Team.slab.ptr, 4096));
Team.slab.size = 4096;
Team.slab.offset = 0;
uint64_t current_usage = GetCurrentRamUsage_PlatformSpecific();
Team.total_memory = (GetTotalRam_PlatformSpecific() / 2) - current_usage;
Team.num_threads = GetNumCores_PlatformSpecific();
Team.threads = AllocateFromSlab(&Team.slab, sizeof(Thread *) * Team.num_threads);
Team.errors = AllocateFromSlab(&Team.slab, sizeof(Error) * Team.num_threads);
for(int i = 0; i < Team.num_threads; i++)
CreateThread(i);
WaitForCreation();
return true;
}
bool ExecuteUntilTrue(_Atomic bool *condition)
{
assert(MyThread.id == 0);
const uint32_t id = 0;
for(bool poll_io = false;
!atomic_load_explicit(condition, memory_order_relaxed);
poll_io = !poll_io)
{
atomic_thread_fence(memory_order_seq_cst);
bool success = poll_io ? PollAsyncIo(id) : PollTaskQueue(id);
if(!success)
{
StopThreadTeam();
return false;
}
if(atomic_load_explicit(&MyThread.terminate, memory_order_relaxed))
{
StopThreadTeam();
for(uint32_t i = 0; i < Team.num_threads; i++)
{
if(Team.errors[i].type != E_None)
{
Team.errors[0] = Team.errors[i];
return false;
}
}
return true;
}
}
return true;
}
static bool ScheduleIoOperation(IoRequest *request)
{
uint32_t submissions = IoSubmissionsInFlight_PlatformSpecific(&MyThread.io_context);
uint32_t completions = IoCompletionsInFlight_PlatformSpecific(&MyThread.io_context);
uint32_t max_num_submissions = MaxSubmissions_PlatformSpecific(&MyThread.io_context);
uint32_t max_num_completions = MaxCompletions_PlatformSpecific(&MyThread.io_context);
bool can_submit = submissions < max_num_submissions
&& (submissions + completions) < max_num_completions;
if(can_submit)
return SubmitIoRequest(&MyThread.io_context, &MyThread.io_continuation_list, request);
else
return PushIoRequest(&MyThread.io_request_pool, request, &MyThread.buffer_pool);
}
bool ScheduleTask(TaskFn fn, Capture capture)
{
Task t;
t.capture = capture;
t.fn = fn;
MyThread.state = Working;
return PushTask(&MyThread.task_pool, &MyThread.buffer_pool, t);
}
bool ReadFile(FileHandle fd, void *buffer, uint64_t offset, uint64_t num_bytes, TaskFn on_finished, Capture capture)
{
IoRequest request;
request.op_code = IO_Read;
request.fd = fd;
request.buffer = buffer;
request.offset = offset;
request.num_bytes = num_bytes;
request.on_finished = (uintptr_t)on_finished;
request.capture_ptr = (uintptr_t)capture.ptr;
request.capture_number = capture.number;
return ScheduleIoOperation(&request);
}
bool WriteFile(FileHandle fd, void *buffer, uint64_t offset, uint64_t num_bytes, TaskFn on_finished, Capture capture)
{
IoRequest request;
request.op_code = IO_Write;
request.fd = fd;
request.buffer = buffer;
request.offset = offset;
request.num_bytes = num_bytes;
request.on_finished = (uintptr_t)on_finished;
request.capture_ptr = (uintptr_t)capture.ptr;
request.capture_number = capture.number;
return ScheduleIoOperation(&request);
}
bool GetSlab(Slab *out_slab)
{
void *buffer = GetBuffer(&MyThread.buffer_pool);
if(!buffer)
{
buffer = TryStealBuffer();
if(!buffer)
return AllocateSlab(out_slab, &MyThread.buffer_pool);
}
out_slab->ptr = buffer;
out_slab->size = SlabSize;
out_slab->offset = 0;
return true;
}
bool ReturnSlab(Slab slab)
{
return ReturnBuffer(slab.ptr, &MyThread.buffer_pool);
}
bool ReturnArena(Arena *in_out_arena)
{
ArenaMark mark = { NULL, 0 };
return RestoreArenaMark(in_out_arena, mark);
}
uint32_t GetThreadIndex()
{
return MyThread.id;
}
uint32_t GetNumThreads()
{
return Team.num_threads;
}