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 struct {
19 Mutex mutex;
20 Handle semaphore;
21 u32 waiting;
22} Condition;
23
24static inline int MutexInit(Mutex* mutex) {
25 LightLock_Init(mutex);
26 return 0;
27}
28
29static inline int MutexDeinit(Mutex* mutex) {
30 UNUSED(mutex);
31 return 0;
32}
33
34static inline int MutexLock(Mutex* mutex) {
35 LightLock_Lock(mutex);
36 return 0;
37}
38
39static inline int MutexTryLock(Mutex* mutex) {
40 return LightLock_TryLock(mutex);
41}
42
43static inline int MutexUnlock(Mutex* mutex) {
44 LightLock_Unlock(mutex);
45 return 0;
46}
47
48static inline int ConditionInit(Condition* cond) {
49 Result res = MutexInit(&cond->mutex);
50 if (res) {
51 return res;
52 }
53 res = svcCreateSemaphore(&cond->semaphore, 0, 1);
54 cond->waiting = 0;
55 return res;
56}
57
58static inline int ConditionDeinit(Condition* cond) {
59 return svcCloseHandle(cond->semaphore);
60}
61
62static inline int ConditionWait(Condition* cond, Mutex* mutex) {
63 MutexLock(&cond->mutex);
64 ++cond->waiting;
65 MutexUnlock(mutex);
66 MutexUnlock(&cond->mutex);
67 svcWaitSynchronization(cond->semaphore, U64_MAX);
68 MutexLock(mutex);
69 return 0;
70}
71
72static inline int ConditionWaitTimed(Condition* cond, Mutex* mutex, int32_t timeoutMs) {
73 MutexLock(&cond->mutex);
74 ++cond->waiting;
75 MutexUnlock(mutex);
76 MutexUnlock(&cond->mutex);
77 svcWaitSynchronization(cond->semaphore, timeoutMs * 10000000LL);
78 MutexLock(mutex);
79 return 0;
80}
81
82static inline int ConditionWake(Condition* cond) {
83 MutexLock(&cond->mutex);
84 if (cond->waiting) {
85 --cond->waiting;
86 s32 count = 0;
87 svcReleaseSemaphore(&count, cond->semaphore, 1);
88 }
89 MutexUnlock(&cond->mutex);
90 return 0;
91}
92
93static inline int ThreadCreate(Thread* thread, ThreadEntry entry, void* context) {
94 if (!entry || !thread) {
95 return 1;
96 }
97 *thread = threadCreate(entry, context, 0x8000, 0x18, 2, true);
98 return !*thread;
99}
100
101static inline int ThreadJoin(Thread thread) {
102 return threadJoin(thread, U64_MAX);
103}
104
105static inline void ThreadSetName(const char* name) {
106 UNUSED(name);
107 // Unimplemented
108}
109
110#endif