libstdc++
atomic_timed_wait.h
Go to the documentation of this file.
1 // -*- C++ -*- header.
2 
3 // Copyright (C) 2020-2021 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10 
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19 
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24 
25 /** @file bits/atomic_timed_wait.h
26  * This is an internal header file, included by other library headers.
27  * Do not attempt to use it directly. @headername{atomic}
28  */
29 
30 #ifndef _GLIBCXX_ATOMIC_TIMED_WAIT_H
31 #define _GLIBCXX_ATOMIC_TIMED_WAIT_H 1
32 
33 #pragma GCC system_header
34 
35 #include <bits/atomic_wait.h>
36 
37 #if __cpp_lib_atomic_wait
38 #include <bits/functional_hash.h>
39 
40 #include <chrono>
41 
42 #ifdef _GLIBCXX_HAVE_LINUX_FUTEX
43 #include <exception> // std::terminate
44 #include <sys/time.h>
45 #endif
46 
47 namespace std _GLIBCXX_VISIBILITY(default)
48 {
49 _GLIBCXX_BEGIN_NAMESPACE_VERSION
50 
51  enum class __atomic_wait_status { no_timeout, timeout };
52 
53  namespace __detail
54  {
55 #ifdef _GLIBCXX_HAVE_LINUX_FUTEX
56  using __platform_wait_clock_t = chrono::steady_clock;
57 
58  template<typename _Duration>
59  __atomic_wait_status
60  __platform_wait_until_impl(__platform_wait_t* __addr,
61  __platform_wait_t __val,
62  const chrono::time_point<
63  __platform_wait_clock_t, _Duration>&
64  __atime) noexcept
65  {
66  auto __s = chrono::time_point_cast<chrono::seconds>(__atime);
67  auto __ns = chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
68 
69  struct timespec __rt =
70  {
71  static_cast<std::time_t>(__s.time_since_epoch().count()),
72  static_cast<long>(__ns.count())
73  };
74 
75  auto __e = syscall (SYS_futex, __addr,
76  static_cast<int>(__futex_wait_flags::
77  __wait_bitset_private),
78  __val, &__rt, nullptr,
79  static_cast<int>(__futex_wait_flags::
80  __bitset_match_any));
81  if (__e && !(errno == EINTR || errno == EAGAIN || errno == ETIMEDOUT))
83  return (__platform_wait_clock_t::now() < __atime)
84  ? __atomic_wait_status::no_timeout
85  : __atomic_wait_status::timeout;
86  }
87 
88  template<typename _Clock, typename _Duration>
89  __atomic_wait_status
90  __platform_wait_until(__platform_wait_t* __addr, __platform_wait_t __val,
91  const chrono::time_point<_Clock, _Duration>&
92  __atime)
93  {
94  if constexpr (is_same_v<__platform_wait_clock_t, _Clock>)
95  {
96  return __detail::__platform_wait_until_impl(__addr, __val, __atime);
97  }
98  else
99  {
100  const typename _Clock::time_point __c_entry = _Clock::now();
101  const __platform_wait_clock_t::time_point __s_entry =
102  __platform_wait_clock_t::now();
103  const auto __delta = __atime - __c_entry;
104  const auto __s_atime = __s_entry + __delta;
105  if (__detail::__platform_wait_until_impl(__addr, __val, __s_atime)
106  == __atomic_wait_status::no_timeout)
107  return __atomic_wait_status::no_timeout;
108 
109  // We got a timeout when measured against __clock_t but
110  // we need to check against the caller-supplied clock
111  // to tell whether we should return a timeout.
112  if (_Clock::now() < __atime)
113  return __atomic_wait_status::no_timeout;
114  return __atomic_wait_status::timeout;
115  }
116  }
117 #else // ! FUTEX
118 
119 #ifdef _GLIBCXX_USE_PTHREAD_COND_CLOCKWAIT
120  template<typename _Duration>
121  __atomic_wait_status
122  __cond_wait_until_impl(__condvar& __cv, mutex& __mx,
123  const chrono::time_point<chrono::steady_clock, _Duration>& __atime)
124  {
125  auto __s = chrono::time_point_cast<chrono::seconds>(__atime);
126  auto __ns = chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
127 
128  __gthread_time_t __ts =
129  {
130  static_cast<std::time_t>(__s.time_since_epoch().count()),
131  static_cast<long>(__ns.count())
132  };
133 
134  __cv.wait_until(__mx, CLOCK_MONOTONIC, __ts);
135 
136  return (chrono::steady_clock::now() < __atime)
137  ? __atomic_wait_status::no_timeout
138  : __atomic_wait_status::timeout;
139  }
140 #endif
141 
142  template<typename _Duration>
143  __atomic_wait_status
144  __cond_wait_until_impl(__condvar& __cv, mutex& __mx,
145  const chrono::time_point<chrono::system_clock, _Duration>& __atime)
146  {
147  auto __s = chrono::time_point_cast<chrono::seconds>(__atime);
148  auto __ns = chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
149 
150  __gthread_time_t __ts =
151  {
152  static_cast<std::time_t>(__s.time_since_epoch().count()),
153  static_cast<long>(__ns.count())
154  };
155 
156  __cv.wait_until(__mx, __ts);
157 
158  return (chrono::system_clock::now() < __atime)
159  ? __atomic_wait_status::no_timeout
160  : __atomic_wait_status::timeout;
161  }
162 
163  // return true if timeout
164  template<typename _Clock, typename _Duration>
165  __atomic_wait_status
166  __cond_wait_until(__condvar& __cv, mutex& __mx,
167  const chrono::time_point<_Clock, _Duration>& __atime)
168  {
169 #ifndef _GLIBCXX_USE_PTHREAD_COND_CLOCKWAIT
170  using __clock_t = chrono::system_clock;
171 #else
172  using __clock_t = chrono::steady_clock;
173  if constexpr (is_same_v<_Clock, chrono::steady_clock>)
174  return __detail::__cond_wait_until_impl(__cv, __mx, __atime);
175  else
176 #endif
177  if constexpr (is_same_v<_Clock, chrono::system_clock>)
178  return __detail::__cond_wait_until_impl(__cv, __mx, __atime);
179  else
180  {
181  const typename _Clock::time_point __c_entry = _Clock::now();
182  const __clock_t::time_point __s_entry = __clock_t::now();
183  const auto __delta = __atime - __c_entry;
184  const auto __s_atime = __s_entry + __delta;
185  if (__detail::__cond_wait_until_impl(__cv, __mx, __s_atime)
186  == __atomic_wait_status::no_timeout)
187  return __atomic_wait_status::no_timeout;
188  // We got a timeout when measured against __clock_t but
189  // we need to check against the caller-supplied clock
190  // to tell whether we should return a timeout.
191  if (_Clock::now() < __atime)
192  return __atomic_wait_status::no_timeout;
193  return __atomic_wait_status::timeout;
194  }
195  }
196 #endif // FUTEX
197 
198  struct __timed_waiters : __waiters
199  {
200  template<typename _Clock, typename _Duration>
201  __atomic_wait_status
202  _M_do_wait_until(__platform_wait_t __version,
203  const chrono::time_point<_Clock, _Duration>& __atime)
204  {
205 #ifdef _GLIBCXX_HAVE_LINUX_FUTEX
206  return __detail::__platform_wait_until(&_M_ver, __version, __atime);
207 #else
208  __platform_wait_t __cur = 0;
209  __waiters::__lock_t __l(_M_mtx);
210  while (__cur <= __version)
211  {
212  if (__detail::__cond_wait_until(_M_cv, _M_mtx, __atime)
213  == __atomic_wait_status::timeout)
214  return __atomic_wait_status::timeout;
215 
216  __platform_wait_t __last = __cur;
217  __atomic_load(&_M_ver, &__cur, __ATOMIC_ACQUIRE);
218  if (__cur < __last)
219  break; // break the loop if version overflows
220  }
221  return __atomic_wait_status::no_timeout;
222 #endif
223  }
224 
225  static __timed_waiters&
226  _S_timed_for(void* __t)
227  {
228  static_assert(sizeof(__timed_waiters) == sizeof(__waiters));
229  return static_cast<__timed_waiters&>(__waiters::_S_for(__t));
230  }
231  };
232  } // namespace __detail
233 
234  template<typename _Tp, typename _Pred,
235  typename _Clock, typename _Duration>
236  bool
237  __atomic_wait_until(const _Tp* __addr, _Tp __old, _Pred __pred,
238  const chrono::time_point<_Clock, _Duration>&
239  __atime) noexcept
240  {
241  using namespace __detail;
242 
243  if (std::__atomic_spin(__pred))
244  return true;
245 
246  auto& __w = __timed_waiters::_S_timed_for((void*)__addr);
247  auto __version = __w._M_enter_wait();
248  do
249  {
250  __atomic_wait_status __res;
251 #ifdef _GLIBCXX_HAVE_LINUX_FUTEX
252  if constexpr (__platform_wait_uses_type<_Tp>)
253  {
254  __res = __detail::__platform_wait_until((__platform_wait_t*)(void*) __addr,
255  __old, __atime);
256  }
257  else
258 #endif
259  {
260  __res = __w._M_do_wait_until(__version, __atime);
261  }
262  if (__res == __atomic_wait_status::timeout)
263  return false;
264  }
265  while (!__pred() && __atime < _Clock::now());
266  __w._M_leave_wait();
267 
268  // if timed out, return false
269  return (_Clock::now() < __atime);
270  }
271 
272  template<typename _Tp, typename _Pred,
273  typename _Rep, typename _Period>
274  bool
275  __atomic_wait_for(const _Tp* __addr, _Tp __old, _Pred __pred,
276  const chrono::duration<_Rep, _Period>& __rtime) noexcept
277  {
278  using namespace __detail;
279 
280  if (std::__atomic_spin(__pred))
281  return true;
282 
283  if (!__rtime.count())
284  return false; // no rtime supplied, and spin did not acquire
285 
286  using __dur = chrono::steady_clock::duration;
287  auto __reltime = chrono::duration_cast<__dur>(__rtime);
288  if (__reltime < __rtime)
289  ++__reltime;
290 
291  return __atomic_wait_until(__addr, __old, std::move(__pred),
292  chrono::steady_clock::now() + __reltime);
293  }
294 _GLIBCXX_END_NAMESPACE_VERSION
295 } // namespace std
296 #endif // __cpp_lib_atomic_wait
297 #endif // _GLIBCXX_ATOMIC_TIMED_WAIT_H
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition: move.h:101
void terminate() noexcept
ISO C++ entities toplevel namespace is std.