8#include <coreinit/thread.h>
9#include <coreinit/mutex.h>
10#include <coreinit/time.h>
11#include <coreinit/condition.h>
14#include "WutThreadDriver.h"
18 struct WutThreadHandle
20 OSThread * thread =
nullptr;
21 void * stack =
nullptr;
22 ThreadEntry entry =
nullptr;
29 int WutThreadTrampoline(
int argc,
void * argv)
32 WutThreadHandle * handle =
static_cast<WutThreadHandle *
>(argv);
33 handle->entry(handle->arg);
41 case ThreadPriority::Idle:
return 31;
42 case ThreadPriority::Low:
return 24;
43 case ThreadPriority::Normal:
return 16;
44 case ThreadPriority::High:
return 10;
45 case ThreadPriority::TimeCritical:
return 3;
61 WutThreadHandle * handle =
new WutThreadHandle();
62 handle->entry = entry;
69 handle->thread =
static_cast<OSThread *
>(memalign(8,
sizeof(OSThread)));
76 uint32_t alignedStackSize = (stackSize + 31) & ~31u;
77 handle->stack = memalign(32, alignedStackSize);
89 uint8_t * stackTop =
static_cast<uint8_t *
>(handle->stack) + alignedStackSize;
90 int nativePriority = MapWutPriority(priority);
92 BOOL ok = OSCreateThread(handle->thread, WutThreadTrampoline, 0, handle,
93 stackTop, alignedStackSize, nativePriority, OS_THREAD_ATTRIB_AFFINITY_ANY);
106 OSResumeThread(handle->thread);
116 WutThreadHandle * handle =
static_cast<WutThreadHandle *
>(thread);
117 OSJoinThread(handle->thread,
nullptr);
119 free(handle->thread);
137 WutThreadHandle * handle =
static_cast<WutThreadHandle *
>(thread);
138 OSCancelThread(handle->thread);
147 OSSuspendThread(
static_cast<WutThreadHandle *
>(thread)->thread);
155 OSResumeThread(
static_cast<WutThreadHandle *
>(thread)->thread);
163 return OSIsThreadSuspended(
static_cast<WutThreadHandle *
>(thread)->thread) != FALSE;
168 OSMutex * mutex =
new OSMutex();
178 delete static_cast<OSMutex *
>(mutex);
186 OSLockMutex(
static_cast<OSMutex *
>(mutex));
194 OSUnlockMutex(
static_cast<OSMutex *
>(mutex));
199 OSCondition * cond =
new OSCondition();
209 delete static_cast<OSCondition *
>(cond);
219 OSWaitCond(
static_cast<OSCondition *
>(cond),
static_cast<OSMutex *
>(mutex));
229 OSSignalCond(
static_cast<OSCondition *
>(cond));
235 OSSleepTicks(OSMillisecondsToTicks(ms));
245 return reinterpret_cast<uintptr_t
>(OSGetCurrentThread());
void waitCond(void *cond, void *mutex) override
Atomically unlocks mutex and blocks the calling thread until signalCond() is called,...
void sleepMilliseconds(uint32_t ms) override
Sleeps the calling thread for at least the given duration.
void shutdown() override
Shuts the threading backend down. Every thread must already have been joined.
void lockMutex(void *mutex) override
Blocks until the mutex is acquired.
void joinThread(void *thread) override
Blocks until the thread exits, then releases the handle.
void resumeThread(void *thread) override
Resumes a thread suspended with suspendThread().
void * createCond() override
Creates a condition variable.
void suspendThread(void *thread) override
Suspends a running thread until resumeThread() is called.
void * createMutex() override
Creates an unlocked mutex.
void destroyMutex(void *mutex) override
Destroys a mutex created by createMutex(). It must not be locked.
void signalCond(void *cond) override
Wakes every thread currently blocked in waitCond() on this condition variable.
void destroyCond(void *cond) override
Destroys a condition variable created by createCond(). No thread may be waiting on it.
void cancelThread(void *thread) override
Requests the thread stop running and releases the handle without blocking on it.
uintptr_t getCurrentThreadId() override
Returns an opaque, comparable identifier for the calling thread.
bool isThreadSuspended(void *thread) override
void unlockMutex(void *mutex) override
Releases a mutex held by the calling thread.
bool createThread(ThreadEntry entry, void *arg, uint32_t stackSize, ThreadPriority priority, void **outHandle) override
Creates and starts a thread running entry(arg).
void init() override
Initializes the threading backend.
ThreadPriority
Abstract thread priority, mapped to each platform's native priority scheme.