-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy paththread.c
1231 lines (1023 loc) · 31 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
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
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2008-2014 Travis Geiselbrecht
*
* 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.
*/
/**
* @file
* @brief Kernel threading
*
* This file is the core kernel threading interface.
*
* @defgroup thread Threads
* @{
*/
#include <debug.h>
#include <assert.h>
#include <list.h>
#include <malloc.h>
#include <string.h>
#include <printf.h>
#include <err.h>
#include <lib/dpc.h>
#include <kernel/thread.h>
#include <kernel/timer.h>
#include <kernel/debug.h>
#include <kernel/mp.h>
#include <platform.h>
#include <target.h>
#include <lib/heap.h>
#if LK_DEBUGLEVEL > 1
#define THREAD_CHECKS 1
#endif
#if THREAD_STATS
struct thread_stats thread_stats[SMP_MAX_CPUS];
#endif
/* global thread list */
static struct list_node thread_list;
/* master thread spinlock */
spin_lock_t thread_lock = SPIN_LOCK_INITIAL_VALUE;
/* the run queue */
static struct list_node run_queue[NUM_PRIORITIES];
static uint32_t run_queue_bitmap;
/* make sure the bitmap is large enough to cover our number of priorities */
STATIC_ASSERT(NUM_PRIORITIES <= sizeof(run_queue_bitmap) * 8);
/* the idle thread(s) (statically allocated) */
static thread_t idle_threads[SMP_MAX_CPUS];
/* local routines */
static void thread_resched(void);
static void idle_thread_routine(void) __NO_RETURN;
#if PLATFORM_HAS_DYNAMIC_TIMER
/* preemption timer */
static timer_t preempt_timer[SMP_MAX_CPUS];
#endif
/* run queue manipulation */
static void insert_in_run_queue_head(thread_t *t)
{
#if THREAD_CHECKS
ASSERT(t->magic == THREAD_MAGIC);
ASSERT(t->state == THREAD_READY);
ASSERT(!list_in_list(&t->queue_node));
ASSERT(arch_ints_disabled());
ASSERT(spin_lock_held(&thread_lock));
#endif
list_add_head(&run_queue[t->priority], &t->queue_node);
run_queue_bitmap |= (1<<t->priority);
}
static void insert_in_run_queue_tail(thread_t *t)
{
#if THREAD_CHECKS
ASSERT(t->magic == THREAD_MAGIC);
ASSERT(t->state == THREAD_READY);
ASSERT(!list_in_list(&t->queue_node));
ASSERT(arch_ints_disabled());
ASSERT(spin_lock_held(&thread_lock));
#endif
list_add_tail(&run_queue[t->priority], &t->queue_node);
run_queue_bitmap |= (1<<t->priority);
}
static void init_thread_struct(thread_t *t, const char *name)
{
memset(t, 0, sizeof(thread_t));
t->magic = THREAD_MAGIC;
t->pinned_cpu = -1;
strlcpy(t->name, name, sizeof(t->name));
}
/**
* @brief Create a new thread
*
* This function creates a new thread. The thread is initially suspended, so you
* need to call thread_resume() to execute it.
*
* @param name Name of thread
* @param entry Entry point of thread
* @param arg Arbitrary argument passed to entry()
* @param priority Execution priority for the thread.
* @param stack_size Stack size for the thread.
*
* Thread priority is an integer from 0 (lowest) to 31 (highest). Some standard
* prioritys are defined in <kernel/thread.h>:
*
* HIGHEST_PRIORITY
* DPC_PRIORITY
* HIGH_PRIORITY
* DEFAULT_PRIORITY
* LOW_PRIORITY
* IDLE_PRIORITY
* LOWEST_PRIORITY
*
* Stack size is typically set to DEFAULT_STACK_SIZE
*
* @return Pointer to thread object, or NULL on failure.
*/
thread_t *thread_create_etc(thread_t *t, const char *name, thread_start_routine entry, void *arg, int priority, void *stack, size_t stack_size)
{
unsigned int flags = 0;
if (!t) {
t = malloc(sizeof(thread_t));
if (!t)
return NULL;
flags |= THREAD_FLAG_FREE_STRUCT;
}
init_thread_struct(t, name);
t->entry = entry;
t->arg = arg;
t->priority = priority;
t->state = THREAD_SUSPENDED;
t->blocking_wait_queue = NULL;
t->wait_queue_block_ret = NO_ERROR;
t->curr_cpu = -1;
t->retcode = 0;
wait_queue_init(&t->retcode_wait_queue);
/* create the stack */
if (!stack) {
t->stack = malloc(stack_size);
if (!t->stack) {
if (flags & THREAD_FLAG_FREE_STRUCT)
free(t);
return NULL;
}
flags |= THREAD_FLAG_FREE_STACK;
}
t->stack_size = stack_size;
/* save whether or not we need to free the thread struct and/or stack */
t->flags = flags;
/* inheirit thread local storage from the parent */
thread_t *current_thread = get_current_thread();
int i;
for (i=0; i < MAX_TLS_ENTRY; i++)
t->tls[i] = current_thread->tls[i];
/* set up the initial stack frame */
arch_thread_initialize(t);
/* add it to the global thread list */
THREAD_LOCK(state);
list_add_head(&thread_list, &t->thread_list_node);
THREAD_UNLOCK(state);
return t;
}
thread_t *thread_create(const char *name, thread_start_routine entry, void *arg, int priority, size_t stack_size)
{
return thread_create_etc(NULL, name, entry, arg, priority, NULL, stack_size);
}
/**
* @brief Flag a thread as real time
*
* @param t Thread to flag
*
* @return NO_ERROR on success
*/
status_t thread_set_real_time(thread_t *t)
{
if (!t)
return ERR_INVALID_ARGS;
#if THREAD_CHECKS
ASSERT(t->magic == THREAD_MAGIC);
#endif
THREAD_LOCK(state);
#if PLATFORM_HAS_DYNAMIC_TIMER
if (t == get_current_thread()) {
/* if we're currently running, cancel the preemption timer. */
timer_cancel(&preempt_timer[arch_curr_cpu_num()]);
}
#endif
t->flags |= THREAD_FLAG_REAL_TIME;
THREAD_UNLOCK(state);
return NO_ERROR;
}
static bool thread_is_realtime(thread_t *t)
{
return !!(t->flags & THREAD_FLAG_REAL_TIME);
}
static bool thread_is_idle(thread_t *t)
{
return !!(t->flags & THREAD_FLAG_IDLE);
}
static bool thread_is_real_time_or_idle(thread_t *t)
{
return !!(t->flags & (THREAD_FLAG_REAL_TIME | THREAD_FLAG_IDLE));
}
/**
* @brief Make a suspended thread executable.
*
* This function is typically called to start a thread which has just been
* created with thread_create()
*
* @param t Thread to resume
*
* @return NO_ERROR on success, ERR_NOT_SUSPENDED if thread was not suspended.
*/
status_t thread_resume(thread_t *t)
{
#if THREAD_CHECKS
ASSERT(t->magic == THREAD_MAGIC);
ASSERT(t->state != THREAD_DEATH);
#endif
bool resched = false;
THREAD_LOCK(state);
if (t->state == THREAD_SUSPENDED) {
t->state = THREAD_READY;
insert_in_run_queue_head(t);
resched = true;
}
mp_reschedule(MP_CPU_ALL_BUT_LOCAL, 0);
THREAD_UNLOCK(state);
if (resched)
thread_yield();
return NO_ERROR;
}
status_t thread_detach_and_resume(thread_t *t)
{
status_t err;
err = thread_detach(t);
if (err < 0)
return err;
return thread_resume(t);
}
status_t thread_join(thread_t *t, int *retcode, lk_time_t timeout)
{
#if THREAD_CHECKS
ASSERT(t->magic == THREAD_MAGIC);
#endif
THREAD_LOCK(state);
if (t->flags & THREAD_FLAG_DETACHED) {
/* the thread is detached, go ahead and exit */
THREAD_UNLOCK(state);
return ERR_THREAD_DETACHED;
}
/* wait for the thread to die */
if (t->state != THREAD_DEATH) {
status_t err = wait_queue_block(&t->retcode_wait_queue, timeout);
if (err < 0) {
THREAD_UNLOCK(state);
return err;
}
}
#if THREAD_CHECKS
ASSERT(t->magic == THREAD_MAGIC);
ASSERT(t->state == THREAD_DEATH);
ASSERT(t->blocking_wait_queue == NULL);
ASSERT(!list_in_list(&t->queue_node));
#endif
/* save the return code */
if (retcode)
*retcode = t->retcode;
/* remove it from the master thread list */
list_delete(&t->thread_list_node);
/* clear the structure's magic */
t->magic = 0;
THREAD_UNLOCK(state);
/* free its stack and the thread structure itself */
if (t->flags & THREAD_FLAG_FREE_STACK && t->stack)
free(t->stack);
if (t->flags & THREAD_FLAG_FREE_STRUCT)
free(t);
return NO_ERROR;
}
status_t thread_detach(thread_t *t)
{
#if THREAD_CHECKS
ASSERT(t->magic == THREAD_MAGIC);
#endif
THREAD_LOCK(state);
/* if another thread is blocked inside thread_join() on this thread,
* wake them up with a specific return code */
wait_queue_wake_all(&t->retcode_wait_queue, false, ERR_THREAD_DETACHED);
/* if it's already dead, then just do what join would have and exit */
if (t->state == THREAD_DEATH) {
t->flags &= ~THREAD_FLAG_DETACHED; /* makes sure thread_join continues */
THREAD_UNLOCK(state);
return thread_join(t, NULL, 0);
} else {
t->flags |= THREAD_FLAG_DETACHED;
THREAD_UNLOCK(state);
return NO_ERROR;
}
}
/**
* @brief Terminate the current thread
*
* Current thread exits with the specified return code.
*
* This function does not return.
*/
void thread_exit(int retcode)
{
thread_t *current_thread = get_current_thread();
#if THREAD_CHECKS
ASSERT(current_thread->magic == THREAD_MAGIC);
ASSERT(current_thread->state == THREAD_RUNNING);
ASSERT(!thread_is_idle(current_thread));
#endif
// dprintf("thread_exit: current %p\n", current_thread);
THREAD_LOCK(state);
/* enter the dead state */
current_thread->state = THREAD_DEATH;
current_thread->retcode = retcode;
/* if we're detached, then do our teardown here */
if (current_thread->flags & THREAD_FLAG_DETACHED) {
/* remove it from the master thread list */
list_delete(¤t_thread->thread_list_node);
/* clear the structure's magic */
current_thread->magic = 0;
/* free its stack and the thread structure itself */
if (current_thread->flags & THREAD_FLAG_FREE_STACK && current_thread->stack)
heap_delayed_free(current_thread->stack);
if (current_thread->flags & THREAD_FLAG_FREE_STRUCT)
heap_delayed_free(current_thread);
} else {
/* signal if anyone is waiting */
wait_queue_wake_all(¤t_thread->retcode_wait_queue, false, 0);
}
/* reschedule */
thread_resched();
panic("somehow fell through thread_exit()\n");
}
static void idle_thread_routine(void)
{
for (;;)
arch_idle();
}
static thread_t *get_top_thread(int cpu)
{
thread_t *newthread;
uint32_t local_run_queue_bitmap = run_queue_bitmap;
int next_queue;
while (local_run_queue_bitmap) {
next_queue = HIGHEST_PRIORITY - __builtin_clz(local_run_queue_bitmap) - (32 - NUM_PRIORITIES);
list_for_every_entry(&run_queue[next_queue], newthread, thread_t, queue_node) {
if (newthread->pinned_cpu < 0 || newthread->pinned_cpu == cpu) {
list_delete(&newthread->queue_node);
if (list_is_empty(&run_queue[next_queue]))
run_queue_bitmap &= ~(1<<next_queue);
return newthread;
}
}
local_run_queue_bitmap &= ~(1<<next_queue);
}
return NULL;
}
/**
* @brief Cause another thread to be executed.
*
* Internal reschedule routine. The current thread needs to already be in whatever
* state and queues it needs to be in. This routine simply picks the next thread and
* switches to it.
*
* This is probably not the function you're looking for. See
* thread_yield() instead.
*/
void thread_resched(void)
{
thread_t *oldthread;
thread_t *newthread;
thread_t *current_thread = get_current_thread();
uint cpu = arch_curr_cpu_num();
#if THREAD_CHECKS
ASSERT(arch_ints_disabled());
ASSERT(spin_lock_held(&thread_lock));
ASSERT(current_thread->state != THREAD_RUNNING);
#endif
THREAD_STATS_INC(reschedules);
newthread = get_top_thread(cpu);
if (unlikely(!newthread)) {
/* no threads to run, select the idle thread for this cpu */
newthread = &idle_threads[cpu];
}
#if THREAD_CHECKS
ASSERT(newthread);
#endif
newthread->state = THREAD_RUNNING;
oldthread = current_thread;
if (newthread == oldthread)
return;
/* set up quantum for the new thread if it was consumed */
if (newthread->remaining_quantum <= 0) {
newthread->remaining_quantum = 5; // XXX make this smarter
}
/* mark the cpu ownership of the threads */
oldthread->curr_cpu = -1;
newthread->curr_cpu = cpu;
if (thread_is_idle(newthread)) {
mp_set_cpu_idle(cpu);
} else {
mp_set_cpu_busy(cpu);
}
if (thread_is_realtime(newthread)) {
mp_set_cpu_realtime(cpu);
} else {
mp_set_cpu_non_realtime(cpu);
}
#if THREAD_STATS
THREAD_STATS_INC(context_switches);
if (thread_is_idle(oldthread)) {
lk_bigtime_t now = current_time_hires();
thread_stats[cpu].idle_time += now - thread_stats[cpu].last_idle_timestamp;
}
if (thread_is_idle(newthread)) {
thread_stats[cpu].last_idle_timestamp = current_time_hires();
}
#endif
KEVLOG_THREAD_SWITCH(oldthread, newthread);
#if PLATFORM_HAS_DYNAMIC_TIMER
if (thread_is_real_time_or_idle(newthread)) {
if (!thread_is_real_time_or_idle(oldthread)) {
/* if we're switching from a non real time to a real time, cancel
* the preemption timer. */
#ifdef DEBUG_THREAD_CONTEXT_SWITCH
dprintf(ALWAYS, "arch_context_switch: stop preempt, cpu %d, old %p (%s), new %p (%s)\n",
cpu, oldthread, oldthread->name, newthread, newthread->name);
#endif
timer_cancel(&preempt_timer[cpu]);
}
} else if (thread_is_real_time_or_idle(oldthread)) {
/* if we're switching from a real time (or idle thread) to a regular one,
* set up a periodic timer to run our preemption tick. */
#ifdef DEBUG_THREAD_CONTEXT_SWITCH
dprintf(ALWAYS, "arch_context_switch: start preempt, cpu %d, old %p (%s), new %p (%s)\n",
cpu, oldthread, oldthread->name, newthread, newthread->name);
#endif
timer_set_periodic(&preempt_timer[cpu], 10, (timer_callback)thread_timer_tick, NULL);
}
#endif
/* set some optional target debug leds */
target_set_debug_led(0, !thread_is_idle(&idle_threads[cpu]));
/* do the switch */
set_current_thread(newthread);
#ifdef DEBUG_THREAD_CONTEXT_SWITCH
dprintf(ALWAYS, "arch_context_switch: cpu %d, old %p (%s, pri %d, flags 0x%x), new %p (%s, pri %d, flags 0x%x)\n",
cpu, oldthread, oldthread->name, oldthread->priority,
oldthread->flags, newthread, newthread->name,
newthread->priority, newthread->flags);
#endif
#ifdef WITH_LIB_UTHREAD
uthread_context_switch(oldthread, newthread);
#endif
arch_context_switch(oldthread, newthread);
}
/**
* @brief Yield the cpu to another thread
*
* This function places the current thread at the end of the run queue
* and yields the cpu to another waiting thread (if any.)
*
* This function will return at some later time. Possibly immediately if
* no other threads are waiting to execute.
*/
void thread_yield(void)
{
thread_t *current_thread = get_current_thread();
#if THREAD_CHECKS
ASSERT(current_thread->magic == THREAD_MAGIC);
ASSERT(current_thread->state == THREAD_RUNNING);
#endif
THREAD_LOCK(state);
THREAD_STATS_INC(yields);
/* we are yielding the cpu, so stick ourselves into the tail of the run queue and reschedule */
current_thread->state = THREAD_READY;
current_thread->remaining_quantum = 0;
if (likely(!thread_is_idle(current_thread))) { /* idle thread doesn't go in the run queue */
insert_in_run_queue_tail(current_thread);
}
thread_resched();
THREAD_UNLOCK(state);
}
/**
* @brief Briefly yield cpu to another thread
*
* This function is similar to thread_yield(), except that it will
* restart more quickly.
*
* This function places the current thread at the head of the run
* queue and then yields the cpu to another thread.
*
* Exception: If the time slice for this thread has expired, then
* the thread goes to the end of the run queue.
*
* This function will return at some later time. Possibly immediately if
* no other threads are waiting to execute.
*/
void thread_preempt(void)
{
thread_t *current_thread = get_current_thread();
#if THREAD_CHECKS
ASSERT(current_thread->magic == THREAD_MAGIC);
ASSERT(current_thread->state == THREAD_RUNNING);
#endif
#if THREAD_STATS
if (!thread_is_idle(current_thread))
THREAD_STATS_INC(preempts); /* only track when a meaningful preempt happens */
#endif
KEVLOG_THREAD_PREEMPT(current_thread);
THREAD_LOCK(state);
/* we are being preempted, so we get to go back into the front of the run queue if we have quantum left */
current_thread->state = THREAD_READY;
if (likely(!thread_is_idle(current_thread))) { /* idle thread doesn't go in the run queue */
if (current_thread->remaining_quantum > 0)
insert_in_run_queue_head(current_thread);
else
insert_in_run_queue_tail(current_thread); /* if we're out of quantum, go to the tail of the queue */
}
thread_resched();
THREAD_UNLOCK(state);
}
/**
* @brief Suspend thread until woken.
*
* This function schedules another thread to execute. This function does not
* return until the thread is made runable again by some other module.
*
* You probably don't want to call this function directly; it's meant to be called
* from other modules, such as mutex, which will presumably set the thread's
* state to blocked and add it to some queue or another.
*/
void thread_block(void)
{
#if THREAD_CHECKS
thread_t *current_thread = get_current_thread();
ASSERT(current_thread->magic == THREAD_MAGIC);
ASSERT(current_thread->state == THREAD_BLOCKED);
ASSERT(spin_lock_held(&thread_lock));
ASSERT(!thread_is_idle(current_thread));
#endif
/* we are blocking on something. the blocking code should have already stuck us on a queue */
thread_resched();
}
void thread_unblock(thread_t *t, bool resched)
{
#if THREAD_CHECKS
ASSERT(t->magic == THREAD_MAGIC);
ASSERT(t->state == THREAD_BLOCKED);
ASSERT(spin_lock_held(&thread_lock));
ASSERT(!thread_is_idle(t));
#endif
t->state = THREAD_READY;
insert_in_run_queue_head(t);
mp_reschedule(MP_CPU_ALL_BUT_LOCAL, 0);
if (resched)
thread_resched();
}
enum handler_return thread_timer_tick(void)
{
thread_t *current_thread = get_current_thread();
if (thread_is_real_time_or_idle(current_thread))
return INT_NO_RESCHEDULE;
current_thread->remaining_quantum--;
if (current_thread->remaining_quantum <= 0) {
return INT_RESCHEDULE;
} else {
return INT_NO_RESCHEDULE;
}
}
/* timer callback to wake up a sleeping thread */
static enum handler_return thread_sleep_handler(timer_t *timer, lk_time_t now, void *arg)
{
thread_t *t = (thread_t *)arg;
#if THREAD_CHECKS
ASSERT(t->magic == THREAD_MAGIC);
ASSERT(t->state == THREAD_SLEEPING);
#endif
THREAD_LOCK(state);
t->state = THREAD_READY;
insert_in_run_queue_head(t);
THREAD_UNLOCK(state);
return INT_RESCHEDULE;
}
/**
* @brief Put thread to sleep; delay specified in ms
*
* This function puts the current thread to sleep until the specified
* delay in ms has expired.
*
* Note that this function could sleep for longer than the specified delay if
* other threads are running. When the timer expires, this thread will
* be placed at the head of the run queue.
*/
void thread_sleep(lk_time_t delay)
{
timer_t timer;
thread_t *current_thread = get_current_thread();
#if THREAD_CHECKS
ASSERT(current_thread->magic == THREAD_MAGIC);
ASSERT(current_thread->state == THREAD_RUNNING);
ASSERT(!thread_is_idle(current_thread));
#endif
timer_initialize(&timer);
THREAD_LOCK(state);
timer_set_oneshot(&timer, delay, thread_sleep_handler, (void *)current_thread);
current_thread->state = THREAD_SLEEPING;
thread_resched();
THREAD_UNLOCK(state);
}
/**
* @brief Initialize threading system
*
* This function is called once, from kmain()
*/
void thread_init_early(void)
{
int i;
DEBUG_ASSERT(arch_curr_cpu_num() == 0);
/* initialize the run queues */
for (i=0; i < NUM_PRIORITIES; i++)
list_initialize(&run_queue[i]);
/* initialize the thread list */
list_initialize(&thread_list);
/* create a thread to cover the current running state */
thread_t *t = &idle_threads[0];
init_thread_struct(t, "bootstrap");
/* half construct this thread, since we're already running */
t->priority = HIGHEST_PRIORITY;
t->state = THREAD_RUNNING;
t->flags = THREAD_FLAG_DETACHED;
t->curr_cpu = 0;
t->pinned_cpu = 0;
wait_queue_init(&t->retcode_wait_queue);
list_add_head(&thread_list, &t->thread_list_node);
set_current_thread(t);
}
/**
* @brief Complete thread initialization
*
* This function is called once at boot time
*/
void thread_init(void)
{
#if PLATFORM_HAS_DYNAMIC_TIMER
for (uint i = 0; i < SMP_MAX_CPUS; i++) {
timer_initialize(&preempt_timer[i]);
}
#endif
}
/**
* @brief Change name of current thread
*/
void thread_set_name(const char *name)
{
thread_t *current_thread = get_current_thread();
strlcpy(current_thread->name, name, sizeof(current_thread->name));
}
/**
* @brief Change priority of current thread
*
* See thread_create() for a discussion of priority values.
*/
void thread_set_priority(int priority)
{
thread_t *current_thread = get_current_thread();
THREAD_LOCK(state);
if (priority <= IDLE_PRIORITY)
priority = IDLE_PRIORITY + 1;
if (priority > HIGHEST_PRIORITY)
priority = HIGHEST_PRIORITY;
current_thread->priority = priority;
current_thread->state = THREAD_READY;
insert_in_run_queue_head(current_thread);
thread_resched();
THREAD_UNLOCK(state);
}
/**
* @brief Become an idle thread
*
* This function marks the current thread as the idle thread -- the one which
* executes when there is nothing else to do. This function does not return.
* This function is called once at boot time.
*/
void thread_become_idle(void)
{
DEBUG_ASSERT(arch_ints_disabled());
thread_t *t = get_current_thread();
char name[16];
snprintf(name, sizeof(name), "idle %d", arch_curr_cpu_num());
thread_set_name(name);
/* mark ourself as idle */
t->priority = IDLE_PRIORITY;
t->flags |= THREAD_FLAG_IDLE;
t->pinned_cpu = arch_curr_cpu_num();
mp_set_curr_cpu_active(true);
mp_set_cpu_idle(arch_curr_cpu_num());
/* enable interrupts and start the scheduler */
arch_enable_ints();
thread_yield();
idle_thread_routine();
}
/* create an idle thread for the cpu we're on, and start scheduling */
void thread_secondary_cpu_init_early(void)
{
DEBUG_ASSERT(arch_ints_disabled());
/* construct an idle thread to cover our cpu */
uint cpu = arch_curr_cpu_num();
thread_t *t = &idle_threads[cpu];
char name[16];
snprintf(name, sizeof(name), "idle %d", cpu);
init_thread_struct(t, name);
t->pinned_cpu = cpu;
/* half construct this thread, since we're already running */
t->priority = HIGHEST_PRIORITY;
t->state = THREAD_RUNNING;
t->flags = THREAD_FLAG_DETACHED | THREAD_FLAG_IDLE;
t->curr_cpu = cpu;
t->pinned_cpu = cpu;
wait_queue_init(&t->retcode_wait_queue);
THREAD_LOCK(state);
list_add_head(&thread_list, &t->thread_list_node);
set_current_thread(t);
THREAD_UNLOCK(state);
}
void thread_secondary_cpu_entry(void)
{
uint cpu = arch_curr_cpu_num();
thread_t *t = get_current_thread();
t->priority = IDLE_PRIORITY;
mp_set_curr_cpu_active(true);
mp_set_cpu_idle(cpu);
/* enable interrupts and start the scheduler on this cpu */
arch_enable_ints();
thread_yield();
idle_thread_routine();
}
static const char *thread_state_to_str(enum thread_state state)
{
switch (state) {
case THREAD_SUSPENDED: return "susp";
case THREAD_READY: return "rdy";
case THREAD_RUNNING: return "run";
case THREAD_BLOCKED: return "blok";
case THREAD_SLEEPING: return "slep";
case THREAD_DEATH: return "deth";
default: return "unkn";
}
}
/**
* @brief Dump debugging info about the specified thread.
*/
void dump_thread(thread_t *t)
{
dprintf(INFO, "dump_thread: t %p (%s)\n", t, t->name);
dprintf(INFO, "\tstate %s, curr_cpu %d, pinned_cpu %d, priority %d, remaining quantum %d\n",
thread_state_to_str(t->state), t->curr_cpu, t->pinned_cpu, t->priority, t->remaining_quantum);
dprintf(INFO, "\tstack %p, stack_size %zd\n", t->stack, t->stack_size);
dprintf(INFO, "\tentry %p, arg %p, flags 0x%x\n", t->entry, t->arg, t->flags);
dprintf(INFO, "\twait queue %p, wait queue ret %d\n", t->blocking_wait_queue, t->wait_queue_block_ret);
dprintf(INFO, "\ttls:");
int i;
for (i=0; i < MAX_TLS_ENTRY; i++) {
dprintf(INFO, " 0x%lx", t->tls[i]);
}
dprintf(INFO, "\n");
arch_dump_thread(t);
}
/**
* @brief Dump debugging info about all threads
*/
void dump_all_threads(void)
{
thread_t *t;
THREAD_LOCK(state);
list_for_every_entry(&thread_list, t, thread_t, thread_list_node) {
dump_thread(t);
}
THREAD_UNLOCK(state);
}
/** @} */
/**
* @defgroup wait Wait Queue
* @{
*/
void wait_queue_init(wait_queue_t *wait)
{
*wait = (wait_queue_t)WAIT_QUEUE_INITIAL_VALUE(*wait);
}
static enum handler_return wait_queue_timeout_handler(timer_t *timer, lk_time_t now, void *arg)
{
thread_t *thread = (thread_t *)arg;
#if THREAD_CHECKS
ASSERT(thread->magic == THREAD_MAGIC);
#endif
spin_lock(&thread_lock);
enum handler_return ret = INT_NO_RESCHEDULE;
if (thread_unblock_from_wait_queue(thread, ERR_TIMED_OUT) >= NO_ERROR) {
ret = INT_RESCHEDULE;
}
spin_unlock(&thread_lock);
return ret;
}
/**
* @brief Block until a wait queue is notified.
*
* This function puts the current thread at the end of a wait
* queue and then blocks until some other thread wakes the queue