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
|
//
// Generated By: dol2asm
// Translation Unit: OSThread
//
#include "dolphin/os/OSThread.h"
#include "dolphin/os.h"
OSThread* __OSCurrentThread : OS_BASE_CACHED + 0x00E4;
OSThreadQueue __OSActiveThreadQueue : OS_BASE_CACHED + 0x00DC;
volatile OSContext __OSCurrentContext : OS_BASE_CACHED + 0x00D4;
volatile OSContext* __OSFPUContext : OS_BASE_CACHED + 0x00D8;
#define AddTail(queue, thread, link) \
do { \
OSThread* prev; \
\
prev = (queue)->tail; \
if (prev == NULL) \
(queue)->head = (thread); \
else \
prev->link.next = (thread); \
(thread)->link.prev = prev; \
(thread)->link.next = NULL; \
(queue)->tail = (thread); \
} while (0)
#define AddPrio(queue, thread, link) \
do { \
OSThread *prev, *next; \
\
for (next = (queue)->head; next && next->effective_priority <= thread->effective_priority; \
next = next->link.next) \
; \
if (next == NULL) \
AddTail(queue, thread, link); \
else { \
(thread)->link.next = next; \
prev = next->link.prev; \
next->link.prev = (thread); \
(thread)->link.prev = prev; \
if (prev == NULL) \
(queue)->head = (thread); \
else \
prev->link.next = (thread); \
} \
} while (0)
#define RemoveItem(queue, thread, link) \
do { \
OSThread *next, *prev; \
next = (thread)->link.next; \
prev = (thread)->link.prev; \
if (next == NULL) \
(queue)->tail = prev; \
else \
next->link.prev = prev; \
if (prev == NULL) \
(queue)->head = next; \
else \
prev->link.next = next; \
} while (0)
#define RemoveHead(queue, thread, link) \
do { \
OSThread* __next; \
(thread) = (queue)->head; \
__next = (thread)->link.next; \
if (__next == NULL) \
(queue)->tail = NULL; \
else \
__next->link.prev = NULL; \
(queue)->head = __next; \
} while (0)
//
// External References:
//
extern OSErrorHandlerEx __OSErrorTable[17];
extern u32 __OSFpscrEnableBits;
void _epilog();
//
// Declarations:
//
/* 80340AA4-80340AA8 33B3E4 0004+00 2/1 0/0 0/0 .text DefaultSwitchThreadCallback */
static void DefaultSwitchThreadCallback(OSThread* from, OSThread* to) {}
/* ############################################################################################## */
/* 804509B8-804509BC -00001 0004+00 3/3 0/0 0/0 .sdata SwitchThreadCallback */
static OSSwitchThreadCallback SwitchThreadCallback = DefaultSwitchThreadCallback;
/* 80340AA8-80340B1C 33B3E8 0074+00 0/0 1/1 0/0 .text OSSetSwitchThreadCallback */
OSSwitchThreadCallback OSSetSwitchThreadCallback(OSSwitchThreadCallback func) {
BOOL enable = OSDisableInterrupts();
OSSwitchThreadCallback prev = SwitchThreadCallback;
OSSwitchThreadCallback temp;
if (func) {
temp = func;
} else {
temp = DefaultSwitchThreadCallback;
}
SwitchThreadCallback = temp;
OSRestoreInterrupts(enable);
if (prev == DefaultSwitchThreadCallback) {
return NULL;
}
return prev;
}
/* ############################################################################################## */
/* 8044BB78-8044BC78 078898 0100+00 6/6 0/0 0/0 .bss RunQueue */
static OSThreadQueue RunQueue[32];
/* 8044BC78-8044BF90 078998 0318+00 0/0 0/0 0/0 .bss IdleThread */
#pragma push
#pragma force_active on
static OSThread IdleThread;
#pragma pop
/* 8044BF90-8044C2A8 078CB0 0318+00 0/1 0/0 0/0 .bss DefaultThread */
#pragma push
#pragma force_active on
static OSThread DefaultThread;
#pragma pop
/* 8044C2A8-8044C570 078FC8 02C8+00 0/2 0/0 0/0 .bss IdleContext */
#pragma push
#pragma force_active on
static OSContext IdleContext;
#pragma pop
/* 804516C0-804516C4 000BC0 0004+00 7/7 0/0 0/0 .sbss RunQueueBits */
static vu32 RunQueueBits;
/* 804516C4-804516C8 000BC4 0004+00 11/11 0/0 0/0 .sbss RunQueueHint */
static volatile BOOL RunQueueHint;
/* 804516C8-804516D0 000BC8 0004+04 4/4 0/0 0/0 .sbss Reschedule */
static volatile s32 Reschedule;
extern u8 _stack_end[];
extern u8 _stack_addr[];
static inline void OSInitMutexQueue(OSMutexQueue* queue) {
queue->head = queue->tail = NULL;
}
static inline void OSSetCurrentThread(OSThread* thread) {
SwitchThreadCallback(__OSCurrentThread, thread);
__OSCurrentThread = thread;
}
/* 80340B1C-80340C74 33B45C 0158+00 0/0 1/1 0/0 .text __OSThreadInit */
void __OSThreadInit() {
OSThread* thread = &DefaultThread;
int prio;
thread->state = OS_THREAD_STATE_RUNNING;
thread->attributes = OS_THREAD_ATTR_DETACH;
thread->effective_priority = thread->base_priority = 16;
thread->suspend_count = 0;
thread->exit_value = (void*)-1;
thread->mutex = NULL;
OSInitThreadQueue(&thread->join_queue);
OSInitMutexQueue(&thread->owned_mutexes);
__OSFPUContext = &thread->context;
OSClearContext(&thread->context);
OSSetCurrentContext(&thread->context);
thread->stack_base = (void*)_stack_addr;
thread->stack_end = (void*)_stack_end;
*(thread->stack_end) = OS_THREAD_STACK_MAGIC;
OSSetCurrentThread(thread);
OSClearStack(0);
RunQueueBits = 0;
RunQueueHint = FALSE;
for (prio = OS_PRIORITY_MIN; prio <= OS_PRIORITY_MAX; ++prio) {
OSInitThreadQueue(&RunQueue[prio]);
}
OSInitThreadQueue(&__OSActiveThreadQueue);
AddTail(&__OSActiveThreadQueue, thread, active_threads_link);
OSClearContext(&IdleContext);
Reschedule = 0;
}
/* 80340C74-80340C84 33B5B4 0010+00 1/1 9/9 0/0 .text OSInitThreadQueue */
void OSInitThreadQueue(OSThreadQueue* queue) {
queue->tail = NULL;
queue->head = NULL;
}
/* 80340C84-80340C90 33B5C4 000C+00 0/0 20/20 0/0 .text OSGetCurrentThread */
OSThread* OSGetCurrentThread(void) {
return OS_CURRENT_THREAD;
}
/* 80340C90-80340CC4 33B5D0 0034+00 0/0 1/1 0/0 .text OSIsThreadTerminated */
BOOL OSIsThreadTerminated(OSThread* thread) {
return thread->state == OS_THREAD_STATE_DEAD || thread->state == OS_THREAD_STATE_UNINITIALIZED ?
TRUE :
FALSE;
}
/* 80340CC4-80340D04 33B604 0040+00 0/0 8/8 0/0 .text OSDisableScheduler */
s32 OSDisableScheduler(void) {
BOOL intr = OSDisableInterrupts();
s32 ret = Reschedule++;
OSRestoreInterrupts(intr);
return ret;
}
/* 80340D04-80340D44 33B644 0040+00 0/0 10/10 0/0 .text OSEnableScheduler */
s32 OSEnableScheduler(void) {
BOOL intr = OSDisableInterrupts();
s32 ret = Reschedule--;
OSRestoreInterrupts(intr);
return ret;
}
static inline void SetRun(OSThread* thread) {
thread->queue = &RunQueue[thread->effective_priority];
AddTail(thread->queue, thread, link);
RunQueueBits |= 1u << (OS_PRIORITY_MAX - thread->effective_priority);
RunQueueHint = TRUE;
}
/* 80340D44-80340DAC 33B684 0068+00 3/3 0/0 0/0 .text UnsetRun */
#pragma dont_inline on
static void UnsetRun(OSThread* thread) {
OSThreadQueue* queue;
OSThread* next;
OSThread* prev;
prev = thread->link.next;
queue = thread->queue;
next = thread->link.prev;
if (prev == NULL) {
queue->tail = next;
} else {
prev->link.prev = next;
}
if (next == NULL) {
queue->head = prev;
} else {
next->link.next = prev;
}
if (queue->head == NULL) {
RunQueueBits &= ~(1 << 31 - thread->effective_priority);
}
thread->queue = NULL;
}
#pragma dont_inline reset
/* 80340DAC-80340DE8 33B6EC 003C+00 4/4 2/2 0/0 .text __OSGetEffectivePriority */
s32 __OSGetEffectivePriority(OSThread* thread) {
s32 prio = thread->base_priority;
OSMutex* mutex;
for (mutex = thread->owned_mutexes.head; mutex != NULL; mutex = mutex->link.next) {
OSThread* mutexThread = mutex->queue.head;
if (mutexThread != NULL && mutexThread->effective_priority < prio) {
prio = mutexThread->effective_priority;
}
}
return prio;
}
/* 80340DE8-80340FA8 33B728 01C0+00 5/5 0/0 0/0 .text SetEffectivePriority */
static OSThread* SetEffectivePriority(OSThread* thread, s32 priority) {
switch (thread->state) {
case OS_THREAD_STATE_READY:
UnsetRun(thread);
thread->effective_priority = priority;
SetRun(thread);
break;
case OS_THREAD_STATE_WAITING:
RemoveItem(thread->queue, thread, link);
thread->effective_priority = priority;
AddPrio(thread->queue, thread, link);
if (thread->mutex) {
return thread->mutex->thread;
}
break;
case OS_THREAD_STATE_RUNNING:
RunQueueHint = TRUE;
thread->effective_priority = priority;
break;
}
return NULL;
}
static void UpdatePriority(OSThread* thread) {
OSPriority priority;
do {
if (0 < thread->suspend_count) {
break;
}
priority = __OSGetEffectivePriority(thread);
if (thread->effective_priority == priority) {
break;
}
thread = SetEffectivePriority(thread, priority);
} while (thread);
}
/* 80340FA8-80340FF8 33B8E8 0050+00 0/0 1/1 0/0 .text __OSPromoteThread */
void __OSPromoteThread(OSThread* thread, s32 priority) {
do {
if (thread->suspend_count > 0 || thread->effective_priority <= priority) {
break;
}
thread = SetEffectivePriority(thread, priority);
} while (thread != NULL);
}
static inline void __OSSwitchThread(OSThread* nextThread) {
OSSetCurrentThread(nextThread);
OSSetCurrentContext(&nextThread->context);
OSLoadContext(&nextThread->context);
}
/* 80340FF8-80341220 33B938 0228+00 9/9 0/0 0/0 .text SelectThread */
inline OSThread* i_OSGetCurrentThread(void) {
return OS_CURRENT_THREAD;
}
static OSThread* SelectThread(BOOL yield) {
OSContext* currentContext;
OSThread* currentThread;
OSThread* nextThread;
OSPriority priority;
OSThreadQueue* queue;
if (0 < Reschedule) {
return 0;
}
currentContext = OSGetCurrentContext();
currentThread = i_OSGetCurrentThread();
if (currentContext != ¤tThread->context) {
return 0;
}
if (currentThread) {
if (currentThread->state == OS_THREAD_STATE_RUNNING) {
if (!yield) {
priority = __cntlzw(RunQueueBits);
if (currentThread->effective_priority <= priority) {
return 0;
}
}
currentThread->state = OS_THREAD_STATE_READY;
SetRun(currentThread);
}
if (!(currentThread->context.state & OS_CONTEXT_STATE_EXC) &&
OSSaveContext(¤tThread->context))
{
return 0;
}
}
if (RunQueueBits == 0) {
OSSetCurrentThread(NULL);
OSSetCurrentContext(&IdleContext);
do {
OSEnableInterrupts();
while (RunQueueBits == 0)
;
OSDisableInterrupts();
} while (RunQueueBits == 0);
OSClearContext(&IdleContext);
}
RunQueueHint = FALSE;
priority = __cntlzw(RunQueueBits);
queue = &RunQueue[priority];
RemoveHead(queue, nextThread, link);
if (queue->head == 0) {
RunQueueBits &= ~(1u << (OS_PRIORITY_MAX - priority));
}
nextThread->queue = NULL;
nextThread->state = OS_THREAD_STATE_RUNNING;
__OSSwitchThread(nextThread);
return nextThread;
}
/* 80341220-80341250 33BB60 0030+00 0/0 3/3 0/0 .text __OSReschedule */
void __OSReschedule(void) {
if (!RunQueueHint) {
return;
}
SelectThread(FALSE);
}
/* 80341250-8034128C 33BB90 003C+00 0/0 2/2 0/0 .text OSYieldThread */
void OSYieldThread(void) {
BOOL enabled;
enabled = OSDisableInterrupts();
SelectThread(TRUE);
OSRestoreInterrupts(enabled);
}
/* 8034128C-80341474 33BBCC 01E8+00 0/0 5/5 3/3 .text OSCreateThread */
BOOL OSCreateThread(OSThread* thread_, void* func, void* param, void* stackBase, u32 stackSize,
s32 priority, u16 attribute) {
BOOL enabled;
u32 i;
u32* stack;
OSThread* thread;
u32 stack1, stack2;
if (priority < 0 || priority > 31) {
return FALSE;
}
thread = thread_;
thread->state = OS_THREAD_STATE_READY;
thread->attributes = attribute & 1;
thread->base_priority = priority;
thread->effective_priority = priority;
thread->suspend_count = 1;
thread->exit_value = (void*)-1;
thread->mutex = NULL;
thread->join_queue.tail = NULL;
thread->join_queue.head = NULL;
thread->owned_mutexes.tail = NULL;
thread->owned_mutexes.head = NULL;
stack = (u32*)((u32)stackBase & 0xfffffff8);
stack[-2] = 0;
stack[-1] = 0;
OSInitContext(&thread->context, (u32)func, (u32)stack - 8);
thread->context.lr = (u32)OSExitThread;
thread->context.gpr[3] = (u32)param;
thread->stack_base = (u8*)stackBase;
thread->stack_end = (void*)((u32)stackBase - stackSize);
*(u32*)thread->stack_end = OS_THREAD_STACK_MAGIC;
thread->error_code = NULL;
thread->data[0] = NULL;
thread->data[1] = NULL;
enabled = OSDisableInterrupts();
if (__OSErrorTable[16]) {
thread->context.srr1 |= 0x900;
thread->context.state |= 1;
thread->context.fpscr = (__OSFpscrEnableBits & 0xf8) | 4;
for (i = 0; i < 32; ++i) {
*(u64*)&thread->context.fpr[i] = (u64)0xffffffffffffffffLL;
*(u64*)&thread->context.ps[i] = (u64)0xffffffffffffffffLL;
}
}
AddTail(&OS_THREAD_QUEUE, thread, active_threads_link);
OSRestoreInterrupts(enabled);
return TRUE;
}
/* 80341474-80341558 33BDB4 00E4+00 1/1 1/1 0/0 .text OSExitThread */
void OSExitThread(void* exitValue) {
OSThread* currentThread;
BOOL enabled;
enabled = OSDisableInterrupts();
currentThread = OS_CURRENT_THREAD;
OSClearContext(¤tThread->context);
if (currentThread->attributes & OS_THREAD_ATTR_DETACH) {
RemoveItem(&OS_THREAD_QUEUE, currentThread, active_threads_link);
currentThread->state = OS_THREAD_STATE_UNINITIALIZED;
} else {
currentThread->state = OS_THREAD_STATE_DEAD;
currentThread->exit_value = exitValue;
}
__OSUnlockAllMutex(currentThread);
OSWakeupThread(¤tThread->join_queue);
RunQueueHint = TRUE;
if (RunQueueHint) {
SelectThread(FALSE);
}
OSRestoreInterrupts(enabled);
}
/* 80341558-80341714 33BE98 01BC+00 0/0 4/4 3/3 .text OSCancelThread */
void OSCancelThread(OSThread* thread) {
BOOL enabled;
enabled = OSDisableInterrupts();
switch (thread->state) {
case OS_THREAD_STATE_READY:
if (!(0 < thread->suspend_count)) {
UnsetRun(thread);
}
break;
case OS_THREAD_STATE_RUNNING:
RunQueueHint = TRUE;
break;
case OS_THREAD_STATE_WAITING:
RemoveItem(thread->queue, thread, link);
thread->queue = NULL;
if (!(0 < thread->suspend_count) && thread->mutex) {
UpdatePriority(thread->mutex->thread);
}
break;
default:
OSRestoreInterrupts(enabled);
return;
}
OSClearContext(&thread->context);
if (thread->attributes & OS_THREAD_ATTR_DETACH) {
RemoveItem(&__OSActiveThreadQueue, thread, active_threads_link);
thread->state = 0;
} else {
thread->state = OS_THREAD_STATE_DEAD;
}
__OSUnlockAllMutex(thread);
OSWakeupThread(&thread->join_queue);
__OSReschedule();
OSRestoreInterrupts(enabled);
}
/* 80341714-803417B4 33C054 00A0+00 0/0 2/2 0/0 .text OSDetachThread */
void OSDetachThread(OSThread* thread) {
BOOL enabled;
enabled = OSDisableInterrupts();
thread->attributes |= OS_THREAD_ATTR_DETACH;
if (thread->state == OS_THREAD_STATE_DEAD) {
RemoveItem(&__OSActiveThreadQueue, thread, active_threads_link);
thread->state = OS_THREAD_STATE_UNINITIALIZED;
}
OSWakeupThread(&thread->join_queue);
OSRestoreInterrupts(enabled);
}
/* 803417B4-80341A3C 33C0F4 0288+00 0/0 13/13 3/3 .text OSResumeThread */
s32 OSResumeThread(OSThread* thread) {
BOOL enabled;
s32 suspendCount;
enabled = OSDisableInterrupts();
suspendCount = thread->suspend_count--;
if (thread->suspend_count < 0) {
thread->suspend_count = 0;
} else if (thread->suspend_count == 0) {
switch (thread->state) {
case OS_THREAD_STATE_READY:
thread->effective_priority = __OSGetEffectivePriority(thread);
SetRun(thread);
break;
case OS_THREAD_STATE_WAITING:
RemoveItem(thread->queue, thread, link);
thread->effective_priority = __OSGetEffectivePriority(thread);
AddPrio(thread->queue, thread, link);
if (thread->mutex) {
UpdatePriority(thread->mutex->thread);
}
break;
}
__OSReschedule();
}
OSRestoreInterrupts(enabled);
return suspendCount;
}
/* 80341A3C-80341BAC 33C37C 0170+00 0/0 7/7 4/4 .text OSSuspendThread */
s32 OSSuspendThread(OSThread* thread) {
BOOL enabled;
s32 suspendCount;
enabled = OSDisableInterrupts();
suspendCount = thread->suspend_count++;
if (suspendCount == 0) {
switch (thread->state) {
case OS_THREAD_STATE_RUNNING:
RunQueueHint = TRUE;
thread->state = OS_THREAD_STATE_READY;
break;
case OS_THREAD_STATE_READY:
UnsetRun(thread);
break;
case OS_THREAD_STATE_WAITING:
RemoveItem(thread->queue, thread, link);
thread->effective_priority = 32;
AddTail(thread->queue, thread, link);
if (thread->mutex) {
UpdatePriority(thread->mutex->thread);
}
break;
}
__OSReschedule();
}
OSRestoreInterrupts(enabled);
return suspendCount;
}
/* 80341BAC-80341C98 33C4EC 00EC+00 0/0 12/12 0/0 .text OSSleepThread */
void OSSleepThread(OSThreadQueue* queue) {
BOOL enabled;
OSThread* currentThread;
enabled = OSDisableInterrupts();
currentThread = OSGetCurrentThread();
currentThread->state = OS_THREAD_STATE_WAITING;
currentThread->queue = queue;
AddPrio(queue, currentThread, link);
RunQueueHint = TRUE;
__OSReschedule();
OSRestoreInterrupts(enabled);
}
/* 80341C98-80341D9C 33C5D8 0104+00 3/3 13/13 0/0 .text OSWakeupThread */
void OSWakeupThread(OSThreadQueue* queue) {
BOOL enabled;
OSThread* thread;
enabled = OSDisableInterrupts();
while (queue->head) {
RemoveHead(queue, thread, link);
thread->state = OS_THREAD_STATE_READY;
if (!(0 < thread->suspend_count)) {
SetRun(thread);
}
}
__OSReschedule();
OSRestoreInterrupts(enabled);
}
/* 80341D9C-80341E5C 33C6DC 00C0+00 0/0 1/1 0/0 .text OSSetThreadPriority */
s32 OSSetThreadPriority(OSThread* thread, s32 priority) {
BOOL enabled;
if (priority < 0 || priority > 31) {
return FALSE;
}
enabled = OSDisableInterrupts();
if ((s32)thread->base_priority != priority) {
thread->base_priority = priority;
UpdatePriority(thread);
__OSReschedule();
}
OSRestoreInterrupts(enabled);
return TRUE;
}
/* 80341E5C-80341E64 33C79C 0008+00 0/0 4/4 0/0 .text OSGetThreadPriority */
s32 OSGetThreadPriority(OSThread* thread) {
return thread->base_priority;
}
/* 80341E64-80341F00 33C7A4 009C+00 1/1 0/0 0/0 .text CheckThreadQueue */
s32 CheckThreadQueue(OSThreadQueue* thread) {
OSThread* current;
if (thread->head && thread->head->link.prev) {
return 0;
}
if (thread->tail && thread->tail->link.next) {
return 0;
}
current = thread->head;
while (current) {
if (current->link.next && current != current->link.next->link.prev) {
return 0;
}
if (current->link.prev && current != current->link.prev->link.next) {
return 0;
}
current = current->link.next;
}
return 1;
}
static BOOL isMember(OSThreadQueue* queue, OSThread* thread) {
OSThread* current = queue->head;
while (current != NULL) {
if (thread == current) {
return TRUE;
}
current = current->link.next;
}
return FALSE;
}
/* 80341F00-80342650 33C840 0750+00 0/0 1/1 0/0 .text OSCheckActiveThreads */
s32 OSCheckActiveThreads(void) {
s32 i;
OSThread* thread;
s32 rv = 0;
BOOL enabled;
enabled = OSDisableInterrupts();
for (i = 0; i <= 31; i++) {
if (RunQueueBits & (1 << (31 - i))) {
if (RunQueue[i].head == NULL || RunQueue[i].tail == NULL) {
OSReport("OSCheckActiveThreads: Failed RunQueue[prio].head != NULL && "
"RunQueue[prio].tail != NULL in %d\n",
0x5e0);
OSPanic(__FILE__, 0x5e0, "");
}
} else {
if (RunQueue[i].head != NULL || RunQueue[i].tail != NULL) {
OSReport("OSCheckActiveThreads: Failed RunQueue[prio].head == NULL && "
"RunQueue[prio].tail == NULL in %d\n",
0x5e5);
OSPanic(__FILE__, 0x5e5, "");
}
}
if (CheckThreadQueue(&RunQueue[i]) == 0) {
OSReport("OSCheckActiveThreads: Failed CheckThreadQueue(&RunQueue[prio]) in %d\n",
0x5e7);
OSPanic(__FILE__, 0x5e7, "");
}
}
if (OS_THREAD_QUEUE.head != NULL && OS_THREAD_QUEUE.head->active_threads_link.prev != NULL) {
OSReport("OSCheckActiveThreads: Failed __OSActiveThreadQueue.head == NULL || "
"__OSActiveThreadQueue.head->linkActive.prev == NULL in %d\n",
0x5ec);
OSPanic(__FILE__, 0x5ec, "");
}
if (OS_THREAD_QUEUE.tail != NULL && OS_THREAD_QUEUE.tail->active_threads_link.next != NULL) {
OSReport("OSCheckActiveThreads: Failed __OSActiveThreadQueue.tail == NULL || "
"__OSActiveThreadQueue.tail->linkActive.next == NULL in %d\n",
0x5ee);
OSPanic(__FILE__, 0x5ee, "");
}
thread = OS_THREAD_QUEUE.head;
while (thread != NULL) {
rv++;
if (thread->active_threads_link.next != NULL &&
thread != thread->active_threads_link.next->active_threads_link.prev)
{
OSReport("OSCheckActiveThreads: Failed thread->linkActive.next == NULL || thread == "
"thread->linkActive.next->linkActive.prev in %d\n",
0x5f6);
OSPanic(__FILE__, 0x5f6, "");
}
if (thread->active_threads_link.prev != NULL &&
thread != thread->active_threads_link.prev->active_threads_link.next)
{
OSReport("OSCheckActiveThreads: Failed thread->linkActive.prev == NULL || thread == "
"thread->linkActive.prev->linkActive.next in %d\n",
0x5f8);
OSPanic(__FILE__, 0x5f8, "");
}
if (*(u32*)thread->stack_end != OS_THREAD_STACK_MAGIC) {
OSReport(
"OSCheckActiveThreads: Failed *(thread->stackEnd) == OS_THREAD_STACK_MAGIC in %d\n",
0x5fb);
OSPanic(__FILE__, 0x5fb, "");
}
if (OS_PRIORITY_MIN > thread->effective_priority ||
thread->effective_priority > OS_PRIORITY_MAX + 1)
{
OSReport("OSCheckActiveThreads: Failed OS_PRIORITY_MIN <= thread->priority && "
"thread->priority <= OS_PRIORITY_MAX+1 in %d\n",
0x5fe);
OSPanic(__FILE__, 0x5fe, "");
}
if (thread->suspend_count < 0) {
OSReport("OSCheckActiveThreads: Failed 0 <= thread->suspend in %d\n", 0x5ff);
OSPanic(__FILE__, 0x5ff, "");
}
if (!CheckThreadQueue(&thread->join_queue)) {
OSReport("OSCheckActiveThreads: Failed CheckThreadQueue(&thread->queueJoin) in %d\n",
0x600);
OSPanic(__FILE__, 0x600, "");
}
switch (thread->state) {
case OS_THREAD_STATE_READY:
if (thread->suspend_count <= 0) {
if (thread->queue != &RunQueue[thread->effective_priority]) {
OSReport("OSCheckActiveThreads: Failed thread->queue == "
"&RunQueue[thread->priority] in %d\n",
0x606);
OSPanic(__FILE__, 0x606, "");
}
if (!isMember(&RunQueue[thread->effective_priority], thread)) {
OSReport("OSCheckActiveThreads: Failed IsMember(&RunQueue[thread->priority], "
"thread) in %d\n",
0x607);
OSPanic(__FILE__, 0x607, "");
}
if (thread->effective_priority != __OSGetEffectivePriority(thread)) {
OSReport("OSCheckActiveThreads: Failed thread->priority == "
"__OSGetEffectivePriority(thread) in %d\n",
0x608);
OSPanic(__FILE__, 0x608, "");
}
}
break;
case OS_THREAD_STATE_RUNNING:
if (thread->suspend_count > 0) {
OSReport("OSCheckActiveThreads: Failed !IsSuspended(thread->suspend) in %d\n",
0x60c);
OSPanic(__FILE__, 0x60c, "");
}
if (thread->queue != NULL) {
OSReport("OSCheckActiveThreads: Failed thread->queue == NULL in %d\n", 0x60d);
OSPanic(__FILE__, 0x60d, "");
}
if (thread->effective_priority != __OSGetEffectivePriority(thread)) {
OSReport("OSCheckActiveThreads: Failed thread->priority == "
"__OSGetEffectivePriority(thread) in %d\n",
0x60e);
OSPanic(__FILE__, 0x60e, "");
}
break;
case OS_THREAD_STATE_WAITING:
if (thread->queue == NULL) {
OSReport("OSCheckActiveThreads: Failed thread->queue != NULL in %d\n", 0x611);
OSPanic(__FILE__, 0x611, "");
}
if (CheckThreadQueue(thread->queue) == 0) {
OSReport("OSCheckActiveThreads: Failed CheckThreadQueue(thread->queue) in %d\n",
0x612);
OSPanic(__FILE__, 0x612, "");
}
if (!isMember(thread->queue, thread)) {
OSReport("OSCheckActiveThreads: Failed IsMember(thread->queue, thread) in %d\n",
0x613);
OSPanic(__FILE__, 0x613, "");
}
if (thread->suspend_count <= 0) {
if (thread->effective_priority != __OSGetEffectivePriority(thread)) {
OSReport("OSCheckActiveThreads: Failed thread->priority == "
"__OSGetEffectivePriority(thread) in %d\n",
0x616);
OSPanic(__FILE__, 0x616, "");
}
} else if (thread->effective_priority != 32) {
OSReport("OSCheckActiveThreads: Failed thread->priority == 32 in %d\n", 0x61a);
OSPanic(__FILE__, 0x61a, "");
}
if (__OSCheckDeadLock(thread)) {
OSReport("OSCheckActiveThreads: Failed !__OSCheckDeadLock(thread) in %d\n", 0x61c);
OSPanic(__FILE__, 0x61c, "");
}
break;
case OS_THREAD_STATE_DEAD:
if (thread->owned_mutexes.head != NULL || thread->owned_mutexes.tail != NULL) {
OSReport("OSCheckActiveThreads: Failed thread->queueMutex.head == NULL && "
"thread->queueMutex.tail == NULL in %d\n",
0x620);
OSPanic(__FILE__, 0x620, "");
}
break;
default:
OSReport("OSCheckActiveThreads: Failed. unkown thread state (%d) of thread %p\n",
thread->state, thread);
OSPanic(__FILE__, 0x626, "");
break;
}
if (!__OSCheckMutexes(thread)) {
OSReport("OSCheckActiveThreads: Failed __OSCheckMutexes(thread) in %d\n", 0x62b);
OSPanic(__FILE__, 0x62b, "");
}
thread = thread->active_threads_link.next;
}
OSRestoreInterrupts(enabled);
return rv;
}
/* 80342650-803426FC 33CF90 00AC+00 1/1 0/0 0/0 .text OSClearStack */
void OSClearStack(u8 val) {
u32* sp;
u32* p;
u32 pattern;
pattern = ((u32)val << 24) | ((u32)val << 16) | ((u32)val << 8) | (u32)val;
sp = (u32*)OSGetStackPointer();
for (p = ((u32*)__OSCurrentThread->stack_end) + 1; p < sp; ++p) {
*p = pattern;
}
}
/* ############################################################################################## */
/* 804516D0-804516D8 000BD0 0008+00 0/0 2/1 0/0 .sbss None */
extern u8 data_804516D0[8];
u8 data_804516D0[8] ALIGN_DECL(8);
|