all repos — mgba @ 8a68de198cd39e351ea84e040ecf5882b6234c03

mGBA Game Boy Advance Emulator

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 LightLock Mutex;
 27typedef struct {
 28	Mutex mutex;
 29	Handle semaphore;
 30	u32 waiting;
 31} Condition;
 32
 33static inline int MutexInit(Mutex* mutex) {
 34	LightLock_Init(mutex);
 35	return 0;
 36}
 37
 38static inline int MutexDeinit(Mutex* mutex) {
 39	UNUSED(mutex);
 40	return 0;
 41}
 42
 43static inline int MutexLock(Mutex* mutex) {
 44	LightLock_Lock(mutex);
 45	return 0;
 46}
 47
 48static inline int MutexTryLock(Mutex* mutex) {
 49	return LightLock_TryLock(mutex);
 50}
 51
 52static inline int MutexUnlock(Mutex* mutex) {
 53	LightLock_Unlock(mutex);
 54	return 0;
 55}
 56
 57static inline int ConditionInit(Condition* cond) {
 58	Result res = MutexInit(&cond->mutex);
 59	if (res) {
 60		return res;
 61	}
 62	res = svcCreateSemaphore(&cond->semaphore, 0, 1);
 63	cond->waiting = 0;
 64	return res;
 65}
 66
 67static inline int ConditionDeinit(Condition* cond) {
 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	bool isNew3DS;
111	APT_CheckNew3DS(&isNew3DS);
112	if (isNew3DS && svcCreateThread(&thread->handle, entry, (u32) context, (u32*) &thread->stack[0x8000], 0x18, 2) == 0) {
113		return 0;
114	}
115	return svcCreateThread(&thread->handle, entry, (u32) context, (u32*) &thread->stack[0x8000], 0x18, -1);
116}
117
118static inline int ThreadJoin(Thread thread) {
119	svcWaitSynchronization(thread.handle, U64_MAX);
120	free(thread.stack);
121	return 0;
122}
123
124static inline void ThreadSetName(const char* name) {
125	UNUSED(name);
126	// Unimplemented
127}
128
129#endif