all repos — mgba @ 57bdbcd91ebf6d7d1fdf6fed0149075759b9e7f7

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#define THREAD_ENTRY void
 15typedef ThreadFunc ThreadEntry;
 16
 17typedef struct {
 18	Handle handle;
 19	u8* stack;
 20} Thread;
 21typedef Handle Mutex;
 22typedef struct {
 23	Mutex mutex;
 24	Handle semaphore;
 25	u32 waiting;
 26} Condition;
 27
 28static inline int MutexInit(Mutex* mutex) {
 29	return svcCreateMutex(mutex, false);
 30}
 31
 32static inline int MutexDeinit(Mutex* mutex) {
 33	return svcCloseHandle(*mutex);
 34}
 35
 36static inline int MutexLock(Mutex* mutex) {
 37	return svcWaitSynchronization(*mutex, U64_MAX);
 38}
 39
 40static inline int MutexUnlock(Mutex* mutex) {
 41	return svcReleaseMutex(*mutex);
 42}
 43
 44static inline int ConditionInit(Condition* cond) {
 45	Result res = svcCreateMutex(&cond->mutex, false);
 46	if (res) {
 47		return res;
 48	}
 49	res = svcCreateSemaphore(&cond->semaphore, 0, 1);
 50	if (res) {
 51		svcCloseHandle(cond->mutex);
 52	}
 53	cond->waiting = 0;
 54	return res;
 55}
 56
 57static inline int ConditionDeinit(Condition* cond) {
 58	svcCloseHandle(cond->mutex);
 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 1;
 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 1;
 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->stack = memalign(8, 0x8000);
 98	if (!thread->stack) {
 99		return 1;
100	}
101	return svcCreateThread(&thread->handle, entry, (u32) context, (u32*) &thread->stack[0x8000], 0x18, -1);
102}
103
104static inline int ThreadJoin(Thread thread) {
105	svcWaitSynchronization(thread.handle, U64_MAX);
106	free(thread.stack);
107	return 0;
108}
109
110static inline void ThreadSetName(const char* name) {
111	UNUSED(name);
112	// Unimplemented
113}
114
115#endif