all repos — mgba @ f6755a6e1b7b0cf2b944cd8ca842746f11d6bf82

mGBA Game Boy Advance Emulator

include/mgba-util/platform/windows/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 WINDOWS_THREADING_H
 7#define WINDOWS_THREADING_H
 8
 9#include <mgba-util/common.h>
10
11#define _WIN32_WINNT 0x0600
12#include <windows.h>
13#define THREAD_ENTRY DWORD WINAPI
14typedef THREAD_ENTRY ThreadEntry(LPVOID);
15
16typedef HANDLE Thread;
17typedef CRITICAL_SECTION Mutex;
18typedef CONDITION_VARIABLE Condition;
19
20static inline int MutexInit(Mutex* mutex) {
21	InitializeCriticalSection(mutex);
22	return GetLastError();
23}
24
25static inline int MutexDeinit(Mutex* mutex) {
26	DeleteCriticalSection(mutex);
27	return GetLastError();
28}
29
30static inline int MutexLock(Mutex* mutex) {
31	EnterCriticalSection(mutex);
32	return GetLastError();
33}
34
35static inline int MutexTryLock(Mutex* mutex) {
36	if (TryEnterCriticalSection(mutex)) {
37		return 0;
38	}
39	return 1;
40}
41
42static inline int MutexUnlock(Mutex* mutex) {
43	LeaveCriticalSection(mutex);
44	return GetLastError();
45}
46
47static inline int ConditionInit(Condition* cond) {
48	InitializeConditionVariable(cond);
49	return GetLastError();
50}
51
52static inline int ConditionDeinit(Condition* cond) {
53	// This is a no-op on Windows
54	UNUSED(cond);
55	return 0;
56}
57
58static inline int ConditionWait(Condition* cond, Mutex* mutex) {
59	SleepConditionVariableCS(cond, mutex, INFINITE);
60	return GetLastError();
61}
62
63static inline int ConditionWaitTimed(Condition* cond, Mutex* mutex, int32_t timeoutMs) {
64	SleepConditionVariableCS(cond, mutex, timeoutMs);
65	return GetLastError();
66}
67
68static inline int ConditionWake(Condition* cond) {
69	WakeAllConditionVariable(cond);
70	return GetLastError();
71}
72
73static inline int ThreadCreate(Thread* thread, ThreadEntry entry, void* context) {
74	*thread = CreateThread(NULL, 0, entry, context, 0, 0);
75	return GetLastError();
76}
77
78static inline int ThreadJoin(Thread* thread) {
79	DWORD error = WaitForSingleObject(*thread, INFINITE);
80	if (error == WAIT_FAILED) {
81		return GetLastError();
82	}
83	return 0;
84}
85
86static inline int ThreadSetName(const char* name) {
87	UNUSED(name);
88	return -1;
89}
90
91#endif