mgba/src/platform/psp2/threading.h

126 lines
2.8 KiB
C
Raw Normal View History

/* Copyright (c) 2013-2015 Jeffrey Pfau
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef SCE_THREADING_H
#define SCE_THREADING_H
#include <psp2/kernel/threadmgr.h>
typedef SceUID Thread;
typedef SceUID Mutex;
typedef struct {
Mutex mutex;
SceUID semaphore;
int waiting;
} Condition;
#define THREAD_ENTRY int
typedef THREAD_ENTRY (*ThreadEntry)(void*);
static inline int MutexInit(Mutex* mutex) {
Mutex id = sceKernelCreateMutex("mutex", 0, 0, 0);
if (id < 0) {
return id;
}
*mutex = id;
return 0;
}
static inline int MutexDeinit(Mutex* mutex) {
return sceKernelDeleteMutex(*mutex);
}
static inline int MutexLock(Mutex* mutex) {
return sceKernelLockMutex(*mutex, 1, 0);
}
static inline int MutexUnlock(Mutex* mutex) {
return sceKernelUnlockMutex(*mutex, 1);
}
static inline int ConditionInit(Condition* cond) {
int res = MutexInit(&cond->mutex);
if (res < 0) {
return res;
}
cond->semaphore = sceKernelCreateSema("SceCondSema", 0, 0, 1, 0);
if (cond->semaphore < 0) {
MutexDeinit(&cond->mutex);
res = cond->semaphore;
}
cond->waiting = 0;
return res;
}
static inline int ConditionDeinit(Condition* cond) {
MutexDeinit(&cond->mutex);
return sceKernelDeleteSema(cond->semaphore);
}
static inline int ConditionWait(Condition* cond, Mutex* mutex) {
MutexLock(&cond->mutex);
++cond->waiting;
MutexUnlock(mutex);
MutexUnlock(&cond->mutex);
sceKernelWaitSema(cond->semaphore, 1, 0);
MutexLock(mutex);
return 0;
}
static inline int ConditionWaitTimed(Condition* cond, Mutex* mutex, int32_t timeoutMs) {
MutexLock(&cond->mutex);
++cond->waiting;
MutexUnlock(mutex);
MutexUnlock(&cond->mutex);
SceUInt timeout = 0;
if (timeoutMs > 0) {
timeout = timeoutMs;
}
2015-07-06 01:19:26 +00:00
int ret = sceKernelWaitSema(cond->semaphore, 1, &timeout);
MutexLock(mutex);
2015-07-06 01:19:26 +00:00
return ret;
}
static inline int ConditionWake(Condition* cond) {
MutexLock(&cond->mutex);
if (cond->waiting) {
--cond->waiting;
sceKernelSignalSema(cond->semaphore, 1);
}
MutexUnlock(&cond->mutex);
return 0;
}
struct SceThreadEntryArgs {
void* context;
ThreadEntry entry;
};
static inline int _sceThreadEntry(SceSize args, void* argp) {
UNUSED(args);
struct SceThreadEntryArgs* arg = argp;
return arg->entry(arg->context);
}
static inline int ThreadCreate(Thread* thread, ThreadEntry entry, void* context) {
Thread id = sceKernelCreateThread("SceThread", _sceThreadEntry, 0x10000100, 0x10000, 0, 0, 0);
if (id < 0) {
return id;
}
*thread = id;
struct SceThreadEntryArgs args = { context, entry };
sceKernelStartThread(id, sizeof(args), &args);
return 0;
}
static inline int ThreadJoin(Thread thread) {
return sceKernelWaitThreadEnd(thread, 0);
}
static inline int ThreadSetName(const char* name) {
UNUSED(name);
return -1;
}
#endif