xref: /aosp_15_r20/external/mesa3d/src/c11/impl/threads_win32.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1*61046927SAndroid Build Coastguard Worker /*
2*61046927SAndroid Build Coastguard Worker  * C11 <threads.h> emulation library
3*61046927SAndroid Build Coastguard Worker  *
4*61046927SAndroid Build Coastguard Worker  * (C) Copyright yohhoy 2012.
5*61046927SAndroid Build Coastguard Worker  * Distributed under the Boost Software License, Version 1.0.
6*61046927SAndroid Build Coastguard Worker  *
7*61046927SAndroid Build Coastguard Worker  * Permission is hereby granted, free of charge, to any person or organization
8*61046927SAndroid Build Coastguard Worker  * obtaining a copy of the software and accompanying documentation covered by
9*61046927SAndroid Build Coastguard Worker  * this license (the "Software") to use, reproduce, display, distribute,
10*61046927SAndroid Build Coastguard Worker  * execute, and transmit the Software, and to prepare [[derivative work]]s of the
11*61046927SAndroid Build Coastguard Worker  * Software, and to permit third-parties to whom the Software is furnished to
12*61046927SAndroid Build Coastguard Worker  * do so, all subject to the following:
13*61046927SAndroid Build Coastguard Worker  *
14*61046927SAndroid Build Coastguard Worker  * The copyright notices in the Software and this entire statement, including
15*61046927SAndroid Build Coastguard Worker  * the above license grant, this restriction and the following disclaimer,
16*61046927SAndroid Build Coastguard Worker  * must be included in all copies of the Software, in whole or in part, and
17*61046927SAndroid Build Coastguard Worker  * all derivative works of the Software, unless such copies or derivative
18*61046927SAndroid Build Coastguard Worker  * works are solely in the form of machine-executable object code generated by
19*61046927SAndroid Build Coastguard Worker  * a source language processor.
20*61046927SAndroid Build Coastguard Worker  *
21*61046927SAndroid Build Coastguard Worker  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22*61046927SAndroid Build Coastguard Worker  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23*61046927SAndroid Build Coastguard Worker  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24*61046927SAndroid Build Coastguard Worker  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25*61046927SAndroid Build Coastguard Worker  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26*61046927SAndroid Build Coastguard Worker  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27*61046927SAndroid Build Coastguard Worker  * DEALINGS IN THE SOFTWARE.
28*61046927SAndroid Build Coastguard Worker  */
29*61046927SAndroid Build Coastguard Worker #include <assert.h>
30*61046927SAndroid Build Coastguard Worker #include <limits.h>
31*61046927SAndroid Build Coastguard Worker #include <errno.h>
32*61046927SAndroid Build Coastguard Worker #include <process.h>  // MSVCRT
33*61046927SAndroid Build Coastguard Worker #include <stdlib.h>
34*61046927SAndroid Build Coastguard Worker #include <stdbool.h>
35*61046927SAndroid Build Coastguard Worker 
36*61046927SAndroid Build Coastguard Worker #include "c11/threads.h"
37*61046927SAndroid Build Coastguard Worker 
38*61046927SAndroid Build Coastguard Worker #include "threads_win32.h"
39*61046927SAndroid Build Coastguard Worker 
40*61046927SAndroid Build Coastguard Worker #include <windows.h>
41*61046927SAndroid Build Coastguard Worker 
42*61046927SAndroid Build Coastguard Worker /*
43*61046927SAndroid Build Coastguard Worker Configuration macro:
44*61046927SAndroid Build Coastguard Worker   EMULATED_THREADS_TSS_DTOR_SLOTNUM
45*61046927SAndroid Build Coastguard Worker     Max registerable TSS dtor number.
46*61046927SAndroid Build Coastguard Worker */
47*61046927SAndroid Build Coastguard Worker 
48*61046927SAndroid Build Coastguard Worker #define EMULATED_THREADS_TSS_DTOR_SLOTNUM 64  // see TLS_MINIMUM_AVAILABLE
49*61046927SAndroid Build Coastguard Worker 
50*61046927SAndroid Build Coastguard Worker 
51*61046927SAndroid Build Coastguard Worker static_assert(sizeof(cnd_t) == sizeof(CONDITION_VARIABLE), "The size of cnd_t must equal to CONDITION_VARIABLE");
52*61046927SAndroid Build Coastguard Worker static_assert(sizeof(thrd_t) == sizeof(HANDLE), "The size of thrd_t must equal to HANDLE");
53*61046927SAndroid Build Coastguard Worker static_assert(sizeof(tss_t) == sizeof(DWORD), "The size of tss_t must equal to DWORD");
54*61046927SAndroid Build Coastguard Worker static_assert(sizeof(mtx_t) == sizeof(CRITICAL_SECTION), "The size of mtx_t must equal to CRITICAL_SECTION");
55*61046927SAndroid Build Coastguard Worker static_assert(sizeof(once_flag) == sizeof(INIT_ONCE), "The size of once_flag must equal to INIT_ONCE");
56*61046927SAndroid Build Coastguard Worker 
57*61046927SAndroid Build Coastguard Worker /*
58*61046927SAndroid Build Coastguard Worker Implementation limits:
59*61046927SAndroid Build Coastguard Worker   - Emulated `mtx_timelock()' with mtx_trylock() + *busy loop*
60*61046927SAndroid Build Coastguard Worker */
61*61046927SAndroid Build Coastguard Worker 
62*61046927SAndroid Build Coastguard Worker struct impl_thrd_param {
63*61046927SAndroid Build Coastguard Worker     thrd_start_t func;
64*61046927SAndroid Build Coastguard Worker     void *arg;
65*61046927SAndroid Build Coastguard Worker     thrd_t thrd;
66*61046927SAndroid Build Coastguard Worker };
67*61046927SAndroid Build Coastguard Worker 
68*61046927SAndroid Build Coastguard Worker struct thrd_state {
69*61046927SAndroid Build Coastguard Worker     thrd_t thrd;
70*61046927SAndroid Build Coastguard Worker     bool handle_need_close;
71*61046927SAndroid Build Coastguard Worker };
72*61046927SAndroid Build Coastguard Worker 
73*61046927SAndroid Build Coastguard Worker static thread_local struct thrd_state impl_current_thread = { 0 };
74*61046927SAndroid Build Coastguard Worker 
impl_thrd_routine(void * p)75*61046927SAndroid Build Coastguard Worker static unsigned __stdcall impl_thrd_routine(void *p)
76*61046927SAndroid Build Coastguard Worker {
77*61046927SAndroid Build Coastguard Worker     struct impl_thrd_param *pack_p = (struct impl_thrd_param *)p;
78*61046927SAndroid Build Coastguard Worker     struct impl_thrd_param pack;
79*61046927SAndroid Build Coastguard Worker     int code;
80*61046927SAndroid Build Coastguard Worker     impl_current_thread.thrd = pack_p->thrd;
81*61046927SAndroid Build Coastguard Worker     impl_current_thread.handle_need_close = false;
82*61046927SAndroid Build Coastguard Worker     memcpy(&pack, pack_p, sizeof(struct impl_thrd_param));
83*61046927SAndroid Build Coastguard Worker     free(p);
84*61046927SAndroid Build Coastguard Worker     code = pack.func(pack.arg);
85*61046927SAndroid Build Coastguard Worker     return (unsigned)code;
86*61046927SAndroid Build Coastguard Worker }
87*61046927SAndroid Build Coastguard Worker 
impl_timespec2msec(const struct timespec * ts)88*61046927SAndroid Build Coastguard Worker static time_t impl_timespec2msec(const struct timespec *ts)
89*61046927SAndroid Build Coastguard Worker {
90*61046927SAndroid Build Coastguard Worker     return (ts->tv_sec * 1000U) + (ts->tv_nsec / 1000000L);
91*61046927SAndroid Build Coastguard Worker }
92*61046927SAndroid Build Coastguard Worker 
impl_abs2relmsec(const struct timespec * abs_time)93*61046927SAndroid Build Coastguard Worker static DWORD impl_abs2relmsec(const struct timespec *abs_time)
94*61046927SAndroid Build Coastguard Worker {
95*61046927SAndroid Build Coastguard Worker     const time_t abs_ms = impl_timespec2msec(abs_time);
96*61046927SAndroid Build Coastguard Worker     struct timespec now;
97*61046927SAndroid Build Coastguard Worker     timespec_get(&now, TIME_UTC);
98*61046927SAndroid Build Coastguard Worker     const time_t now_ms = impl_timespec2msec(&now);
99*61046927SAndroid Build Coastguard Worker     const DWORD rel_ms = (abs_ms > now_ms) ? (DWORD)(abs_ms - now_ms) : 0;
100*61046927SAndroid Build Coastguard Worker     return rel_ms;
101*61046927SAndroid Build Coastguard Worker }
102*61046927SAndroid Build Coastguard Worker 
103*61046927SAndroid Build Coastguard Worker struct impl_call_once_param { void (*func)(void); };
impl_call_once_callback(PINIT_ONCE InitOnce,PVOID Parameter,PVOID * Context)104*61046927SAndroid Build Coastguard Worker static BOOL CALLBACK impl_call_once_callback(PINIT_ONCE InitOnce, PVOID Parameter, PVOID *Context)
105*61046927SAndroid Build Coastguard Worker {
106*61046927SAndroid Build Coastguard Worker     struct impl_call_once_param *param = (struct impl_call_once_param*)Parameter;
107*61046927SAndroid Build Coastguard Worker     (param->func)();
108*61046927SAndroid Build Coastguard Worker     ((void)InitOnce); ((void)Context);  // suppress warning
109*61046927SAndroid Build Coastguard Worker     return true;
110*61046927SAndroid Build Coastguard Worker }
111*61046927SAndroid Build Coastguard Worker 
112*61046927SAndroid Build Coastguard Worker static struct impl_tss_dtor_entry {
113*61046927SAndroid Build Coastguard Worker     tss_t key;
114*61046927SAndroid Build Coastguard Worker     tss_dtor_t dtor;
115*61046927SAndroid Build Coastguard Worker } impl_tss_dtor_tbl[EMULATED_THREADS_TSS_DTOR_SLOTNUM];
116*61046927SAndroid Build Coastguard Worker 
impl_tss_dtor_register(tss_t key,tss_dtor_t dtor)117*61046927SAndroid Build Coastguard Worker static int impl_tss_dtor_register(tss_t key, tss_dtor_t dtor)
118*61046927SAndroid Build Coastguard Worker {
119*61046927SAndroid Build Coastguard Worker     int i;
120*61046927SAndroid Build Coastguard Worker     for (i = 0; i < EMULATED_THREADS_TSS_DTOR_SLOTNUM; i++) {
121*61046927SAndroid Build Coastguard Worker         if (!impl_tss_dtor_tbl[i].dtor)
122*61046927SAndroid Build Coastguard Worker             break;
123*61046927SAndroid Build Coastguard Worker     }
124*61046927SAndroid Build Coastguard Worker     if (i == EMULATED_THREADS_TSS_DTOR_SLOTNUM)
125*61046927SAndroid Build Coastguard Worker         return 1;
126*61046927SAndroid Build Coastguard Worker     impl_tss_dtor_tbl[i].key = key;
127*61046927SAndroid Build Coastguard Worker     impl_tss_dtor_tbl[i].dtor = dtor;
128*61046927SAndroid Build Coastguard Worker     return 0;
129*61046927SAndroid Build Coastguard Worker }
130*61046927SAndroid Build Coastguard Worker 
impl_tss_dtor_invoke(void)131*61046927SAndroid Build Coastguard Worker static void impl_tss_dtor_invoke(void)
132*61046927SAndroid Build Coastguard Worker {
133*61046927SAndroid Build Coastguard Worker     int i;
134*61046927SAndroid Build Coastguard Worker     for (i = 0; i < EMULATED_THREADS_TSS_DTOR_SLOTNUM; i++) {
135*61046927SAndroid Build Coastguard Worker         if (impl_tss_dtor_tbl[i].dtor) {
136*61046927SAndroid Build Coastguard Worker             void* val = tss_get(impl_tss_dtor_tbl[i].key);
137*61046927SAndroid Build Coastguard Worker             if (val)
138*61046927SAndroid Build Coastguard Worker                 (impl_tss_dtor_tbl[i].dtor)(val);
139*61046927SAndroid Build Coastguard Worker         }
140*61046927SAndroid Build Coastguard Worker     }
141*61046927SAndroid Build Coastguard Worker }
142*61046927SAndroid Build Coastguard Worker 
143*61046927SAndroid Build Coastguard Worker 
144*61046927SAndroid Build Coastguard Worker /*--------------- 7.25.2 Initialization functions ---------------*/
145*61046927SAndroid Build Coastguard Worker // 7.25.2.1
146*61046927SAndroid Build Coastguard Worker void
call_once(once_flag * flag,void (* func)(void))147*61046927SAndroid Build Coastguard Worker call_once(once_flag *flag, void (*func)(void))
148*61046927SAndroid Build Coastguard Worker {
149*61046927SAndroid Build Coastguard Worker     assert(flag && func);
150*61046927SAndroid Build Coastguard Worker     struct impl_call_once_param param;
151*61046927SAndroid Build Coastguard Worker     param.func = func;
152*61046927SAndroid Build Coastguard Worker     InitOnceExecuteOnce((PINIT_ONCE)flag, impl_call_once_callback, (PVOID)&param, NULL);
153*61046927SAndroid Build Coastguard Worker }
154*61046927SAndroid Build Coastguard Worker 
155*61046927SAndroid Build Coastguard Worker 
156*61046927SAndroid Build Coastguard Worker /*------------- 7.25.3 Condition variable functions -------------*/
157*61046927SAndroid Build Coastguard Worker // 7.25.3.1
158*61046927SAndroid Build Coastguard Worker int
cnd_broadcast(cnd_t * cond)159*61046927SAndroid Build Coastguard Worker cnd_broadcast(cnd_t *cond)
160*61046927SAndroid Build Coastguard Worker {
161*61046927SAndroid Build Coastguard Worker     assert(cond != NULL);
162*61046927SAndroid Build Coastguard Worker     WakeAllConditionVariable((PCONDITION_VARIABLE)cond);
163*61046927SAndroid Build Coastguard Worker     return thrd_success;
164*61046927SAndroid Build Coastguard Worker }
165*61046927SAndroid Build Coastguard Worker 
166*61046927SAndroid Build Coastguard Worker // 7.25.3.2
167*61046927SAndroid Build Coastguard Worker void
cnd_destroy(cnd_t * cond)168*61046927SAndroid Build Coastguard Worker cnd_destroy(cnd_t *cond)
169*61046927SAndroid Build Coastguard Worker {
170*61046927SAndroid Build Coastguard Worker     (void)cond;
171*61046927SAndroid Build Coastguard Worker     assert(cond != NULL);
172*61046927SAndroid Build Coastguard Worker     // do nothing
173*61046927SAndroid Build Coastguard Worker }
174*61046927SAndroid Build Coastguard Worker 
175*61046927SAndroid Build Coastguard Worker // 7.25.3.3
176*61046927SAndroid Build Coastguard Worker int
cnd_init(cnd_t * cond)177*61046927SAndroid Build Coastguard Worker cnd_init(cnd_t *cond)
178*61046927SAndroid Build Coastguard Worker {
179*61046927SAndroid Build Coastguard Worker     assert(cond != NULL);
180*61046927SAndroid Build Coastguard Worker     InitializeConditionVariable((PCONDITION_VARIABLE)cond);
181*61046927SAndroid Build Coastguard Worker     return thrd_success;
182*61046927SAndroid Build Coastguard Worker }
183*61046927SAndroid Build Coastguard Worker 
184*61046927SAndroid Build Coastguard Worker // 7.25.3.4
185*61046927SAndroid Build Coastguard Worker int
cnd_signal(cnd_t * cond)186*61046927SAndroid Build Coastguard Worker cnd_signal(cnd_t *cond)
187*61046927SAndroid Build Coastguard Worker {
188*61046927SAndroid Build Coastguard Worker     assert(cond != NULL);
189*61046927SAndroid Build Coastguard Worker     WakeConditionVariable((PCONDITION_VARIABLE)cond);
190*61046927SAndroid Build Coastguard Worker     return thrd_success;
191*61046927SAndroid Build Coastguard Worker }
192*61046927SAndroid Build Coastguard Worker 
193*61046927SAndroid Build Coastguard Worker // 7.25.3.5
194*61046927SAndroid Build Coastguard Worker int
cnd_timedwait(cnd_t * cond,mtx_t * mtx,const struct timespec * abs_time)195*61046927SAndroid Build Coastguard Worker cnd_timedwait(cnd_t *cond, mtx_t *mtx, const struct timespec *abs_time)
196*61046927SAndroid Build Coastguard Worker {
197*61046927SAndroid Build Coastguard Worker     assert(cond != NULL);
198*61046927SAndroid Build Coastguard Worker     assert(mtx != NULL);
199*61046927SAndroid Build Coastguard Worker     assert(abs_time != NULL);
200*61046927SAndroid Build Coastguard Worker     const DWORD timeout = impl_abs2relmsec(abs_time);
201*61046927SAndroid Build Coastguard Worker     if (SleepConditionVariableCS((PCONDITION_VARIABLE)cond, (PCRITICAL_SECTION)mtx, timeout))
202*61046927SAndroid Build Coastguard Worker         return thrd_success;
203*61046927SAndroid Build Coastguard Worker     return (GetLastError() == ERROR_TIMEOUT) ? thrd_timedout : thrd_error;
204*61046927SAndroid Build Coastguard Worker }
205*61046927SAndroid Build Coastguard Worker 
206*61046927SAndroid Build Coastguard Worker // 7.25.3.6
207*61046927SAndroid Build Coastguard Worker int
cnd_wait(cnd_t * cond,mtx_t * mtx)208*61046927SAndroid Build Coastguard Worker cnd_wait(cnd_t *cond, mtx_t *mtx)
209*61046927SAndroid Build Coastguard Worker {
210*61046927SAndroid Build Coastguard Worker     assert(cond != NULL);
211*61046927SAndroid Build Coastguard Worker     assert(mtx != NULL);
212*61046927SAndroid Build Coastguard Worker     SleepConditionVariableCS((PCONDITION_VARIABLE)cond, (PCRITICAL_SECTION)mtx, INFINITE);
213*61046927SAndroid Build Coastguard Worker     return thrd_success;
214*61046927SAndroid Build Coastguard Worker }
215*61046927SAndroid Build Coastguard Worker 
216*61046927SAndroid Build Coastguard Worker 
217*61046927SAndroid Build Coastguard Worker /*-------------------- 7.25.4 Mutex functions --------------------*/
218*61046927SAndroid Build Coastguard Worker // 7.25.4.1
219*61046927SAndroid Build Coastguard Worker void
mtx_destroy(mtx_t * mtx)220*61046927SAndroid Build Coastguard Worker mtx_destroy(mtx_t *mtx)
221*61046927SAndroid Build Coastguard Worker {
222*61046927SAndroid Build Coastguard Worker     assert(mtx);
223*61046927SAndroid Build Coastguard Worker     DeleteCriticalSection((PCRITICAL_SECTION)mtx);
224*61046927SAndroid Build Coastguard Worker }
225*61046927SAndroid Build Coastguard Worker 
226*61046927SAndroid Build Coastguard Worker // 7.25.4.2
227*61046927SAndroid Build Coastguard Worker int
mtx_init(mtx_t * mtx,int type)228*61046927SAndroid Build Coastguard Worker mtx_init(mtx_t *mtx, int type)
229*61046927SAndroid Build Coastguard Worker {
230*61046927SAndroid Build Coastguard Worker     assert(mtx != NULL);
231*61046927SAndroid Build Coastguard Worker     if (type != mtx_plain && type != mtx_timed
232*61046927SAndroid Build Coastguard Worker       && type != (mtx_plain|mtx_recursive)
233*61046927SAndroid Build Coastguard Worker       && type != (mtx_timed|mtx_recursive))
234*61046927SAndroid Build Coastguard Worker         return thrd_error;
235*61046927SAndroid Build Coastguard Worker     InitializeCriticalSection((PCRITICAL_SECTION)mtx);
236*61046927SAndroid Build Coastguard Worker     return thrd_success;
237*61046927SAndroid Build Coastguard Worker }
238*61046927SAndroid Build Coastguard Worker 
239*61046927SAndroid Build Coastguard Worker // 7.25.4.3
240*61046927SAndroid Build Coastguard Worker int
mtx_lock(mtx_t * mtx)241*61046927SAndroid Build Coastguard Worker mtx_lock(mtx_t *mtx)
242*61046927SAndroid Build Coastguard Worker {
243*61046927SAndroid Build Coastguard Worker     assert(mtx != NULL);
244*61046927SAndroid Build Coastguard Worker     EnterCriticalSection((PCRITICAL_SECTION)mtx);
245*61046927SAndroid Build Coastguard Worker     return thrd_success;
246*61046927SAndroid Build Coastguard Worker }
247*61046927SAndroid Build Coastguard Worker 
248*61046927SAndroid Build Coastguard Worker // 7.25.4.4
249*61046927SAndroid Build Coastguard Worker int
mtx_timedlock(mtx_t * mtx,const struct timespec * ts)250*61046927SAndroid Build Coastguard Worker mtx_timedlock(mtx_t *mtx, const struct timespec *ts)
251*61046927SAndroid Build Coastguard Worker {
252*61046927SAndroid Build Coastguard Worker     assert(mtx != NULL);
253*61046927SAndroid Build Coastguard Worker     assert(ts != NULL);
254*61046927SAndroid Build Coastguard Worker     while (mtx_trylock(mtx) != thrd_success) {
255*61046927SAndroid Build Coastguard Worker         if (impl_abs2relmsec(ts) == 0)
256*61046927SAndroid Build Coastguard Worker             return thrd_timedout;
257*61046927SAndroid Build Coastguard Worker         // busy loop!
258*61046927SAndroid Build Coastguard Worker         thrd_yield();
259*61046927SAndroid Build Coastguard Worker     }
260*61046927SAndroid Build Coastguard Worker     return thrd_success;
261*61046927SAndroid Build Coastguard Worker }
262*61046927SAndroid Build Coastguard Worker 
263*61046927SAndroid Build Coastguard Worker // 7.25.4.5
264*61046927SAndroid Build Coastguard Worker int
mtx_trylock(mtx_t * mtx)265*61046927SAndroid Build Coastguard Worker mtx_trylock(mtx_t *mtx)
266*61046927SAndroid Build Coastguard Worker {
267*61046927SAndroid Build Coastguard Worker     assert(mtx != NULL);
268*61046927SAndroid Build Coastguard Worker     return TryEnterCriticalSection((PCRITICAL_SECTION)mtx) ? thrd_success : thrd_busy;
269*61046927SAndroid Build Coastguard Worker }
270*61046927SAndroid Build Coastguard Worker 
271*61046927SAndroid Build Coastguard Worker // 7.25.4.6
272*61046927SAndroid Build Coastguard Worker int
mtx_unlock(mtx_t * mtx)273*61046927SAndroid Build Coastguard Worker mtx_unlock(mtx_t *mtx)
274*61046927SAndroid Build Coastguard Worker {
275*61046927SAndroid Build Coastguard Worker     assert(mtx != NULL);
276*61046927SAndroid Build Coastguard Worker     LeaveCriticalSection((PCRITICAL_SECTION)mtx);
277*61046927SAndroid Build Coastguard Worker     return thrd_success;
278*61046927SAndroid Build Coastguard Worker }
279*61046927SAndroid Build Coastguard Worker 
280*61046927SAndroid Build Coastguard Worker void
__threads_win32_tls_callback(void)281*61046927SAndroid Build Coastguard Worker __threads_win32_tls_callback(void)
282*61046927SAndroid Build Coastguard Worker {
283*61046927SAndroid Build Coastguard Worker     struct thrd_state *state = &impl_current_thread;
284*61046927SAndroid Build Coastguard Worker     impl_tss_dtor_invoke();
285*61046927SAndroid Build Coastguard Worker     if (state->handle_need_close) {
286*61046927SAndroid Build Coastguard Worker         state->handle_need_close = false;
287*61046927SAndroid Build Coastguard Worker         CloseHandle(state->thrd.handle);
288*61046927SAndroid Build Coastguard Worker     }
289*61046927SAndroid Build Coastguard Worker }
290*61046927SAndroid Build Coastguard Worker 
291*61046927SAndroid Build Coastguard Worker /*------------------- 7.25.5 Thread functions -------------------*/
292*61046927SAndroid Build Coastguard Worker // 7.25.5.1
293*61046927SAndroid Build Coastguard Worker int
thrd_create(thrd_t * thr,thrd_start_t func,void * arg)294*61046927SAndroid Build Coastguard Worker thrd_create(thrd_t *thr, thrd_start_t func, void *arg)
295*61046927SAndroid Build Coastguard Worker {
296*61046927SAndroid Build Coastguard Worker     struct impl_thrd_param *pack;
297*61046927SAndroid Build Coastguard Worker     uintptr_t handle;
298*61046927SAndroid Build Coastguard Worker     assert(thr != NULL);
299*61046927SAndroid Build Coastguard Worker     pack = (struct impl_thrd_param *)malloc(sizeof(struct impl_thrd_param));
300*61046927SAndroid Build Coastguard Worker     if (!pack) return thrd_nomem;
301*61046927SAndroid Build Coastguard Worker     pack->func = func;
302*61046927SAndroid Build Coastguard Worker     pack->arg = arg;
303*61046927SAndroid Build Coastguard Worker     handle = _beginthreadex(NULL, 0, impl_thrd_routine, pack, CREATE_SUSPENDED, NULL);
304*61046927SAndroid Build Coastguard Worker     if (handle == 0) {
305*61046927SAndroid Build Coastguard Worker         free(pack);
306*61046927SAndroid Build Coastguard Worker         if (errno == EAGAIN || errno == EACCES)
307*61046927SAndroid Build Coastguard Worker             return thrd_nomem;
308*61046927SAndroid Build Coastguard Worker         return thrd_error;
309*61046927SAndroid Build Coastguard Worker     }
310*61046927SAndroid Build Coastguard Worker     thr->handle = (void*)handle;
311*61046927SAndroid Build Coastguard Worker     pack->thrd = *thr;
312*61046927SAndroid Build Coastguard Worker     ResumeThread((HANDLE)handle);
313*61046927SAndroid Build Coastguard Worker     return thrd_success;
314*61046927SAndroid Build Coastguard Worker }
315*61046927SAndroid Build Coastguard Worker 
316*61046927SAndroid Build Coastguard Worker // 7.25.5.2
317*61046927SAndroid Build Coastguard Worker thrd_t
thrd_current(void)318*61046927SAndroid Build Coastguard Worker thrd_current(void)
319*61046927SAndroid Build Coastguard Worker {
320*61046927SAndroid Build Coastguard Worker     /* GetCurrentThread() returns a pseudo-handle, which we need
321*61046927SAndroid Build Coastguard Worker      * to pass to DuplicateHandle(). Only the resulting handle can be used
322*61046927SAndroid Build Coastguard Worker      * from other threads.
323*61046927SAndroid Build Coastguard Worker      *
324*61046927SAndroid Build Coastguard Worker      * Note that neither handle can be compared to the one by thread_create.
325*61046927SAndroid Build Coastguard Worker      * Only the thread IDs - as returned by GetThreadId() and GetCurrentThreadId()
326*61046927SAndroid Build Coastguard Worker      * can be compared directly.
327*61046927SAndroid Build Coastguard Worker      *
328*61046927SAndroid Build Coastguard Worker      * Other potential solutions would be:
329*61046927SAndroid Build Coastguard Worker      * - define thrd_t as a thread Ids, but this would mean we'd need to OpenThread for many operations
330*61046927SAndroid Build Coastguard Worker      * - use malloc'ed memory for thrd_t. This would imply using TLS for current thread.
331*61046927SAndroid Build Coastguard Worker      *
332*61046927SAndroid Build Coastguard Worker      * Neither is particularly nice.
333*61046927SAndroid Build Coastguard Worker      *
334*61046927SAndroid Build Coastguard Worker      * Life would be much easier if C11 threads had different abstractions for
335*61046927SAndroid Build Coastguard Worker      * threads and thread IDs, just like C++11 threads does...
336*61046927SAndroid Build Coastguard Worker      */
337*61046927SAndroid Build Coastguard Worker     struct thrd_state *state = &impl_current_thread;
338*61046927SAndroid Build Coastguard Worker     if (state->thrd.handle == NULL)
339*61046927SAndroid Build Coastguard Worker     {
340*61046927SAndroid Build Coastguard Worker         if (!DuplicateHandle(GetCurrentProcess(), GetCurrentThread(), GetCurrentProcess(),
341*61046927SAndroid Build Coastguard Worker             &(state->thrd.handle), 0, false, DUPLICATE_SAME_ACCESS))
342*61046927SAndroid Build Coastguard Worker         {
343*61046927SAndroid Build Coastguard Worker             abort();
344*61046927SAndroid Build Coastguard Worker         }
345*61046927SAndroid Build Coastguard Worker         state->handle_need_close = true;
346*61046927SAndroid Build Coastguard Worker     }
347*61046927SAndroid Build Coastguard Worker     return state->thrd;
348*61046927SAndroid Build Coastguard Worker }
349*61046927SAndroid Build Coastguard Worker 
350*61046927SAndroid Build Coastguard Worker // 7.25.5.3
351*61046927SAndroid Build Coastguard Worker int
thrd_detach(thrd_t thr)352*61046927SAndroid Build Coastguard Worker thrd_detach(thrd_t thr)
353*61046927SAndroid Build Coastguard Worker {
354*61046927SAndroid Build Coastguard Worker     CloseHandle(thr.handle);
355*61046927SAndroid Build Coastguard Worker     return thrd_success;
356*61046927SAndroid Build Coastguard Worker }
357*61046927SAndroid Build Coastguard Worker 
358*61046927SAndroid Build Coastguard Worker // 7.25.5.4
359*61046927SAndroid Build Coastguard Worker int
thrd_equal(thrd_t thr0,thrd_t thr1)360*61046927SAndroid Build Coastguard Worker thrd_equal(thrd_t thr0, thrd_t thr1)
361*61046927SAndroid Build Coastguard Worker {
362*61046927SAndroid Build Coastguard Worker     return GetThreadId(thr0.handle) == GetThreadId(thr1.handle);
363*61046927SAndroid Build Coastguard Worker }
364*61046927SAndroid Build Coastguard Worker 
365*61046927SAndroid Build Coastguard Worker // 7.25.5.5
366*61046927SAndroid Build Coastguard Worker _Noreturn
367*61046927SAndroid Build Coastguard Worker void
thrd_exit(int res)368*61046927SAndroid Build Coastguard Worker thrd_exit(int res)
369*61046927SAndroid Build Coastguard Worker {
370*61046927SAndroid Build Coastguard Worker     _endthreadex((unsigned)res);
371*61046927SAndroid Build Coastguard Worker }
372*61046927SAndroid Build Coastguard Worker 
373*61046927SAndroid Build Coastguard Worker // 7.25.5.6
374*61046927SAndroid Build Coastguard Worker int
thrd_join(thrd_t thr,int * res)375*61046927SAndroid Build Coastguard Worker thrd_join(thrd_t thr, int *res)
376*61046927SAndroid Build Coastguard Worker {
377*61046927SAndroid Build Coastguard Worker     DWORD w, code;
378*61046927SAndroid Build Coastguard Worker     if (thr.handle == NULL) {
379*61046927SAndroid Build Coastguard Worker         return thrd_error;
380*61046927SAndroid Build Coastguard Worker     }
381*61046927SAndroid Build Coastguard Worker     w = WaitForSingleObject(thr.handle, INFINITE);
382*61046927SAndroid Build Coastguard Worker     if (w != WAIT_OBJECT_0)
383*61046927SAndroid Build Coastguard Worker         return thrd_error;
384*61046927SAndroid Build Coastguard Worker     if (res) {
385*61046927SAndroid Build Coastguard Worker         if (!GetExitCodeThread(thr.handle, &code)) {
386*61046927SAndroid Build Coastguard Worker             CloseHandle(thr.handle);
387*61046927SAndroid Build Coastguard Worker             return thrd_error;
388*61046927SAndroid Build Coastguard Worker         }
389*61046927SAndroid Build Coastguard Worker         *res = (int)code;
390*61046927SAndroid Build Coastguard Worker     }
391*61046927SAndroid Build Coastguard Worker     CloseHandle(thr.handle);
392*61046927SAndroid Build Coastguard Worker     return thrd_success;
393*61046927SAndroid Build Coastguard Worker }
394*61046927SAndroid Build Coastguard Worker 
395*61046927SAndroid Build Coastguard Worker // 7.25.5.7
396*61046927SAndroid Build Coastguard Worker int
thrd_sleep(const struct timespec * time_point,struct timespec * remaining)397*61046927SAndroid Build Coastguard Worker thrd_sleep(const struct timespec *time_point, struct timespec *remaining)
398*61046927SAndroid Build Coastguard Worker {
399*61046927SAndroid Build Coastguard Worker     (void)remaining;
400*61046927SAndroid Build Coastguard Worker     assert(time_point);
401*61046927SAndroid Build Coastguard Worker     assert(!remaining); /* not implemented */
402*61046927SAndroid Build Coastguard Worker     Sleep((DWORD)impl_timespec2msec(time_point));
403*61046927SAndroid Build Coastguard Worker     return 0;
404*61046927SAndroid Build Coastguard Worker }
405*61046927SAndroid Build Coastguard Worker 
406*61046927SAndroid Build Coastguard Worker // 7.25.5.8
407*61046927SAndroid Build Coastguard Worker void
thrd_yield(void)408*61046927SAndroid Build Coastguard Worker thrd_yield(void)
409*61046927SAndroid Build Coastguard Worker {
410*61046927SAndroid Build Coastguard Worker     SwitchToThread();
411*61046927SAndroid Build Coastguard Worker }
412*61046927SAndroid Build Coastguard Worker 
413*61046927SAndroid Build Coastguard Worker 
414*61046927SAndroid Build Coastguard Worker /*----------- 7.25.6 Thread-specific storage functions -----------*/
415*61046927SAndroid Build Coastguard Worker // 7.25.6.1
416*61046927SAndroid Build Coastguard Worker int
tss_create(tss_t * key,tss_dtor_t dtor)417*61046927SAndroid Build Coastguard Worker tss_create(tss_t *key, tss_dtor_t dtor)
418*61046927SAndroid Build Coastguard Worker {
419*61046927SAndroid Build Coastguard Worker     assert(key != NULL);
420*61046927SAndroid Build Coastguard Worker     *key = TlsAlloc();
421*61046927SAndroid Build Coastguard Worker     if (dtor) {
422*61046927SAndroid Build Coastguard Worker         if (impl_tss_dtor_register(*key, dtor)) {
423*61046927SAndroid Build Coastguard Worker             TlsFree(*key);
424*61046927SAndroid Build Coastguard Worker             return thrd_error;
425*61046927SAndroid Build Coastguard Worker         }
426*61046927SAndroid Build Coastguard Worker     }
427*61046927SAndroid Build Coastguard Worker     return (*key != 0xFFFFFFFF) ? thrd_success : thrd_error;
428*61046927SAndroid Build Coastguard Worker }
429*61046927SAndroid Build Coastguard Worker 
430*61046927SAndroid Build Coastguard Worker // 7.25.6.2
431*61046927SAndroid Build Coastguard Worker void
tss_delete(tss_t key)432*61046927SAndroid Build Coastguard Worker tss_delete(tss_t key)
433*61046927SAndroid Build Coastguard Worker {
434*61046927SAndroid Build Coastguard Worker     TlsFree(key);
435*61046927SAndroid Build Coastguard Worker }
436*61046927SAndroid Build Coastguard Worker 
437*61046927SAndroid Build Coastguard Worker // 7.25.6.3
438*61046927SAndroid Build Coastguard Worker void *
tss_get(tss_t key)439*61046927SAndroid Build Coastguard Worker tss_get(tss_t key)
440*61046927SAndroid Build Coastguard Worker {
441*61046927SAndroid Build Coastguard Worker     return TlsGetValue(key);
442*61046927SAndroid Build Coastguard Worker }
443*61046927SAndroid Build Coastguard Worker 
444*61046927SAndroid Build Coastguard Worker // 7.25.6.4
445*61046927SAndroid Build Coastguard Worker int
tss_set(tss_t key,void * val)446*61046927SAndroid Build Coastguard Worker tss_set(tss_t key, void *val)
447*61046927SAndroid Build Coastguard Worker {
448*61046927SAndroid Build Coastguard Worker     return TlsSetValue(key, val) ? thrd_success : thrd_error;
449*61046927SAndroid Build Coastguard Worker }
450