winpthreads.h 43 KB

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