aboutsummaryrefslogtreecommitdiff
path: root/libstdc++-v3/include/bits/atomic_timed_wait.h
blob: 30f7ff6168404041940772214c742e120273bf76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// -*- C++ -*- header.

// Copyright (C) 2020-2025 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.

// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
// <http://www.gnu.org/licenses/>.

/** @file bits/atomic_timed_wait.h
 *  This is an internal header file, included by other library headers.
 *  Do not attempt to use it directly. @headername{atomic}
 */

#ifndef _GLIBCXX_ATOMIC_TIMED_WAIT_H
#define _GLIBCXX_ATOMIC_TIMED_WAIT_H 1

#ifdef _GLIBCXX_SYSHDR
#pragma GCC system_header
#endif

#include <bits/atomic_wait.h>

#if __glibcxx_atomic_wait
#include <bits/this_thread_sleep.h>
#include <bits/chrono.h>

#ifdef _GLIBCXX_HAVE_LINUX_FUTEX
#include <sys/time.h>
#endif

namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION

  namespace __detail
  {
    using __wait_clock_t = chrono::steady_clock;

    template<typename _Clock, typename _Dur>
      __wait_clock_t::time_point
      __to_wait_clock(const chrono::time_point<_Clock, _Dur>& __atime) noexcept
      {
	const typename _Clock::time_point __c_entry = _Clock::now();
	const __wait_clock_t::time_point __w_entry = __wait_clock_t::now();
	const auto __delta = __atime - __c_entry;
	using __w_dur = typename __wait_clock_t::duration;
	return __w_entry + chrono::ceil<__w_dur>(__delta);
      }

    template<typename _Dur>
      __wait_clock_t::time_point
      __to_wait_clock(const chrono::time_point<__wait_clock_t,
					       _Dur>& __atime) noexcept
      {
	using __w_dur = typename __wait_clock_t::duration;
	if constexpr (is_same_v<__w_dur, _Dur>)
	  return __atime;
	else
	  return chrono::ceil<__w_dur>(__atime);
      }

#ifdef _GLIBCXX_HAVE_LINUX_FUTEX
#define _GLIBCXX_HAVE_PLATFORM_TIMED_WAIT
#else
// define _GLIBCXX_HAVE_PLATFORM_TIMED_WAIT and implement __platform_wait_until
// if there is a more efficient primitive supported by the platform
// (e.g. __ulock_wait) which is better than pthread_cond_clockwait.
#endif // ! HAVE_LINUX_FUTEX

    __wait_result_type
    __wait_until_impl(const void* __addr, __wait_args_base& __args,
		      const __wait_clock_t::duration& __atime);

    template<typename _Clock, typename _Dur>
      __wait_result_type
      __wait_until(const void* __addr, __wait_args_base& __args,
		   const chrono::time_point<_Clock, _Dur>& __atime) noexcept
      {
	auto __at = __detail::__to_wait_clock(__atime);
	auto __res = __detail::__wait_until_impl(__addr, __args,
						 __at.time_since_epoch());

	if constexpr (!is_same_v<__wait_clock_t, _Clock>)
	  if (__res._M_timeout)
	    {
	      // We got a timeout when measured against __clock_t but
	      // we need to check against the caller-supplied clock
	      // to tell whether we should return a timeout.
	      if (_Clock::now() < __atime)
		__res._M_timeout = false;
	    }
	return __res;
      }

    template<typename _Rep, typename _Period>
      __wait_result_type
      __wait_for(const void* __addr, __wait_args_base& __args,
		 const chrono::duration<_Rep, _Period>& __rtime) noexcept
      {
	if (!__rtime.count())
	  {
	    // no rtime supplied, just spin a bit
	    __args._M_flags |= __wait_flags::__do_spin | __wait_flags::__spin_only;
	    return __detail::__wait_impl(__addr, __args);
	  }

	auto const __reltime = chrono::ceil<__wait_clock_t::duration>(__rtime);
	auto const __atime = chrono::steady_clock::now() + __reltime;
	return __detail::__wait_until(__addr, __args, __atime);
      }
  } // namespace __detail

  // returns true if wait ended before timeout
  template<typename _Tp,
	   typename _Pred, typename _ValFn,
	   typename _Clock, typename _Dur>
    bool
    __atomic_wait_address_until(const _Tp* __addr, _Pred&& __pred,
				_ValFn&& __vfn,
				const chrono::time_point<_Clock, _Dur>& __atime,
				bool __bare_wait = false) noexcept
    {
      __detail::__wait_args __args{ __addr, __bare_wait };
      _Tp __val = __args._M_setup_wait(__addr, __vfn);
      while (!__pred(__val))
	{
	  auto __res = __detail::__wait_until(__addr, __args, __atime);
	  if (__res._M_timeout)
	    return false; // C++26 will also return last observed __val
	  __val = __args._M_setup_wait(__addr, __vfn, __res);
	}
      return true; // C++26 will also return last observed __val
    }

  template<typename _Clock, typename _Dur>
    bool
    __atomic_wait_address_until_v(const __detail::__platform_wait_t* __addr,
				  __detail::__platform_wait_t __old,
				  int __order,
				  const chrono::time_point<_Clock, _Dur>& __atime,
				  bool __bare_wait = false) noexcept
    {
#ifndef _GLIBCXX_HAVE_PLATFORM_TIMED_WAIT
      __glibcxx_assert(false); // This function can't be used for proxy wait.
#endif
      __detail::__wait_args __args{ __addr, __old, __order, __bare_wait };
      auto __res = __detail::__wait_until(__addr, __args, __atime);
      return !__res._M_timeout; // C++26 will also return last observed __val
    }

  template<typename _Tp, typename _ValFn,
	   typename _Clock, typename _Dur>
    bool
    __atomic_wait_address_until_v(const _Tp* __addr, _Tp&& __old,
				  _ValFn&& __vfn,
				  const chrono::time_point<_Clock, _Dur>& __atime,
				  bool __bare_wait = false) noexcept
    {
      auto __pfn = [&](const _Tp& __val) {
	return !__detail::__atomic_eq(__old, __val);
      };
      return std::__atomic_wait_address_until(__addr, __pfn, __vfn, __atime,
					      __bare_wait);
    }

  template<typename _Tp,
	   typename _Pred, typename _ValFn,
	   typename _Rep, typename _Period>
    bool
    __atomic_wait_address_for(const _Tp* __addr, _Pred&& __pred,
			      _ValFn&& __vfn,
			      const chrono::duration<_Rep, _Period>& __rtime,
			      bool __bare_wait = false) noexcept
    {
      __detail::__wait_args __args{ __addr, __bare_wait };
      _Tp __val = __args._M_setup_wait(__addr, __vfn);
      while (!__pred(__val))
	{
	  auto __res = __detail::__wait_for(__addr, __args, __rtime);
	  if (__res._M_timeout)
	    return false; // C++26 will also return last observed __val
	  __val = __args._M_setup_wait(__addr, __vfn);
	}
      return true; // C++26 will also return last observed __val
    }

  template<typename _Rep, typename _Period>
    bool
    __atomic_wait_address_for_v(const __detail::__platform_wait_t* __addr,
				__detail::__platform_wait_t __old,
				int __order,
				const chrono::duration<_Rep, _Period>& __rtime,
				bool __bare_wait = false) noexcept
    {
#ifndef _GLIBCXX_HAVE_PLATFORM_TIMED_WAIT
      __glibcxx_assert(false); // This function can't be used for proxy wait.
#endif
      __detail::__wait_args __args{ __addr, __old, __order, __bare_wait };
      auto __res = __detail::__wait_for(__addr, __args, __rtime);
      return !__res._M_timeout; // C++26 will also return last observed __val
    }

  template<typename _Tp, typename _ValFn,
	   typename _Rep, typename _Period>
    bool
    __atomic_wait_address_for_v(const _Tp* __addr, _Tp&& __old, _ValFn&& __vfn,
				const chrono::duration<_Rep, _Period>& __rtime,
				bool __bare_wait = false) noexcept
    {
      auto __pfn = [&](const _Tp& __val) {
	return !__detail::__atomic_eq(__old, __val);
      };
      return __atomic_wait_address_for(__addr, __pfn, forward<_ValFn>(__vfn),
				       __rtime, __bare_wait);
    }
_GLIBCXX_END_NAMESPACE_VERSION
} // namespace std
#endif // __cpp_lib_atomic_wait
#endif // _GLIBCXX_ATOMIC_TIMED_WAIT_H