forked from kugel-/rockbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththread.c
2386 lines (2018 loc) · 74.4 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
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id$
*
* Copyright (C) 2002 by Ulf Ralberg
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#include "config.h"
#ifdef HAVE_SIGALTSTACK_THREADS
/*
* The sp check in glibc __longjmp_chk() will cause
* a fatal error when switching threads via longjmp().
*/
#undef _FORTIFY_SOURCE
#endif
#include <stdbool.h>
#include <stdio.h>
#include "thread.h"
#include "panic.h"
#include "system.h"
#include "kernel.h"
#include "cpu.h"
#include "string.h"
#ifdef RB_PROFILE
#include <profile.h>
#endif
#include "gcc_extensions.h"
/****************************************************************************
* ATTENTION!! *
* See notes below on implementing processor-specific portions! *
***************************************************************************/
/* Define THREAD_EXTRA_CHECKS as 1 to enable additional state checks */
#ifdef DEBUG
#define THREAD_EXTRA_CHECKS 1 /* Always 1 for DEBUG */
#else
#define THREAD_EXTRA_CHECKS 0
#endif
/**
* General locking order to guarantee progress. Order must be observed but
* all stages are not nescessarily obligatory. Going from 1) to 3) is
* perfectly legal.
*
* 1) IRQ
* This is first because of the likelyhood of having an interrupt occur that
* also accesses one of the objects farther down the list. Any non-blocking
* synchronization done may already have a lock on something during normal
* execution and if an interrupt handler running on the same processor as
* the one that has the resource locked were to attempt to access the
* resource, the interrupt handler would wait forever waiting for an unlock
* that will never happen. There is no danger if the interrupt occurs on
* a different processor because the one that has the lock will eventually
* unlock and the other processor's handler may proceed at that time. Not
* nescessary when the resource in question is definitely not available to
* interrupt handlers.
*
* 2) Kernel Object
* 1) May be needed beforehand if the kernel object allows dual-use such as
* event queues. The kernel object must have a scheme to protect itself from
* access by another processor and is responsible for serializing the calls
* to block_thread(_w_tmo) and wakeup_thread both to themselves and to each
* other. Objects' queues are also protected here.
*
* 3) Thread Slot
* This locks access to the thread's slot such that its state cannot be
* altered by another processor when a state change is in progress such as
* when it is in the process of going on a blocked list. An attempt to wake
* a thread while it is still blocking will likely desync its state with
* the other resources used for that state.
*
* 4) Core Lists
* These lists are specific to a particular processor core and are accessible
* by all processor cores and interrupt handlers. The running (rtr) list is
* the prime example where a thread may be added by any means.
*/
/*---------------------------------------------------------------------------
* Processor specific: core_sleep/core_wake/misc. notes
*
* ARM notes:
* FIQ is not dealt with by the scheduler code and is simply restored if it
* must by masked for some reason - because threading modifies a register
* that FIQ may also modify and there's no way to accomplish it atomically.
* s3c2440 is such a case.
*
* Audio interrupts are generally treated at a higher priority than others
* usage of scheduler code with interrupts higher than HIGHEST_IRQ_LEVEL
* are not in general safe. Special cases may be constructed on a per-
* source basis and blocking operations are not available.
*
* core_sleep procedure to implement for any CPU to ensure an asychronous
* wakup never results in requiring a wait until the next tick (up to
* 10000uS!). May require assembly and careful instruction ordering.
*
* 1) On multicore, stay awake if directed to do so by another. If so, goto
* step 4.
* 2) If processor requires, atomically reenable interrupts and perform step
* 3.
* 3) Sleep the CPU core. If wakeup itself enables interrupts (stop #0x2000
* on Coldfire) goto step 5.
* 4) Enable interrupts.
* 5) Exit procedure.
*
* core_wake and multprocessor notes for sleep/wake coordination:
* If possible, to wake up another processor, the forcing of an interrupt on
* the woken core by the waker core is the easiest way to ensure a non-
* delayed wake and immediate execution of any woken threads. If that isn't
* available then some careful non-blocking synchonization is needed (as on
* PP targets at the moment).
*---------------------------------------------------------------------------
*/
/* Cast to the the machine pointer size, whose size could be < 4 or > 32
* (someday :). */
#define DEADBEEF ((uintptr_t)0xdeadbeefdeadbeefull)
static struct core_entry cores[NUM_CORES] IBSS_ATTR;
struct thread_entry threads[MAXTHREADS] IBSS_ATTR;
static const char main_thread_name[] = "main";
#if (CONFIG_PLATFORM & PLATFORM_NATIVE)
extern uintptr_t stackbegin[];
extern uintptr_t stackend[];
#else
extern uintptr_t *stackbegin;
extern uintptr_t *stackend;
#endif
static inline void core_sleep(IF_COP_VOID(unsigned int core))
__attribute__((always_inline));
void check_tmo_threads(void)
__attribute__((noinline));
static inline void block_thread_on_l(struct thread_entry *thread, unsigned state)
__attribute__((always_inline));
static void add_to_list_tmo(struct thread_entry *thread)
__attribute__((noinline));
static void core_schedule_wakeup(struct thread_entry *thread)
__attribute__((noinline));
#if NUM_CORES > 1
static inline void run_blocking_ops(
unsigned int core, struct thread_entry *thread)
__attribute__((always_inline));
#endif
static void thread_stkov(struct thread_entry *thread)
__attribute__((noinline));
static inline void store_context(void* addr)
__attribute__((always_inline));
static inline void load_context(const void* addr)
__attribute__((always_inline));
#if NUM_CORES > 1
static void thread_final_exit_do(struct thread_entry *current)
__attribute__((noinline)) NORETURN_ATTR USED_ATTR;
#else
static inline void thread_final_exit(struct thread_entry *current)
__attribute__((always_inline)) NORETURN_ATTR;
#endif
void switch_thread(void)
__attribute__((noinline));
/****************************************************************************
* Processor/OS-specific section - include necessary core support
*/
#include "asm/thread.c"
#if defined (CPU_PP)
#include "thread-pp.c"
#endif /* CPU_PP */
#ifndef IF_NO_SKIP_YIELD
#define IF_NO_SKIP_YIELD(...)
#endif
/*
* End Processor-specific section
***************************************************************************/
#if THREAD_EXTRA_CHECKS
static void thread_panicf(const char *msg, struct thread_entry *thread)
{
IF_COP( const unsigned int core = thread->core; )
static char name[32];
thread_get_name(name, 32, thread);
panicf ("%s %s" IF_COP(" (%d)"), msg, name IF_COP(, core));
}
static void thread_stkov(struct thread_entry *thread)
{
thread_panicf("Stkov", thread);
}
#define THREAD_PANICF(msg, thread) \
thread_panicf(msg, thread)
#define THREAD_ASSERT(exp, msg, thread) \
({ if (!({ exp; })) thread_panicf((msg), (thread)); })
#else
static void thread_stkov(struct thread_entry *thread)
{
IF_COP( const unsigned int core = thread->core; )
static char name[32];
thread_get_name(name, 32, thread);
panicf("Stkov %s" IF_COP(" (%d)"), name IF_COP(, core));
}
#define THREAD_PANICF(msg, thread)
#define THREAD_ASSERT(exp, msg, thread)
#endif /* THREAD_EXTRA_CHECKS */
/* Thread locking */
#if NUM_CORES > 1
#define LOCK_THREAD(thread) \
({ corelock_lock(&(thread)->slot_cl); })
#define TRY_LOCK_THREAD(thread) \
({ corelock_try_lock(&(thread)->slot_cl); })
#define UNLOCK_THREAD(thread) \
({ corelock_unlock(&(thread)->slot_cl); })
#define UNLOCK_THREAD_AT_TASK_SWITCH(thread) \
({ unsigned int _core = (thread)->core; \
cores[_core].blk_ops.flags |= TBOP_UNLOCK_CORELOCK; \
cores[_core].blk_ops.cl_p = &(thread)->slot_cl; })
#else
#define LOCK_THREAD(thread) \
({ })
#define TRY_LOCK_THREAD(thread) \
({ })
#define UNLOCK_THREAD(thread) \
({ })
#define UNLOCK_THREAD_AT_TASK_SWITCH(thread) \
({ })
#endif
/* RTR list */
#define RTR_LOCK(core) \
({ corelock_lock(&cores[core].rtr_cl); })
#define RTR_UNLOCK(core) \
({ corelock_unlock(&cores[core].rtr_cl); })
#ifdef HAVE_PRIORITY_SCHEDULING
#define rtr_add_entry(core, priority) \
prio_add_entry(&cores[core].rtr, (priority))
#define rtr_subtract_entry(core, priority) \
prio_subtract_entry(&cores[core].rtr, (priority))
#define rtr_move_entry(core, from, to) \
prio_move_entry(&cores[core].rtr, (from), (to))
#else
#define rtr_add_entry(core, priority)
#define rtr_add_entry_inl(core, priority)
#define rtr_subtract_entry(core, priority)
#define rtr_subtract_entry_inl(core, priotity)
#define rtr_move_entry(core, from, to)
#define rtr_move_entry_inl(core, from, to)
#endif
/*---------------------------------------------------------------------------
* Thread list structure - circular:
* +------------------------------+
* | |
* +--+---+<-+---+<-+---+<-+---+<-+
* Head->| T | | T | | T | | T |
* +->+---+->+---+->+---+->+---+--+
* | |
* +------------------------------+
*---------------------------------------------------------------------------
*/
/*---------------------------------------------------------------------------
* Adds a thread to a list of threads using "insert last". Uses the "l"
* links.
*---------------------------------------------------------------------------
*/
static void add_to_list_l(struct thread_entry **list,
struct thread_entry *thread)
{
struct thread_entry *l = *list;
if (l == NULL)
{
/* Insert into unoccupied list */
thread->l.prev = thread;
thread->l.next = thread;
*list = thread;
return;
}
/* Insert last */
thread->l.prev = l->l.prev;
thread->l.next = l;
l->l.prev->l.next = thread;
l->l.prev = thread;
}
/*---------------------------------------------------------------------------
* Removes a thread from a list of threads. Uses the "l" links.
*---------------------------------------------------------------------------
*/
static void remove_from_list_l(struct thread_entry **list,
struct thread_entry *thread)
{
struct thread_entry *prev, *next;
next = thread->l.next;
if (thread == next)
{
/* The only item */
*list = NULL;
return;
}
if (thread == *list)
{
/* List becomes next item */
*list = next;
}
prev = thread->l.prev;
/* Fix links to jump over the removed entry. */
next->l.prev = prev;
prev->l.next = next;
}
/*---------------------------------------------------------------------------
* Timeout list structure - circular reverse (to make "remove item" O(1)),
* NULL-terminated forward (to ease the far more common forward traversal):
* +------------------------------+
* | |
* +--+---+<-+---+<-+---+<-+---+<-+
* Head->| T | | T | | T | | T |
* +---+->+---+->+---+->+---+-X
*---------------------------------------------------------------------------
*/
/*---------------------------------------------------------------------------
* Add a thread from the core's timout list by linking the pointers in its
* tmo structure.
*---------------------------------------------------------------------------
*/
static void add_to_list_tmo(struct thread_entry *thread)
{
struct thread_entry *tmo = cores[IF_COP_CORE(thread->core)].timeout;
THREAD_ASSERT(thread->tmo.prev == NULL,
"add_to_list_tmo->already listed", thread);
thread->tmo.next = NULL;
if (tmo == NULL)
{
/* Insert into unoccupied list */
thread->tmo.prev = thread;
cores[IF_COP_CORE(thread->core)].timeout = thread;
return;
}
/* Insert Last */
thread->tmo.prev = tmo->tmo.prev;
tmo->tmo.prev->tmo.next = thread;
tmo->tmo.prev = thread;
}
/*---------------------------------------------------------------------------
* Remove a thread from the core's timout list by unlinking the pointers in
* its tmo structure. Sets thread->tmo.prev to NULL to indicate the timeout
* is cancelled.
*---------------------------------------------------------------------------
*/
static void remove_from_list_tmo(struct thread_entry *thread)
{
struct thread_entry **list = &cores[IF_COP_CORE(thread->core)].timeout;
struct thread_entry *prev = thread->tmo.prev;
struct thread_entry *next = thread->tmo.next;
THREAD_ASSERT(prev != NULL, "remove_from_list_tmo->not listed", thread);
if (next != NULL)
next->tmo.prev = prev;
if (thread == *list)
{
/* List becomes next item and empty if next == NULL */
*list = next;
/* Mark as unlisted */
thread->tmo.prev = NULL;
}
else
{
if (next == NULL)
(*list)->tmo.prev = prev;
prev->tmo.next = next;
/* Mark as unlisted */
thread->tmo.prev = NULL;
}
}
#ifdef HAVE_PRIORITY_SCHEDULING
/*---------------------------------------------------------------------------
* Priority distribution structure (one category for each possible priority):
*
* +----+----+----+ ... +-----+
* hist: | F0 | F1 | F2 | | F31 |
* +----+----+----+ ... +-----+
* mask: | b0 | b1 | b2 | | b31 |
* +----+----+----+ ... +-----+
*
* F = count of threads at priority category n (frequency)
* b = bitmask of non-zero priority categories (occupancy)
*
* / if H[n] != 0 : 1
* b[n] = |
* \ else : 0
*
*---------------------------------------------------------------------------
* Basic priority inheritance priotocol (PIP):
*
* Mn = mutex n, Tn = thread n
*
* A lower priority thread inherits the priority of the highest priority
* thread blocked waiting for it to complete an action (such as release a
* mutex or respond to a message via queue_send):
*
* 1) T2->M1->T1
*
* T1 owns M1, T2 is waiting for M1 to realease M1. If T2 has a higher
* priority than T1 then T1 inherits the priority of T2.
*
* 2) T3
* \/
* T2->M1->T1
*
* Situation is like 1) but T2 and T3 are both queued waiting for M1 and so
* T1 inherits the higher of T2 and T3.
*
* 3) T3->M2->T2->M1->T1
*
* T1 owns M1, T2 owns M2. If T3 has a higher priority than both T1 and T2,
* then T1 inherits the priority of T3 through T2.
*
* Blocking chains can grow arbitrarily complex (though it's best that they
* not form at all very often :) and build-up from these units.
*---------------------------------------------------------------------------
*/
/*---------------------------------------------------------------------------
* Increment frequency at category "priority"
*---------------------------------------------------------------------------
*/
static inline unsigned int prio_add_entry(
struct priority_distribution *pd, int priority)
{
unsigned int count;
/* Enough size/instruction count difference for ARM makes it worth it to
* use different code (192 bytes for ARM). Only thing better is ASM. */
#ifdef CPU_ARM
count = pd->hist[priority];
if (++count == 1)
pd->mask |= 1 << priority;
pd->hist[priority] = count;
#else /* This one's better for Coldfire */
if ((count = ++pd->hist[priority]) == 1)
pd->mask |= 1 << priority;
#endif
return count;
}
/*---------------------------------------------------------------------------
* Decrement frequency at category "priority"
*---------------------------------------------------------------------------
*/
static inline unsigned int prio_subtract_entry(
struct priority_distribution *pd, int priority)
{
unsigned int count;
#ifdef CPU_ARM
count = pd->hist[priority];
if (--count == 0)
pd->mask &= ~(1 << priority);
pd->hist[priority] = count;
#else
if ((count = --pd->hist[priority]) == 0)
pd->mask &= ~(1 << priority);
#endif
return count;
}
/*---------------------------------------------------------------------------
* Remove from one category and add to another
*---------------------------------------------------------------------------
*/
static inline void prio_move_entry(
struct priority_distribution *pd, int from, int to)
{
uint32_t mask = pd->mask;
#ifdef CPU_ARM
unsigned int count;
count = pd->hist[from];
if (--count == 0)
mask &= ~(1 << from);
pd->hist[from] = count;
count = pd->hist[to];
if (++count == 1)
mask |= 1 << to;
pd->hist[to] = count;
#else
if (--pd->hist[from] == 0)
mask &= ~(1 << from);
if (++pd->hist[to] == 1)
mask |= 1 << to;
#endif
pd->mask = mask;
}
/*---------------------------------------------------------------------------
* Change the priority and rtr entry for a running thread
*---------------------------------------------------------------------------
*/
static inline void set_running_thread_priority(
struct thread_entry *thread, int priority)
{
const unsigned int core = IF_COP_CORE(thread->core);
RTR_LOCK(core);
rtr_move_entry(core, thread->priority, priority);
thread->priority = priority;
RTR_UNLOCK(core);
}
/*---------------------------------------------------------------------------
* Finds the highest priority thread in a list of threads. If the list is
* empty, the PRIORITY_IDLE is returned.
*
* It is possible to use the struct priority_distribution within an object
* instead of scanning the remaining threads in the list but as a compromise,
* the resulting per-object memory overhead is saved at a slight speed
* penalty under high contention.
*---------------------------------------------------------------------------
*/
static int find_highest_priority_in_list_l(
struct thread_entry * const thread)
{
if (LIKELY(thread != NULL))
{
/* Go though list until the ending up at the initial thread */
int highest_priority = thread->priority;
struct thread_entry *curr = thread;
do
{
int priority = curr->priority;
if (priority < highest_priority)
highest_priority = priority;
curr = curr->l.next;
}
while (curr != thread);
return highest_priority;
}
return PRIORITY_IDLE;
}
/*---------------------------------------------------------------------------
* Register priority with blocking system and bubble it down the chain if
* any until we reach the end or something is already equal or higher.
*
* NOTE: A simultaneous circular wait could spin deadlock on multiprocessor
* targets but that same action also guarantees a circular block anyway and
* those are prevented, right? :-)
*---------------------------------------------------------------------------
*/
static struct thread_entry *
blocker_inherit_priority(struct thread_entry *current)
{
const int priority = current->priority;
struct blocker *bl = current->blocker;
struct thread_entry * const tstart = current;
struct thread_entry *bl_t = bl->thread;
/* Blocker cannot change since the object protection is held */
LOCK_THREAD(bl_t);
for (;;)
{
struct thread_entry *next;
int bl_pr = bl->priority;
if (priority >= bl_pr)
break; /* Object priority already high enough */
bl->priority = priority;
/* Add this one */
prio_add_entry(&bl_t->pdist, priority);
if (bl_pr < PRIORITY_IDLE)
{
/* Not first waiter - subtract old one */
prio_subtract_entry(&bl_t->pdist, bl_pr);
}
if (priority >= bl_t->priority)
break; /* Thread priority high enough */
if (bl_t->state == STATE_RUNNING)
{
/* Blocking thread is a running thread therefore there are no
* further blockers. Change the "run queue" on which it
* resides. */
set_running_thread_priority(bl_t, priority);
break;
}
bl_t->priority = priority;
/* If blocking thread has a blocker, apply transitive inheritance */
bl = bl_t->blocker;
if (bl == NULL)
break; /* End of chain or object doesn't support inheritance */
next = bl->thread;
if (UNLIKELY(next == tstart))
break; /* Full-circle - deadlock! */
UNLOCK_THREAD(current);
#if NUM_CORES > 1
for (;;)
{
LOCK_THREAD(next);
/* Blocker could change - retest condition */
if (LIKELY(bl->thread == next))
break;
UNLOCK_THREAD(next);
next = bl->thread;
}
#endif
current = bl_t;
bl_t = next;
}
UNLOCK_THREAD(bl_t);
return current;
}
/*---------------------------------------------------------------------------
* Readjust priorities when waking a thread blocked waiting for another
* in essence "releasing" the thread's effect on the object owner. Can be
* performed from any context.
*---------------------------------------------------------------------------
*/
struct thread_entry *
wakeup_priority_protocol_release(struct thread_entry *thread)
{
const int priority = thread->priority;
struct blocker *bl = thread->blocker;
struct thread_entry * const tstart = thread;
struct thread_entry *bl_t = bl->thread;
/* Blocker cannot change since object will be locked */
LOCK_THREAD(bl_t);
thread->blocker = NULL; /* Thread not blocked */
for (;;)
{
struct thread_entry *next;
int bl_pr = bl->priority;
if (priority > bl_pr)
break; /* Object priority higher */
next = *thread->bqp;
if (next == NULL)
{
/* No more threads in queue */
prio_subtract_entry(&bl_t->pdist, bl_pr);
bl->priority = PRIORITY_IDLE;
}
else
{
/* Check list for highest remaining priority */
int queue_pr = find_highest_priority_in_list_l(next);
if (queue_pr == bl_pr)
break; /* Object priority not changing */
/* Change queue priority */
prio_move_entry(&bl_t->pdist, bl_pr, queue_pr);
bl->priority = queue_pr;
}
if (bl_pr > bl_t->priority)
break; /* thread priority is higher */
bl_pr = find_first_set_bit(bl_t->pdist.mask);
if (bl_pr == bl_t->priority)
break; /* Thread priority not changing */
if (bl_t->state == STATE_RUNNING)
{
/* No further blockers */
set_running_thread_priority(bl_t, bl_pr);
break;
}
bl_t->priority = bl_pr;
/* If blocking thread has a blocker, apply transitive inheritance */
bl = bl_t->blocker;
if (bl == NULL)
break; /* End of chain or object doesn't support inheritance */
next = bl->thread;
if (UNLIKELY(next == tstart))
break; /* Full-circle - deadlock! */
UNLOCK_THREAD(thread);
#if NUM_CORES > 1
for (;;)
{
LOCK_THREAD(next);
/* Blocker could change - retest condition */
if (LIKELY(bl->thread == next))
break;
UNLOCK_THREAD(next);
next = bl->thread;
}
#endif
thread = bl_t;
bl_t = next;
}
UNLOCK_THREAD(bl_t);
#if NUM_CORES > 1
if (UNLIKELY(thread != tstart))
{
/* Relock original if it changed */
LOCK_THREAD(tstart);
}
#endif
return cores[CURRENT_CORE].running;
}
/*---------------------------------------------------------------------------
* Transfer ownership to a thread waiting for an objects and transfer
* inherited priority boost from other waiters. This algorithm knows that
* blocking chains may only unblock from the very end.
*
* Only the owning thread itself may call this and so the assumption that
* it is the running thread is made.
*---------------------------------------------------------------------------
*/
struct thread_entry *
wakeup_priority_protocol_transfer(struct thread_entry *thread)
{
/* Waking thread inherits priority boost from object owner */
struct blocker *bl = thread->blocker;
struct thread_entry *bl_t = bl->thread;
struct thread_entry *next;
int bl_pr;
THREAD_ASSERT(cores[CURRENT_CORE].running == bl_t,
"UPPT->wrong thread", cores[CURRENT_CORE].running);
LOCK_THREAD(bl_t);
bl_pr = bl->priority;
/* Remove the object's boost from the owning thread */
if (prio_subtract_entry(&bl_t->pdist, bl_pr) == 0 &&
bl_pr <= bl_t->priority)
{
/* No more threads at this priority are waiting and the old level is
* at least the thread level */
int priority = find_first_set_bit(bl_t->pdist.mask);
if (priority != bl_t->priority)
{
/* Adjust this thread's priority */
set_running_thread_priority(bl_t, priority);
}
}
next = *thread->bqp;
if (LIKELY(next == NULL))
{
/* Expected shortcut - no more waiters */
bl_pr = PRIORITY_IDLE;
}
else
{
if (thread->priority <= bl_pr)
{
/* Need to scan threads remaining in queue */
bl_pr = find_highest_priority_in_list_l(next);
}
if (prio_add_entry(&thread->pdist, bl_pr) == 1 &&
bl_pr < thread->priority)
{
/* Thread priority must be raised */
thread->priority = bl_pr;
}
}
bl->thread = thread; /* This thread pwns */
bl->priority = bl_pr; /* Save highest blocked priority */
thread->blocker = NULL; /* Thread not blocked */
UNLOCK_THREAD(bl_t);
return bl_t;
}
/*---------------------------------------------------------------------------
* No threads must be blocked waiting for this thread except for it to exit.
* The alternative is more elaborate cleanup and object registration code.
* Check this for risk of silent data corruption when objects with
* inheritable blocking are abandoned by the owner - not precise but may
* catch something.
*---------------------------------------------------------------------------
*/
static void __attribute__((noinline)) check_for_obj_waiters(
const char *function, struct thread_entry *thread)
{
/* Only one bit in the mask should be set with a frequency on 1 which
* represents the thread's own base priority */
uint32_t mask = thread->pdist.mask;
if ((mask & (mask - 1)) != 0 ||
thread->pdist.hist[find_first_set_bit(mask)] > 1)
{
unsigned char name[32];
thread_get_name(name, 32, thread);
panicf("%s->%s with obj. waiters", function, name);
}
}
#endif /* HAVE_PRIORITY_SCHEDULING */
/*---------------------------------------------------------------------------
* Move a thread back to a running state on its core.
*---------------------------------------------------------------------------
*/
static void core_schedule_wakeup(struct thread_entry *thread)
{
const unsigned int core = IF_COP_CORE(thread->core);
RTR_LOCK(core);
thread->state = STATE_RUNNING;
add_to_list_l(&cores[core].running, thread);
rtr_add_entry(core, thread->priority);
RTR_UNLOCK(core);
#if NUM_CORES > 1
if (core != CURRENT_CORE)
core_wake(core);
#endif
}
/*---------------------------------------------------------------------------
* Check the core's timeout list when at least one thread is due to wake.
* Filtering for the condition is done before making the call. Resets the
* tick when the next check will occur.
*---------------------------------------------------------------------------
*/
void check_tmo_threads(void)
{
const unsigned int core = CURRENT_CORE;
const long tick = current_tick; /* snapshot the current tick */
long next_tmo_check = tick + 60*HZ; /* minimum duration: once/minute */
struct thread_entry *next = cores[core].timeout;
/* If there are no processes waiting for a timeout, just keep the check
tick from falling into the past. */
/* Break the loop once we have walked through the list of all
* sleeping processes or have removed them all. */
while (next != NULL)
{
/* Check sleeping threads. Allow interrupts between checks. */
enable_irq();
struct thread_entry *curr = next;
next = curr->tmo.next;
/* Lock thread slot against explicit wakeup */
disable_irq();
LOCK_THREAD(curr);
unsigned state = curr->state;
if (state < TIMEOUT_STATE_FIRST)
{
/* Cleanup threads no longer on a timeout but still on the
* list. */
remove_from_list_tmo(curr);
}
else if (LIKELY(TIME_BEFORE(tick, curr->tmo_tick)))
{
/* Timeout still pending - this will be the usual case */
if (TIME_BEFORE(curr->tmo_tick, next_tmo_check))
{
/* Earliest timeout found so far - move the next check up
to its time */
next_tmo_check = curr->tmo_tick;
}
}
else
{
/* Sleep timeout has been reached so bring the thread back to
* life again. */
if (state == STATE_BLOCKED_W_TMO)
{
#ifdef HAVE_CORELOCK_OBJECT
/* Lock the waiting thread's kernel object */
struct corelock *ocl = curr->obj_cl;
if (UNLIKELY(corelock_try_lock(ocl) == 0))
{
/* Need to retry in the correct order though the need is
* unlikely */
UNLOCK_THREAD(curr);
corelock_lock(ocl);
LOCK_THREAD(curr);
if (UNLIKELY(curr->state != STATE_BLOCKED_W_TMO))
{
/* Thread was woken or removed explicitely while slot
* was unlocked */
corelock_unlock(ocl);
remove_from_list_tmo(curr);
UNLOCK_THREAD(curr);
continue;
}
}
#endif /* NUM_CORES */
remove_from_list_l(curr->bqp, curr);
#ifdef HAVE_WAKEUP_EXT_CB
if (curr->wakeup_ext_cb != NULL)
curr->wakeup_ext_cb(curr);
#endif
#ifdef HAVE_PRIORITY_SCHEDULING
if (curr->blocker != NULL)
wakeup_priority_protocol_release(curr);