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.cpp53
1 files changed, 25 insertions, 28 deletions
diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp
index 56950ebd4..3987f9608 100644
--- a/src/core/hle/kernel/thread.cpp
+++ b/src/core/hle/kernel/thread.cpp
@@ -4,7 +4,6 @@
4 4
5#include <algorithm> 5#include <algorithm>
6#include <list> 6#include <list>
7#include <map>
8#include <vector> 7#include <vector>
9 8
10#include "common/common.h" 9#include "common/common.h"
@@ -41,6 +40,9 @@ static Thread* current_thread;
41static const u32 INITIAL_THREAD_ID = 1; ///< The first available thread id at startup 40static const u32 INITIAL_THREAD_ID = 1; ///< The first available thread id at startup
42static u32 next_thread_id; ///< The next available thread id 41static u32 next_thread_id; ///< The next available thread id
43 42
43Thread::Thread() {}
44Thread::~Thread() {}
45
44Thread* GetCurrentThread() { 46Thread* GetCurrentThread() {
45 return current_thread; 47 return current_thread;
46} 48}
@@ -108,6 +110,9 @@ void Thread::Stop(const char* reason) {
108 WakeupAllWaitingThreads(); 110 WakeupAllWaitingThreads();
109 111
110 // Stopped threads are never waiting. 112 // Stopped threads are never waiting.
113 for (auto& wait_object : wait_objects) {
114 wait_object->RemoveWaitingThread(this);
115 }
111 wait_objects.clear(); 116 wait_objects.clear();
112 wait_address = 0; 117 wait_address = 0;
113} 118}
@@ -228,13 +233,15 @@ void WaitCurrentThread_ArbitrateAddress(VAddr wait_address) {
228 233
229/// Event type for the thread wake up event 234/// Event type for the thread wake up event
230static int ThreadWakeupEventType = -1; 235static int ThreadWakeupEventType = -1;
236// TODO(yuriks): This can be removed if Thread objects are explicitly pooled in the future, allowing
237// us to simply use a pool index or similar.
238static Kernel::HandleTable wakeup_callback_handle_table;
231 239
232/// Callback that will wake up the thread it was scheduled for 240/// Callback that will wake up the thread it was scheduled for
233static void ThreadWakeupCallback(u64 parameter, int cycles_late) { 241static void ThreadWakeupCallback(u64 thread_handle, int cycles_late) {
234 Handle handle = static_cast<Handle>(parameter); 242 SharedPtr<Thread> thread = wakeup_callback_handle_table.Get<Thread>((Handle)thread_handle);
235 SharedPtr<Thread> thread = Kernel::g_handle_table.Get<Thread>(handle);
236 if (thread == nullptr) { 243 if (thread == nullptr) {
237 LOG_ERROR(Kernel, "Thread doesn't exist %u", handle); 244 LOG_CRITICAL(Kernel, "Callback fired for invalid thread %08X", thread_handle);
238 return; 245 return;
239 } 246 }
240 247
@@ -248,14 +255,13 @@ static void ThreadWakeupCallback(u64 parameter, int cycles_late) {
248} 255}
249 256
250 257
251void WakeThreadAfterDelay(Thread* thread, s64 nanoseconds) { 258void Thread::WakeAfterDelay(s64 nanoseconds) {
252 // Don't schedule a wakeup if the thread wants to wait forever 259 // Don't schedule a wakeup if the thread wants to wait forever
253 if (nanoseconds == -1) 260 if (nanoseconds == -1)
254 return; 261 return;
255 _dbg_assert_(Kernel, thread != nullptr);
256 262
257 u64 microseconds = nanoseconds / 1000; 263 u64 microseconds = nanoseconds / 1000;
258 CoreTiming::ScheduleEvent(usToCycles(microseconds), ThreadWakeupEventType, thread->GetHandle()); 264 CoreTiming::ScheduleEvent(usToCycles(microseconds), ThreadWakeupEventType, callback_handle);
259} 265}
260 266
261void Thread::ReleaseWaitObject(WaitObject* wait_object) { 267void Thread::ReleaseWaitObject(WaitObject* wait_object) {
@@ -302,7 +308,7 @@ void Thread::ReleaseWaitObject(WaitObject* wait_object) {
302 308
303void Thread::ResumeFromWait() { 309void Thread::ResumeFromWait() {
304 // Cancel any outstanding wakeup events 310 // Cancel any outstanding wakeup events
305 CoreTiming::UnscheduleEvent(ThreadWakeupEventType, GetHandle()); 311 CoreTiming::UnscheduleEvent(ThreadWakeupEventType, callback_handle);
306 312
307 status &= ~THREADSTATUS_WAIT; 313 status &= ~THREADSTATUS_WAIT;
308 314
@@ -326,11 +332,11 @@ static void DebugThreadQueue() {
326 if (!thread) { 332 if (!thread) {
327 return; 333 return;
328 } 334 }
329 LOG_DEBUG(Kernel, "0x%02X 0x%08X (current)", thread->current_priority, GetCurrentThread()->GetHandle()); 335 LOG_DEBUG(Kernel, "0x%02X %u (current)", thread->current_priority, GetCurrentThread()->GetObjectId());
330 for (auto& t : thread_list) { 336 for (auto& t : thread_list) {
331 s32 priority = thread_ready_queue.contains(t.get()); 337 s32 priority = thread_ready_queue.contains(t.get());
332 if (priority != -1) { 338 if (priority != -1) {
333 LOG_DEBUG(Kernel, "0x%02X 0x%08X", priority, t->GetHandle()); 339 LOG_DEBUG(Kernel, "0x%02X %u", priority, t->GetObjectId());
334 } 340 }
335 } 341 }
336} 342}
@@ -362,14 +368,6 @@ ResultVal<SharedPtr<Thread>> Thread::Create(std::string name, VAddr entry_point,
362 368
363 SharedPtr<Thread> thread(new Thread); 369 SharedPtr<Thread> thread(new Thread);
364 370
365 // TODO(yuriks): Thread requires a handle to be inserted into the various scheduling queues for
366 // the time being. Create a handle here, it will be copied to the handle field in
367 // the object and use by the rest of the code. This should be removed when other
368 // code doesn't rely on the handle anymore.
369 ResultVal<Handle> handle = Kernel::g_handle_table.Create(thread);
370 if (handle.Failed())
371 return handle.Code();
372
373 thread_list.push_back(thread); 371 thread_list.push_back(thread);
374 thread_ready_queue.prepare(priority); 372 thread_ready_queue.prepare(priority);
375 373
@@ -385,6 +383,7 @@ ResultVal<SharedPtr<Thread>> Thread::Create(std::string name, VAddr entry_point,
385 thread->wait_objects.clear(); 383 thread->wait_objects.clear();
386 thread->wait_address = 0; 384 thread->wait_address = 0;
387 thread->name = std::move(name); 385 thread->name = std::move(name);
386 thread->callback_handle = wakeup_callback_handle_table.Create(thread).MoveFrom();
388 387
389 ResetThread(thread.get(), arg, 0); 388 ResetThread(thread.get(), arg, 0);
390 CallThread(thread.get()); 389 CallThread(thread.get());
@@ -418,16 +417,14 @@ void Thread::SetPriority(s32 priority) {
418 } 417 }
419} 418}
420 419
421Handle SetupIdleThread() { 420SharedPtr<Thread> SetupIdleThread() {
422 // We need to pass a few valid values to get around parameter checking in Thread::Create. 421 // We need to pass a few valid values to get around parameter checking in Thread::Create.
423 auto thread_res = Thread::Create("idle", Memory::KERNEL_MEMORY_VADDR, THREADPRIO_LOWEST, 0, 422 auto thread = Thread::Create("idle", Memory::KERNEL_MEMORY_VADDR, THREADPRIO_LOWEST, 0,
424 THREADPROCESSORID_0, 0, Kernel::DEFAULT_STACK_SIZE); 423 THREADPROCESSORID_0, 0, Kernel::DEFAULT_STACK_SIZE).MoveFrom();
425 _dbg_assert_(Kernel, thread_res.Succeeded());
426 SharedPtr<Thread> thread = std::move(*thread_res);
427 424
428 thread->idle = true; 425 thread->idle = true;
429 CallThread(thread.get()); 426 CallThread(thread.get());
430 return thread->GetHandle(); 427 return thread;
431} 428}
432 429
433SharedPtr<Thread> SetupMainThread(s32 priority, u32 stack_size) { 430SharedPtr<Thread> SetupMainThread(s32 priority, u32 stack_size) {
@@ -460,13 +457,13 @@ void Reschedule() {
460 HLE::g_reschedule = false; 457 HLE::g_reschedule = false;
461 458
462 if (next != nullptr) { 459 if (next != nullptr) {
463 LOG_TRACE(Kernel, "context switch 0x%08X -> 0x%08X", prev->GetHandle(), next->GetHandle()); 460 LOG_TRACE(Kernel, "context switch %u -> %u", prev->GetObjectId(), next->GetObjectId());
464 SwitchContext(next); 461 SwitchContext(next);
465 } else { 462 } else {
466 LOG_TRACE(Kernel, "cannot context switch from 0x%08X, no higher priority thread!", prev->GetHandle()); 463 LOG_TRACE(Kernel, "cannot context switch from %u, no higher priority thread!", prev->GetObjectId());
467 464
468 for (auto& thread : thread_list) { 465 for (auto& thread : thread_list) {
469 LOG_TRACE(Kernel, "\thandle=0x%08X prio=0x%02X, status=0x%08X", thread->GetHandle(), 466 LOG_TRACE(Kernel, "\tid=%u prio=0x%02X, status=0x%08X", thread->GetObjectId(),
470 thread->current_priority, thread->status); 467 thread->current_priority, thread->status);
471 } 468 }
472 } 469 }