xref: /netbsd-src/external/gpl3/gcc.old/dist/libstdc++-v3/include/experimental/timer (revision 4c3eb207d36f67d31994830c0a694161fc1ca39b)
1627f7eb2Smrg// <experimental/timer> -*- C++ -*-
2627f7eb2Smrg
3*4c3eb207Smrg// Copyright (C) 2015-2020 Free Software Foundation, Inc.
4627f7eb2Smrg//
5627f7eb2Smrg// This file is part of the GNU ISO C++ Library.  This library is free
6627f7eb2Smrg// software; you can redistribute it and/or modify it under the
7627f7eb2Smrg// terms of the GNU General Public License as published by the
8627f7eb2Smrg// Free Software Foundation; either version 3, or (at your option)
9627f7eb2Smrg// any later version.
10627f7eb2Smrg
11627f7eb2Smrg// This library is distributed in the hope that it will be useful,
12627f7eb2Smrg// but WITHOUT ANY WARRANTY; without even the implied warranty of
13627f7eb2Smrg// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14627f7eb2Smrg// GNU General Public License for more details.
15627f7eb2Smrg
16627f7eb2Smrg// Under Section 7 of GPL version 3, you are granted additional
17627f7eb2Smrg// permissions described in the GCC Runtime Library Exception, version
18627f7eb2Smrg// 3.1, as published by the Free Software Foundation.
19627f7eb2Smrg
20627f7eb2Smrg// You should have received a copy of the GNU General Public License and
21627f7eb2Smrg// a copy of the GCC Runtime Library Exception along with this program;
22627f7eb2Smrg// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23627f7eb2Smrg// <http://www.gnu.org/licenses/>.
24627f7eb2Smrg
25627f7eb2Smrg/** @file experimental/timer
26627f7eb2Smrg *  This is a TS C++ Library header.
27*4c3eb207Smrg *  @ingroup networking-ts
28627f7eb2Smrg */
29627f7eb2Smrg
30627f7eb2Smrg#ifndef _GLIBCXX_EXPERIMENTAL_TIMER
31627f7eb2Smrg#define _GLIBCXX_EXPERIMENTAL_TIMER 1
32627f7eb2Smrg
33627f7eb2Smrg#pragma GCC system_header
34627f7eb2Smrg
35627f7eb2Smrg#if __cplusplus >= 201402L
36627f7eb2Smrg
37627f7eb2Smrg#include <chrono>
38627f7eb2Smrg#include <system_error>
39627f7eb2Smrg#include <thread>
40627f7eb2Smrg#include <experimental/netfwd>
41627f7eb2Smrg#include <experimental/io_context>
42627f7eb2Smrg#include <experimental/bits/net.h>
43627f7eb2Smrg
44627f7eb2Smrgnamespace std _GLIBCXX_VISIBILITY(default)
45627f7eb2Smrg{
46627f7eb2Smrg_GLIBCXX_BEGIN_NAMESPACE_VERSION
47627f7eb2Smrgnamespace experimental
48627f7eb2Smrg{
49627f7eb2Smrgnamespace net
50627f7eb2Smrg{
51627f7eb2Smrginline namespace v1
52627f7eb2Smrg{
53627f7eb2Smrg
54*4c3eb207Smrg  /** @addtogroup networking-ts
55627f7eb2Smrg   *  @{
56627f7eb2Smrg   */
57627f7eb2Smrg
58627f7eb2Smrg  template<typename _Clock>
59627f7eb2Smrg    struct wait_traits
60627f7eb2Smrg    {
61627f7eb2Smrg      static typename _Clock::duration
62627f7eb2Smrg      to_wait_duration(const typename _Clock::duration& __d)
63627f7eb2Smrg      { return __d; }
64627f7eb2Smrg
65627f7eb2Smrg      static typename _Clock::duration
66627f7eb2Smrg      to_wait_duration(const typename _Clock::time_point& __t)
67627f7eb2Smrg      {
68627f7eb2Smrg	auto __now = _Clock::now();
69627f7eb2Smrg	auto __diff = __t - __now;
70627f7eb2Smrg	if (__diff > _Clock::duration::max())
71627f7eb2Smrg	  return _Clock::duration::max();
72627f7eb2Smrg	if (__diff < _Clock::duration::min())
73627f7eb2Smrg	  return _Clock::duration::min();
74627f7eb2Smrg	return __diff;
75627f7eb2Smrg      }
76627f7eb2Smrg    };
77627f7eb2Smrg
78627f7eb2Smrg  template<typename _Clock, typename _WaitTraits>
79627f7eb2Smrg    class basic_waitable_timer
80627f7eb2Smrg    {
81627f7eb2Smrg    public:
82627f7eb2Smrg      // types:
83627f7eb2Smrg
84627f7eb2Smrg      typedef io_context::executor_type executor_type;
85627f7eb2Smrg      typedef _Clock clock_type;
86627f7eb2Smrg      typedef typename clock_type::duration duration;
87627f7eb2Smrg      typedef typename clock_type::time_point time_point;
88627f7eb2Smrg      typedef _WaitTraits traits_type;
89627f7eb2Smrg
90627f7eb2Smrg      // construct / copy / destroy:
91627f7eb2Smrg
92627f7eb2Smrg      explicit
93627f7eb2Smrg      basic_waitable_timer(io_context& __ctx)
94627f7eb2Smrg      : _M_ex(__ctx.get_executor()), _M_expiry()
95627f7eb2Smrg      { }
96627f7eb2Smrg
97627f7eb2Smrg      basic_waitable_timer(io_context& __ctx, const time_point& __t)
98627f7eb2Smrg      : _M_ex(__ctx.get_executor()), _M_expiry(__t)
99627f7eb2Smrg      { }
100627f7eb2Smrg
101627f7eb2Smrg      basic_waitable_timer(io_context& __ctx, const duration& __d)
102627f7eb2Smrg      : _M_ex(__ctx.get_executor()), _M_expiry(_Clock::now() + __d)
103627f7eb2Smrg      { }
104627f7eb2Smrg
105627f7eb2Smrg      basic_waitable_timer(const basic_waitable_timer&) = delete;
106627f7eb2Smrg
107627f7eb2Smrg      basic_waitable_timer(basic_waitable_timer&& __rhs)
108627f7eb2Smrg      : _M_ex(std::move(__rhs._M_ex)), _M_expiry(__rhs._M_expiry)
109627f7eb2Smrg      {
110627f7eb2Smrg	_M_key.swap(__rhs._M_key);
111627f7eb2Smrg	__rhs._M_expiry = time_point{};
112627f7eb2Smrg      }
113627f7eb2Smrg
114627f7eb2Smrg      ~basic_waitable_timer() { cancel(); }
115627f7eb2Smrg
116627f7eb2Smrg      basic_waitable_timer& operator=(const basic_waitable_timer&) = delete;
117627f7eb2Smrg
118627f7eb2Smrg      basic_waitable_timer&
119627f7eb2Smrg      operator=(basic_waitable_timer&& __rhs)
120627f7eb2Smrg      {
121627f7eb2Smrg	if (this == std::addressof(__rhs))
122627f7eb2Smrg	  return *this;
123627f7eb2Smrg	cancel();
124627f7eb2Smrg	_M_ex = std::move(__rhs._M_ex);
125627f7eb2Smrg	_M_expiry = __rhs._M_expiry;
126627f7eb2Smrg	__rhs._M_expiry = time_point{};
127627f7eb2Smrg	_M_key.swap(__rhs._M_key);
128627f7eb2Smrg	return *this;
129627f7eb2Smrg      }
130627f7eb2Smrg
131627f7eb2Smrg      // basic_waitable_timer operations:
132627f7eb2Smrg
133627f7eb2Smrg      executor_type get_executor() noexcept { return _M_ex; }
134627f7eb2Smrg
135627f7eb2Smrg      size_t cancel() { return _M_ex.context().cancel(*this); }
136627f7eb2Smrg      size_t cancel_one() { return _M_ex.context().cancel_one(*this); }
137627f7eb2Smrg
138627f7eb2Smrg      time_point expiry() const { return _M_expiry; }
139627f7eb2Smrg
140627f7eb2Smrg      size_t expires_at(const time_point& __t)
141627f7eb2Smrg      {
142627f7eb2Smrg	size_t __cancelled = cancel();
143627f7eb2Smrg	_M_expiry = __t;
144627f7eb2Smrg	return __cancelled;
145627f7eb2Smrg      }
146627f7eb2Smrg
147627f7eb2Smrg      size_t expires_after(const duration& __d)
148627f7eb2Smrg      { return expires_at(_Clock::now() + __d); }
149627f7eb2Smrg
150627f7eb2Smrg      void wait();
151627f7eb2Smrg      void wait(error_code& __ec);
152627f7eb2Smrg
153627f7eb2Smrg      template<typename _CompletionToken>
154627f7eb2Smrg	__deduced_t<_CompletionToken, void(error_code)>
155627f7eb2Smrg	async_wait(_CompletionToken&& __token)
156627f7eb2Smrg	{
157627f7eb2Smrg	  async_completion<_CompletionToken, void(error_code)> __init(__token);
158627f7eb2Smrg	  _M_ex.context().async_wait(*this,
159627f7eb2Smrg				     std::move(__init.completion_handler));
160627f7eb2Smrg	  return __init.result.get();
161627f7eb2Smrg	}
162627f7eb2Smrg
163627f7eb2Smrg    private:
164627f7eb2Smrg      executor_type _M_ex;
165627f7eb2Smrg      time_point _M_expiry;
166627f7eb2Smrg
167627f7eb2Smrg      struct _Key { };  // TODO move _M_expiry into here?
168627f7eb2Smrg      unique_ptr<_Key> _M_key{new _Key};
169627f7eb2Smrg
170627f7eb2Smrg      friend class io_context;
171627f7eb2Smrg    };
172627f7eb2Smrg
173627f7eb2Smrg  typedef basic_waitable_timer<chrono::system_clock> system_timer;
174627f7eb2Smrg  typedef basic_waitable_timer<chrono::steady_clock> steady_timer;
175627f7eb2Smrg  typedef basic_waitable_timer<chrono::high_resolution_clock>
176627f7eb2Smrg    high_resolution_timer;
177627f7eb2Smrg
178627f7eb2Smrg  template<typename _Clock, typename _WaitTraits>
179627f7eb2Smrg    void
180627f7eb2Smrg    basic_waitable_timer<_Clock, _WaitTraits>::wait()
181627f7eb2Smrg    {
182627f7eb2Smrg      _M_ex.dispatch([this] {
183627f7eb2Smrg	  while (clock_type::now() < _M_expiry)
184627f7eb2Smrg	    this_thread::sleep_for(traits_type::to_wait_duration(_M_expiry));
185627f7eb2Smrg      }, allocator<void>{});
186627f7eb2Smrg    }
187627f7eb2Smrg
188627f7eb2Smrg  template<typename _Clock, typename _WaitTraits>
189627f7eb2Smrg    void
190627f7eb2Smrg    basic_waitable_timer<_Clock, _WaitTraits>::wait(error_code&)
191627f7eb2Smrg    {
192627f7eb2Smrg      _M_ex.dispatch([this] {
193627f7eb2Smrg	  while (clock_type::now() < _M_expiry)
194627f7eb2Smrg	    this_thread::sleep_for(traits_type::to_wait_duration(_M_expiry));
195627f7eb2Smrg      }, allocator<void>{});
196627f7eb2Smrg    }
197627f7eb2Smrg
198627f7eb2Smrg  /// @}
199627f7eb2Smrg
200627f7eb2Smrg} // namespace v1
201627f7eb2Smrg} // namespace net
202627f7eb2Smrg} // namespace experimental
203627f7eb2Smrg_GLIBCXX_END_NAMESPACE_VERSION
204627f7eb2Smrg} // namespace std
205627f7eb2Smrg
206627f7eb2Smrg#endif // C++14
207627f7eb2Smrg
208627f7eb2Smrg#endif // _GLIBCXX_EXPERIMENTAL_TIMER
209