__threading_support 17 KB

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