threads_win32.h revision 01e04c3f
1af69d88dSmrg/*
2af69d88dSmrg * C11 <threads.h> emulation library
3af69d88dSmrg *
4af69d88dSmrg * (C) Copyright yohhoy 2012.
5af69d88dSmrg * Distributed under the Boost Software License, Version 1.0.
6af69d88dSmrg *
7af69d88dSmrg * Permission is hereby granted, free of charge, to any person or organization
8af69d88dSmrg * obtaining a copy of the software and accompanying documentation covered by
9af69d88dSmrg * this license (the "Software") to use, reproduce, display, distribute,
10af69d88dSmrg * execute, and transmit the Software, and to prepare [[derivative work]]s of the
11af69d88dSmrg * Software, and to permit third-parties to whom the Software is furnished to
12af69d88dSmrg * do so, all subject to the following:
13af69d88dSmrg *
14af69d88dSmrg * The copyright notices in the Software and this entire statement, including
15af69d88dSmrg * the above license grant, this restriction and the following disclaimer,
16af69d88dSmrg * must be included in all copies of the Software, in whole or in part, and
17af69d88dSmrg * all derivative works of the Software, unless such copies or derivative
18af69d88dSmrg * works are solely in the form of machine-executable object code generated by
19af69d88dSmrg * a source language processor.
20af69d88dSmrg *
21af69d88dSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22af69d88dSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23af69d88dSmrg * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24af69d88dSmrg * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25af69d88dSmrg * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26af69d88dSmrg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27af69d88dSmrg * DEALINGS IN THE SOFTWARE.
28af69d88dSmrg */
29af69d88dSmrg#ifndef assert
30af69d88dSmrg#include <assert.h>
31af69d88dSmrg#endif
32af69d88dSmrg#include <limits.h>
33af69d88dSmrg#include <errno.h>
34af69d88dSmrg#include <process.h>  // MSVCRT
35af69d88dSmrg#include <stdlib.h>
36af69d88dSmrg
37af69d88dSmrg/*
38af69d88dSmrgConfiguration macro:
39af69d88dSmrg
40af69d88dSmrg  EMULATED_THREADS_USE_NATIVE_CALL_ONCE
41af69d88dSmrg    Use native WindowsAPI one-time initialization function.
42af69d88dSmrg    (requires WinVista or later)
43af69d88dSmrg    Otherwise emulate by mtx_trylock() + *busy loop* for WinXP.
44af69d88dSmrg
45af69d88dSmrg  EMULATED_THREADS_USE_NATIVE_CV
46af69d88dSmrg    Use native WindowsAPI condition variable object.
47af69d88dSmrg    (requires WinVista or later)
48af69d88dSmrg    Otherwise use emulated implementation for WinXP.
49af69d88dSmrg
50af69d88dSmrg  EMULATED_THREADS_TSS_DTOR_SLOTNUM
51af69d88dSmrg    Max registerable TSS dtor number.
52af69d88dSmrg*/
53af69d88dSmrg
54af69d88dSmrg// XXX: Retain XP compatability
55af69d88dSmrg#if 0
56af69d88dSmrg#if _WIN32_WINNT >= 0x0600
57af69d88dSmrg// Prefer native WindowsAPI on newer environment.
58af69d88dSmrg#if !defined(__MINGW32__)
59af69d88dSmrg#define EMULATED_THREADS_USE_NATIVE_CALL_ONCE
60af69d88dSmrg#endif
61af69d88dSmrg#define EMULATED_THREADS_USE_NATIVE_CV
62af69d88dSmrg#endif
63af69d88dSmrg#endif
64af69d88dSmrg#define EMULATED_THREADS_TSS_DTOR_SLOTNUM 64  // see TLS_MINIMUM_AVAILABLE
65af69d88dSmrg
66af69d88dSmrg
67af69d88dSmrg#include <windows.h>
68af69d88dSmrg
69af69d88dSmrg// check configuration
70af69d88dSmrg#if defined(EMULATED_THREADS_USE_NATIVE_CALL_ONCE) && (_WIN32_WINNT < 0x0600)
71af69d88dSmrg#error EMULATED_THREADS_USE_NATIVE_CALL_ONCE requires _WIN32_WINNT>=0x0600
72af69d88dSmrg#endif
73af69d88dSmrg
74af69d88dSmrg#if defined(EMULATED_THREADS_USE_NATIVE_CV) && (_WIN32_WINNT < 0x0600)
75af69d88dSmrg#error EMULATED_THREADS_USE_NATIVE_CV requires _WIN32_WINNT>=0x0600
76af69d88dSmrg#endif
77af69d88dSmrg
7801e04c3fSmrg/* Visual Studio 2015 and later */
7901e04c3fSmrg#ifdef _MSC_VER
8001e04c3fSmrg#define HAVE_TIMESPEC_GET
8101e04c3fSmrg#endif
82af69d88dSmrg
83af69d88dSmrg/*---------------------------- macros ----------------------------*/
84af69d88dSmrg#ifdef EMULATED_THREADS_USE_NATIVE_CALL_ONCE
85af69d88dSmrg#define ONCE_FLAG_INIT INIT_ONCE_STATIC_INIT
86af69d88dSmrg#else
87af69d88dSmrg#define ONCE_FLAG_INIT {0}
88af69d88dSmrg#endif
89af69d88dSmrg#define TSS_DTOR_ITERATIONS 1
90af69d88dSmrg
91af69d88dSmrg// FIXME: temporary non-standard hack to ease transition
92af69d88dSmrg#define _MTX_INITIALIZER_NP {(PCRITICAL_SECTION_DEBUG)-1, -1, 0, 0, 0, 0}
93af69d88dSmrg
94af69d88dSmrg/*---------------------------- types ----------------------------*/
95af69d88dSmrgtypedef struct cnd_t {
96af69d88dSmrg#ifdef EMULATED_THREADS_USE_NATIVE_CV
97af69d88dSmrg    CONDITION_VARIABLE condvar;
98af69d88dSmrg#else
99af69d88dSmrg    int blocked;
100af69d88dSmrg    int gone;
101af69d88dSmrg    int to_unblock;
102af69d88dSmrg    HANDLE sem_queue;
103af69d88dSmrg    HANDLE sem_gate;
104af69d88dSmrg    CRITICAL_SECTION monitor;
105af69d88dSmrg#endif
106af69d88dSmrg} cnd_t;
107af69d88dSmrg
108af69d88dSmrgtypedef HANDLE thrd_t;
109af69d88dSmrg
110af69d88dSmrgtypedef DWORD tss_t;
111af69d88dSmrg
112af69d88dSmrgtypedef CRITICAL_SECTION mtx_t;
113af69d88dSmrg
114af69d88dSmrg#ifdef EMULATED_THREADS_USE_NATIVE_CALL_ONCE
115af69d88dSmrgtypedef INIT_ONCE once_flag;
116af69d88dSmrg#else
117af69d88dSmrgtypedef struct once_flag_t {
118af69d88dSmrg    volatile LONG status;
119af69d88dSmrg} once_flag;
120af69d88dSmrg#endif
121af69d88dSmrg
122af69d88dSmrg
123af69d88dSmrgstatic inline void * tss_get(tss_t key);
124af69d88dSmrgstatic inline void thrd_yield(void);
125af69d88dSmrgstatic inline int mtx_trylock(mtx_t *mtx);
126af69d88dSmrgstatic inline int mtx_lock(mtx_t *mtx);
127af69d88dSmrgstatic inline int mtx_unlock(mtx_t *mtx);
128af69d88dSmrg
129af69d88dSmrg/*
130af69d88dSmrgImplementation limits:
131af69d88dSmrg  - Conditionally emulation for "Initialization functions"
132af69d88dSmrg    (see EMULATED_THREADS_USE_NATIVE_CALL_ONCE macro)
133af69d88dSmrg  - Emulated `mtx_timelock()' with mtx_trylock() + *busy loop*
134af69d88dSmrg*/
135af69d88dSmrgstatic void impl_tss_dtor_invoke(void);  // forward decl.
136af69d88dSmrg
137af69d88dSmrgstruct impl_thrd_param {
138af69d88dSmrg    thrd_start_t func;
139af69d88dSmrg    void *arg;
140af69d88dSmrg};
141af69d88dSmrg
142af69d88dSmrgstatic unsigned __stdcall impl_thrd_routine(void *p)
143af69d88dSmrg{
144af69d88dSmrg    struct impl_thrd_param pack;
145af69d88dSmrg    int code;
146af69d88dSmrg    memcpy(&pack, p, sizeof(struct impl_thrd_param));
147af69d88dSmrg    free(p);
148af69d88dSmrg    code = pack.func(pack.arg);
149af69d88dSmrg    impl_tss_dtor_invoke();
150af69d88dSmrg    return (unsigned)code;
151af69d88dSmrg}
152af69d88dSmrg
15301e04c3fSmrgstatic DWORD impl_timespec2msec(const struct timespec *ts)
154af69d88dSmrg{
15501e04c3fSmrg    return (DWORD)((ts->tv_sec * 1000U) + (ts->tv_nsec / 1000000L));
156af69d88dSmrg}
157af69d88dSmrg
158af69d88dSmrg#ifdef EMULATED_THREADS_USE_NATIVE_CALL_ONCE
159af69d88dSmrgstruct impl_call_once_param { void (*func)(void); };
160af69d88dSmrgstatic BOOL CALLBACK impl_call_once_callback(PINIT_ONCE InitOnce, PVOID Parameter, PVOID *Context)
161af69d88dSmrg{
162af69d88dSmrg    struct impl_call_once_param *param = (struct impl_call_once_param*)Parameter;
163af69d88dSmrg    (param->func)();
164af69d88dSmrg    ((void)InitOnce); ((void)Context);  // suppress warning
165af69d88dSmrg    return TRUE;
166af69d88dSmrg}
167af69d88dSmrg#endif  // ifdef EMULATED_THREADS_USE_NATIVE_CALL_ONCE
168af69d88dSmrg
169af69d88dSmrg#ifndef EMULATED_THREADS_USE_NATIVE_CV
170af69d88dSmrg/*
171af69d88dSmrgNote:
172af69d88dSmrg  The implementation of condition variable is ported from Boost.Interprocess
173af69d88dSmrg  See http://www.boost.org/boost/interprocess/sync/windows/condition.hpp
174af69d88dSmrg*/
175af69d88dSmrgstatic void impl_cond_do_signal(cnd_t *cond, int broadcast)
176af69d88dSmrg{
177af69d88dSmrg    int nsignal = 0;
178af69d88dSmrg
179af69d88dSmrg    EnterCriticalSection(&cond->monitor);
180af69d88dSmrg    if (cond->to_unblock != 0) {
181af69d88dSmrg        if (cond->blocked == 0) {
182af69d88dSmrg            LeaveCriticalSection(&cond->monitor);
183af69d88dSmrg            return;
184af69d88dSmrg        }
185af69d88dSmrg        if (broadcast) {
186af69d88dSmrg            cond->to_unblock += nsignal = cond->blocked;
187af69d88dSmrg            cond->blocked = 0;
188af69d88dSmrg        } else {
189af69d88dSmrg            nsignal = 1;
190af69d88dSmrg            cond->to_unblock++;
191af69d88dSmrg            cond->blocked--;
192af69d88dSmrg        }
193af69d88dSmrg    } else if (cond->blocked > cond->gone) {
194af69d88dSmrg        WaitForSingleObject(cond->sem_gate, INFINITE);
195af69d88dSmrg        if (cond->gone != 0) {
196af69d88dSmrg            cond->blocked -= cond->gone;
197af69d88dSmrg            cond->gone = 0;
198af69d88dSmrg        }
199af69d88dSmrg        if (broadcast) {
200af69d88dSmrg            nsignal = cond->to_unblock = cond->blocked;
201af69d88dSmrg            cond->blocked = 0;
202af69d88dSmrg        } else {
203af69d88dSmrg            nsignal = cond->to_unblock = 1;
204af69d88dSmrg            cond->blocked--;
205af69d88dSmrg        }
206af69d88dSmrg    }
207af69d88dSmrg    LeaveCriticalSection(&cond->monitor);
208af69d88dSmrg
209af69d88dSmrg    if (0 < nsignal)
210af69d88dSmrg        ReleaseSemaphore(cond->sem_queue, nsignal, NULL);
211af69d88dSmrg}
212af69d88dSmrg
21301e04c3fSmrgstatic int impl_cond_do_wait(cnd_t *cond, mtx_t *mtx, const struct timespec *ts)
214af69d88dSmrg{
215af69d88dSmrg    int nleft = 0;
216af69d88dSmrg    int ngone = 0;
217af69d88dSmrg    int timeout = 0;
218af69d88dSmrg    DWORD w;
219af69d88dSmrg
220af69d88dSmrg    WaitForSingleObject(cond->sem_gate, INFINITE);
221af69d88dSmrg    cond->blocked++;
222af69d88dSmrg    ReleaseSemaphore(cond->sem_gate, 1, NULL);
223af69d88dSmrg
224af69d88dSmrg    mtx_unlock(mtx);
225af69d88dSmrg
22601e04c3fSmrg    w = WaitForSingleObject(cond->sem_queue, ts ? impl_timespec2msec(ts) : INFINITE);
227af69d88dSmrg    timeout = (w == WAIT_TIMEOUT);
228af69d88dSmrg
229af69d88dSmrg    EnterCriticalSection(&cond->monitor);
230af69d88dSmrg    if ((nleft = cond->to_unblock) != 0) {
231af69d88dSmrg        if (timeout) {
232af69d88dSmrg            if (cond->blocked != 0) {
233af69d88dSmrg                cond->blocked--;
234af69d88dSmrg            } else {
235af69d88dSmrg                cond->gone++;
236af69d88dSmrg            }
237af69d88dSmrg        }
238af69d88dSmrg        if (--cond->to_unblock == 0) {
239af69d88dSmrg            if (cond->blocked != 0) {
240af69d88dSmrg                ReleaseSemaphore(cond->sem_gate, 1, NULL);
241af69d88dSmrg                nleft = 0;
242af69d88dSmrg            }
243af69d88dSmrg            else if ((ngone = cond->gone) != 0) {
244af69d88dSmrg                cond->gone = 0;
245af69d88dSmrg            }
246af69d88dSmrg        }
247af69d88dSmrg    } else if (++cond->gone == INT_MAX/2) {
248af69d88dSmrg        WaitForSingleObject(cond->sem_gate, INFINITE);
249af69d88dSmrg        cond->blocked -= cond->gone;
250af69d88dSmrg        ReleaseSemaphore(cond->sem_gate, 1, NULL);
251af69d88dSmrg        cond->gone = 0;
252af69d88dSmrg    }
253af69d88dSmrg    LeaveCriticalSection(&cond->monitor);
254af69d88dSmrg
255af69d88dSmrg    if (nleft == 1) {
256af69d88dSmrg        while (ngone--)
257af69d88dSmrg            WaitForSingleObject(cond->sem_queue, INFINITE);
258af69d88dSmrg        ReleaseSemaphore(cond->sem_gate, 1, NULL);
259af69d88dSmrg    }
260af69d88dSmrg
261af69d88dSmrg    mtx_lock(mtx);
262af69d88dSmrg    return timeout ? thrd_busy : thrd_success;
263af69d88dSmrg}
264af69d88dSmrg#endif  // ifndef EMULATED_THREADS_USE_NATIVE_CV
265af69d88dSmrg
266af69d88dSmrgstatic struct impl_tss_dtor_entry {
267af69d88dSmrg    tss_t key;
268af69d88dSmrg    tss_dtor_t dtor;
269af69d88dSmrg} impl_tss_dtor_tbl[EMULATED_THREADS_TSS_DTOR_SLOTNUM];
270af69d88dSmrg
271af69d88dSmrgstatic int impl_tss_dtor_register(tss_t key, tss_dtor_t dtor)
272af69d88dSmrg{
273af69d88dSmrg    int i;
274af69d88dSmrg    for (i = 0; i < EMULATED_THREADS_TSS_DTOR_SLOTNUM; i++) {
275af69d88dSmrg        if (!impl_tss_dtor_tbl[i].dtor)
276af69d88dSmrg            break;
277af69d88dSmrg    }
278af69d88dSmrg    if (i == EMULATED_THREADS_TSS_DTOR_SLOTNUM)
279af69d88dSmrg        return 1;
280af69d88dSmrg    impl_tss_dtor_tbl[i].key = key;
281af69d88dSmrg    impl_tss_dtor_tbl[i].dtor = dtor;
282af69d88dSmrg    return 0;
283af69d88dSmrg}
284af69d88dSmrg
285af69d88dSmrgstatic void impl_tss_dtor_invoke()
286af69d88dSmrg{
287af69d88dSmrg    int i;
288af69d88dSmrg    for (i = 0; i < EMULATED_THREADS_TSS_DTOR_SLOTNUM; i++) {
289af69d88dSmrg        if (impl_tss_dtor_tbl[i].dtor) {
290af69d88dSmrg            void* val = tss_get(impl_tss_dtor_tbl[i].key);
291af69d88dSmrg            if (val)
292af69d88dSmrg                (impl_tss_dtor_tbl[i].dtor)(val);
293af69d88dSmrg        }
294af69d88dSmrg    }
295af69d88dSmrg}
296af69d88dSmrg
297af69d88dSmrg
298af69d88dSmrg/*--------------- 7.25.2 Initialization functions ---------------*/
299af69d88dSmrg// 7.25.2.1
300af69d88dSmrgstatic inline void
301af69d88dSmrgcall_once(once_flag *flag, void (*func)(void))
302af69d88dSmrg{
303af69d88dSmrg    assert(flag && func);
304af69d88dSmrg#ifdef EMULATED_THREADS_USE_NATIVE_CALL_ONCE
305af69d88dSmrg    {
306af69d88dSmrg    struct impl_call_once_param param;
307af69d88dSmrg    param.func = func;
308af69d88dSmrg    InitOnceExecuteOnce(flag, impl_call_once_callback, (PVOID)&param, NULL);
309af69d88dSmrg    }
310af69d88dSmrg#else
311af69d88dSmrg    if (InterlockedCompareExchange(&flag->status, 1, 0) == 0) {
312af69d88dSmrg        (func)();
313af69d88dSmrg        InterlockedExchange(&flag->status, 2);
314af69d88dSmrg    } else {
315af69d88dSmrg        while (flag->status == 1) {
316af69d88dSmrg            // busy loop!
317af69d88dSmrg            thrd_yield();
318af69d88dSmrg        }
319af69d88dSmrg    }
320af69d88dSmrg#endif
321af69d88dSmrg}
322af69d88dSmrg
323af69d88dSmrg
324af69d88dSmrg/*------------- 7.25.3 Condition variable functions -------------*/
325af69d88dSmrg// 7.25.3.1
326af69d88dSmrgstatic inline int
327af69d88dSmrgcnd_broadcast(cnd_t *cond)
328af69d88dSmrg{
329af69d88dSmrg    if (!cond) return thrd_error;
330af69d88dSmrg#ifdef EMULATED_THREADS_USE_NATIVE_CV
331af69d88dSmrg    WakeAllConditionVariable(&cond->condvar);
332af69d88dSmrg#else
333af69d88dSmrg    impl_cond_do_signal(cond, 1);
334af69d88dSmrg#endif
335af69d88dSmrg    return thrd_success;
336af69d88dSmrg}
337af69d88dSmrg
338af69d88dSmrg// 7.25.3.2
339af69d88dSmrgstatic inline void
340af69d88dSmrgcnd_destroy(cnd_t *cond)
341af69d88dSmrg{
342af69d88dSmrg    assert(cond);
343af69d88dSmrg#ifdef EMULATED_THREADS_USE_NATIVE_CV
344af69d88dSmrg    // do nothing
345af69d88dSmrg#else
346af69d88dSmrg    CloseHandle(cond->sem_queue);
347af69d88dSmrg    CloseHandle(cond->sem_gate);
348af69d88dSmrg    DeleteCriticalSection(&cond->monitor);
349af69d88dSmrg#endif
350af69d88dSmrg}
351af69d88dSmrg
352af69d88dSmrg// 7.25.3.3
353af69d88dSmrgstatic inline int
354af69d88dSmrgcnd_init(cnd_t *cond)
355af69d88dSmrg{
356af69d88dSmrg    if (!cond) return thrd_error;
357af69d88dSmrg#ifdef EMULATED_THREADS_USE_NATIVE_CV
358af69d88dSmrg    InitializeConditionVariable(&cond->condvar);
359af69d88dSmrg#else
360af69d88dSmrg    cond->blocked = 0;
361af69d88dSmrg    cond->gone = 0;
362af69d88dSmrg    cond->to_unblock = 0;
363af69d88dSmrg    cond->sem_queue = CreateSemaphore(NULL, 0, LONG_MAX, NULL);
364af69d88dSmrg    cond->sem_gate = CreateSemaphore(NULL, 1, 1, NULL);
365af69d88dSmrg    InitializeCriticalSection(&cond->monitor);
366af69d88dSmrg#endif
367af69d88dSmrg    return thrd_success;
368af69d88dSmrg}
369af69d88dSmrg
370af69d88dSmrg// 7.25.3.4
371af69d88dSmrgstatic inline int
372af69d88dSmrgcnd_signal(cnd_t *cond)
373af69d88dSmrg{
374af69d88dSmrg    if (!cond) return thrd_error;
375af69d88dSmrg#ifdef EMULATED_THREADS_USE_NATIVE_CV
376af69d88dSmrg    WakeConditionVariable(&cond->condvar);
377af69d88dSmrg#else
378af69d88dSmrg    impl_cond_do_signal(cond, 0);
379af69d88dSmrg#endif
380af69d88dSmrg    return thrd_success;
381af69d88dSmrg}
382af69d88dSmrg
383af69d88dSmrg// 7.25.3.5
384af69d88dSmrgstatic inline int
38501e04c3fSmrgcnd_timedwait(cnd_t *cond, mtx_t *mtx, const struct timespec *abs_time)
386af69d88dSmrg{
38701e04c3fSmrg    if (!cond || !mtx || !abs_time) return thrd_error;
388af69d88dSmrg#ifdef EMULATED_THREADS_USE_NATIVE_CV
38901e04c3fSmrg    if (SleepConditionVariableCS(&cond->condvar, mtx, impl_timespec2msec(abs_time)))
390af69d88dSmrg        return thrd_success;
391af69d88dSmrg    return (GetLastError() == ERROR_TIMEOUT) ? thrd_busy : thrd_error;
392af69d88dSmrg#else
39301e04c3fSmrg    return impl_cond_do_wait(cond, mtx, abs_time);
394af69d88dSmrg#endif
395af69d88dSmrg}
396af69d88dSmrg
397af69d88dSmrg// 7.25.3.6
398af69d88dSmrgstatic inline int
399af69d88dSmrgcnd_wait(cnd_t *cond, mtx_t *mtx)
400af69d88dSmrg{
401af69d88dSmrg    if (!cond || !mtx) return thrd_error;
402af69d88dSmrg#ifdef EMULATED_THREADS_USE_NATIVE_CV
403af69d88dSmrg    SleepConditionVariableCS(&cond->condvar, mtx, INFINITE);
404af69d88dSmrg#else
405af69d88dSmrg    impl_cond_do_wait(cond, mtx, NULL);
406af69d88dSmrg#endif
407af69d88dSmrg    return thrd_success;
408af69d88dSmrg}
409af69d88dSmrg
410af69d88dSmrg
411af69d88dSmrg/*-------------------- 7.25.4 Mutex functions --------------------*/
412af69d88dSmrg// 7.25.4.1
413af69d88dSmrgstatic inline void
414af69d88dSmrgmtx_destroy(mtx_t *mtx)
415af69d88dSmrg{
416af69d88dSmrg    assert(mtx);
417af69d88dSmrg    DeleteCriticalSection(mtx);
418af69d88dSmrg}
419af69d88dSmrg
420af69d88dSmrg// 7.25.4.2
421af69d88dSmrgstatic inline int
422af69d88dSmrgmtx_init(mtx_t *mtx, int type)
423af69d88dSmrg{
424af69d88dSmrg    if (!mtx) return thrd_error;
425af69d88dSmrg    if (type != mtx_plain && type != mtx_timed && type != mtx_try
426af69d88dSmrg      && type != (mtx_plain|mtx_recursive)
427af69d88dSmrg      && type != (mtx_timed|mtx_recursive)
428af69d88dSmrg      && type != (mtx_try|mtx_recursive))
429af69d88dSmrg        return thrd_error;
430af69d88dSmrg    InitializeCriticalSection(mtx);
431af69d88dSmrg    return thrd_success;
432af69d88dSmrg}
433af69d88dSmrg
434af69d88dSmrg// 7.25.4.3
435af69d88dSmrgstatic inline int
436af69d88dSmrgmtx_lock(mtx_t *mtx)
437af69d88dSmrg{
438af69d88dSmrg    if (!mtx) return thrd_error;
439af69d88dSmrg    EnterCriticalSection(mtx);
440af69d88dSmrg    return thrd_success;
441af69d88dSmrg}
442af69d88dSmrg
443af69d88dSmrg// 7.25.4.4
444af69d88dSmrgstatic inline int
44501e04c3fSmrgmtx_timedlock(mtx_t *mtx, const struct timespec *ts)
446af69d88dSmrg{
447af69d88dSmrg    time_t expire, now;
44801e04c3fSmrg    if (!mtx || !ts) return thrd_error;
449af69d88dSmrg    expire = time(NULL);
45001e04c3fSmrg    expire += ts->tv_sec;
451af69d88dSmrg    while (mtx_trylock(mtx) != thrd_success) {
452af69d88dSmrg        now = time(NULL);
453af69d88dSmrg        if (expire < now)
454af69d88dSmrg            return thrd_busy;
455af69d88dSmrg        // busy loop!
456af69d88dSmrg        thrd_yield();
457af69d88dSmrg    }
458af69d88dSmrg    return thrd_success;
459af69d88dSmrg}
460af69d88dSmrg
461af69d88dSmrg// 7.25.4.5
462af69d88dSmrgstatic inline int
463af69d88dSmrgmtx_trylock(mtx_t *mtx)
464af69d88dSmrg{
465af69d88dSmrg    if (!mtx) return thrd_error;
466af69d88dSmrg    return TryEnterCriticalSection(mtx) ? thrd_success : thrd_busy;
467af69d88dSmrg}
468af69d88dSmrg
469af69d88dSmrg// 7.25.4.6
470af69d88dSmrgstatic inline int
471af69d88dSmrgmtx_unlock(mtx_t *mtx)
472af69d88dSmrg{
473af69d88dSmrg    if (!mtx) return thrd_error;
474af69d88dSmrg    LeaveCriticalSection(mtx);
475af69d88dSmrg    return thrd_success;
476af69d88dSmrg}
477af69d88dSmrg
478af69d88dSmrg
479af69d88dSmrg/*------------------- 7.25.5 Thread functions -------------------*/
480af69d88dSmrg// 7.25.5.1
481af69d88dSmrgstatic inline int
482af69d88dSmrgthrd_create(thrd_t *thr, thrd_start_t func, void *arg)
483af69d88dSmrg{
484af69d88dSmrg    struct impl_thrd_param *pack;
485af69d88dSmrg    uintptr_t handle;
486af69d88dSmrg    if (!thr) return thrd_error;
487af69d88dSmrg    pack = (struct impl_thrd_param *)malloc(sizeof(struct impl_thrd_param));
488af69d88dSmrg    if (!pack) return thrd_nomem;
489af69d88dSmrg    pack->func = func;
490af69d88dSmrg    pack->arg = arg;
491af69d88dSmrg    handle = _beginthreadex(NULL, 0, impl_thrd_routine, pack, 0, NULL);
492af69d88dSmrg    if (handle == 0) {
493af69d88dSmrg        if (errno == EAGAIN || errno == EACCES)
494af69d88dSmrg            return thrd_nomem;
495af69d88dSmrg        return thrd_error;
496af69d88dSmrg    }
497af69d88dSmrg    *thr = (thrd_t)handle;
498af69d88dSmrg    return thrd_success;
499af69d88dSmrg}
500af69d88dSmrg
501af69d88dSmrg#if 0
502af69d88dSmrg// 7.25.5.2
503af69d88dSmrgstatic inline thrd_t
504af69d88dSmrgthrd_current(void)
505af69d88dSmrg{
506af69d88dSmrg    HANDLE hCurrentThread;
507af69d88dSmrg    BOOL bRet;
508af69d88dSmrg
50901e04c3fSmrg    /* GetCurrentThread() returns a pseudo-handle, which we need
51001e04c3fSmrg     * to pass to DuplicateHandle(). Only the resulting handle can be used
51101e04c3fSmrg     * from other threads.
51201e04c3fSmrg     *
51301e04c3fSmrg     * Note that neither handle can be compared to the one by thread_create.
51401e04c3fSmrg     * Only the thread IDs - as returned by GetThreadId() and GetCurrentThreadId()
51501e04c3fSmrg     * can be compared directly.
516af69d88dSmrg     *
517af69d88dSmrg     * Other potential solutions would be:
518af69d88dSmrg     * - define thrd_t as a thread Ids, but this would mean we'd need to OpenThread for many operations
519af69d88dSmrg     * - use malloc'ed memory for thrd_t. This would imply using TLS for current thread.
520af69d88dSmrg     *
521af69d88dSmrg     * Neither is particularly nice.
522af69d88dSmrg     *
523af69d88dSmrg     * Life would be much easier if C11 threads had different abstractions for
524af69d88dSmrg     * threads and thread IDs, just like C++11 threads does...
525af69d88dSmrg     */
526af69d88dSmrg
527af69d88dSmrg    bRet = DuplicateHandle(GetCurrentProcess(), // source process (pseudo) handle
528af69d88dSmrg                           GetCurrentThread(), // source (pseudo) handle
529af69d88dSmrg                           GetCurrentProcess(), // target process
530af69d88dSmrg                           &hCurrentThread, // target handle
531af69d88dSmrg                           0,
532af69d88dSmrg                           FALSE,
533af69d88dSmrg                           DUPLICATE_SAME_ACCESS);
534af69d88dSmrg    assert(bRet);
535af69d88dSmrg    if (!bRet) {
536af69d88dSmrg	hCurrentThread = GetCurrentThread();
537af69d88dSmrg    }
538af69d88dSmrg    return hCurrentThread;
539af69d88dSmrg}
540af69d88dSmrg#endif
541af69d88dSmrg
542af69d88dSmrg// 7.25.5.3
543af69d88dSmrgstatic inline int
544af69d88dSmrgthrd_detach(thrd_t thr)
545af69d88dSmrg{
546af69d88dSmrg    CloseHandle(thr);
547af69d88dSmrg    return thrd_success;
548af69d88dSmrg}
549af69d88dSmrg
550af69d88dSmrg// 7.25.5.4
551af69d88dSmrgstatic inline int
552af69d88dSmrgthrd_equal(thrd_t thr0, thrd_t thr1)
553af69d88dSmrg{
554af69d88dSmrg    return GetThreadId(thr0) == GetThreadId(thr1);
555af69d88dSmrg}
556af69d88dSmrg
557af69d88dSmrg// 7.25.5.5
558af69d88dSmrgstatic inline void
559af69d88dSmrgthrd_exit(int res)
560af69d88dSmrg{
561af69d88dSmrg    impl_tss_dtor_invoke();
562af69d88dSmrg    _endthreadex((unsigned)res);
563af69d88dSmrg}
564af69d88dSmrg
565af69d88dSmrg// 7.25.5.6
566af69d88dSmrgstatic inline int
567af69d88dSmrgthrd_join(thrd_t thr, int *res)
568af69d88dSmrg{
569af69d88dSmrg    DWORD w, code;
570af69d88dSmrg    w = WaitForSingleObject(thr, INFINITE);
571af69d88dSmrg    if (w != WAIT_OBJECT_0)
572af69d88dSmrg        return thrd_error;
573af69d88dSmrg    if (res) {
574af69d88dSmrg        if (!GetExitCodeThread(thr, &code)) {
575af69d88dSmrg            CloseHandle(thr);
576af69d88dSmrg            return thrd_error;
577af69d88dSmrg        }
578af69d88dSmrg        *res = (int)code;
579af69d88dSmrg    }
580af69d88dSmrg    CloseHandle(thr);
581af69d88dSmrg    return thrd_success;
582af69d88dSmrg}
583af69d88dSmrg
584af69d88dSmrg// 7.25.5.7
585af69d88dSmrgstatic inline void
58601e04c3fSmrgthrd_sleep(const struct timespec *time_point, struct timespec *remaining)
587af69d88dSmrg{
58801e04c3fSmrg    assert(time_point);
58901e04c3fSmrg    assert(!remaining); /* not implemented */
59001e04c3fSmrg    Sleep(impl_timespec2msec(time_point));
591af69d88dSmrg}
592af69d88dSmrg
593af69d88dSmrg// 7.25.5.8
594af69d88dSmrgstatic inline void
595af69d88dSmrgthrd_yield(void)
596af69d88dSmrg{
597af69d88dSmrg    SwitchToThread();
598af69d88dSmrg}
599af69d88dSmrg
600af69d88dSmrg
601af69d88dSmrg/*----------- 7.25.6 Thread-specific storage functions -----------*/
602af69d88dSmrg// 7.25.6.1
603af69d88dSmrgstatic inline int
604af69d88dSmrgtss_create(tss_t *key, tss_dtor_t dtor)
605af69d88dSmrg{
606af69d88dSmrg    if (!key) return thrd_error;
607af69d88dSmrg    *key = TlsAlloc();
608af69d88dSmrg    if (dtor) {
609af69d88dSmrg        if (impl_tss_dtor_register(*key, dtor)) {
610af69d88dSmrg            TlsFree(*key);
611af69d88dSmrg            return thrd_error;
612af69d88dSmrg        }
613af69d88dSmrg    }
614af69d88dSmrg    return (*key != 0xFFFFFFFF) ? thrd_success : thrd_error;
615af69d88dSmrg}
616af69d88dSmrg
617af69d88dSmrg// 7.25.6.2
618af69d88dSmrgstatic inline void
619af69d88dSmrgtss_delete(tss_t key)
620af69d88dSmrg{
621af69d88dSmrg    TlsFree(key);
622af69d88dSmrg}
623af69d88dSmrg
624af69d88dSmrg// 7.25.6.3
625af69d88dSmrgstatic inline void *
626af69d88dSmrgtss_get(tss_t key)
627af69d88dSmrg{
628af69d88dSmrg    return TlsGetValue(key);
629af69d88dSmrg}
630af69d88dSmrg
631af69d88dSmrg// 7.25.6.4
632af69d88dSmrgstatic inline int
633af69d88dSmrgtss_set(tss_t key, void *val)
634af69d88dSmrg{
635af69d88dSmrg    return TlsSetValue(key, val) ? thrd_success : thrd_error;
636af69d88dSmrg}
637af69d88dSmrg
638af69d88dSmrg
639af69d88dSmrg/*-------------------- 7.25.7 Time functions --------------------*/
640af69d88dSmrg// 7.25.6.1
64101e04c3fSmrg#ifndef HAVE_TIMESPEC_GET
642af69d88dSmrgstatic inline int
64301e04c3fSmrgtimespec_get(struct timespec *ts, int base)
644af69d88dSmrg{
64501e04c3fSmrg    if (!ts) return 0;
646af69d88dSmrg    if (base == TIME_UTC) {
64701e04c3fSmrg        ts->tv_sec = time(NULL);
64801e04c3fSmrg        ts->tv_nsec = 0;
649af69d88dSmrg        return base;
650af69d88dSmrg    }
651af69d88dSmrg    return 0;
652af69d88dSmrg}
65301e04c3fSmrg#endif
654