src/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 "util/common.h"
10
11#include <3ds.h>
12#include <malloc.h>
13
14#ifdef _3DS
15// ctrulib already has a type called Thread
16#define Thread CustomThread
17#endif
18
19#define THREAD_ENTRY void
20typedef ThreadFunc ThreadEntry;
21
22typedef struct {
23 Handle handle;
24 u8* stack;
25} Thread;
26typedef Handle Mutex;
27typedef struct {
28 Mutex mutex;
29 Handle semaphore;
30 u32 waiting;
31} Condition;
32
33static inline int MutexInit(Mutex* mutex) {
34 return svcCreateMutex(mutex, false);
35}
36
37static inline int MutexDeinit(Mutex* mutex) {
38 return svcCloseHandle(*mutex);
39}
40
41static inline int MutexLock(Mutex* mutex) {
42 return svcWaitSynchronization(*mutex, U64_MAX);
43}
44
45static inline int MutexTryLock(Mutex* mutex) {
46 return svcWaitSynchronization(*mutex, 10);
47}
48
49static inline int MutexUnlock(Mutex* mutex) {
50 return svcReleaseMutex(*mutex);
51}
52
53static inline int ConditionInit(Condition* cond) {
54 Result res = svcCreateMutex(&cond->mutex, false);
55 if (res) {
56 return res;
57 }
58 res = svcCreateSemaphore(&cond->semaphore, 0, 1);
59 if (res) {
60 svcCloseHandle(cond->mutex);
61 }
62 cond->waiting = 0;
63 return res;
64}
65
66static inline int ConditionDeinit(Condition* cond) {
67 svcCloseHandle(cond->mutex);
68 return svcCloseHandle(cond->semaphore);
69}
70
71static inline int ConditionWait(Condition* cond, Mutex* mutex) {
72 MutexLock(&cond->mutex);
73 ++cond->waiting;
74 MutexUnlock(mutex);
75 MutexUnlock(&cond->mutex);
76 svcWaitSynchronization(cond->semaphore, U64_MAX);
77 MutexLock(mutex);
78 return 1;
79}
80
81static inline int ConditionWaitTimed(Condition* cond, Mutex* mutex, int32_t timeoutMs) {
82 MutexLock(&cond->mutex);
83 ++cond->waiting;
84 MutexUnlock(mutex);
85 MutexUnlock(&cond->mutex);
86 svcWaitSynchronization(cond->semaphore, timeoutMs * 10000000LL);
87 MutexLock(mutex);
88 return 1;
89}
90
91static inline int ConditionWake(Condition* cond) {
92 MutexLock(&cond->mutex);
93 if (cond->waiting) {
94 --cond->waiting;
95 s32 count = 0;
96 svcReleaseSemaphore(&count, cond->semaphore, 1);
97 }
98 MutexUnlock(&cond->mutex);
99 return 0;
100}
101
102static inline int ThreadCreate(Thread* thread, ThreadEntry entry, void* context) {
103 if (!entry || !thread) {
104 return 1;
105 }
106 thread->stack = memalign(8, 0x8000);
107 if (!thread->stack) {
108 return 1;
109 }
110 return svcCreateThread(&thread->handle, entry, (u32) context, (u32*) &thread->stack[0x8000], 0x18, -1);
111}
112
113static inline int ThreadJoin(Thread thread) {
114 svcWaitSynchronization(thread.handle, U64_MAX);
115 free(thread.stack);
116 return 0;
117}
118
119static inline void ThreadSetName(const char* name) {
120 UNUSED(name);
121 // Unimplemented
122}
123
124#endif