-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paththread.c
505 lines (393 loc) · 9.14 KB
/
thread.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
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
#define _XOPEN_SOURCE 600
#undef _FORTIFY_SOURCE
#define _FORTIFY_SOURCE 0
#include <stdatomic.h>
#include <stddef.h>
#include <setjmp.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <time.h>
#include <signal.h>
#include <sys/time.h>
struct list_head {
struct list_head *next;
struct list_head *prev;
};
void list_init(struct list_head *list)
{
list->next = list->prev = list;
}
int list_empty(const struct list_head *list)
{
return list->next == list;
}
void _list_insert(struct list_head *node, struct list_head *prev,
struct list_head *next)
{
node->prev = prev;
node->next = next;
prev->next = node;
next->prev = node;
}
void list_add_tail(struct list_head *node, struct list_head *list)
{
_list_insert(node, list->prev, list);
}
void list_add(struct list_head *node, struct list_head *list)
{
_list_insert(node, list, list->next);
}
void list_del(struct list_head *node)
{
struct list_head *prev = node->prev;
struct list_head *next = node->next;
prev->next = next;
next->prev = prev;
}
void _list_splice(struct list_head *first, struct list_head *last,
struct list_head *prev)
{
struct list_head *next = prev->next;
first->prev = prev;
last->next = next;
prev->next = first;
next->prev = last;
}
void list_splice(struct list_head *from, struct list_head *to)
{
if (list_empty(from))
return;
struct list_head *first = from->next;
struct list_head *last = from->prev;
list_init(from);
_list_splice(first, last, to);
}
void list_splice_tail(struct list_head *from, struct list_head *to)
{
if (list_empty(from))
return;
struct list_head *first = from->next;
struct list_head *last = from->prev;
list_init(from);
_list_splice(first, last, to->prev);
}
enum thread_state {
THREAD_ACTIVE,
THREAD_BLOCKED,
THREAD_FINISHING,
THREAD_DEAD
};
struct thread {
struct list_head ll;
void *stack;
size_t stack_size;
enum thread_state state;
jmp_buf ctx;
void (*entry)(void *);
void *arg;
int first_time;
};
static struct thread *current;
static struct thread *idle;
static struct list_head ready;
static int blocked;
int block_signals(void)
{
const int ret = !blocked;
sigset_t ss;
sigfillset(&ss);
assert(!sigprocmask(SIG_BLOCK, &ss, NULL));
blocked = 1;
return ret;
}
void unblock_signals(int enable)
{
if (!enable)
return;
sigset_t ss;
blocked = 0;
sigfillset(&ss);
assert(!sigprocmask(SIG_UNBLOCK, &ss, NULL));
}
void _thread_place(struct thread *thread)
{
if (current->state == THREAD_FINISHING)
current->state = THREAD_DEAD;
current = thread;
}
void _thread_entry(struct thread *thread)
{
void thread_exit(void);
assert(blocked);
_thread_place(thread);
thread->first_time = 0;
unblock_signals(1);
thread->entry(thread->arg);
thread_exit();
}
void _thread_switch(struct thread *from, struct thread *to)
{
assert(blocked);
if (setjmp(from->ctx)) {
_thread_place(from);
return;
}
if (!to->first_time)
longjmp(to->ctx, 1);
char *ptr = (char *)to->stack + to->stack_size;
/**
We need to change stack. Probably it's possible to avoid assembly
here using sigaltstack in the following way:
- allocate a memory region and setup it as an alternate stack
- generate the signal that uses the alternate stack
- inside the handler call setjmp to save the handler context
- use longjmp to jump to the saved context as usual
As you can see this approach is quite complicated. Moreover since
there is no way to pass an argument to a signal handler we would
need to use global variables and so need to consider protection
agains concurrent calls to the routine creating new thread.
All in all, it's much simpler to check whether we switch to a
thread for a first time and switch stacks using assembly code.
**/
__asm__ ("movq %0, %%rdi; movq %1, %%rsp; callq _thread_entry\n"
:
: "r"(to), "r"(ptr)
: "rdi", "rsp", "memory");
}
void _thread_setup(struct thread *thread,
void (*entry)(void *), void *arg,
size_t stack)
{
struct sigaction sa;
assert(thread->stack = malloc(stack));
thread->stack_size = stack;
thread->entry = entry;
thread->arg = arg;
thread->first_time = 1;
}
void thread_setup(struct thread *thread, void (*entry)(void *), void *arg)
{
const size_t DEFAULT_STACK = (size_t)2 * 1024 * 1024;
_thread_setup(thread, entry, arg, DEFAULT_STACK);
}
void thread_release(struct thread *thread)
{
free(thread->stack);
}
struct thread *thread_current(void)
{
return current;
}
void timer_handler(int unused)
{
void schedule(void);
(void) unused;
schedule();
}
void scheduler_setup(void)
{
static struct thread me;
struct itimerval period;
struct sigaction sa;
list_init(&ready);
me.state = THREAD_ACTIVE;
current = &me;
idle = &me;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = &timer_handler;
sa.sa_flags = SA_NODEFER | SA_RESTART;
assert(!sigaction(SIGVTALRM, &sa, NULL));
memset(&period, 0, sizeof(period));
period.it_interval.tv_usec = 5000;
period.it_value.tv_usec = 5000;
assert(!setitimer(ITIMER_VIRTUAL, &period, NULL));
}
void schedule(void)
{
struct thread *me = thread_current();
struct thread *next = NULL;
const int enabled = block_signals();
assert(enabled);
if (!list_empty(&ready)) {
next = (struct thread *)ready.next;
list_del(&next->ll);
}
if (!next && me->state != THREAD_ACTIVE)
next = idle;
if (!next) {
unblock_signals(enabled);
return;
}
if (me != idle && me->state == THREAD_ACTIVE)
list_add_tail(&me->ll, &ready);
_thread_switch(me, next);
unblock_signals(enabled);
}
void thread_join(struct thread *thread)
{
int enabled = block_signals();
assert(enabled);
while (thread->state != THREAD_DEAD) {
unblock_signals(enabled);
schedule();
enabled = block_signals();
}
unblock_signals(enabled);
}
void thread_start(struct thread *thread)
{
const int enabled = block_signals();
thread->state = THREAD_ACTIVE;
list_add_tail(&thread->ll, &ready);
unblock_signals(enabled);
}
void thread_wake(struct thread *thread)
{
thread_start(thread);
}
void thread_block(void)
{
struct thread *me = thread_current();
const int enabled = block_signals();
me->state = THREAD_BLOCKED;
unblock_signals(enabled);
}
void thread_exit(void)
{
struct thread *me = thread_current();
const int enabled = block_signals();
assert(enabled);
me->state = THREAD_FINISHING;
unblock_signals(enabled);
schedule();
}
struct lock {
struct list_head wait;
struct thread *owner;
int magic;
};
struct _wait {
struct list_head ll;
struct thread *thread;
};
void lock_init(struct lock *lock)
{
list_init(&lock->wait);
lock->owner = NULL;
lock->magic = 1263575;
}
void lock_check_magic(const struct lock *lock)
{
assert(lock->magic == 1263575);
}
void lock(struct lock *lock)
{
lock_check_magic(lock);
int enabled = block_signals();
struct thread *thread = thread_current();
struct _wait wait;
assert(enabled);
wait.thread = thread;
list_add_tail(&wait.ll, &lock->wait);
while (lock->owner || lock->wait.next != &wait.ll) {
thread_block();
unblock_signals(enabled);
schedule();
enabled = block_signals();
assert(enabled);
}
lock->owner = thread;
list_del(&wait.ll);
unblock_signals(enabled);
}
void unlock(struct lock *lock)
{
lock_check_magic(lock);
const int enabled = block_signals();
lock->owner = NULL;
if (!list_empty(&lock->wait)) {
struct _wait *wait = (struct _wait *)lock->wait.next;
thread_wake(wait->thread);
}
unblock_signals(enabled);
}
struct condition {
struct list_head wait;
int magic;
};
void condition_init(struct condition *cv)
{
list_init(&cv->wait);
cv->magic = 3981279;
}
void condition_check_magic(const struct condition *cv)
{
assert(cv->magic == 3981279);
}
void wait(struct condition *cv, struct lock *l)
{
condition_check_magic(cv);
const int enabled = block_signals();
struct thread *thread = thread_current();
struct _wait wait;
assert(enabled);
wait.thread = thread;
list_add_tail(&wait.ll, &cv->wait);
thread_block();
unblock_signals(enabled);
unlock(l);
schedule();
lock(l);
}
void notify_one(struct condition *cv)
{
condition_check_magic(cv);
const int enabled = block_signals();
if (!list_empty(&cv->wait)) {
struct _wait *wait = (struct _wait *)cv->wait.next;
list_del(&wait->ll);
thread_wake(wait->thread);
}
unblock_signals(enabled);
}
void notify_all(struct condition *cv)
{
condition_check_magic(cv);
const int enabled = block_signals();
struct list_head list;
list_init(&list);
list_splice(&cv->wait, &list);
unblock_signals(enabled);
for (struct list_head *ptr = list.next; ptr != &list; ) {
struct _wait *wait = (struct _wait *)ptr;
ptr = ptr->next;
thread_wake(wait->thread);
}
}
void threadx(void *arg)
{
struct thread *me = thread_current();
struct lock *io_lock = arg;
for (int i = 0; i != 5; ++i) {
lock(io_lock);
printf("Hello from %p\n", me);
unlock(io_lock);
schedule();
}
}
int main(void)
{
struct lock stdio_lock;
struct thread t[2];
scheduler_setup();
lock_init(&stdio_lock);
thread_setup(&t[0], &threadx, &stdio_lock); thread_start(&t[0]);
thread_setup(&t[1], &threadx, &stdio_lock); thread_start(&t[1]);
thread_join(&t[1]); thread_release(&t[1]);
thread_join(&t[0]); thread_release(&t[0]);
return 0;
}