2015-09-04 07:52:51 +00:00
|
|
|
/* 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 N3DS_THREADING_H
|
|
|
|
#define N3DS_THREADING_H
|
|
|
|
|
2016-12-31 01:00:22 +00:00
|
|
|
#include <mgba-util/common.h>
|
2015-09-04 07:52:51 +00:00
|
|
|
|
|
|
|
#include <3ds.h>
|
|
|
|
#include <malloc.h>
|
|
|
|
|
|
|
|
#define THREAD_ENTRY void
|
|
|
|
typedef ThreadFunc ThreadEntry;
|
|
|
|
|
2016-08-06 05:46:50 +00:00
|
|
|
typedef LightLock Mutex;
|
2020-08-01 02:16:45 +00:00
|
|
|
typedef CondVar Condition;
|
2015-09-04 07:52:51 +00:00
|
|
|
|
|
|
|
static inline int MutexInit(Mutex* mutex) {
|
2016-08-06 05:46:50 +00:00
|
|
|
LightLock_Init(mutex);
|
|
|
|
return 0;
|
2015-09-04 07:52:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int MutexDeinit(Mutex* mutex) {
|
2016-08-06 05:46:50 +00:00
|
|
|
UNUSED(mutex);
|
|
|
|
return 0;
|
2015-09-04 07:52:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int MutexLock(Mutex* mutex) {
|
2016-08-06 05:46:50 +00:00
|
|
|
LightLock_Lock(mutex);
|
|
|
|
return 0;
|
2015-09-04 07:52:51 +00:00
|
|
|
}
|
|
|
|
|
2015-11-13 06:50:09 +00:00
|
|
|
static inline int MutexTryLock(Mutex* mutex) {
|
2016-08-06 05:46:50 +00:00
|
|
|
return LightLock_TryLock(mutex);
|
2015-11-13 06:50:09 +00:00
|
|
|
}
|
|
|
|
|
2015-09-04 07:52:51 +00:00
|
|
|
static inline int MutexUnlock(Mutex* mutex) {
|
2016-08-06 05:46:50 +00:00
|
|
|
LightLock_Unlock(mutex);
|
|
|
|
return 0;
|
2015-09-04 07:52:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int ConditionInit(Condition* cond) {
|
2020-08-01 02:16:45 +00:00
|
|
|
CondVar_Init(cond);
|
|
|
|
return 0;
|
2015-09-04 07:52:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int ConditionDeinit(Condition* cond) {
|
2020-08-01 02:16:45 +00:00
|
|
|
UNUSED(cond);
|
|
|
|
return 0;
|
2015-09-04 07:52:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int ConditionWait(Condition* cond, Mutex* mutex) {
|
2020-08-01 02:16:45 +00:00
|
|
|
CondVar_Wait(cond, mutex);
|
2016-08-06 07:05:29 +00:00
|
|
|
return 0;
|
2015-09-04 07:52:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int ConditionWaitTimed(Condition* cond, Mutex* mutex, int32_t timeoutMs) {
|
2020-08-01 02:16:45 +00:00
|
|
|
return CondVar_WaitTimeout(cond, mutex, timeoutMs * 10000000LL);
|
2015-09-04 07:52:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline int ConditionWake(Condition* cond) {
|
2020-08-01 02:16:45 +00:00
|
|
|
CondVar_Signal(cond);
|
2015-09-04 07:52:51 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int ThreadCreate(Thread* thread, ThreadEntry entry, void* context) {
|
|
|
|
if (!entry || !thread) {
|
|
|
|
return 1;
|
|
|
|
}
|
2020-08-25 01:24:22 +00:00
|
|
|
*thread = threadCreate(entry, context, 0x8000, 0x18, 2, false);
|
2018-01-10 08:39:35 +00:00
|
|
|
return !*thread;
|
2015-09-04 07:52:51 +00:00
|
|
|
}
|
|
|
|
|
2019-05-26 21:26:35 +00:00
|
|
|
static inline int ThreadJoin(Thread* thread) {
|
2020-08-25 01:24:22 +00:00
|
|
|
Result res = threadJoin(*thread, U64_MAX);
|
|
|
|
threadFree(*thread);
|
|
|
|
return res;
|
2015-09-04 07:52:51 +00:00
|
|
|
}
|
|
|
|
|
2015-09-04 07:56:55 +00:00
|
|
|
static inline void ThreadSetName(const char* name) {
|
|
|
|
UNUSED(name);
|
|
|
|
// Unimplemented
|
2015-09-04 07:52:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|