__threading_support 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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 <__config>
  12. #include <chrono>
  13. #include <iosfwd>
  14. #include <errno.h>
  15. #ifndef _LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER
  16. #pragma GCC system_header
  17. #endif
  18. #if defined(_LIBCPP_HAS_THREAD_API_EXTERNAL)
  19. # include <__external_threading>
  20. #elif !defined(_LIBCPP_HAS_NO_THREADS)
  21. #if defined(_LIBCPP_HAS_THREAD_API_PTHREAD)
  22. # include <pthread.h>
  23. # include <sched.h>
  24. #endif
  25. #if defined(_LIBCPP_HAS_THREAD_LIBRARY_EXTERNAL) || \
  26. defined(_LIBCPP_BUILDING_THREAD_LIBRARY_EXTERNAL) || \
  27. defined(_LIBCPP_HAS_THREAD_API_WIN32)
  28. #define _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_FUNC_VIS
  29. #else
  30. #define _LIBCPP_THREAD_ABI_VISIBILITY inline _LIBCPP_INLINE_VISIBILITY
  31. #endif
  32. #if defined(__FreeBSD__) && defined(__clang__) && __has_attribute(no_thread_safety_analysis)
  33. #define _LIBCPP_NO_THREAD_SAFETY_ANALYSIS __attribute__((no_thread_safety_analysis))
  34. #else
  35. #define _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
  36. #endif
  37. typedef ::timespec __libcpp_timespec_t;
  38. #endif // !defined(_LIBCPP_HAS_NO_THREADS)
  39. _LIBCPP_PUSH_MACROS
  40. #include <__undef_macros>
  41. _LIBCPP_BEGIN_NAMESPACE_STD
  42. #if !defined(_LIBCPP_HAS_NO_THREADS)
  43. #if defined(_LIBCPP_HAS_THREAD_API_PTHREAD)
  44. // Mutex
  45. typedef pthread_mutex_t __libcpp_mutex_t;
  46. #define _LIBCPP_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
  47. typedef pthread_mutex_t __libcpp_recursive_mutex_t;
  48. // Condition Variable
  49. typedef pthread_cond_t __libcpp_condvar_t;
  50. #define _LIBCPP_CONDVAR_INITIALIZER PTHREAD_COND_INITIALIZER
  51. // Execute once
  52. typedef pthread_once_t __libcpp_exec_once_flag;
  53. #define _LIBCPP_EXEC_ONCE_INITIALIZER PTHREAD_ONCE_INIT
  54. // Thread id
  55. typedef pthread_t __libcpp_thread_id;
  56. // Thread
  57. #define _LIBCPP_NULL_THREAD 0U
  58. typedef pthread_t __libcpp_thread_t;
  59. // Thread Local Storage
  60. typedef pthread_key_t __libcpp_tls_key;
  61. #define _LIBCPP_TLS_DESTRUCTOR_CC
  62. #elif !defined(_LIBCPP_HAS_THREAD_API_EXTERNAL)
  63. // Mutex
  64. typedef void* __libcpp_mutex_t;
  65. #define _LIBCPP_MUTEX_INITIALIZER 0
  66. #if defined(_M_IX86) || defined(__i386__) || defined(_M_ARM) || defined(__arm__)
  67. typedef void* __libcpp_recursive_mutex_t[6];
  68. #elif defined(_M_AMD64) || defined(__x86_64__) || defined(_M_ARM64) || defined(__aarch64__)
  69. typedef void* __libcpp_recursive_mutex_t[5];
  70. #else
  71. # error Unsupported architecture
  72. #endif
  73. // Condition Variable
  74. typedef void* __libcpp_condvar_t;
  75. #define _LIBCPP_CONDVAR_INITIALIZER 0
  76. // Execute Once
  77. typedef void* __libcpp_exec_once_flag;
  78. #define _LIBCPP_EXEC_ONCE_INITIALIZER 0
  79. // Thread ID
  80. typedef long __libcpp_thread_id;
  81. // Thread
  82. #define _LIBCPP_NULL_THREAD 0U
  83. typedef void* __libcpp_thread_t;
  84. // Thread Local Storage
  85. typedef long __libcpp_tls_key;
  86. #define _LIBCPP_TLS_DESTRUCTOR_CC __stdcall
  87. #endif // !defined(_LIBCPP_HAS_THREAD_API_PTHREAD) && !defined(_LIBCPP_HAS_THREAD_API_EXTERNAL)
  88. #if !defined(_LIBCPP_HAS_THREAD_API_EXTERNAL)
  89. // Mutex
  90. _LIBCPP_THREAD_ABI_VISIBILITY
  91. int __libcpp_recursive_mutex_init(__libcpp_recursive_mutex_t *__m);
  92. _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
  93. int __libcpp_recursive_mutex_lock(__libcpp_recursive_mutex_t *__m);
  94. _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
  95. bool __libcpp_recursive_mutex_trylock(__libcpp_recursive_mutex_t *__m);
  96. _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
  97. int __libcpp_recursive_mutex_unlock(__libcpp_recursive_mutex_t *__m);
  98. _LIBCPP_THREAD_ABI_VISIBILITY
  99. int __libcpp_recursive_mutex_destroy(__libcpp_recursive_mutex_t *__m);
  100. _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
  101. int __libcpp_mutex_lock(__libcpp_mutex_t *__m);
  102. _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
  103. bool __libcpp_mutex_trylock(__libcpp_mutex_t *__m);
  104. _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
  105. int __libcpp_mutex_unlock(__libcpp_mutex_t *__m);
  106. _LIBCPP_THREAD_ABI_VISIBILITY
  107. int __libcpp_mutex_destroy(__libcpp_mutex_t *__m);
  108. // Condition variable
  109. _LIBCPP_THREAD_ABI_VISIBILITY
  110. int __libcpp_condvar_signal(__libcpp_condvar_t* __cv);
  111. _LIBCPP_THREAD_ABI_VISIBILITY
  112. int __libcpp_condvar_broadcast(__libcpp_condvar_t* __cv);
  113. _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
  114. int __libcpp_condvar_wait(__libcpp_condvar_t* __cv, __libcpp_mutex_t* __m);
  115. _LIBCPP_THREAD_ABI_VISIBILITY _LIBCPP_NO_THREAD_SAFETY_ANALYSIS
  116. int __libcpp_condvar_timedwait(__libcpp_condvar_t *__cv, __libcpp_mutex_t *__m,
  117. __libcpp_timespec_t *__ts);
  118. _LIBCPP_THREAD_ABI_VISIBILITY
  119. int __libcpp_condvar_destroy(__libcpp_condvar_t* __cv);
  120. // Execute once
  121. _LIBCPP_THREAD_ABI_VISIBILITY
  122. int __libcpp_execute_once(__libcpp_exec_once_flag *flag,
  123. void (*init_routine)());
  124. // Thread id
  125. _LIBCPP_THREAD_ABI_VISIBILITY
  126. bool __libcpp_thread_id_equal(__libcpp_thread_id t1, __libcpp_thread_id t2);
  127. _LIBCPP_THREAD_ABI_VISIBILITY
  128. bool __libcpp_thread_id_less(__libcpp_thread_id t1, __libcpp_thread_id t2);
  129. // Thread
  130. _LIBCPP_THREAD_ABI_VISIBILITY
  131. bool __libcpp_thread_isnull(const __libcpp_thread_t *__t);
  132. _LIBCPP_THREAD_ABI_VISIBILITY
  133. int __libcpp_thread_create(__libcpp_thread_t *__t, void *(*__func)(void *),
  134. void *__arg);
  135. _LIBCPP_THREAD_ABI_VISIBILITY
  136. __libcpp_thread_id __libcpp_thread_get_current_id();
  137. _LIBCPP_THREAD_ABI_VISIBILITY
  138. __libcpp_thread_id __libcpp_thread_get_id(const __libcpp_thread_t *__t);
  139. _LIBCPP_THREAD_ABI_VISIBILITY
  140. int __libcpp_thread_join(__libcpp_thread_t *__t);
  141. _LIBCPP_THREAD_ABI_VISIBILITY
  142. int __libcpp_thread_detach(__libcpp_thread_t *__t);
  143. _LIBCPP_THREAD_ABI_VISIBILITY
  144. void __libcpp_thread_yield();
  145. _LIBCPP_THREAD_ABI_VISIBILITY
  146. void __libcpp_thread_sleep_for(const chrono::nanoseconds& __ns);
  147. // Thread local storage
  148. _LIBCPP_THREAD_ABI_VISIBILITY
  149. int __libcpp_tls_create(__libcpp_tls_key* __key,
  150. void(_LIBCPP_TLS_DESTRUCTOR_CC* __at_exit)(void*));
  151. _LIBCPP_THREAD_ABI_VISIBILITY
  152. void *__libcpp_tls_get(__libcpp_tls_key __key);
  153. _LIBCPP_THREAD_ABI_VISIBILITY
  154. int __libcpp_tls_set(__libcpp_tls_key __key, void *__p);
  155. #endif // !defined(_LIBCPP_HAS_THREAD_API_EXTERNAL)
  156. #if (!defined(_LIBCPP_HAS_THREAD_LIBRARY_EXTERNAL) || \
  157. defined(_LIBCPP_BUILDING_THREAD_LIBRARY_EXTERNAL)) && \
  158. defined(_LIBCPP_HAS_THREAD_API_PTHREAD)
  159. int __libcpp_recursive_mutex_init(__libcpp_recursive_mutex_t *__m)
  160. {
  161. pthread_mutexattr_t attr;
  162. int __ec = pthread_mutexattr_init(&attr);
  163. if (__ec)
  164. return __ec;
  165. __ec = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
  166. if (__ec) {
  167. pthread_mutexattr_destroy(&attr);
  168. return __ec;
  169. }
  170. __ec = pthread_mutex_init(__m, &attr);
  171. if (__ec) {
  172. pthread_mutexattr_destroy(&attr);
  173. return __ec;
  174. }
  175. __ec = pthread_mutexattr_destroy(&attr);
  176. if (__ec) {
  177. pthread_mutex_destroy(__m);
  178. return __ec;
  179. }
  180. return 0;
  181. }
  182. int __libcpp_recursive_mutex_lock(__libcpp_recursive_mutex_t *__m)
  183. {
  184. return pthread_mutex_lock(__m);
  185. }
  186. bool __libcpp_recursive_mutex_trylock(__libcpp_recursive_mutex_t *__m)
  187. {
  188. return pthread_mutex_trylock(__m) == 0;
  189. }
  190. int __libcpp_recursive_mutex_unlock(__libcpp_mutex_t *__m)
  191. {
  192. return pthread_mutex_unlock(__m);
  193. }
  194. int __libcpp_recursive_mutex_destroy(__libcpp_recursive_mutex_t *__m)
  195. {
  196. return pthread_mutex_destroy(__m);
  197. }
  198. int __libcpp_mutex_lock(__libcpp_mutex_t *__m)
  199. {
  200. return pthread_mutex_lock(__m);
  201. }
  202. bool __libcpp_mutex_trylock(__libcpp_mutex_t *__m)
  203. {
  204. return pthread_mutex_trylock(__m) == 0;
  205. }
  206. int __libcpp_mutex_unlock(__libcpp_mutex_t *__m)
  207. {
  208. return pthread_mutex_unlock(__m);
  209. }
  210. int __libcpp_mutex_destroy(__libcpp_mutex_t *__m)
  211. {
  212. return pthread_mutex_destroy(__m);
  213. }
  214. // Condition Variable
  215. int __libcpp_condvar_signal(__libcpp_condvar_t *__cv)
  216. {
  217. return pthread_cond_signal(__cv);
  218. }
  219. int __libcpp_condvar_broadcast(__libcpp_condvar_t *__cv)
  220. {
  221. return pthread_cond_broadcast(__cv);
  222. }
  223. int __libcpp_condvar_wait(__libcpp_condvar_t *__cv, __libcpp_mutex_t *__m)
  224. {
  225. return pthread_cond_wait(__cv, __m);
  226. }
  227. int __libcpp_condvar_timedwait(__libcpp_condvar_t *__cv, __libcpp_mutex_t *__m,
  228. __libcpp_timespec_t *__ts)
  229. {
  230. return pthread_cond_timedwait(__cv, __m, __ts);
  231. }
  232. int __libcpp_condvar_destroy(__libcpp_condvar_t *__cv)
  233. {
  234. return pthread_cond_destroy(__cv);
  235. }
  236. // Execute once
  237. int __libcpp_execute_once(__libcpp_exec_once_flag *flag,
  238. void (*init_routine)()) {
  239. return pthread_once(flag, init_routine);
  240. }
  241. // Thread id
  242. // Returns non-zero if the thread ids are equal, otherwise 0
  243. bool __libcpp_thread_id_equal(__libcpp_thread_id t1, __libcpp_thread_id t2)
  244. {
  245. return pthread_equal(t1, t2) != 0;
  246. }
  247. // Returns non-zero if t1 < t2, otherwise 0
  248. bool __libcpp_thread_id_less(__libcpp_thread_id t1, __libcpp_thread_id t2)
  249. {
  250. return t1 < t2;
  251. }
  252. // Thread
  253. bool __libcpp_thread_isnull(const __libcpp_thread_t *__t) {
  254. return *__t == 0;
  255. }
  256. int __libcpp_thread_create(__libcpp_thread_t *__t, void *(*__func)(void *),
  257. void *__arg)
  258. {
  259. return pthread_create(__t, 0, __func, __arg);
  260. }
  261. __libcpp_thread_id __libcpp_thread_get_current_id()
  262. {
  263. return pthread_self();
  264. }
  265. __libcpp_thread_id __libcpp_thread_get_id(const __libcpp_thread_t *__t)
  266. {
  267. return *__t;
  268. }
  269. int __libcpp_thread_join(__libcpp_thread_t *__t)
  270. {
  271. return pthread_join(*__t, 0);
  272. }
  273. int __libcpp_thread_detach(__libcpp_thread_t *__t)
  274. {
  275. return pthread_detach(*__t);
  276. }
  277. void __libcpp_thread_yield()
  278. {
  279. sched_yield();
  280. }
  281. void __libcpp_thread_sleep_for(const chrono::nanoseconds& __ns)
  282. {
  283. using namespace chrono;
  284. seconds __s = duration_cast<seconds>(__ns);
  285. __libcpp_timespec_t __ts;
  286. typedef decltype(__ts.tv_sec) ts_sec;
  287. _LIBCPP_CONSTEXPR ts_sec __ts_sec_max = numeric_limits<ts_sec>::max();
  288. if (__s.count() < __ts_sec_max)
  289. {
  290. __ts.tv_sec = static_cast<ts_sec>(__s.count());
  291. __ts.tv_nsec = static_cast<decltype(__ts.tv_nsec)>((__ns - __s).count());
  292. }
  293. else
  294. {
  295. __ts.tv_sec = __ts_sec_max;
  296. __ts.tv_nsec = 999999999; // (10^9 - 1)
  297. }
  298. while (nanosleep(&__ts, &__ts) == -1 && errno == EINTR);
  299. }
  300. // Thread local storage
  301. int __libcpp_tls_create(__libcpp_tls_key *__key, void (*__at_exit)(void *))
  302. {
  303. return pthread_key_create(__key, __at_exit);
  304. }
  305. void *__libcpp_tls_get(__libcpp_tls_key __key)
  306. {
  307. return pthread_getspecific(__key);
  308. }
  309. int __libcpp_tls_set(__libcpp_tls_key __key, void *__p)
  310. {
  311. return pthread_setspecific(__key, __p);
  312. }
  313. #endif // !_LIBCPP_HAS_THREAD_LIBRARY_EXTERNAL || _LIBCPP_BUILDING_THREAD_LIBRARY_EXTERNAL
  314. class _LIBCPP_TYPE_VIS thread;
  315. class _LIBCPP_TYPE_VIS __thread_id;
  316. namespace this_thread
  317. {
  318. _LIBCPP_INLINE_VISIBILITY __thread_id get_id() _NOEXCEPT;
  319. } // this_thread
  320. template<> struct hash<__thread_id>;
  321. class _LIBCPP_TEMPLATE_VIS __thread_id
  322. {
  323. // FIXME: pthread_t is a pointer on Darwin but a long on Linux.
  324. // NULL is the no-thread value on Darwin. Someone needs to check
  325. // on other platforms. We assume 0 works everywhere for now.
  326. __libcpp_thread_id __id_;
  327. public:
  328. _LIBCPP_INLINE_VISIBILITY
  329. __thread_id() _NOEXCEPT : __id_(0) {}
  330. friend _LIBCPP_INLINE_VISIBILITY
  331. bool operator==(__thread_id __x, __thread_id __y) _NOEXCEPT
  332. { // don't pass id==0 to underlying routines
  333. if (__x.__id_ == 0) return __y.__id_ == 0;
  334. if (__y.__id_ == 0) return false;
  335. return __libcpp_thread_id_equal(__x.__id_, __y.__id_);
  336. }
  337. friend _LIBCPP_INLINE_VISIBILITY
  338. bool operator!=(__thread_id __x, __thread_id __y) _NOEXCEPT
  339. {return !(__x == __y);}
  340. friend _LIBCPP_INLINE_VISIBILITY
  341. bool operator< (__thread_id __x, __thread_id __y) _NOEXCEPT
  342. { // id==0 is always less than any other thread_id
  343. if (__x.__id_ == 0) return __y.__id_ != 0;
  344. if (__y.__id_ == 0) return false;
  345. return __libcpp_thread_id_less(__x.__id_, __y.__id_);
  346. }
  347. friend _LIBCPP_INLINE_VISIBILITY
  348. bool operator<=(__thread_id __x, __thread_id __y) _NOEXCEPT
  349. {return !(__y < __x);}
  350. friend _LIBCPP_INLINE_VISIBILITY
  351. bool operator> (__thread_id __x, __thread_id __y) _NOEXCEPT
  352. {return __y < __x ;}
  353. friend _LIBCPP_INLINE_VISIBILITY
  354. bool operator>=(__thread_id __x, __thread_id __y) _NOEXCEPT
  355. {return !(__x < __y);}
  356. _LIBCPP_INLINE_VISIBILITY
  357. void __reset() { __id_ = 0; }
  358. template<class _CharT, class _Traits>
  359. friend
  360. _LIBCPP_INLINE_VISIBILITY
  361. basic_ostream<_CharT, _Traits>&
  362. operator<<(basic_ostream<_CharT, _Traits>& __os, __thread_id __id);
  363. private:
  364. _LIBCPP_INLINE_VISIBILITY
  365. __thread_id(__libcpp_thread_id __id) : __id_(__id) {}
  366. friend __thread_id this_thread::get_id() _NOEXCEPT;
  367. friend class _LIBCPP_TYPE_VIS thread;
  368. friend struct _LIBCPP_TEMPLATE_VIS hash<__thread_id>;
  369. };
  370. namespace this_thread
  371. {
  372. inline _LIBCPP_INLINE_VISIBILITY
  373. __thread_id
  374. get_id() _NOEXCEPT
  375. {
  376. return __libcpp_thread_get_current_id();
  377. }
  378. } // this_thread
  379. #endif // !_LIBCPP_HAS_NO_THREADS
  380. _LIBCPP_END_NAMESPACE_STD
  381. _LIBCPP_POP_MACROS
  382. #endif // _LIBCPP_THREADING_SUPPORT