blob: 35f69e485fb0fc2a14d515e3c59cc97571929efc (
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
|
#include "KingSystem/Utils/Thread/LowPrioThreadMgr.h"
#include <limits>
#include <mc/seadCoreInfo.h>
#include <thread/seadThreadUtil.h>
#include "KingSystem/Utils/SafeDelete.h"
#include "KingSystem/Utils/Thread/TaskQueue.h"
namespace ksys::util {
SEAD_SINGLETON_DISPOSER_IMPL(LowPrioThreadMgr)
LowPrioThreadMgr::~LowPrioThreadMgr() {
const s32 num_tasks = mCoreThreadTasks.size();
for (s32 i = 0; i < num_tasks; i++) {
mCoreThreadTasks[i].task_thread->quitAndWaitDoneSingleThread(false);
util::safeDelete(mCoreThreadTasks[i].task_thread);
util::safeDelete(mCoreThreadTasks[i].task_mgr);
}
mCoreThreadTasks.freeBuffer();
}
bool LowPrioThreadMgr::startThread(const LowPrioThreadMgr::InitArg& initArg) {
const u32 num_cores = sead::CoreInfo::getNumCores();
mCoreThreadTasks.tryAllocBuffer(num_cores, initArg.heap);
for (u32 i = 0; i < num_cores; i++) {
auto* task_mgr = new (initArg.heap) TaskMgr(initArg.heap);
task_mgr->init<ManagedTask>(initArg.queue_size, initArg.heap);
mCoreThreadTasks[i].task_mgr = task_mgr;
sead::SafeString name;
auto* new_thread = new (initArg.heap) TaskThread(
name, initArg.heap, sead::ThreadUtil::ConvertPrioritySeadToPlatform(initArg.priority),
sead::MessageQueue::BlockType::Blocking, std::numeric_limits<s32>::max(), 0x100000, 32);
{
TaskThread::InitArg arg;
arg.queue = nullptr;
arg.heap = initArg.heap;
arg.num_lanes = 3;
arg.batch_size = 1;
new_thread->init(arg);
}
mCoreThreadTasks[i].task_thread = new_thread;
new_thread->setAffinity(sead::CoreIdMask(i));
new_thread->start();
}
return true;
}
bool LowPrioThreadMgr::submitRequest(const LowPrioThreadMgr::Request& request) {
const s32 num_tasks = mCoreThreadTasks.size();
const sead::BitFlag32 mask = request.flags;
s32 thread_idx = -1;
s32 min = std::numeric_limits<s32>::max();
for (s32 i = 0; i < num_tasks; i++) {
if (mask.isOnBit(i) && mCoreThreadTasks[i].task_thread) {
s32 active_tasks = mCoreThreadTasks[i].task_thread->getNumActiveTasks();
if (active_tasks < min) {
thread_idx = i;
min = active_tasks;
}
}
}
TaskRequest taskRequest(false);
taskRequest.mPostRunCallback = request.post_callback;
taskRequest.mRemoveCallback = request.remove_callback;
taskRequest.mName = request.name;
taskRequest.mUserData = request.user_data;
taskRequest.mDelegate = request.delegate;
taskRequest.mHasHandle = true;
taskRequest.mSynchronous = false;
taskRequest.mLaneId = request.lane_id;
taskRequest.mThread = mCoreThreadTasks[thread_idx].task_thread;
TaskMgrRequest mgrRequest;
mgrRequest.handle = request.handle;
mgrRequest.request = &taskRequest;
mgrRequest.task = request.task;
TaskMgr* task_mgr = request.mgr ? request.mgr : mCoreThreadTasks[thread_idx].task_mgr;
if (request.try_submit) {
return task_mgr->trySubmitRequest(mgrRequest);
}
task_mgr->submitRequest(mgrRequest);
return true;
}
void LowPrioThreadMgr::pauseAllTasks() {
const s32 num_tasks = mCoreThreadTasks.size();
for (s32 i = 0; i < num_tasks; i++) {
mCoreThreadTasks[i].task_thread->pauseAndWaitForAck();
}
}
void LowPrioThreadMgr::resumeAllTasks() {
const s32 num_tasks = mCoreThreadTasks.size();
for (s32 i = 0; i < num_tasks; i++) {
mCoreThreadTasks[i].task_thread->resume();
}
}
void LowPrioThreadMgr::sub_710127AC40() {}
} // namespace ksys::util
|