-
Notifications
You must be signed in to change notification settings - Fork 0
/
threads.c
148 lines (130 loc) · 2.66 KB
/
threads.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
* Copyright (C) 2018, Jason Parker
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*/
#include <stdio.h>
#include "include/threads.h"
#if defined(__cplusplus)
extern "C" {
#endif
int hl_thread_create(hl_thread_t *thread, void *func, void *args) {
#if defined(_WIN32)
if (!(*thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)func, args, 0, NULL))) {
return -1;
}
return 0;
#else
return pthread_create(thread, NULL, func, args);
#endif
}
void hl_thread_destroy(hl_thread_t thread) {
#if defined(_WIN32)
CloseHandle(thread);
#else
pthread_exit(NULL);
#endif
}
int hl_thread_join(hl_thread_t thread) {
#if defined(_WIN32)
switch (WaitForSingleObject(thread, INFINITE)) {
case WAIT_OBJECT_0:
return 0;
default:
return -1;
}
#else
return pthread_join(thread, NULL);
#endif
}
void hl_thread_exit(void) {
#if defined(_WIN32)
WORD ret = 0;
ExitThread(ret);
#else
pthread_exit(NULL);
#endif
}
int hl_mutex_lock(hl_mutex_t *mutex) {
#if defined(_WIN32)
DWORD result = WaitForSingleObject(*mutex, INFINITE);
switch (result) {
case WAIT_OBJECT_0:
return 0;
default:
return 1;
}
#else
return pthread_mutex_lock(mutex);
#endif
}
int hl_mutex_unlock(hl_mutex_t *mutex) {
#if defined(_WIN32)
if (ReleaseMutex(*mutex)) {
return 0;
}
return 1;
#else
return pthread_mutex_unlock(mutex);
#endif
}
void hl_mutex_create(hl_mutex_t *mutex) {
#if defined(_WIN32)
*mutex = CreateMutex(NULL, FALSE, NULL);
#else
pthread_mutex_init(mutex, NULL);
#endif
}
int hl_condlock_lock(hl_condlock_t *condlock) {
#if defined(_WIN32)
EnterCriticalSection(condlock);
return 0;
#else
return hl_mutex_lock(condlock);
#endif
}
int hl_condlock_unlock(hl_condlock_t *condlock) {
#if defined(_WIN32)
LeaveCriticalSection(condlock);
return 0;
#else
return hl_mutex_unlock(condlock);
#endif
}
void hl_condlock_create(hl_condlock_t *condlock) {
#if defined(_WIN32)
InitializeCriticalSection(condlock);
#else
hl_mutex_create(condlock);
#endif
}
int hl_cond_create(hl_cond_t *cond) {
#if defined(_WIN32)
InitializeConditionVariable(cond);
return 0;
#else
return pthread_cond_init(cond, NULL);
#endif
}
int hl_cond_wait(hl_cond_t *cond, hl_condlock_t *condlock) {
#if defined(_WIN32)
SleepConditionVariableCS(cond, condlock, INFINITE);
return 0;
#else
return pthread_cond_wait(cond, condlock);
#endif
}
int hl_cond_signal(hl_cond_t *cond) {
#if defined(_WIN32)
WakeConditionVariable(cond);
return 0;
#else
return pthread_cond_signal(cond);
#endif
}
#if defined(__cplusplus)
}
#endif