summaryrefslogtreecommitdiff
path: root/src/core/hle/kernel/thread.cpp
diff options
context:
space:
mode:
authorGravatar bunnei2014-06-05 22:35:36 -0400
committerGravatar bunnei2014-06-13 09:51:02 -0400
commitf5c7c1543434e25a215286e6db5e71c055ba48cf (patch)
tree488a3fd0c01051453c6f8ccc4867f6b6ea3f2843 /src/core/hle/kernel/thread.cpp
parentqt: updated disassembler to show 2X as many instructions (diff)
downloadyuzu-f5c7c1543434e25a215286e6db5e71c055ba48cf.tar.gz
yuzu-f5c7c1543434e25a215286e6db5e71c055ba48cf.tar.xz
yuzu-f5c7c1543434e25a215286e6db5e71c055ba48cf.zip
Kernel: Added real support for thread and event blocking
- SVC: Added ExitThread support - SVC: Added SignalEvent support - Thread: Added WAITTYPE_EVENT for waiting threads for event signals - Thread: Added support for blocking on other threads to finish (e.g. Thread::Join) - Thread: Added debug function for printing current threads ready for execution - Thread: Removed hack/broken thread ready state code from Kernel::Reschedule - Mutex: Moved WaitCurrentThread from SVC to Mutex::WaitSynchronization - Event: Added support for blocking threads on event signalling Kernel: Added missing algorithm #include for use of std::find on non-Windows platforms.
Diffstat (limited to 'src/core/hle/kernel/thread.cpp')
-rw-r--r--src/core/hle/kernel/thread.cpp121
1 files changed, 88 insertions, 33 deletions
diff --git a/src/core/hle/kernel/thread.cpp b/src/core/hle/kernel/thread.cpp
index c84fdf91d..d372df709 100644
--- a/src/core/hle/kernel/thread.cpp
+++ b/src/core/hle/kernel/thread.cpp
@@ -5,6 +5,7 @@
5#include <stdio.h> 5#include <stdio.h>
6 6
7#include <list> 7#include <list>
8#include <algorithm>
8#include <vector> 9#include <vector>
9#include <map> 10#include <map>
10#include <string> 11#include <string>
@@ -52,7 +53,14 @@ public:
52 * @return Result of operation, 0 on success, otherwise error code 53 * @return Result of operation, 0 on success, otherwise error code
53 */ 54 */
54 Result WaitSynchronization(bool* wait) { 55 Result WaitSynchronization(bool* wait) {
55 // TODO(bunnei): ImplementMe 56 if (status != THREADSTATUS_DORMANT) {
57 Handle thread = GetCurrentThreadHandle();
58 if (std::find(waiting_threads.begin(), waiting_threads.end(), thread) == waiting_threads.end()) {
59 waiting_threads.push_back(thread);
60 }
61 WaitCurrentThread(WAITTYPE_THREADEND, this->GetHandle());
62 *wait = true;
63 }
56 return 0; 64 return 0;
57 } 65 }
58 66
@@ -69,6 +77,9 @@ public:
69 s32 processor_id; 77 s32 processor_id;
70 78
71 WaitType wait_type; 79 WaitType wait_type;
80 Handle wait_handle;
81
82 std::vector<Handle> waiting_threads;
72 83
73 char name[Kernel::MAX_NAME_LENGTH + 1]; 84 char name[Kernel::MAX_NAME_LENGTH + 1];
74}; 85};
@@ -82,7 +93,6 @@ Common::ThreadQueueList<Handle> g_thread_ready_queue;
82Handle g_current_thread_handle; 93Handle g_current_thread_handle;
83Thread* g_current_thread; 94Thread* g_current_thread;
84 95
85
86/// Gets the current thread 96/// Gets the current thread
87inline Thread* GetCurrentThread() { 97inline Thread* GetCurrentThread() {
88 return g_current_thread; 98 return g_current_thread;
@@ -114,15 +124,15 @@ void ResetThread(Thread* t, u32 arg, s32 lowest_priority) {
114 memset(&t->context, 0, sizeof(ThreadContext)); 124 memset(&t->context, 0, sizeof(ThreadContext));
115 125
116 t->context.cpu_registers[0] = arg; 126 t->context.cpu_registers[0] = arg;
117 t->context.pc = t->entry_point; 127 t->context.pc = t->context.cpu_registers[15] = t->entry_point;
118 t->context.sp = t->stack_top; 128 t->context.sp = t->stack_top;
119 t->context.cpsr = 0x1F; // Usermode 129 t->context.cpsr = 0x1F; // Usermode
120 130
121 if (t->current_priority < lowest_priority) { 131 if (t->current_priority < lowest_priority) {
122 t->current_priority = t->initial_priority; 132 t->current_priority = t->initial_priority;
123 } 133 }
124
125 t->wait_type = WAITTYPE_NONE; 134 t->wait_type = WAITTYPE_NONE;
135 t->wait_handle = 0;
126} 136}
127 137
128/// Change a thread to "ready" state 138/// Change a thread to "ready" state
@@ -142,6 +152,43 @@ void ChangeReadyState(Thread* t, bool ready) {
142 } 152 }
143} 153}
144 154
155/// Verify that a thread has not been released from waiting
156inline bool VerifyWait(const Handle& thread, WaitType type, Handle handle) {
157 Handle wait_id = 0;
158 Thread *t = g_object_pool.GetFast<Thread>(thread);
159 if (t) {
160 if (type == t->wait_type && handle == t->wait_handle) {
161 return true;
162 }
163 } else {
164 ERROR_LOG(KERNEL, "thread 0x%08X does not exist", thread);
165 }
166 return false;
167}
168
169/// Stops the current thread
170void StopThread(Handle thread, const char* reason) {
171 u32 error;
172 Thread *t = g_object_pool.Get<Thread>(thread, error);
173 if (t) {
174 ChangeReadyState(t, false);
175 t->status = THREADSTATUS_DORMANT;
176 for (size_t i = 0; i < t->waiting_threads.size(); ++i) {
177 const Handle waiting_thread = t->waiting_threads[i];
178 if (VerifyWait(waiting_thread, WAITTYPE_THREADEND, thread)) {
179 ResumeThreadFromWait(waiting_thread);
180 }
181 }
182 t->waiting_threads.clear();
183
184 // Stopped threads are never waiting.
185 t->wait_type = WAITTYPE_NONE;
186 t->wait_handle = 0;
187 } else {
188 ERROR_LOG(KERNEL, "thread 0x%08X does not exist", thread);
189 }
190}
191
145/// Changes a threads state 192/// Changes a threads state
146void ChangeThreadState(Thread* t, ThreadStatus new_status) { 193void ChangeThreadState(Thread* t, ThreadStatus new_status) {
147 if (!t || t->status == new_status) { 194 if (!t || t->status == new_status) {
@@ -152,7 +199,7 @@ void ChangeThreadState(Thread* t, ThreadStatus new_status) {
152 199
153 if (new_status == THREADSTATUS_WAIT) { 200 if (new_status == THREADSTATUS_WAIT) {
154 if (t->wait_type == WAITTYPE_NONE) { 201 if (t->wait_type == WAITTYPE_NONE) {
155 printf("ERROR: Waittype none not allowed here\n"); 202 ERROR_LOG(KERNEL, "Waittype none not allowed");
156 } 203 }
157 } 204 }
158} 205}
@@ -207,9 +254,10 @@ Thread* NextThread() {
207} 254}
208 255
209/// Puts the current thread in the wait state for the given type 256/// Puts the current thread in the wait state for the given type
210void WaitCurrentThread(WaitType wait_type) { 257void WaitCurrentThread(WaitType wait_type, Handle wait_handle) {
211 Thread* t = GetCurrentThread(); 258 Thread* t = GetCurrentThread();
212 t->wait_type = wait_type; 259 t->wait_type = wait_type;
260 t->wait_handle = wait_handle;
213 ChangeThreadState(t, ThreadStatus(THREADSTATUS_WAIT | (t->status & THREADSTATUS_SUSPEND))); 261 ChangeThreadState(t, ThreadStatus(THREADSTATUS_WAIT | (t->status & THREADSTATUS_SUSPEND)));
214} 262}
215 263
@@ -225,6 +273,22 @@ void ResumeThreadFromWait(Handle handle) {
225 } 273 }
226} 274}
227 275
276/// Prints the thread queue for debugging purposes
277void DebugThreadQueue() {
278 Thread* thread = GetCurrentThread();
279 if (!thread) {
280 return;
281 }
282 INFO_LOG(KERNEL, "0x%02X 0x%08X (current)", thread->current_priority, GetCurrentThreadHandle());
283 for (u32 i = 0; i < g_thread_queue.size(); i++) {
284 Handle handle = g_thread_queue[i];
285 s32 priority = g_thread_ready_queue.contains(handle);
286 if (priority != -1) {
287 INFO_LOG(KERNEL, "0x%02X 0x%08X", priority, handle);
288 }
289 }
290}
291
228/// Creates a new thread 292/// Creates a new thread
229Thread* CreateThread(Handle& handle, const char* name, u32 entry_point, s32 priority, 293Thread* CreateThread(Handle& handle, const char* name, u32 entry_point, s32 priority,
230 s32 processor_id, u32 stack_top, int stack_size) { 294 s32 processor_id, u32 stack_top, int stack_size) {
@@ -233,12 +297,12 @@ Thread* CreateThread(Handle& handle, const char* name, u32 entry_point, s32 prio
233 "CreateThread priority=%d, outside of allowable range!", priority) 297 "CreateThread priority=%d, outside of allowable range!", priority)
234 298
235 Thread* t = new Thread; 299 Thread* t = new Thread;
236 300
237 handle = Kernel::g_object_pool.Create(t); 301 handle = Kernel::g_object_pool.Create(t);
238 302
239 g_thread_queue.push_back(handle); 303 g_thread_queue.push_back(handle);
240 g_thread_ready_queue.prepare(priority); 304 g_thread_ready_queue.prepare(priority);
241 305
242 t->status = THREADSTATUS_DORMANT; 306 t->status = THREADSTATUS_DORMANT;
243 t->entry_point = entry_point; 307 t->entry_point = entry_point;
244 t->stack_top = stack_top; 308 t->stack_top = stack_top;
@@ -246,16 +310,18 @@ Thread* CreateThread(Handle& handle, const char* name, u32 entry_point, s32 prio
246 t->initial_priority = t->current_priority = priority; 310 t->initial_priority = t->current_priority = priority;
247 t->processor_id = processor_id; 311 t->processor_id = processor_id;
248 t->wait_type = WAITTYPE_NONE; 312 t->wait_type = WAITTYPE_NONE;
249 313 t->wait_handle = 0;
314
250 strncpy(t->name, name, Kernel::MAX_NAME_LENGTH); 315 strncpy(t->name, name, Kernel::MAX_NAME_LENGTH);
251 t->name[Kernel::MAX_NAME_LENGTH] = '\0'; 316 t->name[Kernel::MAX_NAME_LENGTH] = '\0';
252 317
253 return t; 318 return t;
254} 319}
255 320
256/// Creates a new thread - wrapper for external user 321/// Creates a new thread - wrapper for external user
257Handle CreateThread(const char* name, u32 entry_point, s32 priority, u32 arg, s32 processor_id, 322Handle CreateThread(const char* name, u32 entry_point, s32 priority, u32 arg, s32 processor_id,
258 u32 stack_top, int stack_size) { 323 u32 stack_top, int stack_size) {
324
259 if (name == NULL) { 325 if (name == NULL) {
260 ERROR_LOG(KERNEL, "CreateThread(): NULL name"); 326 ERROR_LOG(KERNEL, "CreateThread(): NULL name");
261 return -1; 327 return -1;
@@ -289,7 +355,7 @@ Handle CreateThread(const char* name, u32 entry_point, s32 priority, u32 arg, s3
289 355
290 // This won't schedule to the new thread, but it may to one woken from eating cycles. 356 // This won't schedule to the new thread, but it may to one woken from eating cycles.
291 // Technically, this should not eat all at once, and reschedule in the middle, but that's hard. 357 // Technically, this should not eat all at once, and reschedule in the middle, but that's hard.
292 //HLE::Reschedule("thread created"); 358 //HLE::Reschedule(__func__);
293 359
294 return handle; 360 return handle;
295} 361}
@@ -363,35 +429,24 @@ Handle SetupMainThread(s32 priority, int stack_size) {
363 return handle; 429 return handle;
364} 430}
365 431
432
366/// Reschedules to the next available thread (call after current thread is suspended) 433/// Reschedules to the next available thread (call after current thread is suspended)
367void Reschedule() { 434void Reschedule() {
368 Thread* prev = GetCurrentThread(); 435 Thread* prev = GetCurrentThread();
369 Thread* next = NextThread(); 436 Thread* next = NextThread();
437 HLE::g_reschedule = false;
370 if (next > 0) { 438 if (next > 0) {
371 INFO_LOG(KERNEL, "context switch 0x%08X -> 0x%08X", prev->GetHandle(), next->GetHandle()); 439 INFO_LOG(KERNEL, "context switch 0x%08X -> 0x%08X", prev->GetHandle(), next->GetHandle());
372 440
373 SwitchContext(next); 441 SwitchContext(next);
374 442
375 // Hack - automatically change previous thread (which would have been in "wait" state) to 443 // Hack - There is no mechanism yet to waken the primary thread if it has been put to sleep
376 // "ready" state, so that we can immediately resume to it when new thread yields. FixMe to 444 // by a simulated VBLANK thread switch. So, we'll just immediately set it to "ready" again.
377 // actually wait for whatever event it is supposed to be waiting on. 445 // This results in the current thread yielding on a VBLANK once, and then it will be
378 446 // immediately placed back in the queue for execution.
379 ChangeReadyState(prev, true); 447 if (prev->wait_type == WAITTYPE_VBLANK) {
380 } else { 448 ResumeThreadFromWait(prev->GetHandle());
381 INFO_LOG(KERNEL, "no ready threads, staying on 0x%08X", prev->GetHandle()); 449 }
382
383 // Hack - no other threads are available, so decrement current PC to the last instruction,
384 // and then resume current thread. This should always be called on a blocking instruction
385 // (e.g. svcWaitSynchronization), and the result should be that the instruction is repeated
386 // until it no longer blocks.
387
388 // TODO(bunnei): A better solution: Have the CPU switch to an idle thread
389
390 ThreadContext ctx;
391 SaveContext(ctx);
392 ctx.pc -= 4;
393 LoadContext(ctx);
394 ChangeReadyState(prev, true);
395 } 450 }
396} 451}
397 452