all repos — mgba @ 631b287b3a6ecc66eec765a9cf7987a2e7cfb364

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 MutexTryLock(Mutex* mutex) {
 41	return svcWaitSynchronization(*mutex, 10);
 42}
 43
 44static inline int MutexUnlock(Mutex* mutex) {
 45	return svcReleaseMutex(*mutex);
 46}
 47
 48static inline int ConditionInit(Condition* cond) {
 49	Result res = svcCreateMutex(&cond->mutex, false);
 50	if (res) {
 51		return res;
 52	}
 53	res = svcCreateSemaphore(&cond->semaphore, 0, 1);
 54	if (res) {
 55		svcCloseHandle(cond->mutex);
 56	}
 57	cond->waiting = 0;
 58	return res;
 59}
 60
 61static inline int ConditionDeinit(Condition* cond) {
 62	svcCloseHandle(cond->mutex);
 63	return svcCloseHandle(cond->semaphore);
 64}
 65
 66static inline int ConditionWait(Condition* cond, Mutex* mutex) {
 67	MutexLock(&cond->mutex);
 68	++cond->waiting;
 69	MutexUnlock(mutex);
 70	MutexUnlock(&cond->mutex);
 71	svcWaitSynchronization(cond->semaphore, U64_MAX);
 72	MutexLock(mutex);
 73	return 1;
 74}
 75
 76static inline int ConditionWaitTimed(Condition* cond, Mutex* mutex, int32_t timeoutMs) {
 77	MutexLock(&cond->mutex);
 78	++cond->waiting;
 79	MutexUnlock(mutex);
 80	MutexUnlock(&cond->mutex);
 81	svcWaitSynchronization(cond->semaphore, timeoutMs * 10000000LL);
 82	MutexLock(mutex);
 83	return 1;
 84}
 85
 86static inline int ConditionWake(Condition* cond) {
 87	MutexLock(&cond->mutex);
 88	if (cond->waiting) {
 89		--cond->waiting;
 90		s32 count = 0;
 91		svcReleaseSemaphore(&count, cond->semaphore, 1);
 92	}
 93	MutexUnlock(&cond->mutex);
 94	return 0;
 95}
 96
 97static inline int ThreadCreate(Thread* thread, ThreadEntry entry, void* context) {
 98	if (!entry || !thread) {
 99		return 1;
100	}
101	thread->stack = memalign(8, 0x8000);
102	if (!thread->stack) {
103		return 1;
104	}
105	return svcCreateThread(&thread->handle, entry, (u32) context, (u32*) &thread->stack[0x8000], 0x18, -1);
106}
107
108static inline int ThreadJoin(Thread thread) {
109	svcWaitSynchronization(thread.handle, U64_MAX);
110	free(thread.stack);
111	return 0;
112}
113
114static inline void ThreadSetName(const char* name) {
115	UNUSED(name);
116	// Unimplemented
117}
118
119#endif