summaryrefslogtreecommitdiff
path: root/src/Dolphin/os/OSThread.c
blob: 3bd1e5db64cad5530e3114e630f725db4076d81e (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
#include "dolphin/os/OSPriv.h"

static vu32 RunQueueBits;
static volatile BOOL RunQueueHint;
static vs32 Reschedule;

static OSThreadQueue RunQueue[32];
static OSThread IdleThread;
static OSThread DefaultThread;
static OSContext IdleContext;
static void DefaultSwitchThreadCallback(OSThread* from, OSThread* to);
static OSSwitchThreadCallback SwitchThreadCallback = DefaultSwitchThreadCallback;

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;

static void DefaultSwitchThreadCallback(OSThread* from, OSThread* to) {}

extern u8 _stack_addr[];
extern u8 _stack_end[];

#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->priority <= thread->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)

static inline void OSInitMutexQueue(OSMutexQueue* queue) { queue->head = queue->tail = NULL; }

static inline void OSSetCurrentThread(OSThread* thread) {
  SwitchThreadCallback(__OSCurrentThread, thread);
  __OSCurrentThread = thread;
}

void __OSThreadInit() {
  OSThread* thread = &DefaultThread;
  int prio;

  thread->state = OS_THREAD_STATE_RUNNING;
  thread->attr = OS_THREAD_ATTR_DETACH;
  thread->priority = thread->base = 16;
  thread->suspend = 0;
  thread->val = (void*)-1;
  thread->mutex = NULL;
  OSInitThreadQueue(&thread->queueJoin);
  OSInitMutexQueue(&thread->queueMutex);

  __OSFPUContext = &thread->context;

  OSClearContext(&thread->context);
  OSSetCurrentContext(&thread->context);
  thread->stackBase = (void*)_stack_addr;
  thread->stackEnd = (void*)_stack_end;
  *(thread->stackEnd) = 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, linkActive);
  OSClearContext(&IdleContext);
  Reschedule = 0;
}

void OSInitThreadQueue(OSThreadQueue* queue) { queue->head = queue->tail = NULL; }

OSThread* OSGetCurrentThread() { return __OSCurrentThread; }

#ifdef FULL_FRANK
/* Code matches, stack epilogue bug*/
s32 OSDisableScheduler() {
  BOOL enabled;
  s32 count;

  enabled = OSDisableInterrupts();
  count = Reschedule++;
  OSRestoreInterrupts(enabled);
  return count;
}
#else
/* clang-format off */
#pragma push
#pragma optimization_level
#pragma optimizewithasm off
asm s32 OSDisableScheduler() {
  nofralloc
  mflr r0
  stw r0, 4(r1)
  stwu r1, -0x10(r1)
  stw r31, 0xc(r1)
  bl OSDisableInterrupts
  lwz r4, Reschedule
  addi r0, r4, 1
  stw r0, Reschedule
  mr r31, r4
  bl OSRestoreInterrupts
  mr r3, r31
  lwz r0, 0x14(r1)
  lwz r31, 0xc(r1)
  addi r1, r1, 0x10
  mtlr r0
  blr
}
/* clang-format on */
#pragma pop
#endif

#ifdef FULL_FRANK
/* Code matches, stack epilogue bug*/
s32 OSEnableScheduler() {
  BOOL enabled;
  s32 count;

  enabled = OSDisableInterrupts();
  count = Reschedule--;
  OSRestoreInterrupts(enabled);
  return count;
}
#else
/* clang-format off */
#pragma push
#pragma optimization_level
#pragma optimizewithasm off
asm s32 OSEnableScheduler() {
  nofralloc
  mflr r0
  stw r0, 4(r1)
  stwu r1, -0x10(r1)
  stw r31, 0xc(r1)
  bl OSDisableInterrupts
  lwz r4, Reschedule
  subi r0, r4, 1
  stw r0, Reschedule
  mr r31, r4
  bl OSRestoreInterrupts
  mr r3, r31
  lwz r0, 0x14(r1)
  lwz r31, 0xc(r1)
  addi r1, r1, 0x10
  mtlr r0
  blr
}
/* clang-format on */
#pragma pop
#endif

static void SetRun(OSThread* thread) {
  thread->queue = &RunQueue[thread->priority];
  AddTail(thread->queue, thread, link);
  RunQueueBits |= 1u << (OS_PRIORITY_MAX - thread->priority);
  RunQueueHint = TRUE;
}
#pragma dont_inline on
static void UnsetRun(OSThread* thread) {
  OSThreadQueue* queue;
  queue = thread->queue;
  RemoveItem(queue, thread, link);
  if (queue->head == 0)
    RunQueueBits &= ~(1u << (OS_PRIORITY_MAX - thread->priority));
  thread->queue = NULL;
}
#pragma dont_inline reset

OSPriority __OSGetEffectivePriority(OSThread* thread) {
  OSPriority priority;
  OSMutex* mutex;
  OSThread* blocked;

  priority = thread->base;
  for (mutex = thread->queueMutex.head; mutex; mutex = mutex->link.next) {
    blocked = mutex->queue.head;
    if (blocked && blocked->priority < priority) {
      priority = blocked->priority;
    }
  }
  return priority;
}

static OSThread* SetEffectivePriority(OSThread* thread, OSPriority priority) {
  switch (thread->state) {
  case OS_THREAD_STATE_READY:
    UnsetRun(thread);
    thread->priority = priority;
    SetRun(thread);
    break;
  case OS_THREAD_STATE_WAITING:
    RemoveItem(thread->queue, thread, link);
    thread->priority = priority;
    AddPrio(thread->queue, thread, link);
    if (thread->mutex) {
      return thread->mutex->thread;
    }
    break;
  case OS_THREAD_STATE_RUNNING:
    RunQueueHint = TRUE;
    thread->priority = priority;
    break;
  }
  return NULL;
}

static void UpdatePriority(OSThread* thread) {
  OSPriority priority;

  do {
    if (0 < thread->suspend) {
      break;
    }
    priority = __OSGetEffectivePriority(thread);
    if (thread->priority == priority) {
      break;
    }
    thread = SetEffectivePriority(thread, priority);
  } while (thread);
}

static void __OSSwitchThread(OSThread* nextThread) {
  OSSetCurrentThread(nextThread);
  OSSetCurrentContext(&nextThread->context);
  OSLoadContext(&nextThread->context);
}

static OSThread* SelectThread(BOOL yield) {
  OSContext* currentContext;
  OSThread* currentThread;
  OSThread* nextThread;
  OSPriority priority;
  OSThreadQueue* queue;

  if (0 < Reschedule) {
    return 0;
  }

  currentContext = OSGetCurrentContext();
  currentThread = OSGetCurrentThread();
  if (currentContext != &currentThread->context) {
    return 0;
  }

  if (currentThread) {
    if (currentThread->state == OS_THREAD_STATE_RUNNING) {
      if (!yield) {
        priority = __cntlzw(RunQueueBits);
        if (currentThread->priority <= priority) {
          return 0;
        }
      }
      currentThread->state = OS_THREAD_STATE_READY;
      SetRun(currentThread);
    }

    if (!(currentThread->context.state & OS_CONTEXT_STATE_EXC) &&
        OSSaveContext(&currentThread->context)) {
      return 0;
    }
  }

  OSSetCurrentThread(NULL);
  if (RunQueueBits == 0) {
    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;
}

void __OSReschedule() {
  if (!RunQueueHint) {
    return;
  }

  SelectThread(FALSE);
}

void OSYieldThread(void) {
  BOOL enabled;

  enabled = OSDisableInterrupts();
  SelectThread(TRUE);
  OSRestoreInterrupts(enabled);
}

void OSCancelThread(OSThread* thread) {
  BOOL enabled;

  enabled = OSDisableInterrupts();

  switch (thread->state) {
  case OS_THREAD_STATE_READY:
    if (!(0 < thread->suspend)) {
      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) && thread->mutex) {
      UpdatePriority(thread->mutex->thread);
    }
    break;
  default:
    OSRestoreInterrupts(enabled);
    return;
  }

  OSClearContext(&thread->context);
  if (thread->attr & OS_THREAD_ATTR_DETACH) {
    RemoveItem(&__OSActiveThreadQueue, thread, linkActive);
    thread->state = 0;
  } else {
    thread->state = OS_THREAD_STATE_MORIBUND;
  }

  __OSUnlockAllMutex(thread);

  OSWakeupThread(&thread->queueJoin);

  __OSReschedule();
  OSRestoreInterrupts(enabled);

  return;
}

#ifdef FULL_FRANK
/* Code matches, stack epilogue bug*/
s32 OSResumeThread(OSThread* thread) {
  BOOL enabled;
  s32 suspendCount;

  enabled = OSDisableInterrupts();
  suspendCount = thread->suspend--;
  if (thread->suspend < 0) {
    thread->suspend = 0;
  } else if (thread->suspend == 0) {
    switch (thread->state) {
    case OS_THREAD_STATE_READY:
      thread->priority = __OSGetEffectivePriority(thread);
      SetRun(thread);
      break;
    case OS_THREAD_STATE_WAITING:
      RemoveItem(thread->queue, thread, link);
      thread->priority = __OSGetEffectivePriority(thread);
      AddPrio(thread->queue, thread, link);
      if (thread->mutex) {
        UpdatePriority(thread->mutex->thread);
      }
      break;
    }
    __OSReschedule();
  }
  OSRestoreInterrupts(enabled);
  return suspendCount;
}
#else
/* clang-format off */
#pragma push
#pragma optimization_level 0
#pragma optimizewithasm off
asm s32 OSResumeThread(OSThread* thread) {
  nofralloc
  mflr r0
  stw r0, 4(r1)
  stwu r1, -0x28(r1)
  stw r31, 0x24(r1)
  stw r30, 0x20(r1)
  stw r29, 0x1c(r1)
  mr r29, r3
  bl OSDisableInterrupts
  lwz r4, 0x2cc(r29)
  addi r31, r3, 0
  addi r0, r4, -1
  stw r0, 0x2cc(r29)
  mr r30, r4
  lwz r0, 0x2cc(r29)
  cmpwi r0, 0
  bge lbl_80384D60
  li r0, 0
  stw r0, 0x2cc(r29)
  b lbl_80384F74
lbl_80384D60:
  bne lbl_80384F74
  lhz r0, 0x2c8(r29)
  cmpwi r0, 4
  beq lbl_80384E24
  bge lbl_80384F60
  cmpwi r0, 1
  beq lbl_80384D80
  b lbl_80384F60
lbl_80384D80:
  lwz r0, 0x2d4(r29)
  lwz r3, 0x2f4(r29)
  b lbl_80384DAC
lbl_80384D8C:
  lwz r4, 0(r3)
  cmplwi r4, 0
  beq lbl_80384DA8
  lwz r4, 0x2d0(r4)
  cmpw r4, r0
  bge lbl_80384DA8
  mr r0, r4
lbl_80384DA8:
  lwz r3, 0x10(r3)
lbl_80384DAC:
  cmplwi r3, 0
  bne lbl_80384D8C
  stw r0, 0x2d0(r29)
  lis r3, RunQueue@ha
  addi r0, r3, RunQueue@l
  lwz r3, 0x2d0(r29)
  slwi r3, r3, 3
  add r0, r0, r3
  stw r0, 0x2dc(r29)
  lwz r4, 0x2dc(r29)
  lwz r3, 4(r4)
  cmplwi r3, 0
  bne lbl_80384DE8
  stw r29, 0(r4)
  b lbl_80384DEC
lbl_80384DE8:
  stw r29, 0x2e0(r3)
lbl_80384DEC:
  stw r3, 0x2e4(r29)
  li r0, 0
  li r3, 1
  stw r0, 0x2e0(r29)
  lwz r4, 0x2dc(r29)
  stw r29, 4(r4)
  lwz r0, 0x2d0(r29)
  lwz r4, RunQueueBits
  subfic r0, r0, 0x1f
  slw r0, r3, r0
  or r0, r4, r0
  stw r0, RunQueueBits
  stw r3, RunQueueHint
  b lbl_80384F60
lbl_80384E24:
  lwz r4, 0x2e0(r29)
  lwz r5, 0x2e4(r29)
  cmplwi r4, 0
  bne lbl_80384E40
  lwz r3, 0x2dc(r29)
  stw r5, 4(r3)
  b lbl_80384E44
lbl_80384E40:
  stw r5, 0x2e4(r4)
lbl_80384E44:
  cmplwi r5, 0
  bne lbl_80384E58
  lwz r3, 0x2dc(r29)
  stw r4, 0(r3)
  b lbl_80384E5C
lbl_80384E58:
  stw r4, 0x2e0(r5)
lbl_80384E5C:
  lwz r0, 0x2d4(r29)
  lwz r3, 0x2f4(r29)
  b lbl_80384E88
lbl_80384E68:
  lwz r4, 0(r3)
  cmplwi r4, 0
  beq lbl_80384E84
  lwz r4, 0x2d0(r4)
  cmpw r4, r0
  bge lbl_80384E84
  mr r0, r4
lbl_80384E84:
  lwz r3, 0x10(r3)
lbl_80384E88:
  cmplwi r3, 0
  bne lbl_80384E68
  stw r0, 0x2d0(r29)
  lwz r4, 0x2dc(r29)
  lwz r5, 0(r4)
  b lbl_80384EA4
lbl_80384EA0:
  lwz r5, 0x2e0(r5)
lbl_80384EA4:
  cmplwi r5, 0
  beq lbl_80384EBC
  lwz r3, 0x2d0(r5)
  lwz r0, 0x2d0(r29)
  cmpw r3, r0
  ble lbl_80384EA0
lbl_80384EBC:
  cmplwi r5, 0
  bne lbl_80384EF4
  lwz r3, 4(r4)
  cmplwi r3, 0
  bne lbl_80384ED8
  stw r29, 0(r4)
  b lbl_80384EDC
lbl_80384ED8:
  stw r29, 0x2e0(r3)
lbl_80384EDC:
  stw r3, 0x2e4(r29)
  li r0, 0
  stw r0, 0x2e0(r29)
  lwz r3, 0x2dc(r29)
  stw r29, 4(r3)
  b lbl_80384F1C
lbl_80384EF4:
  stw r5, 0x2e0(r29)
  lwz r3, 0x2e4(r5)
  stw r29, 0x2e4(r5)
  cmplwi r3, 0
  stw r3, 0x2e4(r29)
  bne lbl_80384F18
  lwz r3, 0x2dc(r29)
  stw r29, 0(r3)
  b lbl_80384F1C
lbl_80384F18:
  stw r29, 0x2e0(r3)
lbl_80384F1C:
  lwz r3, 0x2f0(r29)
  cmplwi r3, 0
  beq lbl_80384F60
  lwz r29, 8(r3)
lbl_80384F2C:
  lwz r0, 0x2cc(r29)
  cmpwi r0, 0
  bgt lbl_80384F60
  mr r3, r29
  bl __OSGetEffectivePriority
  lwz r0, 0x2d0(r29)
  addi r4, r3, 0
  cmpw r0, r4
  beq lbl_80384F60
  mr r3, r29
  bl SetEffectivePriority
  or. r29, r3, r3
  bne lbl_80384F2C
lbl_80384F60:
  lwz r0, RunQueueHint
  cmpwi r0, 0
  beq lbl_80384F74
  li r3, 0
  bl SelectThread
lbl_80384F74:
  mr r3, r31
  bl OSRestoreInterrupts
  mr r3, r30
  lwz r0, 0x2c(r1)
  lwz r31, 0x24(r1)
  lwz r30, 0x20(r1)
  lwz r29, 0x1c(r1)
  addi r1, r1, 0x28
  mtlr r0
  blr
}
#pragma pop
/* clang-format on */

#endif

#ifdef FULL_FRANK
/* Code matches, stack epilogue bug*/
s32 OSSuspendThread(OSThread* thread) {
  BOOL enabled;
  s32 suspendCount;

  enabled = OSDisableInterrupts();
  suspendCount = thread->suspend++;
  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->priority = 32;
      AddTail(thread->queue, thread, link);
      if (thread->mutex) {
        UpdatePriority(thread->mutex->thread);
      }
      break;
    }

    __OSReschedule();
  }
  OSRestoreInterrupts(enabled);
  return suspendCount;
}
#else
/* clang-format off */
#pragma push
#pragma optimization_level 0
#pragma optimizewithasm off
asm s32 OSSuspendThread(OSThread* thread) {
  nofralloc
  mflr r0
  stw r0, 4(r1)
  stwu r1, -0x20(r1)
  stw r31, 0x1c(r1)
  stw r30, 0x18(r1)
  stw r29, 0x14(r1)
  mr r29, r3
  bl OSDisableInterrupts
  lwz r4, 0x2cc(r29)
  addi r31, r3, 0
  addi r0, r4, 1
  or. r30, r4, r4
  stw r0, 0x2cc(r29)
  bne lbl_803850E4
  lhz r0, 0x2c8(r29)
  cmpwi r0, 3
  beq lbl_803850D0
  bge lbl_80384FF4
  cmpwi r0, 1
  beq lbl_80385010
  bge lbl_80385000
  b lbl_803850D0
lbl_80384FF4:
  cmpwi r0, 5
  bge lbl_803850D0
  b lbl_8038501C
lbl_80385000:
  li r0, 1
  stw r0, RunQueueHint
  sth r0, 0x2c8(r29)
  b lbl_803850D0
lbl_80385010:
  mr r3, r29
  bl UnsetRun
  b lbl_803850D0
lbl_8038501C:
  lwz r4, 0x2e0(r29)
  lwz r5, 0x2e4(r29)
  cmplwi r4, 0
  bne lbl_80385038
  lwz r3, 0x2dc(r29)
  stw r5, 4(r3)
  b lbl_8038503C
lbl_80385038:
  stw r5, 0x2e4(r4)
lbl_8038503C:
  cmplwi r5, 0
  bne lbl_80385050
  lwz r3, 0x2dc(r29)
  stw r4, 0(r3)
  b lbl_80385054
lbl_80385050:
  stw r4, 0x2e0(r5)
lbl_80385054:
  li r0, 0x20
  stw r0, 0x2d0(r29)
  lwz r4, 0x2dc(r29)
  lwz r3, 4(r4)
  cmplwi r3, 0
  bne lbl_80385074
  stw r29, 0(r4)
  b lbl_80385078
lbl_80385074:
  stw r29, 0x2e0(r3)
lbl_80385078:
  stw r3, 0x2e4(r29)
  li r0, 0
  stw r0, 0x2e0(r29)
  lwz r3, 0x2dc(r29)
  stw r29, 4(r3)
  lwz r3, 0x2f0(r29)
  cmplwi r3, 0
  beq lbl_803850D0
  lwz r29, 8(r3)
lbl_8038509C:
  lwz r0, 0x2cc(r29)
  cmpwi r0, 0
  bgt lbl_803850D0
  mr r3, r29
  bl __OSGetEffectivePriority
  lwz r0, 0x2d0(r29)
  addi r4, r3, 0
  cmpw r0, r4
  beq lbl_803850D0
  mr r3, r29
  bl SetEffectivePriority
  or. r29, r3, r3
  bne lbl_8038509C
lbl_803850D0:
  lwz r0, RunQueueHint
  cmpwi r0, 0
  beq lbl_803850E4
  li r3, 0
  bl SelectThread
lbl_803850E4:
  mr r3, r31
  bl OSRestoreInterrupts
  mr r3, r30
  lwz r0, 0x24(r1)
  lwz r31, 0x1c(r1)
  lwz r30, 0x18(r1)
  lwz r29, 0x14(r1)
  addi r1, r1, 0x20
  mtlr r0
  blr
}
#pragma pop
/* clang-format on */
#endif

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);
}

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)) {
      SetRun(thread);
    }
  }
  __OSReschedule();
  OSRestoreInterrupts(enabled);
}

BOOL OSSetThreadPriority(OSThread* thread, s32 prio) {
    BOOL enabled;

    if (prio < OS_PRIORITY_MIN || prio > OS_PRIORITY_MAX) {
        return FALSE;
    }

    enabled = OSDisableInterrupts();

    if (thread->base != prio) {
        thread->base = prio;
        UpdatePriority(thread);
        __OSReschedule();
    }

    OSRestoreInterrupts(enabled);
    return TRUE;
}

OSPriority OSGetThreadPriority(OSThread *thread) {
	return thread->base;
}

void OSClearStack(u8 val) {
  register u32 sp;
  register u32* p;
  register u32 pattern;

  pattern = ((u32)val << 24) | ((u32)val << 16) | ((u32)val << 8) | (u32)val;
  sp = OSGetStackPointer();
  for (p = __OSCurrentThread->stackEnd + 1; p < (u32*)sp; ++p) {
    *p = pattern;
  }
}