|
0 |
/*
|
|
1 |
* Posix Threads library for Microsoft Windows
|
|
2 |
*
|
|
3 |
* Use at own risk, there is no implied warranty to this code.
|
|
4 |
* It uses undocumented features of Microsoft Windows that can change
|
|
5 |
* at any time in the future.
|
|
6 |
*
|
|
7 |
* (C) 2010 Lockless Inc.
|
|
8 |
* All rights reserved.
|
|
9 |
*
|
|
10 |
* Redistribution and use in source and binary forms, with or without modification,
|
|
11 |
* are permitted provided that the following conditions are met:
|
|
12 |
*
|
|
13 |
*
|
|
14 |
* * Redistributions of source code must retain the above copyright notice,
|
|
15 |
* this list of conditions and the following disclaimer.
|
|
16 |
* * Redistributions in binary form must reproduce the above copyright notice,
|
|
17 |
* this list of conditions and the following disclaimer in the documentation
|
|
18 |
* and/or other materials provided with the distribution.
|
|
19 |
* * Neither the name of Lockless Inc. nor the names of its contributors may be
|
|
20 |
* used to endorse or promote products derived from this software without
|
|
21 |
* specific prior written permission.
|
|
22 |
*
|
|
23 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AN
|
|
24 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
25 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
26 |
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
|
27 |
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
28 |
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
29 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
30 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
|
|
31 |
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
|
32 |
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
33 |
*/
|
|
34 |
|
|
35 |
/*
|
|
36 |
* Version 1.0.1 Released 2 Feb 2012
|
|
37 |
* Fixes pthread_barrier_destroy() to wait for threads to exit the barrier.
|
|
38 |
*/
|
|
39 |
|
|
40 |
#include "winpthreads.h"
|
|
41 |
#include <sys/timeb.h>
|
|
42 |
#include <setjmp.h>
|
|
43 |
#include <intrin.h>
|
|
44 |
#include <process.h>
|
|
45 |
|
|
46 |
typedef struct _pthread_cleanup _pthread_cleanup;
|
|
47 |
struct _pthread_cleanup
|
|
48 |
{
|
|
49 |
void (*func)(void *);
|
|
50 |
void *arg;
|
|
51 |
_pthread_cleanup *next;
|
|
52 |
};
|
|
53 |
|
|
54 |
struct _pthread_v
|
|
55 |
{
|
|
56 |
void *ret_arg;
|
|
57 |
void *(* func)(void *);
|
|
58 |
_pthread_cleanup *clean;
|
|
59 |
HANDLE h;
|
|
60 |
int cancelled;
|
|
61 |
unsigned p_state;
|
|
62 |
unsigned keymax;
|
|
63 |
void **keyval;
|
|
64 |
|
|
65 |
jmp_buf jb;
|
|
66 |
};
|
|
67 |
|
|
68 |
volatile long _pthread_cancelling;
|
|
69 |
|
|
70 |
int _pthread_concur;
|
|
71 |
|
|
72 |
/* Will default to zero as needed */
|
|
73 |
pthread_once_t _pthread_tls_once;
|
|
74 |
DWORD _pthread_tls;
|
|
75 |
|
|
76 |
/* Note initializer is zero, so this works */
|
|
77 |
pthread_rwlock_t _pthread_key_lock;
|
|
78 |
unsigned _pthread_key_max;
|
|
79 |
unsigned _pthread_key_sch;
|
|
80 |
void (**_pthread_key_dest)(void *);
|
|
81 |
|
|
82 |
|
|
83 |
#define pthread_cleanup_push(F, A)\
|
|
84 |
{\
|
|
85 |
const _pthread_cleanup _pthread_cup = {(F), (A), pthread_self()->clean};\
|
|
86 |
_ReadWriteBarrier();\
|
|
87 |
pthread_self()->clean = (_pthread_cleanup *) &_pthread_cup;\
|
|
88 |
_ReadWriteBarrier()
|
|
89 |
|
|
90 |
/* Note that if async cancelling is used, then there is a race here */
|
|
91 |
#define pthread_cleanup_pop(E)\
|
|
92 |
(pthread_self()->clean = _pthread_cup.next, (E ? _pthread_cup.func(_pthread_cup.arg) : 0));}
|
|
93 |
|
|
94 |
static void _pthread_once_cleanup(pthread_once_t *o)
|
|
95 |
{
|
|
96 |
*o = 0;
|
|
97 |
}
|
|
98 |
|
|
99 |
int pthread_once(pthread_once_t *o, void (*func)(void))
|
|
100 |
{
|
|
101 |
long state = *o;
|
|
102 |
|
|
103 |
_ReadWriteBarrier();
|
|
104 |
|
|
105 |
while (state != 1)
|
|
106 |
{
|
|
107 |
if (!state)
|
|
108 |
{
|
|
109 |
if (!_InterlockedCompareExchange(o, 2, 0))
|
|
110 |
{
|
|
111 |
/* Success */
|
|
112 |
pthread_cleanup_push((void(*)(void*))_pthread_once_cleanup, o);
|
|
113 |
func();
|
|
114 |
pthread_cleanup_pop(0);
|
|
115 |
|
|
116 |
/* Mark as done */
|
|
117 |
*o = 1;
|
|
118 |
|
|
119 |
return 0;
|
|
120 |
}
|
|
121 |
}
|
|
122 |
|
|
123 |
YieldProcessor();
|
|
124 |
|
|
125 |
_ReadWriteBarrier();
|
|
126 |
|
|
127 |
state = *o;
|
|
128 |
}
|
|
129 |
|
|
130 |
/* Done */
|
|
131 |
return 0;
|
|
132 |
}
|
|
133 |
|
|
134 |
static int _pthread_once_raw(pthread_once_t *o, void (*func)(void))
|
|
135 |
{
|
|
136 |
long state = *o;
|
|
137 |
|
|
138 |
_ReadWriteBarrier();
|
|
139 |
|
|
140 |
while (state != 1)
|
|
141 |
{
|
|
142 |
if (!state)
|
|
143 |
{
|
|
144 |
if (!_InterlockedCompareExchange(o, 2, 0))
|
|
145 |
{
|
|
146 |
/* Success */
|
|
147 |
func();
|
|
148 |
|
|
149 |
/* Mark as done */
|
|
150 |
*o = 1;
|
|
151 |
|
|
152 |
return 0;
|
|
153 |
}
|
|
154 |
}
|
|
155 |
|
|
156 |
YieldProcessor();
|
|
157 |
|
|
158 |
_ReadWriteBarrier();
|
|
159 |
|
|
160 |
state = *o;
|
|
161 |
}
|
|
162 |
|
|
163 |
/* Done */
|
|
164 |
return 0;
|
|
165 |
}
|
|
166 |
|
|
167 |
int pthread_mutex_lock(pthread_mutex_t *m)
|
|
168 |
{
|
|
169 |
EnterCriticalSection(m);
|
|
170 |
return 0;
|
|
171 |
}
|
|
172 |
|
|
173 |
int pthread_mutex_unlock(pthread_mutex_t *m)
|
|
174 |
{
|
|
175 |
LeaveCriticalSection(m);
|
|
176 |
return 0;
|
|
177 |
}
|
|
178 |
|
|
179 |
int pthread_mutex_trylock(pthread_mutex_t *m)
|
|
180 |
{
|
|
181 |
return TryEnterCriticalSection(m) ? 0 : EBUSY;
|
|
182 |
}
|
|
183 |
|
|
184 |
int pthread_mutex_init(pthread_mutex_t *m, pthread_mutexattr_t *a)
|
|
185 |
{
|
|
186 |
(void) a;
|
|
187 |
InitializeCriticalSection(m);
|
|
188 |
|
|
189 |
return 0;
|
|
190 |
}
|
|
191 |
|
|
192 |
int pthread_mutex_destroy(pthread_mutex_t *m)
|
|
193 |
{
|
|
194 |
DeleteCriticalSection(m);
|
|
195 |
return 0;
|
|
196 |
}
|
|
197 |
|
|
198 |
int pthread_equal(pthread_t t1, pthread_t t2)
|
|
199 |
{
|
|
200 |
return t1 == t2;
|
|
201 |
}
|
|
202 |
|
|
203 |
int pthread_rwlock_init(pthread_rwlock_t *l, pthread_rwlockattr_t *a)
|
|
204 |
{
|
|
205 |
(void) a;
|
|
206 |
InitializeSRWLock(l);
|
|
207 |
|
|
208 |
return 0;
|
|
209 |
}
|
|
210 |
|
|
211 |
int pthread_rwlock_destroy(pthread_rwlock_t *l)
|
|
212 |
{
|
|
213 |
(void) *l;
|
|
214 |
return 0;
|
|
215 |
}
|
|
216 |
|
|
217 |
int pthread_rwlock_rdlock(pthread_rwlock_t *l)
|
|
218 |
{
|
|
219 |
pthread_testcancel();
|
|
220 |
AcquireSRWLockShared(l);
|
|
221 |
|
|
222 |
return 0;
|
|
223 |
}
|
|
224 |
|
|
225 |
int pthread_rwlock_wrlock(pthread_rwlock_t *l)
|
|
226 |
{
|
|
227 |
pthread_testcancel();
|
|
228 |
AcquireSRWLockExclusive(l);
|
|
229 |
|
|
230 |
return 0;
|
|
231 |
}
|
|
232 |
|
|
233 |
int pthread_rwlock_unlock(pthread_rwlock_t *l)
|
|
234 |
{
|
|
235 |
void *state = *(void **)l;
|
|
236 |
|
|
237 |
if (state == (void *)1)
|
|
238 |
{
|
|
239 |
/* Known to be an exclusive lock */
|
|
240 |
ReleaseSRWLockExclusive(l);
|
|
241 |
}
|
|
242 |
else
|
|
243 |
{
|
|
244 |
/* A shared unlock will work */
|
|
245 |
ReleaseSRWLockShared(l);
|
|
246 |
}
|
|
247 |
|
|
248 |
return 0;
|
|
249 |
}
|
|
250 |
|
|
251 |
int pthread_rwlock_tryrdlock(pthread_rwlock_t *l)
|
|
252 |
{
|
|
253 |
/* Get the current state of the lock */
|
|
254 |
void *state = *(void **)l;
|
|
255 |
|
|
256 |
if (!state)
|
|
257 |
{
|
|
258 |
/* Unlocked to locked */
|
|
259 |
if (!_InterlockedCompareExchangePointer((void *volatile *)l, (void *)0x11, NULL)) return 0;
|
|
260 |
return EBUSY;
|
|
261 |
}
|
|
262 |
|
|
263 |
/* A single writer exists */
|
|
264 |
if (state == (void *)1) return EBUSY;
|
|
265 |
|
|
266 |
/* Multiple writers exist? */
|
|
267 |
if ((uintptr_t)state & 14) return EBUSY;
|
|
268 |
|
|
269 |
if (_InterlockedCompareExchangePointer((void *volatile *)l, (void *)((uintptr_t)state + 16), state) == state) return 0;
|
|
270 |
|
|
271 |
return EBUSY;
|
|
272 |
}
|
|
273 |
|
|
274 |
int pthread_rwlock_trywrlock(pthread_rwlock_t *l)
|
|
275 |
{
|
|
276 |
/* Try to grab lock if it has no users */
|
|
277 |
if (!_InterlockedCompareExchangePointer((void *volatile *)l, (void *)1, NULL)) return 0;
|
|
278 |
|
|
279 |
return EBUSY;
|
|
280 |
}
|
|
281 |
|
|
282 |
void pthread_tls_init(void)
|
|
283 |
{
|
|
284 |
_pthread_tls = TlsAlloc();
|
|
285 |
|
|
286 |
/* Cannot continue if out of indexes */
|
|
287 |
if (_pthread_tls == TLS_OUT_OF_INDEXES) abort();
|
|
288 |
}
|
|
289 |
|
|
290 |
static void _pthread_cleanup_dest(pthread_t t)
|
|
291 |
{
|
|
292 |
unsigned i, j;
|
|
293 |
|
|
294 |
for (j = 0; j < PTHREAD_DESTRUCTOR_ITERATIONS; j++)
|
|
295 |
{
|
|
296 |
int flag = 0;
|
|
297 |
|
|
298 |
for (i = 0; i < t->keymax; i++)
|
|
299 |
{
|
|
300 |
void *val = t->keyval[i];
|
|
301 |
|
|
302 |
if (val)
|
|
303 |
{
|
|
304 |
pthread_rwlock_rdlock(&_pthread_key_lock);
|
|
305 |
if ((uintptr_t) _pthread_key_dest[i] > 1)
|
|
306 |
{
|
|
307 |
/* Call destructor */
|
|
308 |
t->keyval[i] = NULL;
|
|
309 |
_pthread_key_dest[i](val);
|
|
310 |
flag = 1;
|
|
311 |
}
|
|
312 |
pthread_rwlock_unlock(&_pthread_key_lock);
|
|
313 |
}
|
|
314 |
}
|
|
315 |
|
|
316 |
/* Nothing to do? */
|
|
317 |
if (!flag) return;
|
|
318 |
}
|
|
319 |
}
|
|
320 |
|
|
321 |
pthread_t pthread_self(void)
|
|
322 |
{
|
|
323 |
pthread_t t;
|
|
324 |
|
|
325 |
_pthread_once_raw(&_pthread_tls_once, pthread_tls_init);
|
|
326 |
|
|
327 |
t = (struct _pthread_v*)TlsGetValue(_pthread_tls);
|
|
328 |
/* Main thread? */
|
|
329 |
if (!t)
|
|
330 |
{
|
|
331 |
t = (struct _pthread_v*)malloc(sizeof(struct _pthread_v));
|
|
332 |
|
|
333 |
/* If cannot initialize main thread, then the only thing we can do is abort */
|
|
334 |
if (!t) abort();
|
|
335 |
|
|
336 |
t->ret_arg = NULL;
|
|
337 |
t->func = NULL;
|
|
338 |
t->clean = NULL;
|
|
339 |
t->cancelled = 0;
|
|
340 |
t->p_state = PTHREAD_DEFAULT_ATTR;
|
|
341 |
t->keymax = 0;
|
|
342 |
t->keyval = NULL;
|
|
343 |
t->h = GetCurrentThread();
|
|
344 |
|
|
345 |
/* Save for later */
|
|
346 |
TlsSetValue(_pthread_tls, t);
|
|
347 |
|
|
348 |
if (setjmp(t->jb))
|
|
349 |
{
|
|
350 |
/* Make sure we free ourselves if we are detached */
|
|
351 |
if (!t->h) free(t);
|
|
352 |
|
|
353 |
/* Time to die */
|
|
354 |
_endthreadex(0);
|
|
355 |
}
|
|
356 |
}
|
|
357 |
|
|
358 |
return t;
|
|
359 |
}
|
|
360 |
|
|
361 |
static unsigned long long _pthread_time_in_ms(void)
|
|
362 |
{
|
|
363 |
struct __timeb64 tb;
|
|
364 |
|
|
365 |
_ftime64(&tb);
|
|
366 |
|
|
367 |
return tb.time * 1000 + tb.millitm;
|
|
368 |
}
|
|
369 |
|
|
370 |
static unsigned long long _pthread_time_in_ms_from_timespec(const struct timespec *ts)
|
|
371 |
{
|
|
372 |
unsigned long long t = ts->tv_sec * 1000;
|
|
373 |
t += ts->tv_nsec / 1000000;
|
|
374 |
|
|
375 |
return t;
|
|
376 |
}
|
|
377 |
|
|
378 |
static unsigned long long _pthread_rel_time_in_ms(const struct timespec *ts)
|
|
379 |
{
|
|
380 |
unsigned long long t1 = _pthread_time_in_ms_from_timespec(ts);
|
|
381 |
unsigned long long t2 = _pthread_time_in_ms();
|
|
382 |
|
|
383 |
/* Prevent underflow */
|
|
384 |
if (t1 < t2) return 0;
|
|
385 |
return t1 - t2;
|
|
386 |
}
|
|
387 |
|
|
388 |
int pthread_rwlock_timedrdlock(pthread_rwlock_t *l, const struct timespec *ts)
|
|
389 |
{
|
|
390 |
unsigned long long ct = _pthread_time_in_ms();
|
|
391 |
unsigned long long t = _pthread_time_in_ms_from_timespec(ts);
|
|
392 |
|
|
393 |
pthread_testcancel();
|
|
394 |
|
|
395 |
/* Use a busy-loop */
|
|
396 |
while (1)
|
|
397 |
{
|
|
398 |
/* Try to grab lock */
|
|
399 |
if (!pthread_rwlock_tryrdlock(l)) return 0;
|
|
400 |
|
|
401 |
/* Get current time */
|
|
402 |
ct = _pthread_time_in_ms();
|
|
403 |
|
|
404 |
/* Have we waited long enough? */
|
|
405 |
if (ct > t) return ETIMEDOUT;
|
|
406 |
}
|
|
407 |
}
|
|
408 |
|
|
409 |
int pthread_rwlock_timedwrlock(pthread_rwlock_t *l, const struct timespec *ts)
|
|
410 |
{
|
|
411 |
unsigned long long ct = _pthread_time_in_ms();
|
|
412 |
unsigned long long t = _pthread_time_in_ms_from_timespec(ts);
|
|
413 |
|
|
414 |
pthread_testcancel();
|
|
415 |
|
|
416 |
/* Use a busy-loop */
|
|
417 |
while (1)
|
|
418 |
{
|
|
419 |
/* Try to grab lock */
|
|
420 |
if (!pthread_rwlock_trywrlock(l)) return 0;
|
|
421 |
|
|
422 |
/* Get current time */
|
|
423 |
ct = _pthread_time_in_ms();
|
|
424 |
|
|
425 |
/* Have we waited long enough? */
|
|
426 |
if (ct > t) return ETIMEDOUT;
|
|
427 |
}
|
|
428 |
}
|
|
429 |
|
|
430 |
int pthread_get_concurrency(int *val)
|
|
431 |
{
|
|
432 |
*val = _pthread_concur;
|
|
433 |
return 0;
|
|
434 |
}
|
|
435 |
|
|
436 |
int pthread_set_concurrency(int val)
|
|
437 |
{
|
|
438 |
_pthread_concur = val;
|
|
439 |
return 0;
|
|
440 |
}
|
|
441 |
|
|
442 |
int pthread_exit(void *res)
|
|
443 |
{
|
|
444 |
pthread_t t = pthread_self();
|
|
445 |
|
|
446 |
t->ret_arg = res;
|
|
447 |
|
|
448 |
_pthread_cleanup_dest(t);
|
|
449 |
|
|
450 |
longjmp(t->jb, 1);
|
|
451 |
}
|
|
452 |
|
|
453 |
|
|
454 |
static void _pthread_invoke_cancel(void)
|
|
455 |
{
|
|
456 |
_pthread_cleanup *pcup;
|
|
457 |
|
|
458 |
_InterlockedDecrement(&_pthread_cancelling);
|
|
459 |
|
|
460 |
/* Call cancel queue */
|
|
461 |
for (pcup = pthread_self()->clean; pcup; pcup = pcup->next)
|
|
462 |
{
|
|
463 |
pcup->func(pcup->arg);
|
|
464 |
}
|
|
465 |
|
|
466 |
pthread_exit(PTHREAD_CANCELED);
|
|
467 |
}
|
|
468 |
|
|
469 |
void pthread_testcancel(void)
|
|
470 |
{
|
|
471 |
if (_pthread_cancelling)
|
|
472 |
{
|
|
473 |
pthread_t t = pthread_self();
|
|
474 |
|
|
475 |
if (t->cancelled && (t->p_state & PTHREAD_CANCEL_ENABLE))
|
|
476 |
{
|
|
477 |
_pthread_invoke_cancel();
|
|
478 |
}
|
|
479 |
}
|
|
480 |
}
|
|
481 |
|
|
482 |
|
|
483 |
int pthread_cancel(pthread_t t)
|
|
484 |
{
|
|
485 |
#if !defined(WINAPI_FAMILY) || (WINAPI_FAMILY != WINAPI_FAMILY_APP)
|
|
486 |
if (t->p_state & PTHREAD_CANCEL_ASYNCHRONOUS)
|
|
487 |
{
|
|
488 |
/* Dangerous asynchronous cancelling */
|
|
489 |
CONTEXT ctxt;
|
|
490 |
|
|
491 |
/* Already done? */
|
|
492 |
if (t->cancelled) return ESRCH;
|
|
493 |
|
|
494 |
ctxt.ContextFlags = CONTEXT_CONTROL;
|
|
495 |
|
|
496 |
SuspendThread(t->h);
|
|
497 |
GetThreadContext(t->h, &ctxt);
|
|
498 |
#ifdef _M_X64
|
|
499 |
ctxt.Rip = (uintptr_t) _pthread_invoke_cancel;
|
|
500 |
#else
|
|
501 |
ctxt.Eip = (uintptr_t) _pthread_invoke_cancel;
|
|
502 |
#endif
|
|
503 |
SetThreadContext(t->h, &ctxt);
|
|
504 |
|
|
505 |
/* Also try deferred Cancelling */
|
|
506 |
t->cancelled = 1;
|
|
507 |
|
|
508 |
/* Notify everyone to look */
|
|
509 |
_InterlockedIncrement(&_pthread_cancelling);
|
|
510 |
|
|
511 |
ResumeThread(t->h);
|
|
512 |
}
|
|
513 |
else
|
|
514 |
#endif
|
|
515 |
{
|
|
516 |
/* Safe deferred Cancelling */
|
|
517 |
t->cancelled = 1;
|
|
518 |
|
|
519 |
/* Notify everyone to look */
|
|
520 |
_InterlockedIncrement(&_pthread_cancelling);
|
|
521 |
}
|
|
522 |
|
|
523 |
return 0;
|
|
524 |
}
|
|
525 |
|
|
526 |
static unsigned _pthread_get_state(pthread_attr_t *attr, unsigned flag)
|
|
527 |
{
|
|
528 |
return attr->p_state & flag;
|
|
529 |
}
|
|
530 |
|
|
531 |
static int _pthread_set_state(pthread_attr_t *attr, unsigned flag, unsigned val)
|
|
532 |
{
|
|
533 |
if (~flag & val) return EINVAL;
|
|
534 |
attr->p_state &= ~flag;
|
|
535 |
attr->p_state |= val;
|
|
536 |
|
|
537 |
return 0;
|
|
538 |
}
|
|
539 |
|
|
540 |
int pthread_attr_init(pthread_attr_t *attr)
|
|
541 |
{
|
|
542 |
attr->p_state = PTHREAD_DEFAULT_ATTR;
|
|
543 |
attr->stack = NULL;
|
|
544 |
attr->s_size = 0;
|
|
545 |
return 0;
|
|
546 |
}
|
|
547 |
|
|
548 |
int pthread_attr_destroy(pthread_attr_t *attr)
|
|
549 |
{
|
|
550 |
/* No need to do anything */
|
|
551 |
return 0;
|
|
552 |
}
|
|
553 |
|
|
554 |
|
|
555 |
int pthread_attr_setdetachstate(pthread_attr_t *a, int flag)
|
|
556 |
{
|
|
557 |
return _pthread_set_state(a, PTHREAD_CREATE_DETACHED, flag);
|
|
558 |
}
|
|
559 |
|
|
560 |
int pthread_attr_getdetachstate(pthread_attr_t *a, int *flag)
|
|
561 |
{
|
|
562 |
*flag = _pthread_get_state(a, PTHREAD_CREATE_DETACHED);
|
|
563 |
return 0;
|
|
564 |
}
|
|
565 |
|
|
566 |
int pthread_attr_setinheritsched(pthread_attr_t *a, int flag)
|
|
567 |
{
|
|
568 |
return _pthread_set_state(a, PTHREAD_INHERIT_SCHED, flag);
|
|
569 |
}
|
|
570 |
|
|
571 |
int pthread_attr_getinheritsched(pthread_attr_t *a, int *flag)
|
|
572 |
{
|
|
573 |
*flag = _pthread_get_state(a, PTHREAD_INHERIT_SCHED);
|
|
574 |
return 0;
|
|
575 |
}
|
|
576 |
|
|
577 |
int pthread_attr_setscope(pthread_attr_t *a, int flag)
|
|
578 |
{
|
|
579 |
return _pthread_set_state(a, PTHREAD_SCOPE_SYSTEM, flag);
|
|
580 |
}
|
|
581 |
|
|
582 |
int pthread_attr_getscope(pthread_attr_t *a, int *flag)
|
|
583 |
{
|
|
584 |
*flag = _pthread_get_state(a, PTHREAD_SCOPE_SYSTEM);
|
|
585 |
return 0;
|
|
586 |
}
|
|
587 |
|
|
588 |
int pthread_attr_getstackaddr(pthread_attr_t *attr, void **stack)
|
|
589 |
{
|
|
590 |
*stack = attr->stack;
|
|
591 |
return 0;
|
|
592 |
}
|
|
593 |
|
|
594 |
int pthread_attr_setstackaddr(pthread_attr_t *attr, void *stack)
|
|
595 |
{
|
|
596 |
attr->stack = stack;
|
|
597 |
return 0;
|
|
598 |
}
|
|
599 |
|
|
600 |
int pthread_attr_getstacksize(pthread_attr_t *attr, size_t *size)
|
|
601 |
{
|
|
602 |
*size = attr->s_size;
|
|
603 |
return 0;
|
|
604 |
}
|
|
605 |
|
|
606 |
int pthread_attr_setstacksize(pthread_attr_t *attr, size_t size)
|
|
607 |
{
|
|
608 |
attr->s_size = size;
|
|
609 |
return 0;
|
|
610 |
}
|
|
611 |
|
|
612 |
#define pthread_attr_getguardsize(A, S) ENOTSUP
|
|
613 |
#define pthread_attr_setgaurdsize(A, S) ENOTSUP
|
|
614 |
#define pthread_attr_getschedparam(A, S) ENOTSUP
|
|
615 |
#define pthread_attr_setschedparam(A, S) ENOTSUP
|
|
616 |
#define pthread_attr_getschedpolicy(A, S) ENOTSUP
|
|
617 |
#define pthread_attr_setschedpolicy(A, S) ENOTSUP
|
|
618 |
|
|
619 |
|
|
620 |
int pthread_setcancelstate(int state, int *oldstate)
|
|
621 |
{
|
|
622 |
pthread_t t = pthread_self();
|
|
623 |
|
|
624 |
if ((state & PTHREAD_CANCEL_ENABLE) != state) return EINVAL;
|
|
625 |
if (oldstate) *oldstate = t->p_state & PTHREAD_CANCEL_ENABLE;
|
|
626 |
t->p_state &= ~PTHREAD_CANCEL_ENABLE;
|
|
627 |
t->p_state |= state;
|
|
628 |
|
|
629 |
return 0;
|
|
630 |
}
|
|
631 |
|
|
632 |
int pthread_setcanceltype(int type, int *oldtype)
|
|
633 |
{
|
|
634 |
pthread_t t = pthread_self();
|
|
635 |
|
|
636 |
if ((type & PTHREAD_CANCEL_ASYNCHRONOUS) != type) return EINVAL;
|
|
637 |
if (oldtype) *oldtype = t->p_state & PTHREAD_CANCEL_ASYNCHRONOUS;
|
|
638 |
t->p_state &= ~PTHREAD_CANCEL_ASYNCHRONOUS;
|
|
639 |
t->p_state |= type;
|
|
640 |
|
|
641 |
return 0;
|
|
642 |
}
|
|
643 |
|
|
644 |
unsigned __stdcall pthread_create_wrapper(void *args)
|
|
645 |
{
|
|
646 |
struct _pthread_v *tv = (struct _pthread_v*)args;
|
|
647 |
|
|
648 |
_pthread_once_raw(&_pthread_tls_once, pthread_tls_init);
|
|
649 |
|
|
650 |
TlsSetValue(_pthread_tls, tv);
|
|
651 |
|
|
652 |
if (!setjmp(tv->jb))
|
|
653 |
{
|
|
654 |
/* Call function and save return value */
|
|
655 |
tv->ret_arg = tv->func(tv->ret_arg);
|
|
656 |
|
|
657 |
/* Clean up destructors */
|
|
658 |
_pthread_cleanup_dest(tv);
|
|
659 |
}
|
|
660 |
|
|
661 |
/* If we exit too early, then we can race with create */
|
|
662 |
while (tv->h == (HANDLE) -1)
|
|
663 |
{
|
|
664 |
YieldProcessor();
|
|
665 |
_ReadWriteBarrier();
|
|
666 |
}
|
|
667 |
|
|
668 |
/* Make sure we free ourselves if we are detached */
|
|
669 |
if (!tv->h) free(tv);
|
|
670 |
|
|
671 |
return 0;
|
|
672 |
}
|
|
673 |
|
|
674 |
int pthread_create(pthread_t *th, pthread_attr_t *attr, void *(* func)(void *), void *arg)
|
|
675 |
{
|
|
676 |
struct _pthread_v *tv = (struct _pthread_v*)malloc(sizeof(struct _pthread_v));
|
|
677 |
unsigned ssize = 0;
|
|
678 |
|
|
679 |
if (!tv) return 1;
|
|
680 |
|
|
681 |
*th = tv;
|
|
682 |
|
|
683 |
/* Save data in pthread_t */
|
|
684 |
tv->ret_arg = arg;
|
|
685 |
tv->func = func;
|
|
686 |
tv->clean = NULL;
|
|
687 |
tv->cancelled = 0;
|
|
688 |
tv->p_state = PTHREAD_DEFAULT_ATTR;
|
|
689 |
tv->keymax = 0;
|
|
690 |
tv->keyval = NULL;
|
|
691 |
tv->h = (HANDLE) -1;
|
|
692 |
|
|
693 |
if (attr)
|
|
694 |
{
|
|
695 |
tv->p_state = attr->p_state;
|
|
696 |
ssize = (unsigned) attr->s_size;
|
|
697 |
}
|
|
698 |
|
|
699 |
/* Make sure tv->h has value of -1 */
|
|
700 |
_ReadWriteBarrier();
|
|
701 |
|
|
702 |
tv->h = (HANDLE) _beginthreadex(NULL, ssize, pthread_create_wrapper, tv, 0, NULL);
|
|
703 |
|
|
704 |
/* Failed */
|
|
705 |
if (!tv->h) return 1;
|
|
706 |
|
|
707 |
if (tv->p_state & PTHREAD_CREATE_DETACHED)
|
|
708 |
{
|
|
709 |
CloseHandle(tv->h);
|
|
710 |
_ReadWriteBarrier();
|
|
711 |
tv->h = 0;
|
|
712 |
}
|
|
713 |
|
|
714 |
return 0;
|
|
715 |
}
|
|
716 |
|
|
717 |
int pthread_join(pthread_t t, void **res)
|
|
718 |
{
|
|
719 |
struct _pthread_v *tv = t;
|
|
720 |
|
|
721 |
pthread_testcancel();
|
|
722 |
|
|
723 |
WaitForSingleObject(tv->h, INFINITE);
|
|
724 |
CloseHandle(tv->h);
|
|
725 |
|
|
726 |
/* Obtain return value */
|
|
727 |
if (res) *res = tv->ret_arg;
|
|
728 |
|
|
729 |
free(tv);
|
|
730 |
|
|
731 |
return 0;
|
|
732 |
}
|
|
733 |
|
|
734 |
int pthread_detach(pthread_t t)
|
|
735 |
{
|
|
736 |
struct _pthread_v *tv = t;
|
|
737 |
|
|
738 |
/*
|
|
739 |
* This can't race with thread exit because
|
|
740 |
* our call would be undefined if called on a dead thread.
|
|
741 |
*/
|
|
742 |
|
|
743 |
CloseHandle(tv->h);
|
|
744 |
_ReadWriteBarrier();
|
|
745 |
tv->h = 0;
|
|
746 |
|
|
747 |
return 0;
|
|
748 |
}
|
|
749 |
|
|
750 |
int pthread_mutexattr_init(pthread_mutexattr_t *a)
|
|
751 |
{
|
|
752 |
*a = 0;
|
|
753 |
return 0;
|
|
754 |
}
|
|
755 |
|
|
756 |
int pthread_mutexattr_destroy(pthread_mutexattr_t *a)
|
|
757 |
{
|
|
758 |
(void) a;
|
|
759 |
return 0;
|
|
760 |
}
|
|
761 |
|
|
762 |
int pthread_mutexattr_gettype(pthread_mutexattr_t *a, int *type)
|
|
763 |
{
|
|
764 |
*type = *a & 3;
|
|
765 |
|
|
766 |
return 0;
|
|
767 |
}
|
|
768 |
|
|
769 |
int pthread_mutexattr_settype(pthread_mutexattr_t *a, int type)
|
|
770 |
{
|
|
771 |
if ((unsigned) type > 3) return EINVAL;
|
|
772 |
*a &= ~3;
|
|
773 |
*a |= type;
|
|
774 |
|
|
775 |
return 0;
|
|
776 |
}
|
|
777 |
|
|
778 |
int pthread_mutexattr_getpshared(pthread_mutexattr_t *a, int *type)
|
|
779 |
{
|
|
780 |
*type = *a & 4;
|
|
781 |
|
|
782 |
return 0;
|
|
783 |
}
|
|
784 |
|
|
785 |
int pthread_mutexattr_setpshared(pthread_mutexattr_t * a, int type)
|
|
786 |
{
|
|
787 |
if ((type & 4) != type) return EINVAL;
|
|
788 |
|
|
789 |
*a &= ~4;
|
|
790 |
*a |= type;
|
|
791 |
|
|
792 |
return 0;
|
|
793 |
}
|
|
794 |
|
|
795 |
int pthread_mutexattr_getprotocol(pthread_mutexattr_t *a, int *type)
|
|
796 |
{
|
|
797 |
*type = *a & (8 + 16);
|
|
798 |
|
|
799 |
return 0;
|
|
800 |
}
|
|
801 |
|
|
802 |
int pthread_mutexattr_setprotocol(pthread_mutexattr_t *a, int type)
|
|
803 |
{
|
|
804 |
if ((type & (8 + 16)) != 8 + 16) return EINVAL;
|
|
805 |
|
|
806 |
*a &= ~(8 + 16);
|
|
807 |
*a |= type;
|
|
808 |
|
|
809 |
return 0;
|
|
810 |
}
|
|
811 |
|
|
812 |
int pthread_mutexattr_getprioceiling(pthread_mutexattr_t *a, int * prio)
|
|
813 |
{
|
|
814 |
*prio = *a / PTHREAD_PRIO_MULT;
|
|
815 |
return 0;
|
|
816 |
}
|
|
817 |
|
|
818 |
int pthread_mutexattr_setprioceiling(pthread_mutexattr_t *a, int prio)
|
|
819 |
{
|
|
820 |
*a &= (PTHREAD_PRIO_MULT - 1);
|
|
821 |
*a += prio * PTHREAD_PRIO_MULT;
|
|
822 |
|
|
823 |
return 0;
|
|
824 |
}
|
|
825 |
|
|
826 |
int pthread_mutex_timedlock(pthread_mutex_t *m, struct timespec *ts)
|
|
827 |
{
|
|
828 |
unsigned long long t, ct;
|
|
829 |
|
|
830 |
struct _pthread_crit_t
|
|
831 |
{
|
|
832 |
void *debug;
|
|
833 |
LONG count;
|
|
834 |
LONG r_count;
|
|
835 |
HANDLE owner;
|
|
836 |
HANDLE sem;
|
|
837 |
ULONG_PTR spin;
|
|
838 |
};
|
|
839 |
|
|
840 |
/* Try to lock it without waiting */
|
|
841 |
if (!pthread_mutex_trylock(m)) return 0;
|
|
842 |
|
|
843 |
ct = _pthread_time_in_ms();
|
|
844 |
t = _pthread_time_in_ms_from_timespec(ts);
|
|
845 |
|
|
846 |
while (1)
|
|
847 |
{
|
|
848 |
/* Have we waited long enough? */
|
|
849 |
if (ct > t) return ETIMEDOUT;
|
|
850 |
|
|
851 |
/* Wait on semaphore within critical section */
|
|
852 |
WaitForSingleObject(((struct _pthread_crit_t *)m)->sem, (DWORD)(t - ct));
|
|
853 |
|
|
854 |
/* Try to grab lock */
|
|
855 |
if (!pthread_mutex_trylock(m)) return 0;
|
|
856 |
|
|
857 |
/* Get current time */
|
|
858 |
ct = _pthread_time_in_ms();
|
|
859 |
}
|
|
860 |
}
|
|
861 |
|
|
862 |
#define _PTHREAD_BARRIER_FLAG (1<<30)
|
|
863 |
|
|
864 |
int pthread_barrier_destroy(pthread_barrier_t *b)
|
|
865 |
{
|
|
866 |
EnterCriticalSection(&b->m);
|
|
867 |
|
|
868 |
while (b->total > _PTHREAD_BARRIER_FLAG)
|
|
869 |
{
|
|
870 |
/* Wait until everyone exits the barrier */
|
|
871 |
SleepConditionVariableCS(&b->cv, &b->m, INFINITE);
|
|
872 |
}
|
|
873 |
|
|
874 |
LeaveCriticalSection(&b->m);
|
|
875 |
|
|
876 |
DeleteCriticalSection(&b->m);
|
|
877 |
|
|
878 |
return 0;
|
|
879 |
}
|
|
880 |
|
|
881 |
int pthread_barrier_init(pthread_barrier_t *b, void *attr, int count)
|
|
882 |
{
|
|
883 |
/* Ignore attr */
|
|
884 |
(void) attr;
|
|
885 |
|
|
886 |
b->count = count;
|
|
887 |
b->total = 0;
|
|
888 |
|
|
889 |
InitializeCriticalSection(&b->m);
|
|
890 |
InitializeConditionVariable(&b->cv);
|
|
891 |
|
|
892 |
return 0;
|
|
893 |
}
|
|
894 |
|
|
895 |
int pthread_barrier_wait(pthread_barrier_t *b)
|
|
896 |
{
|
|
897 |
EnterCriticalSection(&b->m);
|
|
898 |
|
|
899 |
while (b->total > _PTHREAD_BARRIER_FLAG)
|
|
900 |
{
|
|
901 |
/* Wait until everyone exits the barrier */
|
|
902 |
SleepConditionVariableCS(&b->cv, &b->m, INFINITE);
|
|
903 |
}
|
|
904 |
|
|
905 |
/* Are we the first to enter? */
|
|
906 |
if (b->total == _PTHREAD_BARRIER_FLAG) b->total = 0;
|
|
907 |
|
|
908 |
b->total++;
|
|
909 |
|
|
910 |
if (b->total == b->count)
|
|
911 |
{
|
|
912 |
b->total += _PTHREAD_BARRIER_FLAG - 1;
|
|
913 |
WakeAllConditionVariable(&b->cv);
|
|
914 |
|
|
915 |
LeaveCriticalSection(&b->m);
|
|
916 |
|
|
917 |
return 1;
|
|
918 |
}
|
|
919 |
else
|
|
920 |
{
|
|
921 |
while (b->total < _PTHREAD_BARRIER_FLAG)
|
|
922 |
{
|
|
923 |
/* Wait until enough threads enter the barrier */
|
|
924 |
SleepConditionVariableCS(&b->cv, &b->m, INFINITE);
|
|
925 |
}
|
|
926 |
|
|
927 |
b->total--;
|
|
928 |
|
|
929 |
/* Get entering threads to wake up */
|
|
930 |
if (b->total == _PTHREAD_BARRIER_FLAG) WakeAllConditionVariable(&b->cv);
|
|
931 |
|
|
932 |
LeaveCriticalSection(&b->m);
|
|
933 |
|
|
934 |
return 0;
|
|
935 |
}
|
|
936 |
}
|
|
937 |
|
|
938 |
int pthread_barrierattr_init(void **attr)
|
|
939 |
{
|
|
940 |
*attr = NULL;
|
|
941 |
return 0;
|
|
942 |
}
|
|
943 |
|
|
944 |
int pthread_barrierattr_destroy(void **attr)
|
|
945 |
{
|
|
946 |
/* Ignore attr */
|
|
947 |
(void) attr;
|
|
948 |
|
|
949 |
return 0;
|
|
950 |
}
|
|
951 |
|
|
952 |
int pthread_barrierattr_setpshared(void **attr, int s)
|
|
953 |
{
|
|
954 |
*attr = (void *) s;
|
|
955 |
return 0;
|
|
956 |
}
|
|
957 |
|
|
958 |
int pthread_barrierattr_getpshared(void **attr, int *s)
|
|
959 |
{
|
|
960 |
*s = (int) (size_t) *attr;
|
|
961 |
|
|
962 |
return 0;
|
|
963 |
}
|
|
964 |
|
|
965 |
int pthread_key_create(pthread_key_t *key, void (* dest)(void *))
|
|
966 |
{
|
|
967 |
unsigned i;
|
|
968 |
unsigned nmax;
|
|
969 |
void (**d)(void *);
|
|
970 |
|
|
971 |
if (!key) return EINVAL;
|
|
972 |
|
|
973 |
pthread_rwlock_wrlock(&_pthread_key_lock);
|
|
974 |
|
|
975 |
for (i = _pthread_key_sch; i < _pthread_key_max; i++)
|
|
976 |
{
|
|
977 |
if (!_pthread_key_dest[i])
|
|
978 |
{
|
|
979 |
*key = i;
|
|
980 |
if (dest)
|
|
981 |
{
|
|
982 |
_pthread_key_dest[i] = dest;
|
|
983 |
}
|
|
984 |
else
|
|
985 |
{
|
|
986 |
_pthread_key_dest[i] = (void(*)(void *))1;
|
|
987 |
}
|
|
988 |
pthread_rwlock_unlock(&_pthread_key_lock);
|
|
989 |
|
|
990 |
return 0;
|
|
991 |
}
|
|
992 |
}
|
|
993 |
|
|
994 |
for (i = 0; i < _pthread_key_sch; i++)
|
|
995 |
{
|
|
996 |
if (!_pthread_key_dest[i])
|
|
997 |
{
|
|
998 |
*key = i;
|
|
999 |
if (dest)
|
|
1000 |
{
|
|
1001 |
_pthread_key_dest[i] = dest;
|
|
1002 |
}
|
|
1003 |
else
|
|
1004 |
{
|
|
1005 |
_pthread_key_dest[i] = (void(*)(void *))1;
|
|
1006 |
}
|
|
1007 |
pthread_rwlock_unlock(&_pthread_key_lock);
|
|
1008 |
|
|
1009 |
return 0;
|
|
1010 |
}
|
|
1011 |
}
|
|
1012 |
|
|
1013 |
if (!_pthread_key_max) _pthread_key_max = 1;
|
|
1014 |
if (_pthread_key_max == PTHREAD_KEYS_MAX)
|
|
1015 |
{
|
|
1016 |
pthread_rwlock_unlock(&_pthread_key_lock);
|
|
1017 |
|
|
1018 |
return ENOMEM;
|
|
1019 |
}
|
|
1020 |
|
|
1021 |
nmax = _pthread_key_max * 2;
|
|
1022 |
if (nmax > PTHREAD_KEYS_MAX) nmax = PTHREAD_KEYS_MAX;
|
|
1023 |
|
|
1024 |
/* No spare room anywhere */
|
|
1025 |
d = (void (**)(void*))realloc(_pthread_key_dest, nmax * sizeof(*d));
|
|
1026 |
if (!d)
|
|
1027 |
{
|
|
1028 |
pthread_rwlock_unlock(&_pthread_key_lock);
|
|
1029 |
|
|
1030 |
return ENOMEM;
|
|
1031 |
}
|
|
1032 |
|
|
1033 |
/* Clear new region */
|
|
1034 |
memset((void *) &d[_pthread_key_max], 0, (nmax-_pthread_key_max)*sizeof(void *));
|
|
1035 |
|
|
1036 |
/* Use new region */
|
|
1037 |
_pthread_key_dest = d;
|
|
1038 |
_pthread_key_sch = _pthread_key_max + 1;
|
|
1039 |
*key = _pthread_key_max;
|
|
1040 |
_pthread_key_max = nmax;
|
|
1041 |
|
|
1042 |
if (dest)
|
|
1043 |
{
|
|
1044 |
_pthread_key_dest[*key] = dest;
|
|
1045 |
}
|
|
1046 |
else
|
|
1047 |
{
|
|
1048 |
_pthread_key_dest[*key] = (void(*)(void *))1;
|
|
1049 |
}
|
|
1050 |
|
|
1051 |
pthread_rwlock_unlock(&_pthread_key_lock);
|
|
1052 |
|
|
1053 |
return 0;
|
|
1054 |
}
|
|
1055 |
|
|
1056 |
int pthread_key_delete(pthread_key_t key)
|
|
1057 |
{
|
|
1058 |
if (key > _pthread_key_max) return EINVAL;
|
|
1059 |
if (!_pthread_key_dest) return EINVAL;
|
|
1060 |
|
|
1061 |
pthread_rwlock_wrlock(&_pthread_key_lock);
|
|
1062 |
_pthread_key_dest[key] = NULL;
|
|
1063 |
|
|
1064 |
/* Start next search from our location */
|
|
1065 |
if (_pthread_key_sch > key) _pthread_key_sch = key;
|
|
1066 |
|
|
1067 |
pthread_rwlock_unlock(&_pthread_key_lock);
|
|
1068 |
|
|
1069 |
return 0;
|
|
1070 |
}
|
|
1071 |
|
|
1072 |
void *pthread_getspecific(pthread_key_t key)
|
|
1073 |
{
|
|
1074 |
pthread_t t = pthread_self();
|
|
1075 |
|
|
1076 |
if (key >= t->keymax) return NULL;
|
|
1077 |
|
|
1078 |
return t->keyval[key];
|
|
1079 |
|
|
1080 |
}
|
|
1081 |
|
|
1082 |
int pthread_setspecific(pthread_key_t key, const void *value)
|
|
1083 |
{
|
|
1084 |
pthread_t t = pthread_self();
|
|
1085 |
|
|
1086 |
if (key > t->keymax)
|
|
1087 |
{
|
|
1088 |
int keymax = (key + 1) * 2;
|
|
1089 |
void **kv = (void**)realloc(t->keyval, keymax * sizeof(void *));
|
|
1090 |
|
|
1091 |
if (!kv) return ENOMEM;
|
|
1092 |
|
|
1093 |
/* Clear new region */
|
|
1094 |
memset(&kv[t->keymax], 0, (keymax - t->keymax)*sizeof(void*));
|
|
1095 |
|
|
1096 |
t->keyval = kv;
|
|
1097 |
t->keymax = keymax;
|
|
1098 |
}
|
|
1099 |
|
|
1100 |
t->keyval[key] = (void *) value;
|
|
1101 |
|
|
1102 |
return 0;
|
|
1103 |
}
|
|
1104 |
|
|
1105 |
|
|
1106 |
int pthread_spin_init(pthread_spinlock_t *l, int pshared)
|
|
1107 |
{
|
|
1108 |
(void) pshared;
|
|
1109 |
|
|
1110 |
*l = 0;
|
|
1111 |
return 0;
|
|
1112 |
}
|
|
1113 |
|
|
1114 |
int pthread_spin_destroy(pthread_spinlock_t *l)
|
|
1115 |
{
|
|
1116 |
(void) l;
|
|
1117 |
return 0;
|
|
1118 |
}
|
|
1119 |
|
|
1120 |
/* No-fair spinlock due to lack of knowledge of thread number */
|
|
1121 |
int pthread_spin_lock(pthread_spinlock_t *l)
|
|
1122 |
{
|
|
1123 |
while (_InterlockedExchange(l, EBUSY))
|
|
1124 |
{
|
|
1125 |
/* Don't lock the bus whilst waiting */
|
|
1126 |
while (*l)
|
|
1127 |
{
|
|
1128 |
YieldProcessor();
|
|
1129 |
|
|
1130 |
/* Compiler barrier. Prevent caching of *l */
|
|
1131 |
_ReadWriteBarrier();
|
|
1132 |
}
|
|
1133 |
}
|
|
1134 |
|
|
1135 |
return 0;
|
|
1136 |
}
|
|
1137 |
|
|
1138 |
int pthread_spin_trylock(pthread_spinlock_t *l)
|
|
1139 |
{
|
|
1140 |
return _InterlockedExchange(l, EBUSY);
|
|
1141 |
}
|
|
1142 |
|
|
1143 |
int pthread_spin_unlock(pthread_spinlock_t *l)
|
|
1144 |
{
|
|
1145 |
/* Compiler barrier. The store below acts with release symmantics */
|
|
1146 |
_ReadWriteBarrier();
|
|
1147 |
|
|
1148 |
*l = 0;
|
|
1149 |
|
|
1150 |
return 0;
|
|
1151 |
}
|
|
1152 |
|
|
1153 |
int pthread_cond_init(pthread_cond_t *c, pthread_condattr_t *a)
|
|
1154 |
{
|
|
1155 |
(void) a;
|
|
1156 |
|
|
1157 |
InitializeConditionVariable(c);
|
|
1158 |
return 0;
|
|
1159 |
}
|
|
1160 |
|
|
1161 |
int pthread_cond_signal(pthread_cond_t *c)
|
|
1162 |
{
|
|
1163 |
WakeConditionVariable(c);
|
|
1164 |
return 0;
|
|
1165 |
}
|
|
1166 |
|
|
1167 |
int pthread_cond_broadcast(pthread_cond_t *c)
|
|
1168 |
{
|
|
1169 |
WakeAllConditionVariable(c);
|
|
1170 |
return 0;
|
|
1171 |
}
|
|
1172 |
|
|
1173 |
int pthread_cond_wait(pthread_cond_t *c, pthread_mutex_t *m)
|
|
1174 |
{
|
|
1175 |
pthread_testcancel();
|
|
1176 |
SleepConditionVariableCS(c, m, INFINITE);
|
|
1177 |
return 0;
|
|
1178 |
}
|
|
1179 |
|
|
1180 |
int pthread_cond_destroy(pthread_cond_t *c)
|
|
1181 |
{
|
|
1182 |
(void) c;
|
|
1183 |
return 0;
|
|
1184 |
}
|
|
1185 |
|
|
1186 |
int pthread_cond_timedwait(pthread_cond_t *c, pthread_mutex_t *m, struct timespec *t)
|
|
1187 |
{
|
|
1188 |
unsigned long long tm = _pthread_rel_time_in_ms(t);
|
|
1189 |
|
|
1190 |
pthread_testcancel();
|
|
1191 |
|
|
1192 |
if (!SleepConditionVariableCS(c, m, (DWORD)tm)) return ETIMEDOUT;
|
|
1193 |
|
|
1194 |
/* We can have a spurious wakeup after the timeout */
|
|
1195 |
if (!_pthread_rel_time_in_ms(t)) return ETIMEDOUT;
|
|
1196 |
|
|
1197 |
return 0;
|
|
1198 |
}
|
|
1199 |
|
|
1200 |
int pthread_condattr_destroy(pthread_condattr_t *a)
|
|
1201 |
{
|
|
1202 |
(void) a;
|
|
1203 |
return 0;
|
|
1204 |
}
|
|
1205 |
|
|
1206 |
int pthread_condattr_init(pthread_condattr_t *a)
|
|
1207 |
{
|
|
1208 |
*a = 0;
|
|
1209 |
return 0;
|
|
1210 |
}
|
|
1211 |
|
|
1212 |
int pthread_condattr_getpshared(pthread_condattr_t *a, int *s)
|
|
1213 |
{
|
|
1214 |
*s = *a;
|
|
1215 |
return 0;
|
|
1216 |
}
|
|
1217 |
|
|
1218 |
int pthread_condattr_setpshared(pthread_condattr_t *a, int s)
|
|
1219 |
{
|
|
1220 |
*a = s;
|
|
1221 |
return 0;
|
|
1222 |
}
|
|
1223 |
|
|
1224 |
int pthread_rwlockattr_destroy(pthread_rwlockattr_t *a)
|
|
1225 |
{
|
|
1226 |
(void) a;
|
|
1227 |
return 0;
|
|
1228 |
}
|
|
1229 |
|
|
1230 |
int pthread_rwlockattr_init(pthread_rwlockattr_t *a)
|
|
1231 |
{
|
|
1232 |
*a = 0;
|
|
1233 |
return 0;
|
|
1234 |
}
|
|
1235 |
|
|
1236 |
int pthread_rwlockattr_getpshared(pthread_rwlockattr_t *a, int *s)
|
|
1237 |
{
|
|
1238 |
*s = *a;
|
|
1239 |
return 0;
|
|
1240 |
}
|
|
1241 |
|
|
1242 |
int pthread_rwlockattr_setpshared(pthread_rwlockattr_t *a, int s)
|
|
1243 |
{
|
|
1244 |
*a = s;
|
|
1245 |
return 0;
|
|
1246 |
}
|