atomic_futex.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. // -*- C++ -*- header.
  2. // Copyright (C) 2015-2021 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_futex.h
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly.
  23. */
  24. #ifndef _GLIBCXX_ATOMIC_FUTEX_H
  25. #define _GLIBCXX_ATOMIC_FUTEX_H 1
  26. #pragma GCC system_header
  27. #include <bits/c++config.h>
  28. #include <atomic>
  29. #include <chrono>
  30. #if ! (defined(_GLIBCXX_HAVE_LINUX_FUTEX) && ATOMIC_INT_LOCK_FREE > 1)
  31. #include <mutex>
  32. #include <condition_variable>
  33. #endif
  34. #ifndef _GLIBCXX_ALWAYS_INLINE
  35. #define _GLIBCXX_ALWAYS_INLINE inline __attribute__((__always_inline__))
  36. #endif
  37. namespace std _GLIBCXX_VISIBILITY(default)
  38. {
  39. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  40. #ifdef _GLIBCXX_HAS_GTHREADS
  41. #if defined(_GLIBCXX_HAVE_LINUX_FUTEX) && ATOMIC_INT_LOCK_FREE > 1
  42. struct __atomic_futex_unsigned_base
  43. {
  44. // __s and __ns are measured against CLOCK_REALTIME. Returns false
  45. // iff a timeout occurred.
  46. bool
  47. _M_futex_wait_until(unsigned *__addr, unsigned __val, bool __has_timeout,
  48. chrono::seconds __s, chrono::nanoseconds __ns);
  49. // __s and __ns are measured against CLOCK_MONOTONIC. Returns
  50. // false iff a timeout occurred.
  51. bool
  52. _M_futex_wait_until_steady(unsigned *__addr, unsigned __val,
  53. bool __has_timeout, chrono::seconds __s, chrono::nanoseconds __ns);
  54. // This can be executed after the object has been destroyed.
  55. static void _M_futex_notify_all(unsigned* __addr);
  56. };
  57. template <unsigned _Waiter_bit = 0x80000000>
  58. class __atomic_futex_unsigned : __atomic_futex_unsigned_base
  59. {
  60. typedef chrono::steady_clock __clock_t;
  61. // This must be lock-free and at offset 0.
  62. atomic<unsigned> _M_data;
  63. public:
  64. explicit
  65. __atomic_futex_unsigned(unsigned __data) : _M_data(__data)
  66. { }
  67. _GLIBCXX_ALWAYS_INLINE unsigned
  68. _M_load(memory_order __mo)
  69. {
  70. return _M_data.load(__mo) & ~_Waiter_bit;
  71. }
  72. private:
  73. // If a timeout occurs, returns a current value after the timeout;
  74. // otherwise, returns the operand's value if equal is true or a different
  75. // value if equal is false.
  76. // The assumed value is the caller's assumption about the current value
  77. // when making the call.
  78. // __s and __ns are measured against CLOCK_REALTIME.
  79. unsigned
  80. _M_load_and_test_until(unsigned __assumed, unsigned __operand,
  81. bool __equal, memory_order __mo, bool __has_timeout,
  82. chrono::seconds __s, chrono::nanoseconds __ns)
  83. {
  84. for (;;)
  85. {
  86. // Don't bother checking the value again because we expect the caller
  87. // to have done it recently.
  88. // memory_order_relaxed is sufficient because we can rely on just the
  89. // modification order (store_notify uses an atomic RMW operation too),
  90. // and the futex syscalls synchronize between themselves.
  91. _M_data.fetch_or(_Waiter_bit, memory_order_relaxed);
  92. bool __ret = _M_futex_wait_until((unsigned*)(void*)&_M_data,
  93. __assumed | _Waiter_bit,
  94. __has_timeout, __s, __ns);
  95. // Fetch the current value after waiting (clears _Waiter_bit).
  96. __assumed = _M_load(__mo);
  97. if (!__ret || ((__operand == __assumed) == __equal))
  98. return __assumed;
  99. // TODO adapt wait time
  100. }
  101. }
  102. // If a timeout occurs, returns a current value after the timeout;
  103. // otherwise, returns the operand's value if equal is true or a different
  104. // value if equal is false.
  105. // The assumed value is the caller's assumption about the current value
  106. // when making the call.
  107. // __s and __ns are measured against CLOCK_MONOTONIC.
  108. unsigned
  109. _M_load_and_test_until_steady(unsigned __assumed, unsigned __operand,
  110. bool __equal, memory_order __mo, bool __has_timeout,
  111. chrono::seconds __s, chrono::nanoseconds __ns)
  112. {
  113. for (;;)
  114. {
  115. // Don't bother checking the value again because we expect the caller
  116. // to have done it recently.
  117. // memory_order_relaxed is sufficient because we can rely on just the
  118. // modification order (store_notify uses an atomic RMW operation too),
  119. // and the futex syscalls synchronize between themselves.
  120. _M_data.fetch_or(_Waiter_bit, memory_order_relaxed);
  121. bool __ret = _M_futex_wait_until_steady((unsigned*)(void*)&_M_data,
  122. __assumed | _Waiter_bit,
  123. __has_timeout, __s, __ns);
  124. // Fetch the current value after waiting (clears _Waiter_bit).
  125. __assumed = _M_load(__mo);
  126. if (!__ret || ((__operand == __assumed) == __equal))
  127. return __assumed;
  128. // TODO adapt wait time
  129. }
  130. }
  131. // Returns the operand's value if equal is true or a different value if
  132. // equal is false.
  133. // The assumed value is the caller's assumption about the current value
  134. // when making the call.
  135. unsigned
  136. _M_load_and_test(unsigned __assumed, unsigned __operand,
  137. bool __equal, memory_order __mo)
  138. {
  139. return _M_load_and_test_until(__assumed, __operand, __equal, __mo,
  140. false, {}, {});
  141. }
  142. // If a timeout occurs, returns a current value after the timeout;
  143. // otherwise, returns the operand's value if equal is true or a different
  144. // value if equal is false.
  145. // The assumed value is the caller's assumption about the current value
  146. // when making the call.
  147. template<typename _Dur>
  148. unsigned
  149. _M_load_and_test_until_impl(unsigned __assumed, unsigned __operand,
  150. bool __equal, memory_order __mo,
  151. const chrono::time_point<std::chrono::system_clock, _Dur>& __atime)
  152. {
  153. auto __s = chrono::time_point_cast<chrono::seconds>(__atime);
  154. auto __ns = chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
  155. // XXX correct?
  156. return _M_load_and_test_until(__assumed, __operand, __equal, __mo,
  157. true, __s.time_since_epoch(), __ns);
  158. }
  159. template<typename _Dur>
  160. unsigned
  161. _M_load_and_test_until_impl(unsigned __assumed, unsigned __operand,
  162. bool __equal, memory_order __mo,
  163. const chrono::time_point<std::chrono::steady_clock, _Dur>& __atime)
  164. {
  165. auto __s = chrono::time_point_cast<chrono::seconds>(__atime);
  166. auto __ns = chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
  167. // XXX correct?
  168. return _M_load_and_test_until_steady(__assumed, __operand, __equal, __mo,
  169. true, __s.time_since_epoch(), __ns);
  170. }
  171. public:
  172. _GLIBCXX_ALWAYS_INLINE unsigned
  173. _M_load_when_not_equal(unsigned __val, memory_order __mo)
  174. {
  175. unsigned __i = _M_load(__mo);
  176. if ((__i & ~_Waiter_bit) != __val)
  177. return (__i & ~_Waiter_bit);
  178. // TODO Spin-wait first.
  179. return _M_load_and_test(__i, __val, false, __mo);
  180. }
  181. _GLIBCXX_ALWAYS_INLINE void
  182. _M_load_when_equal(unsigned __val, memory_order __mo)
  183. {
  184. unsigned __i = _M_load(__mo);
  185. if ((__i & ~_Waiter_bit) == __val)
  186. return;
  187. // TODO Spin-wait first.
  188. _M_load_and_test(__i, __val, true, __mo);
  189. }
  190. // Returns false iff a timeout occurred.
  191. template<typename _Rep, typename _Period>
  192. _GLIBCXX_ALWAYS_INLINE bool
  193. _M_load_when_equal_for(unsigned __val, memory_order __mo,
  194. const chrono::duration<_Rep, _Period>& __rtime)
  195. {
  196. using __dur = typename __clock_t::duration;
  197. return _M_load_when_equal_until(__val, __mo,
  198. __clock_t::now() + chrono::__detail::ceil<__dur>(__rtime));
  199. }
  200. // Returns false iff a timeout occurred.
  201. template<typename _Clock, typename _Duration>
  202. _GLIBCXX_ALWAYS_INLINE bool
  203. _M_load_when_equal_until(unsigned __val, memory_order __mo,
  204. const chrono::time_point<_Clock, _Duration>& __atime)
  205. {
  206. typename _Clock::time_point __c_entry = _Clock::now();
  207. do {
  208. const __clock_t::time_point __s_entry = __clock_t::now();
  209. const auto __delta = __atime - __c_entry;
  210. const auto __s_atime = __s_entry +
  211. chrono::__detail::ceil<__clock_t::duration>(__delta);
  212. if (_M_load_when_equal_until(__val, __mo, __s_atime))
  213. return true;
  214. __c_entry = _Clock::now();
  215. } while (__c_entry < __atime);
  216. return false;
  217. }
  218. // Returns false iff a timeout occurred.
  219. template<typename _Duration>
  220. _GLIBCXX_ALWAYS_INLINE bool
  221. _M_load_when_equal_until(unsigned __val, memory_order __mo,
  222. const chrono::time_point<std::chrono::system_clock, _Duration>& __atime)
  223. {
  224. unsigned __i = _M_load(__mo);
  225. if ((__i & ~_Waiter_bit) == __val)
  226. return true;
  227. // TODO Spin-wait first. Ignore effect on timeout.
  228. __i = _M_load_and_test_until_impl(__i, __val, true, __mo, __atime);
  229. return (__i & ~_Waiter_bit) == __val;
  230. }
  231. // Returns false iff a timeout occurred.
  232. template<typename _Duration>
  233. _GLIBCXX_ALWAYS_INLINE bool
  234. _M_load_when_equal_until(unsigned __val, memory_order __mo,
  235. const chrono::time_point<std::chrono::steady_clock, _Duration>& __atime)
  236. {
  237. unsigned __i = _M_load(__mo);
  238. if ((__i & ~_Waiter_bit) == __val)
  239. return true;
  240. // TODO Spin-wait first. Ignore effect on timeout.
  241. __i = _M_load_and_test_until_impl(__i, __val, true, __mo, __atime);
  242. return (__i & ~_Waiter_bit) == __val;
  243. }
  244. _GLIBCXX_ALWAYS_INLINE void
  245. _M_store_notify_all(unsigned __val, memory_order __mo)
  246. {
  247. unsigned* __futex = (unsigned *)(void *)&_M_data;
  248. if (_M_data.exchange(__val, __mo) & _Waiter_bit)
  249. _M_futex_notify_all(__futex);
  250. }
  251. };
  252. #else // ! (_GLIBCXX_HAVE_LINUX_FUTEX && ATOMIC_INT_LOCK_FREE > 1)
  253. // If futexes are not available, use a mutex and a condvar to wait.
  254. // Because we access the data only within critical sections, all accesses
  255. // are sequentially consistent; thus, we satisfy any provided memory_order.
  256. template <unsigned _Waiter_bit = 0x80000000>
  257. class __atomic_futex_unsigned
  258. {
  259. typedef chrono::system_clock __clock_t;
  260. unsigned _M_data;
  261. mutex _M_mutex;
  262. condition_variable _M_condvar;
  263. public:
  264. explicit
  265. __atomic_futex_unsigned(unsigned __data) : _M_data(__data)
  266. { }
  267. _GLIBCXX_ALWAYS_INLINE unsigned
  268. _M_load(memory_order __mo)
  269. {
  270. unique_lock<mutex> __lock(_M_mutex);
  271. return _M_data;
  272. }
  273. _GLIBCXX_ALWAYS_INLINE unsigned
  274. _M_load_when_not_equal(unsigned __val, memory_order __mo)
  275. {
  276. unique_lock<mutex> __lock(_M_mutex);
  277. while (_M_data == __val)
  278. _M_condvar.wait(__lock);
  279. return _M_data;
  280. }
  281. _GLIBCXX_ALWAYS_INLINE void
  282. _M_load_when_equal(unsigned __val, memory_order __mo)
  283. {
  284. unique_lock<mutex> __lock(_M_mutex);
  285. while (_M_data != __val)
  286. _M_condvar.wait(__lock);
  287. }
  288. template<typename _Rep, typename _Period>
  289. _GLIBCXX_ALWAYS_INLINE bool
  290. _M_load_when_equal_for(unsigned __val, memory_order __mo,
  291. const chrono::duration<_Rep, _Period>& __rtime)
  292. {
  293. unique_lock<mutex> __lock(_M_mutex);
  294. return _M_condvar.wait_for(__lock, __rtime,
  295. [&] { return _M_data == __val;});
  296. }
  297. template<typename _Clock, typename _Duration>
  298. _GLIBCXX_ALWAYS_INLINE bool
  299. _M_load_when_equal_until(unsigned __val, memory_order __mo,
  300. const chrono::time_point<_Clock, _Duration>& __atime)
  301. {
  302. unique_lock<mutex> __lock(_M_mutex);
  303. return _M_condvar.wait_until(__lock, __atime,
  304. [&] { return _M_data == __val;});
  305. }
  306. _GLIBCXX_ALWAYS_INLINE void
  307. _M_store_notify_all(unsigned __val, memory_order __mo)
  308. {
  309. unique_lock<mutex> __lock(_M_mutex);
  310. _M_data = __val;
  311. _M_condvar.notify_all();
  312. }
  313. };
  314. #endif // _GLIBCXX_HAVE_LINUX_FUTEX && ATOMIC_INT_LOCK_FREE > 1
  315. #endif // _GLIBCXX_HAS_GTHREADS
  316. _GLIBCXX_END_NAMESPACE_VERSION
  317. } // namespace std
  318. #endif