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;
30 int WutThreadTrampoline(
int argc,
const char ** argv)
33 WutThreadHandle * handle =
reinterpret_cast<WutThreadHandle *
>(argv);
34 handle->entry(handle->arg);
42 case ThreadPriority::Idle:
return 31;
43 case ThreadPriority::Low:
return 24;
44 case ThreadPriority::Normal:
return 16;
45 case ThreadPriority::High:
return 10;
46 case ThreadPriority::TimeCritical:
return 3;
62 WutThreadHandle * handle =
new WutThreadHandle();
63 handle->entry = entry;
70 handle->thread =
static_cast<OSThread *
>(memalign(8,
sizeof(OSThread)));
77 uint32_t alignedStackSize = (stackSize + 31) & ~31u;
78 handle->stack = memalign(32, alignedStackSize);
90 uint8_t * stackTop =
static_cast<uint8_t *
>(handle->stack) + alignedStackSize;
91 int nativePriority = MapWutPriority(priority);
93 BOOL ok = OSCreateThread(handle->thread, WutThreadTrampoline, 0,
reinterpret_cast<char *
>(handle),
94 stackTop, alignedStackSize, nativePriority, OS_THREAD_ATTRIB_AFFINITY_ANY);
100 free(handle->thread);
107 OSResumeThread(handle->thread);
117 WutThreadHandle * handle =
static_cast<WutThreadHandle *
>(thread);
118 OSJoinThread(handle->thread,
nullptr);
120 free(handle->thread);
138 WutThreadHandle * handle =
static_cast<WutThreadHandle *
>(thread);
139 OSCancelThread(handle->thread);
148 OSSuspendThread(
static_cast<WutThreadHandle *
>(thread)->thread);
156 OSResumeThread(
static_cast<WutThreadHandle *
>(thread)->thread);
164 return OSIsThreadSuspended(
static_cast<WutThreadHandle *
>(thread)->thread) != FALSE;
169 OSMutex * mutex =
new OSMutex();
179 delete static_cast<OSMutex *
>(mutex);
187 OSLockMutex(
static_cast<OSMutex *
>(mutex));
195 OSUnlockMutex(
static_cast<OSMutex *
>(mutex));
200 OSCondition * cond =
new OSCondition();
210 delete static_cast<OSCondition *
>(cond);
220 OSWaitCond(
static_cast<OSCondition *
>(cond),
static_cast<OSMutex *
>(mutex));
230 OSSignalCond(
static_cast<OSCondition *
>(cond));
236 OSSleepTicks(OSMillisecondsToTicks(ms));
246 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.