summaryrefslogtreecommitdiff
path: root/src/thread.c
blob: 30f5541670ad81444cbbd34d0c9673ae3f17de74 (plain)
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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
/*
 * Copyright (c) 2017-2018 Richard Braun.
 * Copyright (c) 2017 Jerko Lenstra.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#include <assert.h>
#include <errno.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

#include <lib/macros.h>
#include <lib/list.h>

#include "cpu.h"
#include "panic.h"
#include "thread.h"
#include "timer.h"

/*
 * The compiler expects the stack pointer to be properly aligned when a
 * function is called, and maintains this alignment across a call chain.
 * The constraints are similar to the return value of malloc().
 * See the description of mem_alloc() in mem.h.
 *
 * Note that modern compilers expect the stack to be 16-byte aligned
 * even on 32-bits i386 processors, to cope with SSE instructions which
 * don't support unaligned accesses (see a revised version of the System V
 * Intel386 ABI [1] for more details). Since all floating point support is
 * disabled when building the kernel, this requirement can be safely ignored
 * and the legacy 4-byte alignment used instead.
 *
 * [1] https://www.uclibc.org/docs/psABI-i386.pdf
 */
#define THREAD_STACK_ALIGN 4

/*
 * List of threads sharing the same priority.
 */
struct thread_list {
    struct list threads;
};

/*
 * Run queue.
 *
 * Threads that aren't in the running state aren't tracked in a run queue.
 * The current thread, which is the one currently running on the processor,
 * isn't in any of the thread lists.
 *
 * A thread may be be "voluntarily" preempted, when it yields the processor
 * itself, or "involuntarily" preempted when an interrupt occurs. Since it's
 * not possible to immediately yield the processor when preemption is
 * disabled (including in the middle of an interrupt handler), preemption
 * may only occur on specific events :
 *  - Calling thread_yield() with preemption enabled.
 *  - Preemption is reenabled, i.e. the preemption level goes back to 0.
 *  - Return from an interrupt.
 *
 * Calling thread_yield() is completely voluntary and is ignored in this
 * discussion. Returning from an interrupt actually reenables preemption,
 * so the only case discussed is reenabling preemption. The mechanism used
 * to trigger scheduling when preemption is reenabled is the yield flag.
 * When the scheduler determines that the current thread should yield the
 * processor, e.g. because an interrupt handler has awaken a higher priority
 * thread, it sets the yield flag of the run queue. When preemption is
 * reenabled, the flag is checked, and if true, the current thread yields
 * the processor.
 *
 * The idle thread runs when no other thread is in the running state.
 *
 * Interrupts and preemption must be disabled when accessing a run queue.
 * Interrupts must be disabled to prevent a timer interrupt from corrupting
 * the run queue. Preemption must be disabled to prevent an interrupt handler
 * from causing an early context switch when returning from interrupt.
 */
struct thread_runq {
    struct thread *current;
    bool yield;
    unsigned int preempt_level;
    unsigned int nr_threads;
    struct thread_list lists[THREAD_NR_PRIORITIES];
    struct thread *idle;
};

enum thread_state {
    THREAD_STATE_RUNNING,   /* Thread is in a run queue */
    THREAD_STATE_SLEEPING,  /* Thread is not running */
    THREAD_STATE_DEAD,      /* Thread is sleeping and may not be awaken */
};

/*
 * Thread structure.
 *
 * The purpose of a thread is to support saving and restoring state in order
 * to achieve time sharing between activities.
 *
 * A thread is mostly used to store the saved state of an activity, called
 * its context. The context is usually made up of some registers that are
 * saved on the stack, the stack itself, and the thread structure.
 * When the context is saved, the stack pointer is updated to the value
 * of the stack register at the time the thread context is saved.
 *
 * Accessing threads is subject to the same synchronization rules as a
 * run queue.
 */
struct thread {
    void *sp;
    enum thread_state state;
    struct list node;
    unsigned int priority;
    struct thread *joiner;
    char name[THREAD_NAME_MAX_SIZE];
    void *stack;
};

/*
 * Run queue singleton.
 */
static struct thread_runq thread_runq;

/*
 * Dummy thread context used to make functions that require a thread context
 * work before the thread module is fully initialized.
 */
static struct thread thread_dummy;

/*
 * Declarations for C/assembly functions that are global so that they can
 * be shared between thread.c and thread_asm.S, but are considered private to
 * the thread module.
 */
void thread_load_context(struct thread *thread) __attribute__((noreturn));
void thread_switch_context(struct thread *prev, struct thread *next);
void thread_main(thread_fn_t fn, void *arg);

/*
 * Function implementing the idle thread.
 */
static void
thread_idle(void *arg)
{
    (void)arg;

    for (;;) {
        cpu_idle();
    }
}

static bool
thread_scheduler_locked(void)
{
    return !cpu_intr_enabled() && !thread_preempt_enabled();
}

static void
thread_list_init(struct thread_list *list)
{
    list_init(&list->threads);
}

static void
thread_list_remove(struct thread *thread)
{
    list_remove(&thread->node);
}

static void
thread_list_enqueue(struct thread_list *list, struct thread *thread)
{
    list_insert_tail(&list->threads, &thread->node);
}

static struct thread *
thread_list_dequeue(struct thread_list *list)
{
    struct thread *thread;

    thread = list_first_entry(&list->threads, typeof(*thread), node);
    thread_list_remove(thread);
    return thread;
}

static bool
thread_list_empty(struct thread_list *list)
{
    return list_empty(&list->threads);
}

static bool
thread_is_running(const struct thread *thread)
{
    return thread->state == THREAD_STATE_RUNNING;
}

static void
thread_set_running(struct thread *thread)
{
    thread->state = THREAD_STATE_RUNNING;
}

static void
thread_set_sleeping(struct thread *thread)
{
    thread->state = THREAD_STATE_SLEEPING;
}

static bool
thread_is_dead(const struct thread *thread)
{
    return thread->state == THREAD_STATE_DEAD;
}

static void
thread_set_dead(struct thread *thread)
{
    thread->state = THREAD_STATE_DEAD;
}

static unsigned int
thread_get_priority(struct thread *thread)
{
    return thread->priority;
}

static bool
thread_runq_should_yield(const struct thread_runq *runq)
{
    return runq->yield;
}

static void
thread_runq_set_yield(struct thread_runq *runq)
{
    runq->yield = true;
}

static void
thread_runq_clear_yield(struct thread_runq *runq)
{
    runq->yield = false;
}

static unsigned int
thread_runq_get_preempt_level(const struct thread_runq *runq)
{
    return runq->preempt_level;
}

static void
thread_runq_inc_preempt_level(struct thread_runq *runq)
{
    /*
     * There is no need to protect this increment by disabling interrupts
     * or using a single interrupt-safe instruction such as inc, because
     * the preemption level can only be 0 when other threads access it
     * concurrently. As a result, all threads racing to disable preemption
     * would read 0 and write 1. Although the increment is not atomic, the
     * store is serialized such that the first thread that manages to update
     * memory has effectively prevented all other threads from running,
     * at least until it reenables preemption, which turns the preemption
     * level back to 0, making the read-modify-write increment other threads
     * may have started correct.
     */
    runq->preempt_level++;
    assert(runq->preempt_level != 0);
}

static void
thread_runq_dec_preempt_level(struct thread_runq *runq)
{
    assert(runq->preempt_level != 0);
    runq->preempt_level--;
}

static struct thread_list *
thread_runq_get_list(struct thread_runq *runq, unsigned int priority)
{
    assert(priority < ARRAY_SIZE(runq->lists));
    return &runq->lists[priority];
}

static struct thread *
thread_runq_get_current(struct thread_runq *runq)
{
    return runq->current;
}

static void
thread_runq_put_prev(struct thread_runq *runq, struct thread *thread)
{
    struct thread_list *list;

    if (thread == runq->idle) {
        return;
    }

    list = thread_runq_get_list(runq, thread_get_priority(thread));
    thread_list_enqueue(list, thread);
}

static struct thread *
thread_runq_get_next(struct thread_runq *runq)
{
    struct thread *thread;

    assert(runq->current);

    if (runq->nr_threads == 0) {
        thread = runq->idle;
    } else {
        struct thread_list *list;
        size_t nr_lists;

        nr_lists = ARRAY_SIZE(runq->lists);

        /*
         * Note that size_t is unsigned, which means that when the iterator
         * is 0 and decremented, the new value is actually the maximum value
         * that the type may have, which is necessarily higher than the array
         * size. This behaviour is very well defined by the C specification
         * (6.3.1.3 Signed and unsigned integers), and avoids warnings about
         * mixing types of different signedness.
         */
        for (size_t i = (nr_lists - 1); i < nr_lists; i--) {
            list = thread_runq_get_list(runq, i);

            if (!thread_list_empty(list)) {
                break;
            }
        }

        thread = thread_list_dequeue(list);
    }

    runq->current = thread;
    return thread;
}

static void
thread_runq_add(struct thread_runq *runq, struct thread *thread)
{
    struct thread_list *list;

    assert(thread_scheduler_locked());
    assert(thread_is_running(thread));

    list = thread_runq_get_list(runq, thread_get_priority(thread));
    thread_list_enqueue(list, thread);

    runq->nr_threads++;
    assert(runq->nr_threads != 0);

    if (thread_get_priority(thread) > thread_get_priority(runq->current)) {
        thread_runq_set_yield(runq);
    }
}

static void
thread_runq_remove(struct thread_runq *runq, struct thread *thread)
{
    assert(runq->nr_threads != 0);
    runq->nr_threads--;

    assert(!thread_is_running(thread));
    thread_list_remove(thread);
}

static void
thread_runq_schedule(struct thread_runq *runq)
{
    struct thread *prev, *next;

    prev = thread_runq_get_current(runq);

    assert(thread_scheduler_locked());
    assert(runq->preempt_level == 1);

    thread_runq_put_prev(runq, prev);

    if (!thread_is_running(prev)) {
        thread_runq_remove(runq, prev);
    }

    next = thread_runq_get_next(runq);

    if (prev != next) {
        /*
         * When switching context, it is extremely important that no
         * data access generated by the compiler "leak" across the switch.
         * All operations (i.e. side effects) started before the switch
         * should complete before the switch, and all operations starting
         * after the switch should start after the switch.
         *
         * This is what allows a thread waiting for an event to reliably
         * "see" that event after another thread or interrupt handler has
         * triggered it.
         *
         * This requires a barrier, and, since this is a single-processor
         * scheduler, a compiler barrier (as opposed to memory barriers)
         * is enough. But there is no such barrier here. The reason is that
         * the context switch is implemented in assembly, and the compiler
         * is unable to understand what the assembly code does. As a result,
         * even with aggressive optimizations enabled, the compiler has to
         * assume that memory may have changed in completely unexpected ways,
         * which is equivalent to the inline assembly expression used to
         * implement compiler barriers with GCC (see barrier() in macros.h).
         *
         * See thread_preempt_disable() for a description of compiler barriers.
         */
        thread_switch_context(prev, next);
    }
}

static void
thread_yield_if_needed(void)
{
    if (thread_runq_should_yield(&thread_runq)) {
        thread_yield();
    }
}

void
thread_preempt_disable(void)
{
    thread_runq_inc_preempt_level(&thread_runq);

    /*
     * This is a compiler barrier. It tells the compiler not to reorder
     * the instructions it emits across this point.
     *
     * Reordering is one of the most effective ways to optimize generated
     * machine code, and the C specification allows compilers to optimize
     * very aggressively, which is why C shouldn't be thought of as a
     * "portable assembler" any more.
     *
     * Sometimes, especially when building critical sections, strong control
     * over ordering is required, and this is when compiler barriers should
     * be used. In this kernel, disabling preemption is one of the primary
     * ways to create critical sections. The following barrier makes sure
     * that no instructions from the critical section leak out before it.
     *
     * When using GCC or a compatible compiler such as Clang, compiler
     * barriers are implemented with an inline assembly expression that
     * includes the "memory" clobber. This clobber tells the compiler that
     * memory may change in unexpected ways. All memory accesses started
     * before the barrier must complete before the barrier, and all memory
     * accesses that complete after the barrier must start after the barrier.
     * See barrier() in macros.h.
     *
     * When calling assembly functions, there is usually no need to add
     * an explicit compiler barrier, because the compiler, not knowing
     * what the external function does, normally assumes memory may change,
     * i.e. that the function has unexpected side effects. In C code however,
     * the compiler has better knowledge about side effects. That knowledge
     * comes from the amount of code in a single compilation unit. The
     * traditional compilation unit when building with optimizations is the
     * source file, but with link-time optimizations (LTO), this can be the
     * whole program. This is why specifying barriers in C code is required
     * for a reliable behaviour.
     *
     * Also note that compiler barriers only affect the machine code
     * generated by the compiler. Processors also internally perform
     * various kinds of reordering. When confined to a single processor,
     * this can safely be ignored because the processor model guarantees
     * that the state seen by software matches program order. This
     * assumption breaks on multiprocessor systems though, where memory
     * barriers are also required to enforce ordering across processors.
     */
    barrier();
}

static void
thread_preempt_enable_no_yield(void)
{
    /* See thread_preempt_disable() */
    barrier();

    thread_runq_dec_preempt_level(&thread_runq);
}

void
thread_preempt_enable(void)
{
    thread_preempt_enable_no_yield();
    thread_yield_if_needed();
}

static unsigned int
thread_preempt_level(void)
{
    return thread_runq_get_preempt_level(&thread_runq);
}

bool
thread_preempt_enabled(void)
{
    return thread_preempt_level() == 0;
}

static uint32_t
thread_lock_scheduler(void)
{
    /*
     * When disabling both preemption and interrupts, it is best to do it in
     * this order, since, if an interrupt occurs after preemption is disabled
     * but before interrupts are disabled, it may not cause a context switch.
     *
     * Besides, disabling interrupts first would slightly increase interrupt
     * latencies.
     */
    thread_preempt_disable();
    return cpu_intr_save();
}

static void
thread_unlock_scheduler(uint32_t eflags, bool yield)
{
    cpu_intr_restore(eflags);

    if (yield) {
        thread_preempt_enable();
    } else {
        thread_preempt_enable_no_yield();
    }
}

void
thread_enable_scheduler(void)
{
    struct thread *thread;

    assert(thread_preempt_level() == 1);

    thread = thread_runq_get_next(&thread_runq);
    thread_load_context(thread);

    /* Never reached */
}

void
thread_main(thread_fn_t fn, void *arg)
{
    assert(fn);

    assert(!cpu_intr_enabled());
    assert(thread_preempt_level() == 1);

    cpu_intr_enable();
    thread_preempt_enable();

    fn(arg);

    thread_exit();
}

const char *
thread_name(const struct thread *thread)
{
    return thread->name;
}

static void
thread_set_name(struct thread *thread, const char *name)
{
    snprintf(thread->name, sizeof(thread->name), "%s", name);
}

static void
thread_stack_push(uint32_t **stackp, size_t *stack_sizep, uint32_t word)
{
    uint32_t *stack;
    size_t stack_size;

    stack = *stackp;
    stack_size = *stack_sizep;
    assert(stack_size >= sizeof(word));
    stack--;
    stack_size -= sizeof(word);
    *stack = word;
    *stackp = stack;
    *stack_sizep = stack_size;
}

static void *
thread_stack_forge(char *stack_addr, size_t stack_size,
                   thread_fn_t fn, void *arg)
{
    uint32_t *stack;

    stack = (uint32_t *)(stack_addr + stack_size);

    /*
     * This part of the stack makes context restoration "return" to
     * thread_main() as if it were called from address 0 (which stops
     * backtracing when using a debugger).
     *
     * This is how an assembly call to thread_main() looks like, according
     * to the ABI (System V Intel 386 ABI [1]) :
     *  push arg
     *  push fn
     *  call thread_main
     *
     * Remember that the call instruction pushes the return address on the
     * stack.
     *
     * [1] http://www.sco.com/developers/devspecs/abi386-4.pdf
     */
    thread_stack_push(&stack, &stack_size, (uint32_t)arg); /* 2nd argument */
    thread_stack_push(&stack, &stack_size, (uint32_t)fn);  /* 1st argument */
    thread_stack_push(&stack, &stack_size, (uint32_t)0);   /* Return address */
    thread_stack_push(&stack, &stack_size, (uint32_t)thread_main);

    /*
     * This part of the stack contains the registers that should be restored.
     * The selection of the registers to save is made according to the
     * ABI, which specifies which registers are owned by the caller, and
     * which are owned by the callee. Since, in all cases, switching context
     * is achieved by calling the thread_switch_context() function, it
     * is safe to rely on the ABI for this selection. Naturally, the
     * registers that must be saved are those owned by the caller, since
     * the compiler assumes all registers owned by the callee may have
     * changed on return. See the System V Intel386 ABI "Registers and the
     * Stack Frame".
     *
     * For debugging purposes, a complete save of all the registers may be
     * performed instead, allowing precise inspection of the state of a
     * thread not currently running on the processor.
     *
     * It is recommended to read the assembly code at the thread_restore_context
     * label in thread_asm.S to better understand this stack frame.
     */
    thread_stack_push(&stack, &stack_size, 0);              /* EBP */
    thread_stack_push(&stack, &stack_size, 0);              /* EBX */
    thread_stack_push(&stack, &stack_size, 0);              /* EDI */
    thread_stack_push(&stack, &stack_size, 0);              /* ESI */

    return stack;
}

static void
thread_init(struct thread *thread, thread_fn_t fn, void *arg,
            const char *name, char *stack, size_t stack_size,
            unsigned int priority)
{
    assert(P2ALIGNED((uintptr_t)stack, THREAD_STACK_ALIGN));

    /*
     * New threads are created in a state that is similar to preempted threads,
     * since it makes running them for the first time indistinguishable from
     * redispatching a thread that has actually been preempted. This state
     * is very specific and includes the following properties :
     *  - state is running
     *  - interrupts are disabled
     *  - preemption level must be exactly one (see the description of
     *    thread_sleep() in thread.h)
     *
     * A state is artificially forged on the new stack to make it look like
     * the new thread has been preempted.
     */

    if (stack) {
        thread->sp = thread_stack_forge(stack, stack_size, fn, arg);
    }

    thread->state = THREAD_STATE_RUNNING;
    thread->priority = priority;
    thread->joiner = NULL;
    thread_set_name(thread, name);
    thread->stack = stack;
}

int
thread_create(struct thread **threadp, thread_fn_t fn, void *arg,
              const char *name, size_t stack_size, unsigned int priority)
{
    struct thread *thread;
    uint32_t eflags;
    void *stack;

    assert(fn);

    thread = malloc(sizeof(*thread));

    if (!thread) {
        return ENOMEM;
    }

    if (stack_size < THREAD_STACK_MIN_SIZE) {
        stack_size = THREAD_STACK_MIN_SIZE;
    }

    stack = malloc(stack_size);

    if (!stack) {
        free(thread);
        return ENOMEM;
    }

    thread_init(thread, fn, arg, name, stack, stack_size, priority);

    eflags = thread_lock_scheduler();
    thread_runq_add(&thread_runq, thread);
    thread_unlock_scheduler(eflags, true);

    if (threadp) {
        *threadp = thread;
    }

    return 0;
}

static void
thread_destroy(struct thread *thread)
{
    assert(thread_is_dead(thread));

    free(thread->stack);
    free(thread);
}

void
thread_exit(void)
{
    struct thread *thread;

    thread = thread_self();

    assert(thread_preempt_enabled());

    thread_lock_scheduler();
    assert(thread_is_running(thread));
    thread_set_dead(thread);
    thread_wakeup(thread->joiner);
    thread_runq_schedule(&thread_runq);

    panic("thread: error: dead thread walking");
}

void
thread_join(struct thread *thread)
{
    uint32_t eflags;

    eflags = thread_lock_scheduler();

    thread->joiner = thread_self();

    while (!thread_is_dead(thread)) {
        thread_sleep();
    }

    thread_unlock_scheduler(eflags, true);

    thread_destroy(thread);
}

struct thread *
thread_self(void)
{
    return thread_runq_get_current(&thread_runq);
}

static struct thread *
thread_create_idle(void)
{
    struct thread *idle;
    void *stack;

    idle = malloc(sizeof(*idle));

    if (!idle) {
        panic("thread: unable to allocate idle thread");
    }

    stack = malloc(THREAD_STACK_MIN_SIZE);

    if (!stack) {
        panic("thread: unable to allocate idle thread stack");
    }

    thread_init(idle, thread_idle, NULL, "idle",
                stack, THREAD_STACK_MIN_SIZE, THREAD_IDLE_PRIORITY);
    return idle;
}

static void
thread_runq_init(struct thread_runq *runq)
{
    /*
     * Set a dummy thread context with preemption disabled to prevent
     * scheduling functions called before the scheduler is running from
     * triggering a context switch.
     */
    thread_init(&thread_dummy, NULL, NULL, "dummy", NULL, 0, 0);
    runq->current = &thread_dummy;
    runq->yield = false;
    runq->preempt_level = 1;
    runq->nr_threads = 0;

    for (size_t i = 0; i < ARRAY_SIZE(runq->lists); i++) {
        thread_list_init(&runq->lists[i]);
    }
}

static void
thread_runq_init_idle(struct thread_runq *runq)
{
    runq->idle = thread_create_idle();
}

void
thread_bootstrap(void)
{
    thread_runq_init(&thread_runq);
}

void
thread_setup(void)
{
    thread_runq_init_idle(&thread_runq);
}

void
thread_yield(void)
{
    uint32_t eflags;

    if (!thread_preempt_enabled()) {
        return;
    }

    eflags = thread_lock_scheduler();
    thread_runq_clear_yield(&thread_runq);
    thread_runq_schedule(&thread_runq);
    thread_unlock_scheduler(eflags, false);
}

void
thread_sleep(void)
{
    struct thread *thread;
    uint32_t eflags;

    thread = thread_self();

    eflags = cpu_intr_save();
    assert(thread_is_running(thread));
    thread_set_sleeping(thread);
    thread_runq_schedule(&thread_runq);
    assert(thread_is_running(thread));
    cpu_intr_restore(eflags);
}

void
thread_wakeup(struct thread *thread)
{
    uint32_t eflags;

    if (!thread || (thread == thread_self())) {
        return;
    }

    eflags = thread_lock_scheduler();

    if (!thread_is_running(thread)) {
        assert(!thread_is_dead(thread));
        thread_set_running(thread);
        thread_runq_add(&thread_runq, thread);
    }

    thread_unlock_scheduler(eflags, true);
}

void
thread_report_tick(void)
{
    assert(thread_scheduler_locked());

    thread_runq_set_yield(&thread_runq);
    timer_report_tick();
}