14#include "OgcThreadDriver.h"
18 struct OgcThreadHandle
20 lwp_t thread = LWP_THREAD_NULL;
21 ThreadEntry entry =
nullptr;
25 void * OgcThreadTrampoline(
void * arg)
27 OgcThreadHandle * handle =
static_cast<OgcThreadHandle *
>(arg);
28 return handle->entry(handle->arg);
35 case ThreadPriority::Idle:
return LWP_PRIO_IDLE;
36 case ThreadPriority::Low:
return LWP_PRIO_LOWEST;
37 case ThreadPriority::Normal:
return LWP_PRIO_NORMAL;
38 case ThreadPriority::High:
return LWP_PRIO_HIGHEST;
39 case ThreadPriority::TimeCritical:
return LWP_PRIO_TIME_CRITICAL;
40 default:
return LWP_PRIO_NORMAL;
55 OgcThreadHandle * handle =
new OgcThreadHandle();
56 handle->entry = entry;
67 int nativePriority = MapOgcPriority(priority);
68 int32_t res = LWP_CreateThread(&handle->thread, OgcThreadTrampoline, handle,
nullptr, stackSize, nativePriority);
84 OgcThreadHandle * handle =
static_cast<OgcThreadHandle *
>(thread);
85 LWP_JoinThread(handle->thread,
nullptr);
100 OgcThreadHandle * handle =
static_cast<OgcThreadHandle *
>(thread);
101 LWP_SuspendThread(handle->thread);
110 LWP_SuspendThread(
static_cast<OgcThreadHandle *
>(thread)->thread);
118 LWP_ResumeThread(
static_cast<OgcThreadHandle *
>(thread)->thread);
126 return LWP_ThreadIsSuspended(
static_cast<OgcThreadHandle *
>(thread)->thread) != 0;
131 mutex_t * mutex =
new mutex_t;
132 if(LWP_MutexInit(mutex,
false) != 0)
146 mutex_t * m =
static_cast<mutex_t *
>(mutex);
147 LWP_MutexDestroy(*m);
156 LWP_MutexLock(*
static_cast<mutex_t *
>(mutex));
164 LWP_MutexUnlock(*
static_cast<mutex_t *
>(mutex));
169 cond_t * cond =
new cond_t;
170 if(LWP_CondInit(cond) != 0)
184 cond_t * c =
static_cast<cond_t *
>(cond);
194 LWP_CondWait(*
static_cast<cond_t *
>(cond), *
static_cast<mutex_t *
>(mutex));
203 LWP_CondBroadcast(*
static_cast<cond_t *
>(cond));
216 return (uintptr_t)LWP_GetSelf();
void shutdown() override
Shuts the threading backend down. Every thread must already have been joined.
void * createCond() override
Creates a condition variable.
bool isThreadSuspended(void *thread) override
void unlockMutex(void *mutex) override
Releases a mutex held by the calling thread.
void signalCond(void *cond) override
Wakes every thread currently blocked in waitCond() on this condition variable.
bool createThread(ThreadEntry entry, void *arg, uint32_t stackSize, ThreadPriority priority, void **outHandle) override
Creates and starts a thread running entry(arg).
void * createMutex() override
Creates an unlocked mutex.
void joinThread(void *thread) override
Blocks until the thread exits, then releases the handle.
void suspendThread(void *thread) override
Suspends a running thread until resumeThread() is called.
void sleepMilliseconds(uint32_t ms) override
Sleeps the calling thread for at least the given duration.
uintptr_t getCurrentThreadId() override
Returns an opaque, comparable identifier for the calling thread.
void resumeThread(void *thread) override
Resumes a thread suspended with suspendThread().
void lockMutex(void *mutex) override
Blocks until the mutex is acquired.
void init() override
Initializes the threading backend.
void waitCond(void *cond, void *mutex) override
Atomically unlocks mutex and blocks the calling thread until signalCond() is called,...
void cancelThread(void *thread) override
Requests the thread stop running and releases the handle without blocking on it.
void destroyMutex(void *mutex) override
Destroys a mutex created by createMutex(). It must not be locked.
void destroyCond(void *cond) override
Destroys a condition variable created by createCond(). No thread may be waiting on it.
ThreadPriority
Abstract thread priority, mapped to each platform's native priority scheme.