__threading_support 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP_THREADING_SUPPORT
  10. #define _LIBCPP_THREADING_SUPPORT
  11. #include <__availability>
  12. #include <__config>
  13. #include <__thread/poll_with_backoff.h>
  14. #include <chrono>
  15. #include <errno.h>
  16. #include <iosfwd>
  17. #include <limits>
  18. #ifdef __MVS__
  19. # include <__support/ibm/nanosleep.h>
  20. #endif
  21. #ifndef _LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER
  22. # pragma GCC system_header
  23. #endif
  24. #if defined(_LIBCPP_HAS_THREAD_API_EXTERNAL)
  25. #error #include <__external_threading>
  26. #elif !defined(_LIBCPP_HAS_NO_THREADS)
  27. #if defined(_LIBCPP_HAS_THREAD_API_PTHREAD)
  28. # include <pthread.h>
  29. # include <sched.h>
  30. #elif defined(_LIBCPP_HAS_THREAD_API_C11)
  31. # include <threads.h>
  32. #endif
  33. #if defined(_LIBCPP_HAS_THREAD_LIBRARY_EXTERNAL) || \
  34. defined(_LIBCPP_BUILDING_THREAD_LIBRARY_EXTERNAL) || \
  35. defined(_LIBCPP_HAS_THREAD_API_WIN32)
  36. #define _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_FUNC_VIS
  37. #else
  38. #define _LIBCPP_THREAD_ABI_VISIBILITY inline _LIBCPP_INLINE_VISIBILITY
  39. #endif
  40. #if defined(__FreeBSD__) && defined(__clang__) && __has_attribute(no_thread_safety_analysis)
  41. #define _LIBCPP_NO_THREAD_SAFETY_ANALYSIS __attribute__((no_thread_safety_analysis))
  42. #else
  43. #define _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
  44. #endif
  45. typedef ::timespec __libcpp_timespec_t;
  46. #endif // !defined(_LIBCPP_HAS_NO_THREADS)
  47. _LIBCPP_BEGIN_NAMESPACE_STD
  48. #if !defined(_LIBCPP_HAS_NO_THREADS)
  49. #if defined(_LIBCPP_HAS_THREAD_API_PTHREAD)
  50. // Mutex
  51. typedef pthread_mutex_t __libcpp_mutex_t;
  52. #define _LIBCPP_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
  53. typedef pthread_mutex_t __libcpp_recursive_mutex_t;
  54. // Condition Variable
  55. typedef pthread_cond_t __libcpp_condvar_t;
  56. #define _LIBCPP_CONDVAR_INITIALIZER PTHREAD_COND_INITIALIZER
  57. // Execute once
  58. typedef pthread_once_t __libcpp_exec_once_flag;
  59. #define _LIBCPP_EXEC_ONCE_INITIALIZER PTHREAD_ONCE_INIT
  60. // Thread id
  61. #if defined(__MVS__)
  62. typedef unsigned long long __libcpp_thread_id;
  63. #else
  64. typedef pthread_t __libcpp_thread_id;
  65. #endif
  66. // Thread
  67. #define _LIBCPP_NULL_THREAD ((__libcpp_thread_t()))
  68. typedef pthread_t __libcpp_thread_t;
  69. // Thread Local Storage
  70. typedef pthread_key_t __libcpp_tls_key;
  71. #define _LIBCPP_TLS_DESTRUCTOR_CC
  72. #elif defined(_LIBCPP_HAS_THREAD_API_C11)
  73. // Mutex
  74. typedef mtx_t __libcpp_mutex_t;
  75. // mtx_t is a struct so using {} for initialization is valid.
  76. #define _LIBCPP_MUTEX_INITIALIZER {}
  77. typedef mtx_t __libcpp_recursive_mutex_t;
  78. // Condition Variable
  79. typedef cnd_t __libcpp_condvar_t;
  80. // cnd_t is a struct so using {} for initialization is valid.
  81. #define _LIBCPP_CONDVAR_INITIALIZER {}
  82. // Execute once
  83. typedef once_flag __libcpp_exec_once_flag;
  84. #define _LIBCPP_EXEC_ONCE_INITIALIZER ONCE_FLAG_INIT
  85. // Thread id
  86. typedef thrd_t __libcpp_thread_id;
  87. // Thread
  88. #define _LIBCPP_NULL_THREAD 0U
  89. typedef thrd_t __libcpp_thread_t;
  90. // Thread Local Storage
  91. typedef tss_t __libcpp_tls_key;
  92. #define _LIBCPP_TLS_DESTRUCTOR_CC
  93. #elif !defined(_LIBCPP_HAS_THREAD_API_EXTERNAL)
  94. // Mutex
  95. typedef void* __libcpp_mutex_t;
  96. #define _LIBCPP_MUTEX_INITIALIZER 0
  97. #if defined(_M_IX86) || defined(__i386__) || defined(_M_ARM) || defined(__arm__)
  98. typedef void* __libcpp_recursive_mutex_t[6];
  99. #elif defined(_M_AMD64) || defined(__x86_64__) || defined(_M_ARM64) || defined(__aarch64__)
  100. typedef void* __libcpp_recursive_mutex_t[5];
  101. #else
  102. # error Unsupported architecture
  103. #endif
  104. // Condition Variable
  105. typedef void* __libcpp_condvar_t;
  106. #define _LIBCPP_CONDVAR_INITIALIZER 0
  107. // Execute Once
  108. typedef void* __libcpp_exec_once_flag;
  109. #define _LIBCPP_EXEC_ONCE_INITIALIZER 0
  110. // Thread ID
  111. typedef long __libcpp_thread_id;
  112. // Thread
  113. #define _LIBCPP_NULL_THREAD 0U
  114. typedef void* __libcpp_thread_t;
  115. // Thread Local Storage
  116. typedef long __libcpp_tls_key;
  117. #define _LIBCPP_TLS_DESTRUCTOR_CC __stdcall
  118. #endif // !defined(_LIBCPP_HAS_THREAD_API_PTHREAD) && !defined(_LIBCPP_HAS_THREAD_API_EXTERNAL)
  119. #if !defined(_LIBCPP_HAS_THREAD_API_EXTERNAL)
  120. // Mutex
  121. _LIBCPP_THREAD_ABI_VISIBILITY
  122. int __libcpp_recursive_mutex_init(__libcpp_recursive_mutex_t *__m);
  123. _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
  124. int __libcpp_recursive_mutex_lock(__libcpp_recursive_mutex_t *__m);
  125. _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
  126. bool __libcpp_recursive_mutex_trylock(__libcpp_recursive_mutex_t *__m);
  127. _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
  128. int __libcpp_recursive_mutex_unlock(__libcpp_recursive_mutex_t *__m);
  129. _LIBCPP_THREAD_ABI_VISIBILITY
  130. int __libcpp_recursive_mutex_destroy(__libcpp_recursive_mutex_t *__m);
  131. _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
  132. int __libcpp_mutex_lock(__libcpp_mutex_t *__m);
  133. _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
  134. bool __libcpp_mutex_trylock(__libcpp_mutex_t *__m);
  135. _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
  136. int __libcpp_mutex_unlock(__libcpp_mutex_t *__m);
  137. _LIBCPP_THREAD_ABI_VISIBILITY
  138. int __libcpp_mutex_destroy(__libcpp_mutex_t *__m);
  139. // Condition variable
  140. _LIBCPP_THREAD_ABI_VISIBILITY
  141. int __libcpp_condvar_signal(__libcpp_condvar_t* __cv);
  142. _LIBCPP_THREAD_ABI_VISIBILITY
  143. int __libcpp_condvar_broadcast(__libcpp_condvar_t* __cv);
  144. _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
  145. int __libcpp_condvar_wait(__libcpp_condvar_t* __cv, __libcpp_mutex_t* __m);
  146. _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
  147. int __libcpp_condvar_timedwait(__libcpp_condvar_t *__cv, __libcpp_mutex_t *__m,
  148. __libcpp_timespec_t *__ts);
  149. _LIBCPP_THREAD_ABI_VISIBILITY
  150. int __libcpp_condvar_destroy(__libcpp_condvar_t* __cv);
  151. // Execute once
  152. _LIBCPP_THREAD_ABI_VISIBILITY
  153. int __libcpp_execute_once(__libcpp_exec_once_flag *flag,
  154. void (*init_routine)());
  155. // Thread id
  156. _LIBCPP_THREAD_ABI_VISIBILITY
  157. bool __libcpp_thread_id_equal(__libcpp_thread_id t1, __libcpp_thread_id t2);
  158. _LIBCPP_THREAD_ABI_VISIBILITY
  159. bool __libcpp_thread_id_less(__libcpp_thread_id t1, __libcpp_thread_id t2);
  160. // Thread
  161. _LIBCPP_THREAD_ABI_VISIBILITY
  162. bool __libcpp_thread_isnull(const __libcpp_thread_t *__t);
  163. _LIBCPP_THREAD_ABI_VISIBILITY
  164. int __libcpp_thread_create(__libcpp_thread_t *__t, void *(*__func)(void *),
  165. void *__arg);
  166. _LIBCPP_THREAD_ABI_VISIBILITY
  167. __libcpp_thread_id __libcpp_thread_get_current_id();
  168. _LIBCPP_THREAD_ABI_VISIBILITY
  169. __libcpp_thread_id __libcpp_thread_get_id(const __libcpp_thread_t *__t);
  170. _LIBCPP_THREAD_ABI_VISIBILITY
  171. int __libcpp_thread_join(__libcpp_thread_t *__t);
  172. _LIBCPP_THREAD_ABI_VISIBILITY
  173. int __libcpp_thread_detach(__libcpp_thread_t *__t);
  174. _LIBCPP_THREAD_ABI_VISIBILITY
  175. void __libcpp_thread_yield();
  176. _LIBCPP_THREAD_ABI_VISIBILITY
  177. void __libcpp_thread_sleep_for(const chrono::nanoseconds& __ns);
  178. // Thread local storage
  179. _LIBCPP_THREAD_ABI_VISIBILITY
  180. int __libcpp_tls_create(__libcpp_tls_key* __key,
  181. void(_LIBCPP_TLS_DESTRUCTOR_CC* __at_exit)(void*));
  182. _LIBCPP_THREAD_ABI_VISIBILITY
  183. void *__libcpp_tls_get(__libcpp_tls_key __key);
  184. _LIBCPP_THREAD_ABI_VISIBILITY
  185. int __libcpp_tls_set(__libcpp_tls_key __key, void *__p);
  186. #endif // !defined(_LIBCPP_HAS_THREAD_API_EXTERNAL)
  187. #if (!defined(_LIBCPP_HAS_THREAD_LIBRARY_EXTERNAL) || \
  188. defined(_LIBCPP_BUILDING_THREAD_LIBRARY_EXTERNAL))
  189. #if defined(_LIBCPP_HAS_THREAD_API_PTHREAD)
  190. int __libcpp_recursive_mutex_init(__libcpp_recursive_mutex_t *__m)
  191. {
  192. pthread_mutexattr_t attr;
  193. int __ec = pthread_mutexattr_init(&attr);
  194. if (__ec)
  195. return __ec;
  196. __ec = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
  197. if (__ec) {
  198. pthread_mutexattr_destroy(&attr);
  199. return __ec;
  200. }
  201. __ec = pthread_mutex_init(__m, &attr);
  202. if (__ec) {
  203. pthread_mutexattr_destroy(&attr);
  204. return __ec;
  205. }
  206. __ec = pthread_mutexattr_destroy(&attr);
  207. if (__ec) {
  208. pthread_mutex_destroy(__m);
  209. return __ec;
  210. }
  211. return 0;
  212. }
  213. int __libcpp_recursive_mutex_lock(__libcpp_recursive_mutex_t *__m)
  214. {
  215. return pthread_mutex_lock(__m);
  216. }
  217. bool __libcpp_recursive_mutex_trylock(__libcpp_recursive_mutex_t *__m)
  218. {
  219. return pthread_mutex_trylock(__m) == 0;
  220. }
  221. int __libcpp_recursive_mutex_unlock(__libcpp_recursive_mutex_t *__m)
  222. {
  223. return pthread_mutex_unlock(__m);
  224. }
  225. int __libcpp_recursive_mutex_destroy(__libcpp_recursive_mutex_t *__m)
  226. {
  227. return pthread_mutex_destroy(__m);
  228. }
  229. int __libcpp_mutex_lock(__libcpp_mutex_t *__m)
  230. {
  231. return pthread_mutex_lock(__m);
  232. }
  233. bool __libcpp_mutex_trylock(__libcpp_mutex_t *__m)
  234. {
  235. return pthread_mutex_trylock(__m) == 0;
  236. }
  237. int __libcpp_mutex_unlock(__libcpp_mutex_t *__m)
  238. {
  239. return pthread_mutex_unlock(__m);
  240. }
  241. int __libcpp_mutex_destroy(__libcpp_mutex_t *__m)
  242. {
  243. return pthread_mutex_destroy(__m);
  244. }
  245. // Condition Variable
  246. int __libcpp_condvar_signal(__libcpp_condvar_t *__cv)
  247. {
  248. return pthread_cond_signal(__cv);
  249. }
  250. int __libcpp_condvar_broadcast(__libcpp_condvar_t *__cv)
  251. {
  252. return pthread_cond_broadcast(__cv);
  253. }
  254. int __libcpp_condvar_wait(__libcpp_condvar_t *__cv, __libcpp_mutex_t *__m)
  255. {
  256. return pthread_cond_wait(__cv, __m);
  257. }
  258. int __libcpp_condvar_timedwait(__libcpp_condvar_t *__cv, __libcpp_mutex_t *__m,
  259. __libcpp_timespec_t *__ts)
  260. {
  261. return pthread_cond_timedwait(__cv, __m, __ts);
  262. }
  263. int __libcpp_condvar_destroy(__libcpp_condvar_t *__cv)
  264. {
  265. return pthread_cond_destroy(__cv);
  266. }
  267. // Execute once
  268. int __libcpp_execute_once(__libcpp_exec_once_flag *flag,
  269. void (*init_routine)()) {
  270. return pthread_once(flag, init_routine);
  271. }
  272. // Thread id
  273. // Returns non-zero if the thread ids are equal, otherwise 0
  274. bool __libcpp_thread_id_equal(__libcpp_thread_id t1, __libcpp_thread_id t2)
  275. {
  276. return t1 == t2;
  277. }
  278. // Returns non-zero if t1 < t2, otherwise 0
  279. bool __libcpp_thread_id_less(__libcpp_thread_id t1, __libcpp_thread_id t2)
  280. {
  281. return t1 < t2;
  282. }
  283. // Thread
  284. bool __libcpp_thread_isnull(const __libcpp_thread_t *__t) {
  285. return __libcpp_thread_get_id(__t) == 0;
  286. }
  287. int __libcpp_thread_create(__libcpp_thread_t *__t, void *(*__func)(void *),
  288. void *__arg)
  289. {
  290. return pthread_create(__t, nullptr, __func, __arg);
  291. }
  292. __libcpp_thread_id __libcpp_thread_get_current_id()
  293. {
  294. const __libcpp_thread_t thread = pthread_self();
  295. return __libcpp_thread_get_id(&thread);
  296. }
  297. __libcpp_thread_id __libcpp_thread_get_id(const __libcpp_thread_t *__t)
  298. {
  299. #if defined(__MVS__)
  300. return __t->__;
  301. #else
  302. return *__t;
  303. #endif
  304. }
  305. int __libcpp_thread_join(__libcpp_thread_t *__t)
  306. {
  307. return pthread_join(*__t, nullptr);
  308. }
  309. int __libcpp_thread_detach(__libcpp_thread_t *__t)
  310. {
  311. return pthread_detach(*__t);
  312. }
  313. void __libcpp_thread_yield()
  314. {
  315. sched_yield();
  316. }
  317. void __libcpp_thread_sleep_for(const chrono::nanoseconds& __ns)
  318. {
  319. __libcpp_timespec_t __ts = _VSTD::__convert_to_timespec<__libcpp_timespec_t>(__ns);
  320. while (nanosleep(&__ts, &__ts) == -1 && errno == EINTR);
  321. }
  322. // Thread local storage
  323. int __libcpp_tls_create(__libcpp_tls_key *__key, void (*__at_exit)(void *))
  324. {
  325. return pthread_key_create(__key, __at_exit);
  326. }
  327. void *__libcpp_tls_get(__libcpp_tls_key __key)
  328. {
  329. return pthread_getspecific(__key);
  330. }
  331. int __libcpp_tls_set(__libcpp_tls_key __key, void *__p)
  332. {
  333. return pthread_setspecific(__key, __p);
  334. }
  335. #elif defined(_LIBCPP_HAS_THREAD_API_C11)
  336. int __libcpp_recursive_mutex_init(__libcpp_recursive_mutex_t *__m)
  337. {
  338. return mtx_init(__m, mtx_plain | mtx_recursive) == thrd_success ? 0 : EINVAL;
  339. }
  340. int __libcpp_recursive_mutex_lock(__libcpp_recursive_mutex_t *__m)
  341. {
  342. return mtx_lock(__m) == thrd_success ? 0 : EINVAL;
  343. }
  344. bool __libcpp_recursive_mutex_trylock(__libcpp_recursive_mutex_t *__m)
  345. {
  346. return mtx_trylock(__m) == thrd_success;
  347. }
  348. int __libcpp_recursive_mutex_unlock(__libcpp_recursive_mutex_t *__m)
  349. {
  350. return mtx_unlock(__m) == thrd_success ? 0 : EINVAL;
  351. }
  352. int __libcpp_recursive_mutex_destroy(__libcpp_recursive_mutex_t *__m)
  353. {
  354. mtx_destroy(__m);
  355. return 0;
  356. }
  357. int __libcpp_mutex_lock(__libcpp_mutex_t *__m)
  358. {
  359. return mtx_lock(__m) == thrd_success ? 0 : EINVAL;
  360. }
  361. bool __libcpp_mutex_trylock(__libcpp_mutex_t *__m)
  362. {
  363. return mtx_trylock(__m) == thrd_success;
  364. }
  365. int __libcpp_mutex_unlock(__libcpp_mutex_t *__m)
  366. {
  367. return mtx_unlock(__m) == thrd_success ? 0 : EINVAL;
  368. }
  369. int __libcpp_mutex_destroy(__libcpp_mutex_t *__m)
  370. {
  371. mtx_destroy(__m);
  372. return 0;
  373. }
  374. // Condition Variable
  375. int __libcpp_condvar_signal(__libcpp_condvar_t *__cv)
  376. {
  377. return cnd_signal(__cv) == thrd_success ? 0 : EINVAL;
  378. }
  379. int __libcpp_condvar_broadcast(__libcpp_condvar_t *__cv)
  380. {
  381. return cnd_broadcast(__cv) == thrd_success ? 0 : EINVAL;
  382. }
  383. int __libcpp_condvar_wait(__libcpp_condvar_t *__cv, __libcpp_mutex_t *__m)
  384. {
  385. return cnd_wait(__cv, __m) == thrd_success ? 0 : EINVAL;
  386. }
  387. int __libcpp_condvar_timedwait(__libcpp_condvar_t *__cv, __libcpp_mutex_t *__m,
  388. timespec *__ts)
  389. {
  390. int __ec = cnd_timedwait(__cv, __m, __ts);
  391. return __ec == thrd_timedout ? ETIMEDOUT : __ec;
  392. }
  393. int __libcpp_condvar_destroy(__libcpp_condvar_t *__cv)
  394. {
  395. cnd_destroy(__cv);
  396. return 0;
  397. }
  398. // Execute once
  399. int __libcpp_execute_once(__libcpp_exec_once_flag *flag,
  400. void (*init_routine)(void)) {
  401. ::call_once(flag, init_routine);
  402. return 0;
  403. }
  404. // Thread id
  405. // Returns non-zero if the thread ids are equal, otherwise 0
  406. bool __libcpp_thread_id_equal(__libcpp_thread_id t1, __libcpp_thread_id t2)
  407. {
  408. return thrd_equal(t1, t2) != 0;
  409. }
  410. // Returns non-zero if t1 < t2, otherwise 0
  411. bool __libcpp_thread_id_less(__libcpp_thread_id t1, __libcpp_thread_id t2)
  412. {
  413. return t1 < t2;
  414. }
  415. // Thread
  416. bool __libcpp_thread_isnull(const __libcpp_thread_t *__t) {
  417. return __libcpp_thread_get_id(__t) == 0;
  418. }
  419. int __libcpp_thread_create(__libcpp_thread_t *__t, void *(*__func)(void *),
  420. void *__arg)
  421. {
  422. int __ec = thrd_create(__t, reinterpret_cast<thrd_start_t>(__func), __arg);
  423. return __ec == thrd_nomem ? ENOMEM : __ec;
  424. }
  425. __libcpp_thread_id __libcpp_thread_get_current_id()
  426. {
  427. return thrd_current();
  428. }
  429. __libcpp_thread_id __libcpp_thread_get_id(const __libcpp_thread_t *__t)
  430. {
  431. return *__t;
  432. }
  433. int __libcpp_thread_join(__libcpp_thread_t *__t)
  434. {
  435. return thrd_join(*__t, nullptr) == thrd_success ? 0 : EINVAL;
  436. }
  437. int __libcpp_thread_detach(__libcpp_thread_t *__t)
  438. {
  439. return thrd_detach(*__t) == thrd_success ? 0 : EINVAL;
  440. }
  441. void __libcpp_thread_yield()
  442. {
  443. thrd_yield();
  444. }
  445. void __libcpp_thread_sleep_for(const chrono::nanoseconds& __ns)
  446. {
  447. __libcpp_timespec_t __ts = _VSTD::__convert_to_timespec<__libcpp_timespec_t>(__ns);
  448. thrd_sleep(&__ts, nullptr);
  449. }
  450. // Thread local storage
  451. int __libcpp_tls_create(__libcpp_tls_key *__key, void (*__at_exit)(void *))
  452. {
  453. return tss_create(__key, __at_exit) == thrd_success ? 0 : EINVAL;
  454. }
  455. void *__libcpp_tls_get(__libcpp_tls_key __key)
  456. {
  457. return tss_get(__key);
  458. }
  459. int __libcpp_tls_set(__libcpp_tls_key __key, void *__p)
  460. {
  461. return tss_set(__key, __p) == thrd_success ? 0 : EINVAL;
  462. }
  463. #endif
  464. #endif // !_LIBCPP_HAS_THREAD_LIBRARY_EXTERNAL || _LIBCPP_BUILDING_THREAD_LIBRARY_EXTERNAL
  465. class _LIBCPP_TYPE_VIS thread;
  466. class _LIBCPP_TYPE_VIS __thread_id;
  467. namespace this_thread
  468. {
  469. _LIBCPP_INLINE_VISIBILITY __thread_id get_id() _NOEXCEPT;
  470. } // namespace this_thread
  471. template<> struct hash<__thread_id>;
  472. class _LIBCPP_TEMPLATE_VIS __thread_id
  473. {
  474. // FIXME: pthread_t is a pointer on Darwin but a long on Linux.
  475. // NULL is the no-thread value on Darwin. Someone needs to check
  476. // on other platforms. We assume 0 works everywhere for now.
  477. __libcpp_thread_id __id_;
  478. public:
  479. _LIBCPP_INLINE_VISIBILITY
  480. __thread_id() _NOEXCEPT : __id_(0) {}
  481. friend _LIBCPP_INLINE_VISIBILITY
  482. bool operator==(__thread_id __x, __thread_id __y) _NOEXCEPT
  483. { // don't pass id==0 to underlying routines
  484. if (__x.__id_ == 0) return __y.__id_ == 0;
  485. if (__y.__id_ == 0) return false;
  486. return __libcpp_thread_id_equal(__x.__id_, __y.__id_);
  487. }
  488. friend _LIBCPP_INLINE_VISIBILITY
  489. bool operator!=(__thread_id __x, __thread_id __y) _NOEXCEPT
  490. {return !(__x == __y);}
  491. friend _LIBCPP_INLINE_VISIBILITY
  492. bool operator< (__thread_id __x, __thread_id __y) _NOEXCEPT
  493. { // id==0 is always less than any other thread_id
  494. if (__x.__id_ == 0) return __y.__id_ != 0;
  495. if (__y.__id_ == 0) return false;
  496. return __libcpp_thread_id_less(__x.__id_, __y.__id_);
  497. }
  498. friend _LIBCPP_INLINE_VISIBILITY
  499. bool operator<=(__thread_id __x, __thread_id __y) _NOEXCEPT
  500. {return !(__y < __x);}
  501. friend _LIBCPP_INLINE_VISIBILITY
  502. bool operator> (__thread_id __x, __thread_id __y) _NOEXCEPT
  503. {return __y < __x ;}
  504. friend _LIBCPP_INLINE_VISIBILITY
  505. bool operator>=(__thread_id __x, __thread_id __y) _NOEXCEPT
  506. {return !(__x < __y);}
  507. _LIBCPP_INLINE_VISIBILITY
  508. void __reset() { __id_ = 0; }
  509. template<class _CharT, class _Traits>
  510. friend
  511. _LIBCPP_INLINE_VISIBILITY
  512. basic_ostream<_CharT, _Traits>&
  513. operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id);
  514. private:
  515. _LIBCPP_INLINE_VISIBILITY
  516. __thread_id(__libcpp_thread_id __id) : __id_(__id) {}
  517. friend __thread_id this_thread::get_id() _NOEXCEPT;
  518. friend class _LIBCPP_TYPE_VIS thread;
  519. friend struct _LIBCPP_TEMPLATE_VIS hash<__thread_id>;
  520. };
  521. namespace this_thread
  522. {
  523. inline _LIBCPP_INLINE_VISIBILITY
  524. __thread_id
  525. get_id() _NOEXCEPT
  526. {
  527. return __libcpp_thread_get_current_id();
  528. }
  529. } // namespace this_thread
  530. #endif // !_LIBCPP_HAS_NO_THREADS
  531. _LIBCPP_END_NAMESPACE_STD
  532. #endif // _LIBCPP_THREADING_SUPPORT