summaryrefslogtreecommitdiff
path: root/src/core/hle/kernel/thread.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/hle/kernel/thread.cpp')
-rw-r--r--src/core/hle/kernel/thread.cpp39
1 files changed, 20 insertions, 19 deletions
diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp
index 7166e9b07..735019d96 100644
--- a/src/core/hle/kernel/thread.cpp
+++ b/src/core/hle/kernel/thread.cpp
@@ -50,7 +50,7 @@ void Thread::Stop() {
50 50
51 // Clean up any dangling references in objects that this thread was waiting for 51 // Clean up any dangling references in objects that this thread was waiting for
52 for (auto& wait_object : wait_objects) { 52 for (auto& wait_object : wait_objects) {
53 wait_object->RemoveWaitingThread(this); 53 wait_object->RemoveWaitingThread(SharedFrom(this));
54 } 54 }
55 wait_objects.clear(); 55 wait_objects.clear();
56 56
@@ -147,9 +147,10 @@ static void ResetThreadContext(Core::ARM_Interface::ThreadContext& context, VAdd
147 context.fpcr = 0x03C00000; 147 context.fpcr = 0x03C00000;
148} 148}
149 149
150ResultVal<SharedPtr<Thread>> Thread::Create(KernelCore& kernel, std::string name, VAddr entry_point, 150ResultVal<std::shared_ptr<Thread>> Thread::Create(KernelCore& kernel, std::string name,
151 u32 priority, u64 arg, s32 processor_id, 151 VAddr entry_point, u32 priority, u64 arg,
152 VAddr stack_top, Process& owner_process) { 152 s32 processor_id, VAddr stack_top,
153 Process& owner_process) {
153 // Check if priority is in ranged. Lowest priority -> highest priority id. 154 // Check if priority is in ranged. Lowest priority -> highest priority id.
154 if (priority > THREADPRIO_LOWEST) { 155 if (priority > THREADPRIO_LOWEST) {
155 LOG_ERROR(Kernel_SVC, "Invalid thread priority: {}", priority); 156 LOG_ERROR(Kernel_SVC, "Invalid thread priority: {}", priority);
@@ -168,7 +169,7 @@ ResultVal<SharedPtr<Thread>> Thread::Create(KernelCore& kernel, std::string name
168 } 169 }
169 170
170 auto& system = Core::System::GetInstance(); 171 auto& system = Core::System::GetInstance();
171 SharedPtr<Thread> thread(new Thread(kernel)); 172 std::shared_ptr<Thread> thread = std::make_shared<Thread>(kernel);
172 173
173 thread->thread_id = kernel.CreateNewThreadID(); 174 thread->thread_id = kernel.CreateNewThreadID();
174 thread->status = ThreadStatus::Dormant; 175 thread->status = ThreadStatus::Dormant;
@@ -197,7 +198,7 @@ ResultVal<SharedPtr<Thread>> Thread::Create(KernelCore& kernel, std::string name
197 // to initialize the context 198 // to initialize the context
198 ResetThreadContext(thread->context, stack_top, entry_point, arg); 199 ResetThreadContext(thread->context, stack_top, entry_point, arg);
199 200
200 return MakeResult<SharedPtr<Thread>>(std::move(thread)); 201 return MakeResult<std::shared_ptr<Thread>>(std::move(thread));
201} 202}
202 203
203void Thread::SetPriority(u32 priority) { 204void Thread::SetPriority(u32 priority) {
@@ -215,7 +216,7 @@ void Thread::SetWaitSynchronizationOutput(s32 output) {
215 context.cpu_registers[1] = output; 216 context.cpu_registers[1] = output;
216} 217}
217 218
218s32 Thread::GetWaitObjectIndex(const WaitObject* object) const { 219s32 Thread::GetWaitObjectIndex(std::shared_ptr<WaitObject> object) const {
219 ASSERT_MSG(!wait_objects.empty(), "Thread is not waiting for anything"); 220 ASSERT_MSG(!wait_objects.empty(), "Thread is not waiting for anything");
220 const auto match = std::find(wait_objects.rbegin(), wait_objects.rend(), object); 221 const auto match = std::find(wait_objects.rbegin(), wait_objects.rend(), object);
221 return static_cast<s32>(std::distance(match, wait_objects.rend()) - 1); 222 return static_cast<s32>(std::distance(match, wait_objects.rend()) - 1);
@@ -255,8 +256,8 @@ void Thread::SetStatus(ThreadStatus new_status) {
255 status = new_status; 256 status = new_status;
256} 257}
257 258
258void Thread::AddMutexWaiter(SharedPtr<Thread> thread) { 259void Thread::AddMutexWaiter(std::shared_ptr<Thread> thread) {
259 if (thread->lock_owner == this) { 260 if (thread->lock_owner.get() == this) {
260 // If the thread is already waiting for this thread to release the mutex, ensure that the 261 // If the thread is already waiting for this thread to release the mutex, ensure that the
261 // waiters list is consistent and return without doing anything. 262 // waiters list is consistent and return without doing anything.
262 const auto iter = std::find(wait_mutex_threads.begin(), wait_mutex_threads.end(), thread); 263 const auto iter = std::find(wait_mutex_threads.begin(), wait_mutex_threads.end(), thread);
@@ -276,13 +277,13 @@ void Thread::AddMutexWaiter(SharedPtr<Thread> thread) {
276 wait_mutex_threads.begin(), wait_mutex_threads.end(), 277 wait_mutex_threads.begin(), wait_mutex_threads.end(),
277 [&thread](const auto& entry) { return entry->GetPriority() > thread->GetPriority(); }); 278 [&thread](const auto& entry) { return entry->GetPriority() > thread->GetPriority(); });
278 wait_mutex_threads.insert(insertion_point, thread); 279 wait_mutex_threads.insert(insertion_point, thread);
279 thread->lock_owner = this; 280 thread->lock_owner = SharedFrom(this);
280 281
281 UpdatePriority(); 282 UpdatePriority();
282} 283}
283 284
284void Thread::RemoveMutexWaiter(SharedPtr<Thread> thread) { 285void Thread::RemoveMutexWaiter(std::shared_ptr<Thread> thread) {
285 ASSERT(thread->lock_owner == this); 286 ASSERT(thread->lock_owner.get() == this);
286 287
287 // Ensure that the thread is in the list of mutex waiters 288 // Ensure that the thread is in the list of mutex waiters
288 const auto iter = std::find(wait_mutex_threads.begin(), wait_mutex_threads.end(), thread); 289 const auto iter = std::find(wait_mutex_threads.begin(), wait_mutex_threads.end(), thread);
@@ -310,13 +311,13 @@ void Thread::UpdatePriority() {
310 } 311 }
311 312
312 if (GetStatus() == ThreadStatus::WaitCondVar) { 313 if (GetStatus() == ThreadStatus::WaitCondVar) {
313 owner_process->RemoveConditionVariableThread(this); 314 owner_process->RemoveConditionVariableThread(SharedFrom(this));
314 } 315 }
315 316
316 SetCurrentPriority(new_priority); 317 SetCurrentPriority(new_priority);
317 318
318 if (GetStatus() == ThreadStatus::WaitCondVar) { 319 if (GetStatus() == ThreadStatus::WaitCondVar) {
319 owner_process->InsertConditionVariableThread(this); 320 owner_process->InsertConditionVariableThread(SharedFrom(this));
320 } 321 }
321 322
322 if (!lock_owner) { 323 if (!lock_owner) {
@@ -325,8 +326,8 @@ void Thread::UpdatePriority() {
325 326
326 // Ensure that the thread is within the correct location in the waiting list. 327 // Ensure that the thread is within the correct location in the waiting list.
327 auto old_owner = lock_owner; 328 auto old_owner = lock_owner;
328 lock_owner->RemoveMutexWaiter(this); 329 lock_owner->RemoveMutexWaiter(SharedFrom(this));
329 old_owner->AddMutexWaiter(this); 330 old_owner->AddMutexWaiter(SharedFrom(this));
330 331
331 // Recursively update the priority of the thread that depends on the priority of this one. 332 // Recursively update the priority of the thread that depends on the priority of this one.
332 lock_owner->UpdatePriority(); 333 lock_owner->UpdatePriority();
@@ -339,11 +340,11 @@ void Thread::ChangeCore(u32 core, u64 mask) {
339bool Thread::AllWaitObjectsReady() const { 340bool Thread::AllWaitObjectsReady() const {
340 return std::none_of( 341 return std::none_of(
341 wait_objects.begin(), wait_objects.end(), 342 wait_objects.begin(), wait_objects.end(),
342 [this](const SharedPtr<WaitObject>& object) { return object->ShouldWait(this); }); 343 [this](const std::shared_ptr<WaitObject>& object) { return object->ShouldWait(this); });
343} 344}
344 345
345bool Thread::InvokeWakeupCallback(ThreadWakeupReason reason, SharedPtr<Thread> thread, 346bool Thread::InvokeWakeupCallback(ThreadWakeupReason reason, std::shared_ptr<Thread> thread,
346 SharedPtr<WaitObject> object, std::size_t index) { 347 std::shared_ptr<WaitObject> object, std::size_t index) {
347 ASSERT(wakeup_callback); 348 ASSERT(wakeup_callback);
348 return wakeup_callback(reason, std::move(thread), std::move(object), index); 349 return wakeup_callback(reason, std::move(thread), std::move(object), index);
349} 350}