src/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 "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 MutexUnlock(Mutex* mutex) {
36 LeaveCriticalSection(mutex);
37 return GetLastError();
38}
39
40static inline int ConditionInit(Condition* cond) {
41 InitializeConditionVariable(cond);
42 return GetLastError();
43}
44
45static inline int ConditionDeinit(Condition* cond) {
46 // This is a no-op on Windows
47 UNUSED(cond);
48 return 0;
49}
50
51static inline int ConditionWait(Condition* cond, Mutex* mutex) {
52 SleepConditionVariableCS(cond, mutex, INFINITE);
53 return GetLastError();
54}
55
56static inline int ConditionWaitTimed(Condition* cond, Mutex* mutex, int32_t timeoutMs) {
57 SleepConditionVariableCS(cond, mutex, timeoutMs);
58 return GetLastError();
59}
60
61static inline int ConditionWake(Condition* cond) {
62 WakeAllConditionVariable(cond);
63 return GetLastError();
64}
65
66static inline int ThreadCreate(Thread* thread, ThreadEntry entry, void* context) {
67 *thread = CreateThread(NULL, 0, entry, context, 0, 0);
68 return GetLastError();
69}
70
71static inline int ThreadJoin(Thread thread) {
72 DWORD error = WaitForSingleObject(thread, INFINITE);
73 if (error == WAIT_FAILED) {
74 return GetLastError();
75 }
76 return 0;
77}
78
79static inline int ThreadSetName(const char* name) {
80 UNUSED(name);
81 return -1;
82}
83
84#endif