/
coreinit_Thread.cpp
1368 lines (1182 loc) · 44.4 KB
/
coreinit_Thread.cpp
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
#include "Cafe/OS/common/OSCommon.h"
#include "Cafe/HW/Espresso/PPCCallback.h"
#include "Cafe/OS/RPL/rpl.h"
#include "Cafe/OS/libs/coreinit/coreinit_Thread.h"
#include "Cafe/OS/libs/coreinit/coreinit_Time.h"
#include "Cafe/OS/libs/coreinit/coreinit_Alarm.h"
#include "Cafe/OS/libs/snd_core/ax.h"
#include "Cafe/HW/Espresso/Interpreter/PPCInterpreterInternal.h"
#include "Cafe/HW/Espresso/Recompiler/PPCRecompiler.h"
#include "util/helpers/Semaphore.h"
#include "util/helpers/ConcurrentQueue.h"
#include "util/Fiber/Fiber.h"
#include "util/helpers/helpers.h"
SlimRWLock srwlock_activeThreadList;
// public list of active threads
MPTR activeThread[256];
sint32 activeThreadCount = 0;
MPTR exitThreadPtr = 0;
void nnNfp_update();
namespace coreinit
{
void __OSFiberThreadEntry(void* thread);
void __OSAddReadyThreadToRunQueue(OSThread_t* thread);
void __OSRemoveThreadFromRunQueues(OSThread_t* thread);
};
namespace coreinit
{
// scheduler state
std::atomic<bool> sSchedulerActive;
std::vector<std::thread> sSchedulerThreads;
std::mutex sSchedulerStateMtx;
SysAllocator<OSThreadQueue> g_activeThreadQueue; // list of all threads (can include non-detached inactive threads)
SysAllocator<OSThreadQueue, 3> g_coreRunQueue;
CounterSemaphore g_coreRunQueueThreadCount[3];
bool g_isMulticoreMode;
__declspec(thread) uint32 t_assignedCoreIndex;
__declspec(thread) Fiber* t_schedulerFiber;
struct OSHostThread
{
OSHostThread(OSThread_t* thread) : m_thread(thread), m_fiber(__OSFiberThreadEntry, this, this)
{
}
~OSHostThread()
{
}
OSThread_t* m_thread;
Fiber m_fiber;
// padding (used as stack memory in recompiler)
uint8 padding[1024 * 128];
PPCInterpreter_t ppcInstance;
uint32 selectedCore;
};
std::unordered_map<OSThread_t*, OSHostThread*> s_threadToFiber;
// create host thread (fiber) that will be used to run the PPC instance
// note that host threads are fibers and not actual threads
void __OSCreateHostThread(OSThread_t* thread)
{
cemu_assert_debug(__OSHasSchedulerLock());
cemu_assert_debug(s_threadToFiber.find(thread) == s_threadToFiber.end());
OSHostThread* hostThread = new OSHostThread(thread);
s_threadToFiber.emplace(thread, hostThread);
}
// delete host thread
void __OSDeleteHostThread(OSThread_t* thread)
{
static OSHostThread* _deleteQueue = nullptr;
cemu_assert_debug(__OSHasSchedulerLock());
if (_deleteQueue)
{
delete _deleteQueue;
_deleteQueue = nullptr;
}
// delete with a delay (using queue of length 1)
// since the fiber might still be in use right now we have to delay the deletion
auto hostThread = s_threadToFiber[thread];
s_threadToFiber.erase(thread);
_deleteQueue = hostThread;
}
// add thread to active queue
void __OSActivateThread(OSThread_t* thread)
{
cemu_assert_debug(__OSHasSchedulerLock());
g_activeThreadQueue->addThread(thread, &thread->activeThreadChain); // todo - check if thread already in queue
MPTR threadMPTR = memory_getVirtualOffsetFromPointer(thread);
srwlock_activeThreadList.LockWrite();
bool alreadyActive = false;
for (sint32 i = 0; i < activeThreadCount; i++)
{
if (activeThread[i] == threadMPTR)
{
cemu_assert_debug(false); // should not happen
alreadyActive = true;
}
}
if (alreadyActive == false)
{
activeThread[activeThreadCount] = threadMPTR;
activeThreadCount++;
}
__OSCreateHostThread(thread);
srwlock_activeThreadList.UnlockWrite();
}
// remove thread from active queue. Reset id and state
void __OSDeactivateThread(OSThread_t* thread)
{
cemu_assert_debug(__OSHasSchedulerLock());
// remove thread from active thread list
MPTR t = memory_getVirtualOffsetFromPointer(thread);
srwlock_activeThreadList.LockWrite();
bool noHit = true;
for (sint32 i = 0; i < activeThreadCount; i++)
{
if (activeThread[i] == t)
{
activeThread[i] = activeThread[activeThreadCount - 1];
activeThreadCount--;
noHit = false;
break;
}
}
cemu_assert_debug(noHit == false);
srwlock_activeThreadList.UnlockWrite();
g_activeThreadQueue->removeThread(thread, &thread->activeThreadChain); // todo - check if thread in queue
cemu_assert_debug(thread->state == OSThread_t::THREAD_STATE::STATE_NONE);
thread->id = 0x8000;
__OSDeleteHostThread(thread);
}
bool __OSIsThreadActive(OSThread_t* thread)
{
cemu_assert_debug(__OSHasSchedulerLock());
MPTR threadMPTR = memory_getVirtualOffsetFromPointer(thread);
srwlock_activeThreadList.LockWrite();
bool isRunable = false;
for (sint32 i = 0; i < activeThreadCount; i++)
{
if (activeThread[i] == threadMPTR)
{
srwlock_activeThreadList.UnlockWrite();
return true;
}
}
srwlock_activeThreadList.UnlockWrite();
return false;
}
// thread
OSThread_t* __currentCoreThread[3] = {};
void OSSetCurrentThread(uint32 coreIndex, OSThread_t* thread)
{
if (coreIndex < 3)
__currentCoreThread[coreIndex] = thread;
}
OSThread_t* OSGetCurrentThread()
{
PPCInterpreter_t* currentInstance = PPCInterpreter_getCurrentInstance();
if (currentInstance == nullptr)
return nullptr;
return __currentCoreThread[currentInstance->spr.UPIR];
}
MPTR funcPtr_threadEntry = 0;
void threadEntry(PPCInterpreter_t* hCPU)
{
OSThread_t* currentThread = coreinitThread_getCurrentThreadDepr(hCPU);
uint32 r3 = hCPU->gpr[3];
uint32 r4 = hCPU->gpr[4];
uint32 lr = hCPU->spr.LR;
// cpp exception init callback
//const uint32 im = OSDisableInterrupts(); -> on an actual Wii U interrupts are disabled for this callback, but there are games that yield the thread in the callback (see Angry Birds Star Wars)
if (gCoreinitData->__cpp_exception_init_ptr != MPTR_NULL)
{
PPCCoreCallback(_swapEndianU32(gCoreinitData->__cpp_exception_init_ptr), ¤tThread->crt.eh_globals);
}
//OSRestoreInterrupts(im);
// forward to thread entrypoint
hCPU->spr.LR = lr;
hCPU->gpr[3] = r3;
hCPU->gpr[4] = r4;
hCPU->instructionPointer = _swapEndianU32(currentThread->entrypoint);
}
void coreinitExport_OSExitThreadDepr(PPCInterpreter_t* hCPU);
void initFunctionPointers()
{
exitThreadPtr = PPCInterpreter_makeCallableExportDepr(coreinitExport_OSExitThreadDepr);
funcPtr_threadEntry = PPCInterpreter_makeCallableExportDepr(threadEntry);
}
void OSCreateThreadInternal(OSThread_t* thread, uint32 entryPoint, MPTR stackLowerBaseAddr, uint32 stackSize, uint8 affinityMask, OSThread_t::THREAD_TYPE threadType)
{
cemu_assert_debug(thread != nullptr); // make thread struct mandatory. Caller can always use SysAllocator
__OSLockScheduler();
bool isThreadStillActive = __OSIsThreadActive(thread);
if (isThreadStillActive)
{
// workaround for games that restart threads to quickly
// seen in Fast Racing Neo at boot (0x020617BC OSCreateThread)
forceLog_printf("Game attempting to re-initialize existing thread");
while ((thread->state == OSThread_t::THREAD_STATE::STATE_READY || thread->state == OSThread_t::THREAD_STATE::STATE_RUNNING) && thread->suspendCounter == 0)
{
// wait for thread to finish
__OSUnlockScheduler();
OSSleepTicks(ESPRESSO_TIMER_CLOCK / 2000); // sleep 0.5ms
__OSLockScheduler();
}
if (__OSIsThreadActive(thread) && thread->state == OSThread_t::THREAD_STATE::STATE_MORIBUND)
{
forceLog_printf("Calling OSCreateThread() on thread which is still active (Thread exited without detached flag). Forcing OSDetachThread()...");
__OSUnlockScheduler();
OSDetachThread(thread);
__OSLockScheduler();
}
}
cemu_assert_debug(__OSIsThreadActive(thread) == false);
__OSUnlockScheduler();
initFunctionPointers();
if (thread == nullptr)
thread = (OSThread_t*)memory_getPointerFromVirtualOffset(coreinit_allocFromSysArea(sizeof(OSThread_t), 32));
memset(thread, 0x00, sizeof(OSThread_t));
// init signatures
thread->context.magic0 = OS_CONTEXT_MAGIC_0;
thread->context.magic1 = OS_CONTEXT_MAGIC_1;
thread->magic = 'tHrD';
thread->type = threadType;
thread->state = (entryPoint != MPTR_NULL) ? OSThread_t::THREAD_STATE::STATE_READY : OSThread_t::THREAD_STATE::STATE_NONE;
thread->entrypoint = _swapEndianU32(entryPoint);
__OSSetThreadBasePriority(thread, 0);
__OSUpdateThreadEffectivePriority(thread);
// untested, but seems to work (Batman Arkham City uses these values to calculate the stack size for duplicated threads)
thread->stackBase = _swapEndianU32(stackLowerBaseAddr + stackSize); // these fields are quite important and lots of games rely on them being accurate (Examples: Darksiders 2, SMW3D, Batman Arkham City)
thread->stackEnd = _swapEndianU32(stackLowerBaseAddr);
// init stackpointer
thread->context.gpr[GPR_SP] = _swapEndianU32(stackLowerBaseAddr + stackSize - 0x20); // how many free bytes should there be at the beginning of the stack?
// init misc stuff
thread->attr = affinityMask;
thread->context.setAffinity(affinityMask);
thread->context.srr0 = funcPtr_threadEntry;
thread->context.lr = _swapEndianU32(exitThreadPtr);
thread->id = 0x8000; // Warriors Orochi 3 softlocks if this is zero due to confusing threads (_OSActivateThread should set this?)
// init ugqr
thread->context.gqr[0] = 0x00000000;
thread->context.gqr[1] = 0x00000000;
thread->context.gqr[2] = 0x00040004;
thread->context.gqr[3] = 0x00050005;
thread->context.gqr[4] = 0x00060006;
thread->context.gqr[5] = 0x00070007;
thread->context.gqr[6] = 0x00000000;
thread->context.gqr[7] = 0x00000000;
// init r2 (SDA2) and r3 (SDA)
thread->context.gpr[2] = _swapEndianU32(RPLLoader_GetSDA2Base());
thread->context.gpr[13] = _swapEndianU32(RPLLoader_GetSDA1Base());
// GHS related thread init?
__OSLockScheduler();
// if entrypoint is non-zero then put the thread on the active list and suspend it
if (entryPoint != MPTR_NULL)
{
thread->suspendCounter = 1;
__OSActivateThread(thread);
thread->state = OSThread_t::THREAD_STATE::STATE_READY;
}
else
thread->suspendCounter = 0;
__OSUnlockScheduler();
}
bool OSCreateThreadType(OSThread_t* thread, MPTR entryPoint, sint32 numParam, void* ptrParam, void* stackTop2, sint32 stackSize, sint32 priority, uint32 attr, OSThread_t::THREAD_TYPE threadType)
{
OSCreateThreadInternal(thread, entryPoint, memory_getVirtualOffsetFromPointer(stackTop2) - stackSize, stackSize, attr, threadType);
thread->context.gpr[3] = _swapEndianU32(numParam); // num arguments
thread->context.gpr[4] = _swapEndianU32(memory_getVirtualOffsetFromPointer(ptrParam)); // arguments pointer
__OSSetThreadBasePriority(thread, priority);
__OSUpdateThreadEffectivePriority(thread);
// set affinity
uint8 affinityMask = 0;
affinityMask = attr & 0x7;
// if no core is selected -> set current one
if (affinityMask == 0)
affinityMask |= (1 << PPCInterpreter_getCoreIndex(ppcInterpreterCurrentInstance));
// set attr
// todo: Support for other attr bits
thread->attr = (affinityMask & 0xFF) | (attr & OSThread_t::ATTR_BIT::ATTR_DETACHED);
thread->context.setAffinity(affinityMask);
// recompile entry point function
if (entryPoint != MPTR_NULL)
PPCRecompiler_recompileIfUnvisited(entryPoint);
return true;
}
bool OSCreateThread(OSThread_t* thread, MPTR entryPoint, sint32 numParam, void* ptrParam, void* stackTop2, sint32 stackSize, sint32 priority, uint32 attr)
{
return OSCreateThreadType(thread, entryPoint, numParam, ptrParam, stackTop2, stackSize, priority, attr, OSThread_t::THREAD_TYPE::TYPE_APP);
}
bool OSRunThread(OSThread_t* thread, MPTR funcAddress, sint32 numParam, void* ptrParam)
{
__OSLockScheduler();
cemu_assert_debug(ppcInterpreterCurrentInstance == nullptr || OSGetCurrentThread() != thread); // called on self, what should this function do?
if (thread->state != OSThread_t::THREAD_STATE::STATE_NONE && thread->state != OSThread_t::THREAD_STATE::STATE_MORIBUND)
{
// unsure about this case
forceLogDebug_printf("OSRunThread called on thread which cannot be ran");
__OSUnlockScheduler();
return false;
}
if (thread->state == OSThread_t::THREAD_STATE::STATE_MORIBUND)
{
thread->state = OSThread_t::THREAD_STATE::STATE_NONE;
coreinit::__OSDeactivateThread(thread);
coreinit::__OSRemoveThreadFromRunQueues(thread);
}
// set thread state
// todo - this should fully reinitialize the thread?
thread->entrypoint = _swapEndianU32(funcAddress);
thread->context.srr0 = coreinit::funcPtr_threadEntry;
thread->context.lr = _swapEndianU32(exitThreadPtr);
thread->context.gpr[3] = _swapEndianU32(numParam);
thread->context.gpr[4] = _swapEndianU32(memory_getVirtualOffsetFromPointer(ptrParam));
thread->suspendCounter = 0; // verify
MPTR threadMPTR = memory_getVirtualOffsetFromPointer(thread);
coreinit::__OSActivateThread(thread);
thread->state = OSThread_t::THREAD_STATE::STATE_READY;
__OSAddReadyThreadToRunQueue(thread);
__OSUnlockScheduler();
return true;
}
void OSExitThread(sint32 exitValue)
{
PPCInterpreter_t* hCPU = PPCInterpreter_getCurrentInstance();
hCPU->gpr[3] = exitValue;
OSThread_t* threadBE = coreinitThread_getCurrentThreadDepr(hCPU);
MPTR t = memory_getVirtualOffsetFromPointer(threadBE);
// thread cleanup callback
if (!threadBE->cleanupCallback2.IsNull())
{
threadBE->stateFlags = _swapEndianU32(_swapEndianU32(threadBE->stateFlags) | 0x00000001);
PPCCoreCallback(threadBE->cleanupCallback2.GetMPTR(), threadBE, _swapEndianU32(threadBE->stackEnd));
}
// cpp exception cleanup
if (gCoreinitData->__cpp_exception_cleanup_ptr != 0 && threadBE->crt.eh_globals != nullptr)
{
PPCCoreCallback(_swapEndianU32(gCoreinitData->__cpp_exception_cleanup_ptr), &threadBE->crt.eh_globals);
threadBE->crt.eh_globals = nullptr;
}
// set exit code
threadBE->exitValue = exitValue;
__OSLockScheduler();
// release held synchronization primitives
if (!threadBE->mutexQueue.isEmpty())
{
cemuLog_force("OSExitThread: Thread is holding mutexes");
while (true)
{
OSMutex* mutex = threadBE->mutexQueue.getFirst();
if (!mutex)
break;
if (mutex->owner != threadBE)
{
cemuLog_force("OSExitThread: Thread is holding mutex which it doesn't own");
threadBE->mutexQueue.removeMutex(mutex);
continue;
}
coreinit::OSUnlockMutexInternal(mutex);
}
}
// todo - release all fast mutexes
// handle join queue
if (!threadBE->joinQueue.isEmpty())
threadBE->joinQueue.wakeupEntireWaitQueue(false);
if ((threadBE->attr & 8) != 0)
{
// deactivate thread since it is detached
threadBE->state = OSThread_t::THREAD_STATE::STATE_NONE;
coreinit::__OSDeactivateThread(threadBE);
// queue call to thread deallocator if set
if (!threadBE->deallocatorFunc.IsNull())
__OSQueueThreadDeallocation(threadBE);
}
else
{
// non-detached threads remain active
threadBE->state = OSThread_t::THREAD_STATE::STATE_MORIBUND;
}
PPCCore_switchToSchedulerWithLock();
}
void OSSetThreadSpecific(uint32 index, void* value)
{
OSThread_t* currentThread = OSGetCurrentThread();
if (index >= (uint32)currentThread->specificArray.size())
return;
currentThread->specificArray[index] = value;
}
void* OSGetThreadSpecific(uint32 index)
{
OSThread_t* currentThread = OSGetCurrentThread();
if (index >= (uint32)currentThread->specificArray.size())
return nullptr;
return currentThread->specificArray[index].GetPtr();
}
void OSSetThreadName(OSThread_t* thread, char* name)
{
thread->threadName = name;
}
char* OSGetThreadName(OSThread_t* thread)
{
return thread->threadName.GetPtr();
}
void coreinitExport_OSExitThreadDepr(PPCInterpreter_t* hCPU)
{
ppcDefineParamU32(exitCode, 0);
OSExitThread(exitCode);
}
void OSYieldThread()
{
PPCCore_switchToScheduler();
}
void _OSSleepTicks_alarmHandler(uint64 currentTick, void* context)
{
cemu_assert_debug(__OSHasSchedulerLock());
OSThreadQueue* threadQueue = (OSThreadQueue*)context;
threadQueue->wakeupEntireWaitQueue(false);
}
void OSSleepTicks(uint64 ticks)
{
cemu_assert_debug(__OSHasSchedulerLock() == false);
StackAllocator<OSThreadQueue> _threadQueue;
OSInitThreadQueue(_threadQueue.GetPointer());
__OSLockScheduler();
OSHostAlarm* hostAlarm = OSHostAlarmCreate(coreinit_getOSTime() + ticks, 0, _OSSleepTicks_alarmHandler, _threadQueue.GetPointer());
_threadQueue.GetPointer()->queueAndWait(OSGetCurrentThread());
OSHostAlarmDestroy(hostAlarm);
__OSUnlockScheduler();
}
void OSDetachThread(OSThread_t* thread)
{
__OSLockScheduler();
thread->attr |= OSThread_t::ATTR_BIT::ATTR_DETACHED;
if (thread->state == OSThread_t::THREAD_STATE::STATE_MORIBUND)
{
// exit thread
// ?
// todo -> call deallocator
thread->state = OSThread_t::THREAD_STATE::STATE_NONE;
thread->id = 0x8000;
coreinit::__OSDeactivateThread(thread);
if (!thread->joinQueue.isEmpty())
{
// handle join queue
thread->joinQueue.wakeupEntireWaitQueue(true);
}
}
__OSUnlockScheduler();
}
bool OSJoinThread(OSThread_t* thread, uint32be* exitValue)
{
__OSLockScheduler();
if ((thread->attr & OSThread_t::ATTR_DETACHED) == 0 && thread->state != OSThread_t::THREAD_STATE::STATE_MORIBUND)
{
cemu_assert_debug(thread->joinQueue.isEmpty());
// thread still running, wait in join queue
thread->joinQueue.queueAndWait(OSGetCurrentThread());
}
else if (thread->state != OSThread_t::THREAD_STATE::STATE_MORIBUND)
{
// cannot join detached and active threads
forceLogDebug_printf("Cannot join detached active thread");
__OSUnlockScheduler();
return false;
}
// thread already ended and is still attached, get return value
cemu_assert_debug(thread->state == OSThread_t::THREAD_STATE::STATE_MORIBUND);
cemu_assert_debug((thread->attr & OSThread_t::ATTR_DETACHED) == 0);
if (exitValue)
*exitValue = thread->exitValue;
// end thread
thread->state = OSThread_t::THREAD_STATE::STATE_NONE;
__OSDeactivateThread(thread);
coreinit::__OSRemoveThreadFromRunQueues(thread);
thread->id = 0x8000;
if (!thread->deallocatorFunc.IsNull())
__OSQueueThreadDeallocation(thread);
__OSUnlockScheduler();
return true;
}
// adds the thread to each core's run queue if in runable state
void __OSAddReadyThreadToRunQueue(OSThread_t* thread)
{
cemu_assert_debug(__OSHasSchedulerLock());
if (thread->state != OSThread_t::THREAD_STATE::STATE_READY)
return;
if (thread->suspendCounter != 0)
return;
for (sint32 i = 0; i < PPC_CORE_COUNT; i++)
{
if (thread->currentRunQueue[i] != nullptr)
continue; // already on the queue
// check affinity
if(!thread->context.hasCoreAffinitySet(i))
continue;
g_coreRunQueue.GetPtr()[i].addThread(thread, thread->linkRun + i);
thread->currentRunQueue[i] = (g_coreRunQueue.GetPtr() + i);
g_coreRunQueueThreadCount[i].increment();
}
}
void __OSRemoveThreadFromRunQueues(OSThread_t* thread)
{
cemu_assert_debug(__OSHasSchedulerLock());
for (sint32 i = 0; i < PPC_CORE_COUNT; i++)
{
if(thread->currentRunQueue[i] == nullptr)
continue;
g_coreRunQueue.GetPtr()[i].removeThread(thread, thread->linkRun + i);
thread->currentRunQueue[i] = nullptr;
g_coreRunQueueThreadCount[i].decrement();
}
}
// returns true if thread runs on same core and has higher priority
bool __OSCoreShouldSwitchToThread(OSThread_t* currentThread, OSThread_t* newThread)
{
uint32 coreIndex = OSGetCoreId();
if (!newThread->context.hasCoreAffinitySet(coreIndex))
return false;
return newThread->effectivePriority < currentThread->effectivePriority;
}
sint32 __OSResumeThreadInternal(OSThread_t* thread, sint32 resumeCount)
{
cemu_assert_debug(__OSHasSchedulerLock());
sint32 previousSuspendCount = thread->suspendCounter;
cemu_assert_debug(previousSuspendCount >= 0);
if (previousSuspendCount == 0)
return 0;
thread->suspendCounter = previousSuspendCount - resumeCount;
if (thread->suspendCounter < 0)
thread->suspendCounter = 0;
if (thread->suspendCounter == 0)
{
__OSAddReadyThreadToRunQueue(thread);
// set awakened time if thread is ready
// todo - only set this once?
thread->wakeUpTime = PPCInterpreter_getMainCoreCycleCounter();
// reschedule if thread has higher priority
if (ppcInterpreterCurrentInstance && __OSCoreShouldSwitchToThread(coreinit::OSGetCurrentThread(), thread))
PPCCore_switchToSchedulerWithLock();
}
return previousSuspendCount;
}
sint32 OSResumeThread(OSThread_t* thread)
{
__OSLockScheduler();
sint32 previousSuspendCount = __OSResumeThreadInternal(thread, 1);
__OSUnlockScheduler();
return previousSuspendCount;
}
void OSContinueThread(OSThread_t* thread)
{
__OSLockScheduler();
__OSResumeThreadInternal(thread, thread->suspendCounter);
__OSUnlockScheduler();
}
void __OSSuspendThreadInternal(OSThread_t* thread)
{
cemu_assert_debug(__OSHasSchedulerLock());
cemu_assert_debug(thread->state != OSThread_t::THREAD_STATE::STATE_NONE && thread->state != OSThread_t::THREAD_STATE::STATE_MORIBUND); // how to handle these?
sint32 previousSuspendCount = thread->suspendCounter;
if (OSGetCurrentThread() == thread)
{
thread->suspendCounter = thread->suspendCounter + 1;
PPCCore_switchToSchedulerWithLock();
}
else
{
thread->suspendCounter = thread->suspendCounter + 1;
if (previousSuspendCount == 0)
{
__OSRemoveThreadFromRunQueues(thread);
// todo - if thread is still running find a way to cancel it's timeslice immediately
}
}
}
void OSSuspendThread(OSThread_t* thread)
{
__OSLockScheduler();
__OSSuspendThreadInternal(thread);
__OSUnlockScheduler();
}
void OSSleepThread(OSThreadQueue* threadQueue)
{
__OSLockScheduler();
threadQueue->queueAndWait(OSGetCurrentThread());
__OSUnlockScheduler();
}
void OSWakeupThread(OSThreadQueue* threadQueue)
{
__OSLockScheduler();
threadQueue->wakeupEntireWaitQueue(true);
__OSUnlockScheduler();
}
bool OSSetThreadAffinity(OSThread_t* thread, uint32 affinityMask)
{
cemu_assert_debug((affinityMask & ~7) == 0);
__OSLockScheduler();
uint32 prevAffinityMask = thread->context.getAffinity();
if (thread->state == OSThread_t::THREAD_STATE::STATE_RUNNING)
{
thread->attr = (thread->attr & ~7) | (affinityMask & 7);
thread->context.setAffinity(affinityMask);
// should this reschedule the thread?
}
else if (prevAffinityMask != affinityMask)
{
__OSRemoveThreadFromRunQueues(thread);
thread->attr = (thread->attr & ~7) | (affinityMask & 7);
thread->context.setAffinity(affinityMask);
__OSAddReadyThreadToRunQueue(thread);
}
__OSUnlockScheduler();
return true;
}
uint32 OSGetThreadAffinity(OSThread_t* thread)
{
auto affinityMask = thread->context.getAffinity();
cemu_assert_debug((affinityMask & ~7) == 0);
return affinityMask;
}
void* OSSetThreadDeallocator(OSThread_t* thread, void* newDeallocatorFunc)
{
__OSLockScheduler();
void* previousFunc = thread->deallocatorFunc.GetPtr();
thread->deallocatorFunc = newDeallocatorFunc;
__OSUnlockScheduler();
return previousFunc;
}
void* OSSetThreadCleanupCallback(OSThread_t* thread, void* cleanupCallback)
{
__OSLockScheduler();
void* previousFunc = thread->cleanupCallback2.GetPtr();
thread->cleanupCallback2 = cleanupCallback;
__OSUnlockScheduler();
return previousFunc;
}
void __OSSetThreadBasePriority(OSThread_t* thread, sint32 newPriority)
{
cemu_assert_debug(newPriority >= 0 && newPriority < 32);
newPriority += OSThread_t::GetTypePriorityBase(thread->type);
thread->basePriority = newPriority;
}
void __OSUpdateThreadEffectivePriority(OSThread_t* thread)
{
if (thread->context.boostCount != 0)
{
// temporarily boosted threads have their priority set to 0 (maximum)
thread->effectivePriority = 0;
return;
}
thread->effectivePriority = thread->basePriority;
}
bool OSSetThreadPriority(OSThread_t* thread, sint32 newPriority)
{
if (newPriority < 0 || newPriority >= 0x20)
{
cemu_assert_debug(false);
return false; // invalid priority value
}
__OSLockScheduler();
__OSSetThreadBasePriority(thread, newPriority);
__OSUpdateThreadEffectivePriority(thread);
// reschedule if needed
OSThread_t* currentThread = OSGetCurrentThread();
if (currentThread && currentThread != thread)
{
if (__OSCoreShouldSwitchToThread(currentThread, thread))
PPCCore_switchToSchedulerWithLock();
}
__OSUnlockScheduler();
return true;
}
sint32 OSGetThreadPriority(OSThread_t* thread)
{
sint32 threadPriority = thread->basePriority;
OSThread_t::THREAD_TYPE threadType = thread->type;
threadPriority -= OSThread_t::GetTypePriorityBase(threadType);
cemu_assert_debug(threadPriority >= 0 && threadPriority < 32);
return threadPriority;
}
bool OSIsThreadTerminated(OSThread_t* thread)
{
__OSLockScheduler();
bool isTerminated = false;
if (thread->state == OSThread_t::THREAD_STATE::STATE_MORIBUND || thread->state == OSThread_t::THREAD_STATE::STATE_NONE)
isTerminated = true;
__OSUnlockScheduler();
return isTerminated;
}
bool OSIsThreadSuspended(OSThread_t* thread)
{
__OSLockScheduler();
sint32 suspendCounter = thread->suspendCounter;
__OSUnlockScheduler();
return suspendCounter > 0;
}
void OSCancelThread(OSThread_t* thread)
{
__OSLockScheduler();
cemu_assert_debug(thread->requestFlags == 0 || thread->requestFlags == OSThread_t::REQUEST_FLAG_CANCEL); // todo - how to handle cases where other flags are already set?
thread->requestFlags = OSThread_t::REQUEST_FLAG_CANCEL;
__OSUnlockScheduler();
// if the canceled thread is self, then exit immediately
if (thread == OSGetCurrentThread())
OSExitThread(-1);
}
void OSTestThreadCancelInternal()
{
// also handles suspend request ?
cemu_assert_debug(__OSHasSchedulerLock());
OSThread_t* thread = OSGetCurrentThread();
if (thread->requestFlags == OSThread_t::REQUEST_FLAG_CANCEL)
{
__OSUnlockScheduler();
OSExitThread(-1);
}
}
void OSTestThreadCancel()
{
__OSLockScheduler();
OSTestThreadCancelInternal();
__OSUnlockScheduler();
}
void __OSSwitchToThreadFiber(OSThread_t* thread, uint32 coreIndex)
{
cemu_assert_debug(__OSHasSchedulerLock());
cemu_assert_debug(s_threadToFiber.find(thread) != s_threadToFiber.end());
OSHostThread* hostThread = s_threadToFiber.find(thread)->second;
hostThread->selectedCore = coreIndex;
Fiber::Switch(hostThread->m_fiber);
}
void __OSThreadLoadContext(PPCInterpreter_t* hCPU, OSThread_t* thread)
{
// load GPR
for (uint32 i = 0; i < 32; i++)
hCPU->gpr[i] = _swapEndianU32(thread->context.gpr[i]);
// load SPR cr, lr, ctr, xer
ppc_setCR(hCPU, thread->context.cr);
hCPU->spr.LR = _swapEndianU32(thread->context.lr);
hCPU->spr.CTR = thread->context.ctr;
PPCInterpreter_setXER(hCPU, thread->context.xer);
hCPU->fpscr = thread->context.fpscr.fpscr;
// store floating point and Gekko registers
for (uint32 i = 0; i < 32; i++)
{
hCPU->fpr[i].fp0int = thread->context.fp_ps0[i];
hCPU->fpr[i].fp1int = thread->context.fp_ps1[i];
}
for (uint32 i = 0; i < 8; i++)
{
hCPU->spr.UGQR[0 + i] = thread->context.gqr[i];
}
hCPU->instructionPointer = thread->context.srr0;
}
void __OSThreadStoreContext(PPCInterpreter_t* hCPU, OSThread_t* thread)
{
// store GPR
for (uint32 i = 0; i < 32; i++)
thread->context.gpr[i] = _swapEndianU32(hCPU->gpr[i]);
// store SPRs
thread->context.cr = ppc_getCR(hCPU);
thread->context.lr = _swapEndianU32(hCPU->spr.LR);
thread->context.ctr = hCPU->spr.CTR;
thread->context.xer = PPCInterpreter_getXER(hCPU);
thread->context.fpscr.fpscr = hCPU->fpscr;
thread->context.fpscr.padding = 0;
// store floating point and Gekko registers
for (uint32 i = 0; i < 32; i++)
{
thread->context.fp_ps0[i] = hCPU->fpr[i].fp0int;
thread->context.fp_ps1[i] = hCPU->fpr[i].fp1int;
}
for (uint32 i = 0; i < 8; i++)
{
thread->context.gqr[i] = hCPU->spr.UGQR[0 + i];
}
thread->context.srr0 = hCPU->instructionPointer;
}
void __OSStoreThread(OSThread_t* thread, PPCInterpreter_t* hCPU)
{
if (thread->state == OSThread_t::THREAD_STATE::STATE_RUNNING)
{
thread->state = OSThread_t::THREAD_STATE::STATE_READY;
__OSAddReadyThreadToRunQueue(thread);
}
else if (thread->state == OSThread_t::THREAD_STATE::STATE_MORIBUND || thread->state == OSThread_t::THREAD_STATE::STATE_NONE || thread->state == OSThread_t::THREAD_STATE::STATE_WAITING)
{
// thread exited or suspending/waiting
}
else
{
cemu_assert_debug(false);
}
thread->requestFlags = (OSThread_t::REQUEST_FLAG_BIT)(thread->requestFlags & OSThread_t::REQUEST_FLAG_CANCEL); // remove all flags except cancel flag
// update total cycles
uint64 remainingCycles = std::min((uint64)ppcInterpreterCurrentInstance->remainingCycles, (uint64)thread->quantumTicks);
uint64 executedCycles = thread->quantumTicks - remainingCycles;
if (executedCycles < ppcInterpreterCurrentInstance->skippedCycles)
executedCycles = 0;
else
executedCycles -= ppcInterpreterCurrentInstance->skippedCycles;
thread->totalCycles += executedCycles;
// store context and set current thread to null
__OSThreadStoreContext(hCPU, thread);
OSSetCurrentThread(OSGetCoreId(), nullptr);
ppcInterpreterCurrentInstance = nullptr;
}
void __OSLoadThread(OSThread_t* thread, PPCInterpreter_t* hCPU, uint32 coreIndex)
{
hCPU->LSQE = 1;
hCPU->PSE = 1;
hCPU->reservedMemAddr = MPTR_NULL;
hCPU->reservedMemValue = 0;
hCPU->spr.UPIR = coreIndex;
hCPU->coreInterruptMask = 1;
ppcInterpreterCurrentInstance = hCPU;
OSSetCurrentThread(OSGetCoreId(), thread);
__OSThreadLoadContext(hCPU, thread);
thread->context.upir = coreIndex;
thread->quantumTicks = ppcThreadQuantum;
// statistics
thread->wakeUpTime = PPCInterpreter_getMainCoreCycleCounter();
thread->wakeUpCount = thread->wakeUpCount + 1;
}
OSThread_t* __OSGetNextRunableThread(uint32 coreIndex)
{
cemu_assert_debug(__OSHasSchedulerLock());
// pick thread, then remove from run queue
OSThreadQueue* runQueue = g_coreRunQueue.GetPtr() + coreIndex;
OSThread_t* threadItr = runQueue->head.GetPtr();
OSThread_t* selectedThread = nullptr;
if (!threadItr)
return nullptr;
selectedThread = threadItr;
while (threadItr)
{
if (threadItr->effectivePriority < selectedThread->effectivePriority)
selectedThread = threadItr;
threadItr = threadItr->linkRun[coreIndex].next.GetPtr();
}
cemu_assert_debug(selectedThread->state == OSThread_t::THREAD_STATE::STATE_READY);
__OSRemoveThreadFromRunQueues(selectedThread);
selectedThread->state = OSThread_t::THREAD_STATE::STATE_RUNNING;
return selectedThread;
}
void __OSCheckSystemEvents()
{
// AX update
snd_core::AXOut_update();
// alarm update
coreinit::alarm_update();
// nfp update
nnNfp_update();
}
Fiber* g_idleLoopFiber[3]{};
// idle fiber per core if no thread is runnable
// this is necessary since we can't block in __OSThreadSwitchToNext() (__OSStoreThread + thread switch must happen inside same scheduler lock)
void __OSThreadCoreIdle(void* unusedParam)
{
bool isMainCore = g_isMulticoreMode == false || t_assignedCoreIndex == 1;
sint32 coreIndex = t_assignedCoreIndex;
__OSUnlockScheduler();
while (true)
{
if (!g_coreRunQueueThreadCount[coreIndex].isZero()) // avoid hammering the lock on the main core if there is no runable thread
{
__OSLockScheduler();
OSThread_t* nextThread = __OSGetNextRunableThread(coreIndex);
if (nextThread)
{
cemu_assert_debug(nextThread->state == OSThread_t::THREAD_STATE::STATE_RUNNING);