include/mgba-util/platform/3ds/threading.h (view raw)
1/* Copyright (c) 2013-2015 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#ifndef N3DS_THREADING_H
7#define N3DS_THREADING_H
8
9#include <mgba-util/common.h>
10
11#include <3ds.h>
12#include <malloc.h>
13
14#define THREAD_ENTRY void
15typedef ThreadFunc ThreadEntry;
16
17typedef LightLock Mutex;
18typedef CondVar Condition;
19
20static inline int MutexInit(Mutex* mutex) {
21 LightLock_Init(mutex);
22 return 0;
23}
24
25static inline int MutexDeinit(Mutex* mutex) {
26 UNUSED(mutex);
27 return 0;
28}
29
30static inline int MutexLock(Mutex* mutex) {
31 LightLock_Lock(mutex);
32 return 0;
33}
34
35static inline int MutexTryLock(Mutex* mutex) {
36 return LightLock_TryLock(mutex);
37}
38
39static inline int MutexUnlock(Mutex* mutex) {
40 LightLock_Unlock(mutex);
41 return 0;
42}
43
44static inline int ConditionInit(Condition* cond) {
45 CondVar_Init(cond);
46 return 0;
47}
48
49static inline int ConditionDeinit(Condition* cond) {
50 UNUSED(cond);
51 return 0;
52}
53
54static inline int ConditionWait(Condition* cond, Mutex* mutex) {
55 CondVar_Wait(cond, mutex);
56 return 0;
57}
58
59static inline int ConditionWaitTimed(Condition* cond, Mutex* mutex, int32_t timeoutMs) {
60 return CondVar_WaitTimeout(cond, mutex, timeoutMs * 10000000LL);
61}
62
63static inline int ConditionWake(Condition* cond) {
64 CondVar_Signal(cond);
65 return 0;
66}
67
68static inline int ThreadCreate(Thread* thread, ThreadEntry entry, void* context) {
69 if (!entry || !thread) {
70 return 1;
71 }
72 *thread = threadCreate(entry, context, 0x8000, 0x18, 2, true);
73 return !*thread;
74}
75
76static inline int ThreadJoin(Thread* thread) {
77 return threadJoin(*thread, U64_MAX);
78}
79
80static inline void ThreadSetName(const char* name) {
81 UNUSED(name);
82 // Unimplemented
83}
84
85#endif