Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

291 lines
9.3KB

  1. // -*- C++ -*- header.
  2. // Copyright (C) 2015-2020 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. // Returns false iff a timeout occurred.
  45. bool
  46. _M_futex_wait_until(unsigned *__addr, unsigned __val, bool __has_timeout,
  47. chrono::seconds __s, chrono::nanoseconds __ns);
  48. // This can be executed after the object has been destroyed.
  49. static void _M_futex_notify_all(unsigned* __addr);
  50. };
  51. template <unsigned _Waiter_bit = 0x80000000>
  52. class __atomic_futex_unsigned : __atomic_futex_unsigned_base
  53. {
  54. typedef chrono::system_clock __clock_t;
  55. // This must be lock-free and at offset 0.
  56. atomic<unsigned> _M_data;
  57. public:
  58. explicit
  59. __atomic_futex_unsigned(unsigned __data) : _M_data(__data)
  60. { }
  61. _GLIBCXX_ALWAYS_INLINE unsigned
  62. _M_load(memory_order __mo)
  63. {
  64. return _M_data.load(__mo) & ~_Waiter_bit;
  65. }
  66. private:
  67. // If a timeout occurs, returns a current value after the timeout;
  68. // otherwise, returns the operand's value if equal is true or a different
  69. // value if equal is false.
  70. // The assumed value is the caller's assumption about the current value
  71. // when making the call.
  72. unsigned
  73. _M_load_and_test_until(unsigned __assumed, unsigned __operand,
  74. bool __equal, memory_order __mo, bool __has_timeout,
  75. chrono::seconds __s, chrono::nanoseconds __ns)
  76. {
  77. for (;;)
  78. {
  79. // Don't bother checking the value again because we expect the caller
  80. // to have done it recently.
  81. // memory_order_relaxed is sufficient because we can rely on just the
  82. // modification order (store_notify uses an atomic RMW operation too),
  83. // and the futex syscalls synchronize between themselves.
  84. _M_data.fetch_or(_Waiter_bit, memory_order_relaxed);
  85. bool __ret = _M_futex_wait_until((unsigned*)(void*)&_M_data,
  86. __assumed | _Waiter_bit,
  87. __has_timeout, __s, __ns);
  88. // Fetch the current value after waiting (clears _Waiter_bit).
  89. __assumed = _M_load(__mo);
  90. if (!__ret || ((__operand == __assumed) == __equal))
  91. return __assumed;
  92. // TODO adapt wait time
  93. }
  94. }
  95. // Returns the operand's value if equal is true or a different value if
  96. // equal is false.
  97. // The assumed value is the caller's assumption about the current value
  98. // when making the call.
  99. unsigned
  100. _M_load_and_test(unsigned __assumed, unsigned __operand,
  101. bool __equal, memory_order __mo)
  102. {
  103. return _M_load_and_test_until(__assumed, __operand, __equal, __mo,
  104. false, {}, {});
  105. }
  106. // If a timeout occurs, returns a current value after the timeout;
  107. // otherwise, returns the operand's value if equal is true or a different
  108. // value if equal is false.
  109. // The assumed value is the caller's assumption about the current value
  110. // when making the call.
  111. template<typename _Dur>
  112. unsigned
  113. _M_load_and_test_until_impl(unsigned __assumed, unsigned __operand,
  114. bool __equal, memory_order __mo,
  115. const chrono::time_point<__clock_t, _Dur>& __atime)
  116. {
  117. auto __s = chrono::time_point_cast<chrono::seconds>(__atime);
  118. auto __ns = chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
  119. // XXX correct?
  120. return _M_load_and_test_until(__assumed, __operand, __equal, __mo,
  121. true, __s.time_since_epoch(), __ns);
  122. }
  123. public:
  124. _GLIBCXX_ALWAYS_INLINE unsigned
  125. _M_load_when_not_equal(unsigned __val, memory_order __mo)
  126. {
  127. unsigned __i = _M_load(__mo);
  128. if ((__i & ~_Waiter_bit) != __val)
  129. return (__i & ~_Waiter_bit);
  130. // TODO Spin-wait first.
  131. return _M_load_and_test(__i, __val, false, __mo);
  132. }
  133. _GLIBCXX_ALWAYS_INLINE void
  134. _M_load_when_equal(unsigned __val, memory_order __mo)
  135. {
  136. unsigned __i = _M_load(__mo);
  137. if ((__i & ~_Waiter_bit) == __val)
  138. return;
  139. // TODO Spin-wait first.
  140. _M_load_and_test(__i, __val, true, __mo);
  141. }
  142. // Returns false iff a timeout occurred.
  143. template<typename _Rep, typename _Period>
  144. _GLIBCXX_ALWAYS_INLINE bool
  145. _M_load_when_equal_for(unsigned __val, memory_order __mo,
  146. const chrono::duration<_Rep, _Period>& __rtime)
  147. {
  148. return _M_load_when_equal_until(__val, __mo,
  149. __clock_t::now() + __rtime);
  150. }
  151. // Returns false iff a timeout occurred.
  152. template<typename _Clock, typename _Duration>
  153. _GLIBCXX_ALWAYS_INLINE bool
  154. _M_load_when_equal_until(unsigned __val, memory_order __mo,
  155. const chrono::time_point<_Clock, _Duration>& __atime)
  156. {
  157. // DR 887 - Sync unknown clock to known clock.
  158. const typename _Clock::time_point __c_entry = _Clock::now();
  159. const __clock_t::time_point __s_entry = __clock_t::now();
  160. const auto __delta = __atime - __c_entry;
  161. const auto __s_atime = __s_entry + __delta;
  162. return _M_load_when_equal_until(__val, __mo, __s_atime);
  163. }
  164. // Returns false iff a timeout occurred.
  165. template<typename _Duration>
  166. _GLIBCXX_ALWAYS_INLINE bool
  167. _M_load_when_equal_until(unsigned __val, memory_order __mo,
  168. const chrono::time_point<__clock_t, _Duration>& __atime)
  169. {
  170. unsigned __i = _M_load(__mo);
  171. if ((__i & ~_Waiter_bit) == __val)
  172. return true;
  173. // TODO Spin-wait first. Ignore effect on timeout.
  174. __i = _M_load_and_test_until_impl(__i, __val, true, __mo, __atime);
  175. return (__i & ~_Waiter_bit) == __val;
  176. }
  177. _GLIBCXX_ALWAYS_INLINE void
  178. _M_store_notify_all(unsigned __val, memory_order __mo)
  179. {
  180. unsigned* __futex = (unsigned *)(void *)&_M_data;
  181. if (_M_data.exchange(__val, __mo) & _Waiter_bit)
  182. _M_futex_notify_all(__futex);
  183. }
  184. };
  185. #else // ! (_GLIBCXX_HAVE_LINUX_FUTEX && ATOMIC_INT_LOCK_FREE > 1)
  186. // If futexes are not available, use a mutex and a condvar to wait.
  187. // Because we access the data only within critical sections, all accesses
  188. // are sequentially consistent; thus, we satisfy any provided memory_order.
  189. template <unsigned _Waiter_bit = 0x80000000>
  190. class __atomic_futex_unsigned
  191. {
  192. typedef chrono::system_clock __clock_t;
  193. unsigned _M_data;
  194. mutex _M_mutex;
  195. condition_variable _M_condvar;
  196. public:
  197. explicit
  198. __atomic_futex_unsigned(unsigned __data) : _M_data(__data)
  199. { }
  200. _GLIBCXX_ALWAYS_INLINE unsigned
  201. _M_load(memory_order __mo)
  202. {
  203. unique_lock<mutex> __lock(_M_mutex);
  204. return _M_data;
  205. }
  206. _GLIBCXX_ALWAYS_INLINE unsigned
  207. _M_load_when_not_equal(unsigned __val, memory_order __mo)
  208. {
  209. unique_lock<mutex> __lock(_M_mutex);
  210. while (_M_data == __val)
  211. _M_condvar.wait(__lock);
  212. return _M_data;
  213. }
  214. _GLIBCXX_ALWAYS_INLINE void
  215. _M_load_when_equal(unsigned __val, memory_order __mo)
  216. {
  217. unique_lock<mutex> __lock(_M_mutex);
  218. while (_M_data != __val)
  219. _M_condvar.wait(__lock);
  220. }
  221. template<typename _Rep, typename _Period>
  222. _GLIBCXX_ALWAYS_INLINE bool
  223. _M_load_when_equal_for(unsigned __val, memory_order __mo,
  224. const chrono::duration<_Rep, _Period>& __rtime)
  225. {
  226. unique_lock<mutex> __lock(_M_mutex);
  227. return _M_condvar.wait_for(__lock, __rtime,
  228. [&] { return _M_data == __val;});
  229. }
  230. template<typename _Clock, typename _Duration>
  231. _GLIBCXX_ALWAYS_INLINE bool
  232. _M_load_when_equal_until(unsigned __val, memory_order __mo,
  233. const chrono::time_point<_Clock, _Duration>& __atime)
  234. {
  235. unique_lock<mutex> __lock(_M_mutex);
  236. return _M_condvar.wait_until(__lock, __atime,
  237. [&] { return _M_data == __val;});
  238. }
  239. _GLIBCXX_ALWAYS_INLINE void
  240. _M_store_notify_all(unsigned __val, memory_order __mo)
  241. {
  242. unique_lock<mutex> __lock(_M_mutex);
  243. _M_data = __val;
  244. _M_condvar.notify_all();
  245. }
  246. };
  247. #endif // _GLIBCXX_HAVE_LINUX_FUTEX && ATOMIC_INT_LOCK_FREE > 1
  248. #endif // _GLIBCXX_HAS_GTHREADS
  249. _GLIBCXX_END_NAMESPACE_VERSION
  250. } // namespace std
  251. #endif