atomic_wait.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. // -*- C++ -*- header.
  2. // Copyright (C) 2020-2022 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file bits/atomic_wait.h
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly. @headername{atomic}
  23. */
  24. #ifndef _GLIBCXX_ATOMIC_WAIT_H
  25. #define _GLIBCXX_ATOMIC_WAIT_H 1
  26. #pragma GCC system_header
  27. #include <bits/c++config.h>
  28. #if defined _GLIBCXX_HAS_GTHREADS || defined _GLIBCXX_HAVE_LINUX_FUTEX
  29. #include <bits/functional_hash.h>
  30. #include <bits/gthr.h>
  31. #include <ext/numeric_traits.h>
  32. #ifdef _GLIBCXX_HAVE_LINUX_FUTEX
  33. # include <cerrno>
  34. # include <climits>
  35. # include <unistd.h>
  36. # include <syscall.h>
  37. # include <bits/functexcept.h>
  38. #endif
  39. # include <bits/std_mutex.h> // std::mutex, std::__condvar
  40. #define __cpp_lib_atomic_wait 201907L
  41. namespace std _GLIBCXX_VISIBILITY(default)
  42. {
  43. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  44. namespace __detail
  45. {
  46. #ifdef _GLIBCXX_HAVE_LINUX_FUTEX
  47. #define _GLIBCXX_HAVE_PLATFORM_WAIT 1
  48. using __platform_wait_t = int;
  49. static constexpr size_t __platform_wait_alignment = 4;
  50. #else
  51. // define _GLIBCX_HAVE_PLATFORM_WAIT and implement __platform_wait()
  52. // and __platform_notify() if there is a more efficient primitive supported
  53. // by the platform (e.g. __ulock_wait()/__ulock_wake()) which is better than
  54. // a mutex/condvar based wait.
  55. using __platform_wait_t = uint64_t;
  56. static constexpr size_t __platform_wait_alignment
  57. = __alignof__(__platform_wait_t);
  58. #endif
  59. } // namespace __detail
  60. template<typename _Tp>
  61. inline constexpr bool __platform_wait_uses_type
  62. #ifdef _GLIBCXX_HAVE_PLATFORM_WAIT
  63. = is_scalar_v<_Tp>
  64. && ((sizeof(_Tp) == sizeof(__detail::__platform_wait_t))
  65. && (alignof(_Tp*) >= __detail::__platform_wait_alignment));
  66. #else
  67. = false;
  68. #endif
  69. namespace __detail
  70. {
  71. #ifdef _GLIBCXX_HAVE_LINUX_FUTEX
  72. enum class __futex_wait_flags : int
  73. {
  74. #ifdef _GLIBCXX_HAVE_LINUX_FUTEX_PRIVATE
  75. __private_flag = 128,
  76. #else
  77. __private_flag = 0,
  78. #endif
  79. __wait = 0,
  80. __wake = 1,
  81. __wait_bitset = 9,
  82. __wake_bitset = 10,
  83. __wait_private = __wait | __private_flag,
  84. __wake_private = __wake | __private_flag,
  85. __wait_bitset_private = __wait_bitset | __private_flag,
  86. __wake_bitset_private = __wake_bitset | __private_flag,
  87. __bitset_match_any = -1
  88. };
  89. template<typename _Tp>
  90. void
  91. __platform_wait(const _Tp* __addr, __platform_wait_t __val) noexcept
  92. {
  93. auto __e = syscall (SYS_futex, static_cast<const void*>(__addr),
  94. static_cast<int>(__futex_wait_flags::__wait_private),
  95. __val, nullptr);
  96. if (!__e || errno == EAGAIN)
  97. return;
  98. if (errno != EINTR)
  99. __throw_system_error(errno);
  100. }
  101. template<typename _Tp>
  102. void
  103. __platform_notify(const _Tp* __addr, bool __all) noexcept
  104. {
  105. syscall (SYS_futex, static_cast<const void*>(__addr),
  106. static_cast<int>(__futex_wait_flags::__wake_private),
  107. __all ? INT_MAX : 1);
  108. }
  109. #endif
  110. inline void
  111. __thread_yield() noexcept
  112. {
  113. #if defined _GLIBCXX_HAS_GTHREADS && defined _GLIBCXX_USE_SCHED_YIELD
  114. __gthread_yield();
  115. #endif
  116. }
  117. inline void
  118. __thread_relax() noexcept
  119. {
  120. #if defined __i386__ || defined __x86_64__
  121. __builtin_ia32_pause();
  122. #else
  123. __thread_yield();
  124. #endif
  125. }
  126. constexpr auto __atomic_spin_count_relax = 12;
  127. constexpr auto __atomic_spin_count = 16;
  128. struct __default_spin_policy
  129. {
  130. bool
  131. operator()() const noexcept
  132. { return false; }
  133. };
  134. template<typename _Pred,
  135. typename _Spin = __default_spin_policy>
  136. bool
  137. __atomic_spin(_Pred& __pred, _Spin __spin = _Spin{ }) noexcept
  138. {
  139. for (auto __i = 0; __i < __atomic_spin_count; ++__i)
  140. {
  141. if (__pred())
  142. return true;
  143. if (__i < __atomic_spin_count_relax)
  144. __detail::__thread_relax();
  145. else
  146. __detail::__thread_yield();
  147. }
  148. while (__spin())
  149. {
  150. if (__pred())
  151. return true;
  152. }
  153. return false;
  154. }
  155. // return true if equal
  156. template<typename _Tp>
  157. bool __atomic_compare(const _Tp& __a, const _Tp& __b)
  158. {
  159. // TODO make this do the correct padding bit ignoring comparison
  160. return __builtin_memcmp(&__a, &__b, sizeof(_Tp)) == 0;
  161. }
  162. struct __waiter_pool_base
  163. {
  164. // Don't use std::hardware_destructive_interference_size here because we
  165. // don't want the layout of library types to depend on compiler options.
  166. static constexpr auto _S_align = 64;
  167. alignas(_S_align) __platform_wait_t _M_wait = 0;
  168. #ifndef _GLIBCXX_HAVE_PLATFORM_WAIT
  169. mutex _M_mtx;
  170. #endif
  171. alignas(_S_align) __platform_wait_t _M_ver = 0;
  172. #ifndef _GLIBCXX_HAVE_PLATFORM_WAIT
  173. __condvar _M_cv;
  174. #endif
  175. __waiter_pool_base() = default;
  176. void
  177. _M_enter_wait() noexcept
  178. { __atomic_fetch_add(&_M_wait, 1, __ATOMIC_SEQ_CST); }
  179. void
  180. _M_leave_wait() noexcept
  181. { __atomic_fetch_sub(&_M_wait, 1, __ATOMIC_RELEASE); }
  182. bool
  183. _M_waiting() const noexcept
  184. {
  185. __platform_wait_t __res;
  186. __atomic_load(&_M_wait, &__res, __ATOMIC_SEQ_CST);
  187. return __res != 0;
  188. }
  189. void
  190. _M_notify(const __platform_wait_t* __addr, bool __all, bool __bare) noexcept
  191. {
  192. if (!(__bare || _M_waiting()))
  193. return;
  194. #ifdef _GLIBCXX_HAVE_PLATFORM_WAIT
  195. __platform_notify(__addr, __all);
  196. #else
  197. if (__all)
  198. _M_cv.notify_all();
  199. else
  200. _M_cv.notify_one();
  201. #endif
  202. }
  203. static __waiter_pool_base&
  204. _S_for(const void* __addr) noexcept
  205. {
  206. constexpr uintptr_t __ct = 16;
  207. static __waiter_pool_base __w[__ct];
  208. auto __key = (uintptr_t(__addr) >> 2) % __ct;
  209. return __w[__key];
  210. }
  211. };
  212. struct __waiter_pool : __waiter_pool_base
  213. {
  214. void
  215. _M_do_wait(const __platform_wait_t* __addr, __platform_wait_t __old) noexcept
  216. {
  217. #ifdef _GLIBCXX_HAVE_PLATFORM_WAIT
  218. __platform_wait(__addr, __old);
  219. #else
  220. __platform_wait_t __val;
  221. __atomic_load(__addr, &__val, __ATOMIC_SEQ_CST);
  222. if (__val == __old)
  223. {
  224. lock_guard<mutex> __l(_M_mtx);
  225. _M_cv.wait(_M_mtx);
  226. }
  227. #endif // __GLIBCXX_HAVE_PLATFORM_WAIT
  228. }
  229. };
  230. template<typename _Tp>
  231. struct __waiter_base
  232. {
  233. using __waiter_type = _Tp;
  234. __waiter_type& _M_w;
  235. __platform_wait_t* _M_addr;
  236. template<typename _Up>
  237. static __platform_wait_t*
  238. _S_wait_addr(const _Up* __a, __platform_wait_t* __b)
  239. {
  240. if constexpr (__platform_wait_uses_type<_Up>)
  241. return reinterpret_cast<__platform_wait_t*>(const_cast<_Up*>(__a));
  242. else
  243. return __b;
  244. }
  245. static __waiter_type&
  246. _S_for(const void* __addr) noexcept
  247. {
  248. static_assert(sizeof(__waiter_type) == sizeof(__waiter_pool_base));
  249. auto& res = __waiter_pool_base::_S_for(__addr);
  250. return reinterpret_cast<__waiter_type&>(res);
  251. }
  252. template<typename _Up>
  253. explicit __waiter_base(const _Up* __addr) noexcept
  254. : _M_w(_S_for(__addr))
  255. , _M_addr(_S_wait_addr(__addr, &_M_w._M_ver))
  256. { }
  257. bool
  258. _M_laundered() const
  259. { return _M_addr == &_M_w._M_ver; }
  260. void
  261. _M_notify(bool __all, bool __bare = false)
  262. {
  263. if (_M_laundered())
  264. {
  265. __atomic_fetch_add(_M_addr, 1, __ATOMIC_SEQ_CST);
  266. __all = true;
  267. }
  268. _M_w._M_notify(_M_addr, __all, __bare);
  269. }
  270. template<typename _Up, typename _ValFn,
  271. typename _Spin = __default_spin_policy>
  272. static bool
  273. _S_do_spin_v(__platform_wait_t* __addr,
  274. const _Up& __old, _ValFn __vfn,
  275. __platform_wait_t& __val,
  276. _Spin __spin = _Spin{ })
  277. {
  278. auto const __pred = [=]
  279. { return !__detail::__atomic_compare(__old, __vfn()); };
  280. if constexpr (__platform_wait_uses_type<_Up>)
  281. {
  282. __builtin_memcpy(&__val, &__old, sizeof(__val));
  283. }
  284. else
  285. {
  286. __atomic_load(__addr, &__val, __ATOMIC_ACQUIRE);
  287. }
  288. return __atomic_spin(__pred, __spin);
  289. }
  290. template<typename _Up, typename _ValFn,
  291. typename _Spin = __default_spin_policy>
  292. bool
  293. _M_do_spin_v(const _Up& __old, _ValFn __vfn,
  294. __platform_wait_t& __val,
  295. _Spin __spin = _Spin{ })
  296. { return _S_do_spin_v(_M_addr, __old, __vfn, __val, __spin); }
  297. template<typename _Pred,
  298. typename _Spin = __default_spin_policy>
  299. static bool
  300. _S_do_spin(const __platform_wait_t* __addr,
  301. _Pred __pred,
  302. __platform_wait_t& __val,
  303. _Spin __spin = _Spin{ })
  304. {
  305. __atomic_load(__addr, &__val, __ATOMIC_ACQUIRE);
  306. return __atomic_spin(__pred, __spin);
  307. }
  308. template<typename _Pred,
  309. typename _Spin = __default_spin_policy>
  310. bool
  311. _M_do_spin(_Pred __pred, __platform_wait_t& __val,
  312. _Spin __spin = _Spin{ })
  313. { return _S_do_spin(_M_addr, __pred, __val, __spin); }
  314. };
  315. template<typename _EntersWait>
  316. struct __waiter : __waiter_base<__waiter_pool>
  317. {
  318. using __base_type = __waiter_base<__waiter_pool>;
  319. template<typename _Tp>
  320. explicit __waiter(const _Tp* __addr) noexcept
  321. : __base_type(__addr)
  322. {
  323. if constexpr (_EntersWait::value)
  324. _M_w._M_enter_wait();
  325. }
  326. ~__waiter()
  327. {
  328. if constexpr (_EntersWait::value)
  329. _M_w._M_leave_wait();
  330. }
  331. template<typename _Tp, typename _ValFn>
  332. void
  333. _M_do_wait_v(_Tp __old, _ValFn __vfn)
  334. {
  335. do
  336. {
  337. __platform_wait_t __val;
  338. if (__base_type::_M_do_spin_v(__old, __vfn, __val))
  339. return;
  340. __base_type::_M_w._M_do_wait(__base_type::_M_addr, __val);
  341. }
  342. while (__detail::__atomic_compare(__old, __vfn()));
  343. }
  344. template<typename _Pred>
  345. void
  346. _M_do_wait(_Pred __pred) noexcept
  347. {
  348. do
  349. {
  350. __platform_wait_t __val;
  351. if (__base_type::_M_do_spin(__pred, __val))
  352. return;
  353. __base_type::_M_w._M_do_wait(__base_type::_M_addr, __val);
  354. }
  355. while (!__pred());
  356. }
  357. };
  358. using __enters_wait = __waiter<std::true_type>;
  359. using __bare_wait = __waiter<std::false_type>;
  360. } // namespace __detail
  361. template<typename _Tp, typename _ValFn>
  362. void
  363. __atomic_wait_address_v(const _Tp* __addr, _Tp __old,
  364. _ValFn __vfn) noexcept
  365. {
  366. __detail::__enters_wait __w(__addr);
  367. __w._M_do_wait_v(__old, __vfn);
  368. }
  369. template<typename _Tp, typename _Pred>
  370. void
  371. __atomic_wait_address(const _Tp* __addr, _Pred __pred) noexcept
  372. {
  373. __detail::__enters_wait __w(__addr);
  374. __w._M_do_wait(__pred);
  375. }
  376. // This call is to be used by atomic types which track contention externally
  377. template<typename _Pred>
  378. void
  379. __atomic_wait_address_bare(const __detail::__platform_wait_t* __addr,
  380. _Pred __pred) noexcept
  381. {
  382. #ifdef _GLIBCXX_HAVE_PLATFORM_WAIT
  383. do
  384. {
  385. __detail::__platform_wait_t __val;
  386. if (__detail::__bare_wait::_S_do_spin(__addr, __pred, __val))
  387. return;
  388. __detail::__platform_wait(__addr, __val);
  389. }
  390. while (!__pred());
  391. #else // !_GLIBCXX_HAVE_PLATFORM_WAIT
  392. __detail::__bare_wait __w(__addr);
  393. __w._M_do_wait(__pred);
  394. #endif
  395. }
  396. template<typename _Tp>
  397. void
  398. __atomic_notify_address(const _Tp* __addr, bool __all) noexcept
  399. {
  400. __detail::__bare_wait __w(__addr);
  401. __w._M_notify(__all);
  402. }
  403. // This call is to be used by atomic types which track contention externally
  404. inline void
  405. __atomic_notify_address_bare(const __detail::__platform_wait_t* __addr,
  406. bool __all) noexcept
  407. {
  408. #ifdef _GLIBCXX_HAVE_PLATFORM_WAIT
  409. __detail::__platform_notify(__addr, __all);
  410. #else
  411. __detail::__bare_wait __w(__addr);
  412. __w._M_notify(__all, true);
  413. #endif
  414. }
  415. _GLIBCXX_END_NAMESPACE_VERSION
  416. } // namespace std
  417. #endif // GTHREADS || LINUX_FUTEX
  418. #endif // _GLIBCXX_ATOMIC_WAIT_H