threads_win32.h revision af69d88d
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 78af69d88dSmrg 79af69d88dSmrg/*---------------------------- macros ----------------------------*/ 80af69d88dSmrg#ifdef EMULATED_THREADS_USE_NATIVE_CALL_ONCE 81af69d88dSmrg#define ONCE_FLAG_INIT INIT_ONCE_STATIC_INIT 82af69d88dSmrg#else 83af69d88dSmrg#define ONCE_FLAG_INIT {0} 84af69d88dSmrg#endif 85af69d88dSmrg#define TSS_DTOR_ITERATIONS 1 86af69d88dSmrg 87af69d88dSmrg// FIXME: temporary non-standard hack to ease transition 88af69d88dSmrg#define _MTX_INITIALIZER_NP {(PCRITICAL_SECTION_DEBUG)-1, -1, 0, 0, 0, 0} 89af69d88dSmrg 90af69d88dSmrg/*---------------------------- types ----------------------------*/ 91af69d88dSmrgtypedef struct cnd_t { 92af69d88dSmrg#ifdef EMULATED_THREADS_USE_NATIVE_CV 93af69d88dSmrg CONDITION_VARIABLE condvar; 94af69d88dSmrg#else 95af69d88dSmrg int blocked; 96af69d88dSmrg int gone; 97af69d88dSmrg int to_unblock; 98af69d88dSmrg HANDLE sem_queue; 99af69d88dSmrg HANDLE sem_gate; 100af69d88dSmrg CRITICAL_SECTION monitor; 101af69d88dSmrg#endif 102af69d88dSmrg} cnd_t; 103af69d88dSmrg 104af69d88dSmrgtypedef HANDLE thrd_t; 105af69d88dSmrg 106af69d88dSmrgtypedef DWORD tss_t; 107af69d88dSmrg 108af69d88dSmrgtypedef CRITICAL_SECTION mtx_t; 109af69d88dSmrg 110af69d88dSmrg#ifdef EMULATED_THREADS_USE_NATIVE_CALL_ONCE 111af69d88dSmrgtypedef INIT_ONCE once_flag; 112af69d88dSmrg#else 113af69d88dSmrgtypedef struct once_flag_t { 114af69d88dSmrg volatile LONG status; 115af69d88dSmrg} once_flag; 116af69d88dSmrg#endif 117af69d88dSmrg 118af69d88dSmrg 119af69d88dSmrgstatic inline void * tss_get(tss_t key); 120af69d88dSmrgstatic inline void thrd_yield(void); 121af69d88dSmrgstatic inline int mtx_trylock(mtx_t *mtx); 122af69d88dSmrgstatic inline int mtx_lock(mtx_t *mtx); 123af69d88dSmrgstatic inline int mtx_unlock(mtx_t *mtx); 124af69d88dSmrg 125af69d88dSmrg/* 126af69d88dSmrgImplementation limits: 127af69d88dSmrg - Conditionally emulation for "Initialization functions" 128af69d88dSmrg (see EMULATED_THREADS_USE_NATIVE_CALL_ONCE macro) 129af69d88dSmrg - Emulated `mtx_timelock()' with mtx_trylock() + *busy loop* 130af69d88dSmrg*/ 131af69d88dSmrgstatic void impl_tss_dtor_invoke(void); // forward decl. 132af69d88dSmrg 133af69d88dSmrgstruct impl_thrd_param { 134af69d88dSmrg thrd_start_t func; 135af69d88dSmrg void *arg; 136af69d88dSmrg}; 137af69d88dSmrg 138af69d88dSmrgstatic unsigned __stdcall impl_thrd_routine(void *p) 139af69d88dSmrg{ 140af69d88dSmrg struct impl_thrd_param pack; 141af69d88dSmrg int code; 142af69d88dSmrg memcpy(&pack, p, sizeof(struct impl_thrd_param)); 143af69d88dSmrg free(p); 144af69d88dSmrg code = pack.func(pack.arg); 145af69d88dSmrg impl_tss_dtor_invoke(); 146af69d88dSmrg return (unsigned)code; 147af69d88dSmrg} 148af69d88dSmrg 149af69d88dSmrgstatic DWORD impl_xtime2msec(const xtime *xt) 150af69d88dSmrg{ 151af69d88dSmrg return (DWORD)((xt->sec * 1000U) + (xt->nsec / 1000000L)); 152af69d88dSmrg} 153af69d88dSmrg 154af69d88dSmrg#ifdef EMULATED_THREADS_USE_NATIVE_CALL_ONCE 155af69d88dSmrgstruct impl_call_once_param { void (*func)(void); }; 156af69d88dSmrgstatic BOOL CALLBACK impl_call_once_callback(PINIT_ONCE InitOnce, PVOID Parameter, PVOID *Context) 157af69d88dSmrg{ 158af69d88dSmrg struct impl_call_once_param *param = (struct impl_call_once_param*)Parameter; 159af69d88dSmrg (param->func)(); 160af69d88dSmrg ((void)InitOnce); ((void)Context); // suppress warning 161af69d88dSmrg return TRUE; 162af69d88dSmrg} 163af69d88dSmrg#endif // ifdef EMULATED_THREADS_USE_NATIVE_CALL_ONCE 164af69d88dSmrg 165af69d88dSmrg#ifndef EMULATED_THREADS_USE_NATIVE_CV 166af69d88dSmrg/* 167af69d88dSmrgNote: 168af69d88dSmrg The implementation of condition variable is ported from Boost.Interprocess 169af69d88dSmrg See http://www.boost.org/boost/interprocess/sync/windows/condition.hpp 170af69d88dSmrg*/ 171af69d88dSmrgstatic void impl_cond_do_signal(cnd_t *cond, int broadcast) 172af69d88dSmrg{ 173af69d88dSmrg int nsignal = 0; 174af69d88dSmrg 175af69d88dSmrg EnterCriticalSection(&cond->monitor); 176af69d88dSmrg if (cond->to_unblock != 0) { 177af69d88dSmrg if (cond->blocked == 0) { 178af69d88dSmrg LeaveCriticalSection(&cond->monitor); 179af69d88dSmrg return; 180af69d88dSmrg } 181af69d88dSmrg if (broadcast) { 182af69d88dSmrg cond->to_unblock += nsignal = cond->blocked; 183af69d88dSmrg cond->blocked = 0; 184af69d88dSmrg } else { 185af69d88dSmrg nsignal = 1; 186af69d88dSmrg cond->to_unblock++; 187af69d88dSmrg cond->blocked--; 188af69d88dSmrg } 189af69d88dSmrg } else if (cond->blocked > cond->gone) { 190af69d88dSmrg WaitForSingleObject(cond->sem_gate, INFINITE); 191af69d88dSmrg if (cond->gone != 0) { 192af69d88dSmrg cond->blocked -= cond->gone; 193af69d88dSmrg cond->gone = 0; 194af69d88dSmrg } 195af69d88dSmrg if (broadcast) { 196af69d88dSmrg nsignal = cond->to_unblock = cond->blocked; 197af69d88dSmrg cond->blocked = 0; 198af69d88dSmrg } else { 199af69d88dSmrg nsignal = cond->to_unblock = 1; 200af69d88dSmrg cond->blocked--; 201af69d88dSmrg } 202af69d88dSmrg } 203af69d88dSmrg LeaveCriticalSection(&cond->monitor); 204af69d88dSmrg 205af69d88dSmrg if (0 < nsignal) 206af69d88dSmrg ReleaseSemaphore(cond->sem_queue, nsignal, NULL); 207af69d88dSmrg} 208af69d88dSmrg 209af69d88dSmrgstatic int impl_cond_do_wait(cnd_t *cond, mtx_t *mtx, const xtime *xt) 210af69d88dSmrg{ 211af69d88dSmrg int nleft = 0; 212af69d88dSmrg int ngone = 0; 213af69d88dSmrg int timeout = 0; 214af69d88dSmrg DWORD w; 215af69d88dSmrg 216af69d88dSmrg WaitForSingleObject(cond->sem_gate, INFINITE); 217af69d88dSmrg cond->blocked++; 218af69d88dSmrg ReleaseSemaphore(cond->sem_gate, 1, NULL); 219af69d88dSmrg 220af69d88dSmrg mtx_unlock(mtx); 221af69d88dSmrg 222af69d88dSmrg w = WaitForSingleObject(cond->sem_queue, xt ? impl_xtime2msec(xt) : INFINITE); 223af69d88dSmrg timeout = (w == WAIT_TIMEOUT); 224af69d88dSmrg 225af69d88dSmrg EnterCriticalSection(&cond->monitor); 226af69d88dSmrg if ((nleft = cond->to_unblock) != 0) { 227af69d88dSmrg if (timeout) { 228af69d88dSmrg if (cond->blocked != 0) { 229af69d88dSmrg cond->blocked--; 230af69d88dSmrg } else { 231af69d88dSmrg cond->gone++; 232af69d88dSmrg } 233af69d88dSmrg } 234af69d88dSmrg if (--cond->to_unblock == 0) { 235af69d88dSmrg if (cond->blocked != 0) { 236af69d88dSmrg ReleaseSemaphore(cond->sem_gate, 1, NULL); 237af69d88dSmrg nleft = 0; 238af69d88dSmrg } 239af69d88dSmrg else if ((ngone = cond->gone) != 0) { 240af69d88dSmrg cond->gone = 0; 241af69d88dSmrg } 242af69d88dSmrg } 243af69d88dSmrg } else if (++cond->gone == INT_MAX/2) { 244af69d88dSmrg WaitForSingleObject(cond->sem_gate, INFINITE); 245af69d88dSmrg cond->blocked -= cond->gone; 246af69d88dSmrg ReleaseSemaphore(cond->sem_gate, 1, NULL); 247af69d88dSmrg cond->gone = 0; 248af69d88dSmrg } 249af69d88dSmrg LeaveCriticalSection(&cond->monitor); 250af69d88dSmrg 251af69d88dSmrg if (nleft == 1) { 252af69d88dSmrg while (ngone--) 253af69d88dSmrg WaitForSingleObject(cond->sem_queue, INFINITE); 254af69d88dSmrg ReleaseSemaphore(cond->sem_gate, 1, NULL); 255af69d88dSmrg } 256af69d88dSmrg 257af69d88dSmrg mtx_lock(mtx); 258af69d88dSmrg return timeout ? thrd_busy : thrd_success; 259af69d88dSmrg} 260af69d88dSmrg#endif // ifndef EMULATED_THREADS_USE_NATIVE_CV 261af69d88dSmrg 262af69d88dSmrgstatic struct impl_tss_dtor_entry { 263af69d88dSmrg tss_t key; 264af69d88dSmrg tss_dtor_t dtor; 265af69d88dSmrg} impl_tss_dtor_tbl[EMULATED_THREADS_TSS_DTOR_SLOTNUM]; 266af69d88dSmrg 267af69d88dSmrgstatic int impl_tss_dtor_register(tss_t key, tss_dtor_t dtor) 268af69d88dSmrg{ 269af69d88dSmrg int i; 270af69d88dSmrg for (i = 0; i < EMULATED_THREADS_TSS_DTOR_SLOTNUM; i++) { 271af69d88dSmrg if (!impl_tss_dtor_tbl[i].dtor) 272af69d88dSmrg break; 273af69d88dSmrg } 274af69d88dSmrg if (i == EMULATED_THREADS_TSS_DTOR_SLOTNUM) 275af69d88dSmrg return 1; 276af69d88dSmrg impl_tss_dtor_tbl[i].key = key; 277af69d88dSmrg impl_tss_dtor_tbl[i].dtor = dtor; 278af69d88dSmrg return 0; 279af69d88dSmrg} 280af69d88dSmrg 281af69d88dSmrgstatic void impl_tss_dtor_invoke() 282af69d88dSmrg{ 283af69d88dSmrg int i; 284af69d88dSmrg for (i = 0; i < EMULATED_THREADS_TSS_DTOR_SLOTNUM; i++) { 285af69d88dSmrg if (impl_tss_dtor_tbl[i].dtor) { 286af69d88dSmrg void* val = tss_get(impl_tss_dtor_tbl[i].key); 287af69d88dSmrg if (val) 288af69d88dSmrg (impl_tss_dtor_tbl[i].dtor)(val); 289af69d88dSmrg } 290af69d88dSmrg } 291af69d88dSmrg} 292af69d88dSmrg 293af69d88dSmrg 294af69d88dSmrg/*--------------- 7.25.2 Initialization functions ---------------*/ 295af69d88dSmrg// 7.25.2.1 296af69d88dSmrgstatic inline void 297af69d88dSmrgcall_once(once_flag *flag, void (*func)(void)) 298af69d88dSmrg{ 299af69d88dSmrg assert(flag && func); 300af69d88dSmrg#ifdef EMULATED_THREADS_USE_NATIVE_CALL_ONCE 301af69d88dSmrg { 302af69d88dSmrg struct impl_call_once_param param; 303af69d88dSmrg param.func = func; 304af69d88dSmrg InitOnceExecuteOnce(flag, impl_call_once_callback, (PVOID)¶m, NULL); 305af69d88dSmrg } 306af69d88dSmrg#else 307af69d88dSmrg if (InterlockedCompareExchange(&flag->status, 1, 0) == 0) { 308af69d88dSmrg (func)(); 309af69d88dSmrg InterlockedExchange(&flag->status, 2); 310af69d88dSmrg } else { 311af69d88dSmrg while (flag->status == 1) { 312af69d88dSmrg // busy loop! 313af69d88dSmrg thrd_yield(); 314af69d88dSmrg } 315af69d88dSmrg } 316af69d88dSmrg#endif 317af69d88dSmrg} 318af69d88dSmrg 319af69d88dSmrg 320af69d88dSmrg/*------------- 7.25.3 Condition variable functions -------------*/ 321af69d88dSmrg// 7.25.3.1 322af69d88dSmrgstatic inline int 323af69d88dSmrgcnd_broadcast(cnd_t *cond) 324af69d88dSmrg{ 325af69d88dSmrg if (!cond) return thrd_error; 326af69d88dSmrg#ifdef EMULATED_THREADS_USE_NATIVE_CV 327af69d88dSmrg WakeAllConditionVariable(&cond->condvar); 328af69d88dSmrg#else 329af69d88dSmrg impl_cond_do_signal(cond, 1); 330af69d88dSmrg#endif 331af69d88dSmrg return thrd_success; 332af69d88dSmrg} 333af69d88dSmrg 334af69d88dSmrg// 7.25.3.2 335af69d88dSmrgstatic inline void 336af69d88dSmrgcnd_destroy(cnd_t *cond) 337af69d88dSmrg{ 338af69d88dSmrg assert(cond); 339af69d88dSmrg#ifdef EMULATED_THREADS_USE_NATIVE_CV 340af69d88dSmrg // do nothing 341af69d88dSmrg#else 342af69d88dSmrg CloseHandle(cond->sem_queue); 343af69d88dSmrg CloseHandle(cond->sem_gate); 344af69d88dSmrg DeleteCriticalSection(&cond->monitor); 345af69d88dSmrg#endif 346af69d88dSmrg} 347af69d88dSmrg 348af69d88dSmrg// 7.25.3.3 349af69d88dSmrgstatic inline int 350af69d88dSmrgcnd_init(cnd_t *cond) 351af69d88dSmrg{ 352af69d88dSmrg if (!cond) return thrd_error; 353af69d88dSmrg#ifdef EMULATED_THREADS_USE_NATIVE_CV 354af69d88dSmrg InitializeConditionVariable(&cond->condvar); 355af69d88dSmrg#else 356af69d88dSmrg cond->blocked = 0; 357af69d88dSmrg cond->gone = 0; 358af69d88dSmrg cond->to_unblock = 0; 359af69d88dSmrg cond->sem_queue = CreateSemaphore(NULL, 0, LONG_MAX, NULL); 360af69d88dSmrg cond->sem_gate = CreateSemaphore(NULL, 1, 1, NULL); 361af69d88dSmrg InitializeCriticalSection(&cond->monitor); 362af69d88dSmrg#endif 363af69d88dSmrg return thrd_success; 364af69d88dSmrg} 365af69d88dSmrg 366af69d88dSmrg// 7.25.3.4 367af69d88dSmrgstatic inline int 368af69d88dSmrgcnd_signal(cnd_t *cond) 369af69d88dSmrg{ 370af69d88dSmrg if (!cond) return thrd_error; 371af69d88dSmrg#ifdef EMULATED_THREADS_USE_NATIVE_CV 372af69d88dSmrg WakeConditionVariable(&cond->condvar); 373af69d88dSmrg#else 374af69d88dSmrg impl_cond_do_signal(cond, 0); 375af69d88dSmrg#endif 376af69d88dSmrg return thrd_success; 377af69d88dSmrg} 378af69d88dSmrg 379af69d88dSmrg// 7.25.3.5 380af69d88dSmrgstatic inline int 381af69d88dSmrgcnd_timedwait(cnd_t *cond, mtx_t *mtx, const xtime *xt) 382af69d88dSmrg{ 383af69d88dSmrg if (!cond || !mtx || !xt) return thrd_error; 384af69d88dSmrg#ifdef EMULATED_THREADS_USE_NATIVE_CV 385af69d88dSmrg if (SleepConditionVariableCS(&cond->condvar, mtx, impl_xtime2msec(xt))) 386af69d88dSmrg return thrd_success; 387af69d88dSmrg return (GetLastError() == ERROR_TIMEOUT) ? thrd_busy : thrd_error; 388af69d88dSmrg#else 389af69d88dSmrg return impl_cond_do_wait(cond, mtx, xt); 390af69d88dSmrg#endif 391af69d88dSmrg} 392af69d88dSmrg 393af69d88dSmrg// 7.25.3.6 394af69d88dSmrgstatic inline int 395af69d88dSmrgcnd_wait(cnd_t *cond, mtx_t *mtx) 396af69d88dSmrg{ 397af69d88dSmrg if (!cond || !mtx) return thrd_error; 398af69d88dSmrg#ifdef EMULATED_THREADS_USE_NATIVE_CV 399af69d88dSmrg SleepConditionVariableCS(&cond->condvar, mtx, INFINITE); 400af69d88dSmrg#else 401af69d88dSmrg impl_cond_do_wait(cond, mtx, NULL); 402af69d88dSmrg#endif 403af69d88dSmrg return thrd_success; 404af69d88dSmrg} 405af69d88dSmrg 406af69d88dSmrg 407af69d88dSmrg/*-------------------- 7.25.4 Mutex functions --------------------*/ 408af69d88dSmrg// 7.25.4.1 409af69d88dSmrgstatic inline void 410af69d88dSmrgmtx_destroy(mtx_t *mtx) 411af69d88dSmrg{ 412af69d88dSmrg assert(mtx); 413af69d88dSmrg DeleteCriticalSection(mtx); 414af69d88dSmrg} 415af69d88dSmrg 416af69d88dSmrg// 7.25.4.2 417af69d88dSmrgstatic inline int 418af69d88dSmrgmtx_init(mtx_t *mtx, int type) 419af69d88dSmrg{ 420af69d88dSmrg if (!mtx) return thrd_error; 421af69d88dSmrg if (type != mtx_plain && type != mtx_timed && type != mtx_try 422af69d88dSmrg && type != (mtx_plain|mtx_recursive) 423af69d88dSmrg && type != (mtx_timed|mtx_recursive) 424af69d88dSmrg && type != (mtx_try|mtx_recursive)) 425af69d88dSmrg return thrd_error; 426af69d88dSmrg InitializeCriticalSection(mtx); 427af69d88dSmrg return thrd_success; 428af69d88dSmrg} 429af69d88dSmrg 430af69d88dSmrg// 7.25.4.3 431af69d88dSmrgstatic inline int 432af69d88dSmrgmtx_lock(mtx_t *mtx) 433af69d88dSmrg{ 434af69d88dSmrg if (!mtx) return thrd_error; 435af69d88dSmrg EnterCriticalSection(mtx); 436af69d88dSmrg return thrd_success; 437af69d88dSmrg} 438af69d88dSmrg 439af69d88dSmrg// 7.25.4.4 440af69d88dSmrgstatic inline int 441af69d88dSmrgmtx_timedlock(mtx_t *mtx, const xtime *xt) 442af69d88dSmrg{ 443af69d88dSmrg time_t expire, now; 444af69d88dSmrg if (!mtx || !xt) return thrd_error; 445af69d88dSmrg expire = time(NULL); 446af69d88dSmrg expire += xt->sec; 447af69d88dSmrg while (mtx_trylock(mtx) != thrd_success) { 448af69d88dSmrg now = time(NULL); 449af69d88dSmrg if (expire < now) 450af69d88dSmrg return thrd_busy; 451af69d88dSmrg // busy loop! 452af69d88dSmrg thrd_yield(); 453af69d88dSmrg } 454af69d88dSmrg return thrd_success; 455af69d88dSmrg} 456af69d88dSmrg 457af69d88dSmrg// 7.25.4.5 458af69d88dSmrgstatic inline int 459af69d88dSmrgmtx_trylock(mtx_t *mtx) 460af69d88dSmrg{ 461af69d88dSmrg if (!mtx) return thrd_error; 462af69d88dSmrg return TryEnterCriticalSection(mtx) ? thrd_success : thrd_busy; 463af69d88dSmrg} 464af69d88dSmrg 465af69d88dSmrg// 7.25.4.6 466af69d88dSmrgstatic inline int 467af69d88dSmrgmtx_unlock(mtx_t *mtx) 468af69d88dSmrg{ 469af69d88dSmrg if (!mtx) return thrd_error; 470af69d88dSmrg LeaveCriticalSection(mtx); 471af69d88dSmrg return thrd_success; 472af69d88dSmrg} 473af69d88dSmrg 474af69d88dSmrg 475af69d88dSmrg/*------------------- 7.25.5 Thread functions -------------------*/ 476af69d88dSmrg// 7.25.5.1 477af69d88dSmrgstatic inline int 478af69d88dSmrgthrd_create(thrd_t *thr, thrd_start_t func, void *arg) 479af69d88dSmrg{ 480af69d88dSmrg struct impl_thrd_param *pack; 481af69d88dSmrg uintptr_t handle; 482af69d88dSmrg if (!thr) return thrd_error; 483af69d88dSmrg pack = (struct impl_thrd_param *)malloc(sizeof(struct impl_thrd_param)); 484af69d88dSmrg if (!pack) return thrd_nomem; 485af69d88dSmrg pack->func = func; 486af69d88dSmrg pack->arg = arg; 487af69d88dSmrg handle = _beginthreadex(NULL, 0, impl_thrd_routine, pack, 0, NULL); 488af69d88dSmrg if (handle == 0) { 489af69d88dSmrg if (errno == EAGAIN || errno == EACCES) 490af69d88dSmrg return thrd_nomem; 491af69d88dSmrg return thrd_error; 492af69d88dSmrg } 493af69d88dSmrg *thr = (thrd_t)handle; 494af69d88dSmrg return thrd_success; 495af69d88dSmrg} 496af69d88dSmrg 497af69d88dSmrg#if 0 498af69d88dSmrg// 7.25.5.2 499af69d88dSmrgstatic inline thrd_t 500af69d88dSmrgthrd_current(void) 501af69d88dSmrg{ 502af69d88dSmrg HANDLE hCurrentThread; 503af69d88dSmrg BOOL bRet; 504af69d88dSmrg 505af69d88dSmrg /* GetCurrentThread() returns a pseudo-handle, which is useless. We need 506af69d88dSmrg * to call DuplicateHandle to get a real handle. However the handle value 507af69d88dSmrg * will not match the one returned by thread_create. 508af69d88dSmrg * 509af69d88dSmrg * Other potential solutions would be: 510af69d88dSmrg * - define thrd_t as a thread Ids, but this would mean we'd need to OpenThread for many operations 511af69d88dSmrg * - use malloc'ed memory for thrd_t. This would imply using TLS for current thread. 512af69d88dSmrg * 513af69d88dSmrg * Neither is particularly nice. 514af69d88dSmrg * 515af69d88dSmrg * Life would be much easier if C11 threads had different abstractions for 516af69d88dSmrg * threads and thread IDs, just like C++11 threads does... 517af69d88dSmrg */ 518af69d88dSmrg 519af69d88dSmrg bRet = DuplicateHandle(GetCurrentProcess(), // source process (pseudo) handle 520af69d88dSmrg GetCurrentThread(), // source (pseudo) handle 521af69d88dSmrg GetCurrentProcess(), // target process 522af69d88dSmrg &hCurrentThread, // target handle 523af69d88dSmrg 0, 524af69d88dSmrg FALSE, 525af69d88dSmrg DUPLICATE_SAME_ACCESS); 526af69d88dSmrg assert(bRet); 527af69d88dSmrg if (!bRet) { 528af69d88dSmrg hCurrentThread = GetCurrentThread(); 529af69d88dSmrg } 530af69d88dSmrg return hCurrentThread; 531af69d88dSmrg} 532af69d88dSmrg#endif 533af69d88dSmrg 534af69d88dSmrg// 7.25.5.3 535af69d88dSmrgstatic inline int 536af69d88dSmrgthrd_detach(thrd_t thr) 537af69d88dSmrg{ 538af69d88dSmrg CloseHandle(thr); 539af69d88dSmrg return thrd_success; 540af69d88dSmrg} 541af69d88dSmrg 542af69d88dSmrg// 7.25.5.4 543af69d88dSmrgstatic inline int 544af69d88dSmrgthrd_equal(thrd_t thr0, thrd_t thr1) 545af69d88dSmrg{ 546af69d88dSmrg return GetThreadId(thr0) == GetThreadId(thr1); 547af69d88dSmrg} 548af69d88dSmrg 549af69d88dSmrg// 7.25.5.5 550af69d88dSmrgstatic inline void 551af69d88dSmrgthrd_exit(int res) 552af69d88dSmrg{ 553af69d88dSmrg impl_tss_dtor_invoke(); 554af69d88dSmrg _endthreadex((unsigned)res); 555af69d88dSmrg} 556af69d88dSmrg 557af69d88dSmrg// 7.25.5.6 558af69d88dSmrgstatic inline int 559af69d88dSmrgthrd_join(thrd_t thr, int *res) 560af69d88dSmrg{ 561af69d88dSmrg DWORD w, code; 562af69d88dSmrg w = WaitForSingleObject(thr, INFINITE); 563af69d88dSmrg if (w != WAIT_OBJECT_0) 564af69d88dSmrg return thrd_error; 565af69d88dSmrg if (res) { 566af69d88dSmrg if (!GetExitCodeThread(thr, &code)) { 567af69d88dSmrg CloseHandle(thr); 568af69d88dSmrg return thrd_error; 569af69d88dSmrg } 570af69d88dSmrg *res = (int)code; 571af69d88dSmrg } 572af69d88dSmrg CloseHandle(thr); 573af69d88dSmrg return thrd_success; 574af69d88dSmrg} 575af69d88dSmrg 576af69d88dSmrg// 7.25.5.7 577af69d88dSmrgstatic inline void 578af69d88dSmrgthrd_sleep(const xtime *xt) 579af69d88dSmrg{ 580af69d88dSmrg assert(xt); 581af69d88dSmrg Sleep(impl_xtime2msec(xt)); 582af69d88dSmrg} 583af69d88dSmrg 584af69d88dSmrg// 7.25.5.8 585af69d88dSmrgstatic inline void 586af69d88dSmrgthrd_yield(void) 587af69d88dSmrg{ 588af69d88dSmrg SwitchToThread(); 589af69d88dSmrg} 590af69d88dSmrg 591af69d88dSmrg 592af69d88dSmrg/*----------- 7.25.6 Thread-specific storage functions -----------*/ 593af69d88dSmrg// 7.25.6.1 594af69d88dSmrgstatic inline int 595af69d88dSmrgtss_create(tss_t *key, tss_dtor_t dtor) 596af69d88dSmrg{ 597af69d88dSmrg if (!key) return thrd_error; 598af69d88dSmrg *key = TlsAlloc(); 599af69d88dSmrg if (dtor) { 600af69d88dSmrg if (impl_tss_dtor_register(*key, dtor)) { 601af69d88dSmrg TlsFree(*key); 602af69d88dSmrg return thrd_error; 603af69d88dSmrg } 604af69d88dSmrg } 605af69d88dSmrg return (*key != 0xFFFFFFFF) ? thrd_success : thrd_error; 606af69d88dSmrg} 607af69d88dSmrg 608af69d88dSmrg// 7.25.6.2 609af69d88dSmrgstatic inline void 610af69d88dSmrgtss_delete(tss_t key) 611af69d88dSmrg{ 612af69d88dSmrg TlsFree(key); 613af69d88dSmrg} 614af69d88dSmrg 615af69d88dSmrg// 7.25.6.3 616af69d88dSmrgstatic inline void * 617af69d88dSmrgtss_get(tss_t key) 618af69d88dSmrg{ 619af69d88dSmrg return TlsGetValue(key); 620af69d88dSmrg} 621af69d88dSmrg 622af69d88dSmrg// 7.25.6.4 623af69d88dSmrgstatic inline int 624af69d88dSmrgtss_set(tss_t key, void *val) 625af69d88dSmrg{ 626af69d88dSmrg return TlsSetValue(key, val) ? thrd_success : thrd_error; 627af69d88dSmrg} 628af69d88dSmrg 629af69d88dSmrg 630af69d88dSmrg/*-------------------- 7.25.7 Time functions --------------------*/ 631af69d88dSmrg// 7.25.6.1 632af69d88dSmrgstatic inline int 633af69d88dSmrgxtime_get(xtime *xt, int base) 634af69d88dSmrg{ 635af69d88dSmrg if (!xt) return 0; 636af69d88dSmrg if (base == TIME_UTC) { 637af69d88dSmrg xt->sec = time(NULL); 638af69d88dSmrg xt->nsec = 0; 639af69d88dSmrg return base; 640af69d88dSmrg } 641af69d88dSmrg return 0; 642af69d88dSmrg} 643