atomic_wait.h 12 KB

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