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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
/* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <pthread.h>
#include <semaphore.h>
#include <stdlib.h>
#include <stdint.h>
#include <errno.h>
#include <sys/time.h>
int pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
{
BOOL pending;
if (!InitOnceBeginInitialize(once_control, 0, &pending, NULL))
abort();
if (pending) {
init_routine();
InitOnceComplete(once_control, 0, NULL);
}
return 0;
}
int pthread_mutex_destroy(pthread_mutex_t *mutex)
{
if (mutex->use_cs)
DeleteCriticalSection(&mutex->lock.cs);
return 0;
}
int pthread_mutex_init(pthread_mutex_t *restrict mutex,
const pthread_mutexattr_t *restrict attr)
{
mutex->use_cs = attr && (*attr & PTHREAD_MUTEX_RECURSIVE);
if (mutex->use_cs) {
InitializeCriticalSection(&mutex->lock.cs);
} else {
InitializeSRWLock(&mutex->lock.srw);
}
return 0;
}
int pthread_mutex_lock(pthread_mutex_t *mutex)
{
if (mutex->use_cs) {
EnterCriticalSection(&mutex->lock.cs);
} else {
AcquireSRWLockExclusive(&mutex->lock.srw);
}
return 0;
}
int pthread_mutex_unlock(pthread_mutex_t *mutex)
{
if (mutex->use_cs) {
LeaveCriticalSection(&mutex->lock.cs);
} else {
ReleaseSRWLockExclusive(&mutex->lock.srw);
}
return 0;
}
static int cond_wait(pthread_cond_t *restrict cond,
pthread_mutex_t *restrict mutex,
DWORD ms)
{
BOOL res;
if (mutex->use_cs) {
res = SleepConditionVariableCS(cond, &mutex->lock.cs, ms);
} else {
res = SleepConditionVariableSRW(cond, &mutex->lock.srw, ms, 0);
}
return res ? 0 : ETIMEDOUT;
}
int pthread_cond_timedwait(pthread_cond_t *restrict cond,
pthread_mutex_t *restrict mutex,
const struct timespec *restrict abstime)
{
// mpv uses mingw's gettimeofday() as time source too.
struct timeval tv;
gettimeofday(&tv, NULL);
DWORD timeout_ms = 0;
if (abstime->tv_sec >= INT64_MAX / 10000) {
timeout_ms = INFINITE;
} else if (abstime->tv_sec >= tv.tv_sec) {
long long msec = (abstime->tv_sec - tv.tv_sec) * 1000LL +
abstime->tv_nsec / 1000LL / 1000LL - tv.tv_usec / 1000LL;
if (msec > INT_MAX) {
timeout_ms = INFINITE;
} else if (msec > 0) {
timeout_ms = msec;
}
}
return cond_wait(cond, mutex, timeout_ms);
}
int pthread_cond_wait(pthread_cond_t *restrict cond,
pthread_mutex_t *restrict mutex)
{
return cond_wait(cond, mutex, INFINITE);
}
struct m_thread_info {
HANDLE handle;
void *(*user_fn)(void *);
void *user_arg;
void *res;
};
// Assuming __thread maps to __declspec(thread)
static __thread struct m_thread_info *self;
pthread_t pthread_self(void)
{
return (pthread_t){GetCurrentThreadId(), self};
}
void pthread_exit(void *retval)
{
if (!self)
abort(); // not started with pthread_create
self->res = retval;
if (!self->handle) {
// detached case
free(self);
self = NULL;
}
ExitThread(0);
}
int pthread_join(pthread_t thread, void **retval)
{
if (!thread.info)
abort(); // not started with pthread_create
HANDLE h = thread.info->handle;
if (!h)
abort(); // thread was detached
WaitForSingleObject(h, INFINITE);
CloseHandle(h);
if (retval)
*retval = thread.info->res;
free(thread.info);
return 0;
}
int pthread_detach(pthread_t thread)
{
if (!pthread_equal(thread, pthread_self()))
abort(); // restriction of this wrapper
if (!thread.info)
abort(); // not started with pthread_create
if (!thread.info->handle)
abort(); // already deatched
CloseHandle(thread.info->handle);
thread.info->handle = NULL;
return 0;
}
static DWORD WINAPI run_thread(LPVOID lpParameter)
{
struct m_thread_info *info = lpParameter;
self = info;
pthread_exit(info->user_fn(info->user_arg));
abort(); // not reached
}
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine) (void *), void *arg)
{
struct m_thread_info *info = calloc(1, sizeof(*info));
if (!info)
return EAGAIN;
info->user_fn = start_routine;
info->user_arg = arg;
HANDLE h = CreateThread(NULL, 0, run_thread, info, CREATE_SUSPENDED, NULL);
if (!h) {
free(info);
return EAGAIN;
}
info->handle = h;
*thread = (pthread_t){GetThreadId(h), info};
ResumeThread(h);
return 0;
}
int sem_init(sem_t *sem, int pshared, unsigned int value)
{
if (pshared)
abort(); // unsupported
pthread_mutex_init(&sem->lock, NULL);
pthread_cond_init(&sem->wakeup, NULL);
sem->value = value;
return 0;
}
int sem_destroy(sem_t *sem)
{
pthread_mutex_destroy(&sem->lock);
pthread_cond_destroy(&sem->wakeup);
return 0;
}
int sem_wait(sem_t *sem)
{
pthread_mutex_lock(&sem->lock);
while (!sem->value)
pthread_cond_wait(&sem->wakeup, &sem->lock);
sem->value -= 1;
pthread_mutex_unlock(&sem->lock);
return 0;
}
int sem_trywait(sem_t *sem)
{
pthread_mutex_lock(&sem->lock);
int r;
if (sem->value > 0) {
sem->value -= 1;
r = 0;
} else {
errno = EAGAIN;
r = -1;
}
pthread_mutex_unlock(&sem->lock);
return r;
}
int sem_timedwait(sem_t *sem, const struct timespec *abs_timeout)
{
pthread_mutex_lock(&sem->lock);
while (!sem->value) {
int err = pthread_cond_timedwait(&sem->wakeup, &sem->lock, abs_timeout);
if (err) {
pthread_mutex_unlock(&sem->lock);
errno = err;
return -1;
}
}
sem->value -= 1;
pthread_mutex_unlock(&sem->lock);
return 0;
}
int sem_post(sem_t *sem)
{
pthread_mutex_lock(&sem->lock);
sem->value += 1;
pthread_cond_broadcast(&sem->wakeup);
pthread_mutex_unlock(&sem->lock);
return 0;
}
|