]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/experimental/timer
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / include / experimental / timer
CommitLineData
e5989e71
JW
1// <experimental/timer> -*- C++ -*-
2
a5544970 3// Copyright (C) 2015-2019 Free Software Foundation, Inc.
e5989e71
JW
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 experimental/timer
26 * This is a TS C++ Library header.
27 */
28
29#ifndef _GLIBCXX_EXPERIMENTAL_TIMER
30#define _GLIBCXX_EXPERIMENTAL_TIMER 1
31
32#pragma GCC system_header
33
34#if __cplusplus >= 201402L
35
36#include <chrono>
37#include <system_error>
38#include <thread>
39#include <experimental/netfwd>
40#include <experimental/io_context>
41#include <experimental/bits/net.h>
42
43namespace std _GLIBCXX_VISIBILITY(default)
44{
a70a4be9 45_GLIBCXX_BEGIN_NAMESPACE_VERSION
e5989e71
JW
46namespace experimental
47{
48namespace net
49{
50inline namespace v1
51{
e5989e71
JW
52
53 /**
54 * @ingroup networking
55 * @{
56 */
57
58 template<typename _Clock>
59 struct wait_traits
60 {
61 static typename _Clock::duration
62 to_wait_duration(const typename _Clock::duration& __d)
63 { return __d; }
64
65 static typename _Clock::duration
66 to_wait_duration(const typename _Clock::time_point& __t)
67 {
68 auto __now = _Clock::now();
69 auto __diff = __t - __now;
70 if (__diff > _Clock::duration::max())
71 return _Clock::duration::max();
72 if (__diff < _Clock::duration::min())
73 return _Clock::duration::min();
74 return __diff;
75 }
76 };
77
78 template<typename _Clock, typename _WaitTraits>
79 class basic_waitable_timer
80 {
81 public:
82 // types:
83
84 typedef io_context::executor_type executor_type;
85 typedef _Clock clock_type;
86 typedef typename clock_type::duration duration;
87 typedef typename clock_type::time_point time_point;
88 typedef _WaitTraits traits_type;
89
90 // construct / copy / destroy:
91
92 explicit
93 basic_waitable_timer(io_context& __ctx)
94 : _M_ex(__ctx.get_executor()), _M_expiry()
95 { }
96
97 basic_waitable_timer(io_context& __ctx, const time_point& __t)
98 : _M_ex(__ctx.get_executor()), _M_expiry(__t)
99 { }
100
101 basic_waitable_timer(io_context& __ctx, const duration& __d)
102 : _M_ex(__ctx.get_executor()), _M_expiry(_Clock::now() + __d)
103 { }
104
105 basic_waitable_timer(const basic_waitable_timer&) = delete;
106
107 basic_waitable_timer(basic_waitable_timer&& __rhs)
108 : _M_ex(std::move(__rhs._M_ex)), _M_expiry(__rhs._M_expiry)
109 {
110 _M_key.swap(__rhs._M_key);
111 __rhs._M_expiry = time_point{};
112 }
113
114 ~basic_waitable_timer() { cancel(); }
115
116 basic_waitable_timer& operator=(const basic_waitable_timer&) = delete;
117
118 basic_waitable_timer&
119 operator=(basic_waitable_timer&& __rhs)
120 {
121 if (this == std::addressof(__rhs))
122 return *this;
123 cancel();
124 _M_ex = std::move(__rhs._M_ex);
125 _M_expiry = __rhs._M_expiry;
126 __rhs._M_expiry = time_point{};
127 _M_key.swap(__rhs._M_key);
128 return *this;
129 }
130
131 // basic_waitable_timer operations:
132
133 executor_type get_executor() noexcept { return _M_ex; }
134
135 size_t cancel() { return _M_ex.context().cancel(*this); }
136 size_t cancel_one() { return _M_ex.context().cancel_one(*this); }
137
138 time_point expiry() const { return _M_expiry; }
139
140 size_t expires_at(const time_point& __t)
141 {
142 size_t __cancelled = cancel();
143 _M_expiry = __t;
144 return __cancelled;
145 }
146
147 size_t expires_after(const duration& __d)
148 { return expires_at(_Clock::now() + __d); }
149
150 void wait();
151 void wait(error_code& __ec);
152
153 template<typename _CompletionToken>
154 __deduced_t<_CompletionToken, void(error_code)>
155 async_wait(_CompletionToken&& __token)
156 {
157 async_completion<_CompletionToken, void(error_code)> __init(__token);
158 _M_ex.context().async_wait(*this,
159 std::move(__init.completion_handler));
160 return __init.result.get();
161 }
162
163 private:
164 executor_type _M_ex;
165 time_point _M_expiry;
166
167 struct _Key { }; // TODO move _M_expiry into here?
168 unique_ptr<_Key> _M_key{new _Key};
169
170 friend class io_context;
171 };
172
173 typedef basic_waitable_timer<chrono::system_clock> system_timer;
174 typedef basic_waitable_timer<chrono::steady_clock> steady_timer;
175 typedef basic_waitable_timer<chrono::high_resolution_clock>
176 high_resolution_timer;
177
178 template<typename _Clock, typename _WaitTraits>
179 void
180 basic_waitable_timer<_Clock, _WaitTraits>::wait()
181 {
182 _M_ex.dispatch([this] {
183 while (clock_type::now() < _M_expiry)
184 this_thread::sleep_for(traits_type::to_wait_duration(_M_expiry));
185 }, allocator<void>{});
186 }
187
188 template<typename _Clock, typename _WaitTraits>
189 void
190 basic_waitable_timer<_Clock, _WaitTraits>::wait(error_code&)
191 {
192 _M_ex.dispatch([this] {
193 while (clock_type::now() < _M_expiry)
194 this_thread::sleep_for(traits_type::to_wait_duration(_M_expiry));
195 }, allocator<void>{});
196 }
197
198 /// @}
199
e5989e71
JW
200} // namespace v1
201} // namespace net
202} // namespace experimental
a70a4be9 203_GLIBCXX_END_NAMESPACE_VERSION
e5989e71
JW
204} // namespace std
205
206#endif // C++14
207
208#endif // _GLIBCXX_EXPERIMENTAL_TIMER