MySQL 8.0.39
Source Code Documentation
timer.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2019, 2024, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef MYSQL_HARNESS_NET_TS_TIMER_H_
27#define MYSQL_HARNESS_NET_TS_TIMER_H_
28
29#include <chrono>
30#include <thread> // this_thread
31
32#include "my_compiler.h"
36
37namespace net {
38
39template <class Clock>
41 static typename Clock::duration to_wait_duration(
42 const typename Clock::duration &d) {
43 return d;
44 }
45 static typename Clock::duration to_wait_duration(
46 const typename Clock::time_point &tp) {
47 auto diff = tp - Clock::now();
48
49 if (diff > Clock::duration::max()) return Clock::duration::max();
50 if (diff < Clock::duration::min()) return Clock::duration::min();
51
52 return diff;
53 }
54};
55
56template <class Clock, class WaitTraits>
58 public:
60 using clock_type = Clock;
61 using duration = typename clock_type::duration;
62 using time_point = typename clock_type::time_point;
63 using traits_type = WaitTraits;
64
65 // 15.4.1 construct/copy/destroy
67 : basic_waitable_timer(io_ctx, time_point()) {}
69 : executor_{io_ctx.get_executor()}, expiry_{tp} {}
71 : basic_waitable_timer(io_ctx, Clock::now() + d) {}
72
75 : executor_{std::move(rhs.executor_)}, expiry_{std::move(rhs.expiry_)} {
76 id_.swap(rhs.id_);
77
78 rhs.expiry_ = time_point();
79 }
80
82
85 if (this == std::addressof(rhs)) {
86 return *this;
87 }
88 cancel();
89
90 executor_ = std::move(rhs.executor_);
91 expiry_ = std::move(rhs.expiry_);
92 id_.swap(rhs.id_);
93
94 rhs.expiry_ = time_point();
95
96 return *this;
97 }
98
99 // 15.4.4 ops
100 executor_type get_executor() noexcept { return executor_; }
101 size_t cancel() { return executor_.context().cancel(*this); }
102 size_t cancel_one() { return executor_.context().cancel_one(*this); }
103
104 time_point expiry() const { return expiry_; }
105
106 size_t expires_at(const time_point &t) {
107 size_t cancelled = cancel();
108
109 expiry_ = t;
110
111 return cancelled;
112 }
113
114 size_t expires_after(const duration &d) {
115 return expires_at(clock_type::now() + d);
116 }
117
119 while (clock_type::now() < expiry_) {
120 std::this_thread::sleep_for(traits_type::to_wait_duration(expiry_));
121 }
122
123 return {};
124 }
125
126 template <class CompletionToken>
127 auto async_wait(CompletionToken &&token) {
128 async_completion<CompletionToken, void(std::error_code)> init{token};
129
131 std::move(init.completion_handler));
132
133 return init.result.get();
134 }
135
136 private:
139
140 // every timer needs a unique-id to be able to identify it (e.g. to cancel it)
141 //
142 // Note: empty classes like "Id" have a sizeof() > 0. It would perhaps make
143 // sense to use it for something useful.
144 struct Id {};
145
146 Id *id() const { return id_.get(); }
147
148 std::unique_ptr<Id> id_{new Id};
149
150 friend class io_context;
151};
152
157
158} // namespace net
159
160#endif
static mysql_service_status_t init()
Component initialization.
Definition: audit_api_message_emit.cc:571
Definition: executor.h:72
Definition: timer.h:57
basic_waitable_timer(const basic_waitable_timer &)=delete
executor_type executor_
Definition: timer.h:137
~basic_waitable_timer()
Definition: timer.h:81
basic_waitable_timer(io_context &io_ctx)
Definition: timer.h:66
basic_waitable_timer & operator=(const basic_waitable_timer &)=delete
time_point expiry_
Definition: timer.h:138
basic_waitable_timer(io_context &io_ctx, const duration &d)
Definition: timer.h:70
Id * id() const
Definition: timer.h:146
basic_waitable_timer(basic_waitable_timer &&rhs)
Definition: timer.h:74
size_t cancel()
Definition: timer.h:101
time_point expiry() const
Definition: timer.h:104
basic_waitable_timer(io_context &io_ctx, const time_point &tp)
Definition: timer.h:68
typename clock_type::duration duration
Definition: timer.h:61
typename clock_type::time_point time_point
Definition: timer.h:62
auto async_wait(CompletionToken &&token)
Definition: timer.h:127
WaitTraits traits_type
Definition: timer.h:63
size_t expires_at(const time_point &t)
Definition: timer.h:106
executor_type get_executor() noexcept
Definition: timer.h:100
size_t cancel_one()
Definition: timer.h:102
size_t expires_after(const duration &d)
Definition: timer.h:114
basic_waitable_timer & operator=(basic_waitable_timer &&rhs)
Definition: timer.h:84
stdx::expected< void, std::error_code > wait()
Definition: timer.h:118
Clock clock_type
Definition: timer.h:60
std::unique_ptr< Id > id_
Definition: timer.h:148
Definition: io_context.h:990
io_context & context() const noexcept
Definition: io_context.h:1002
Definition: io_context.h:61
size_t cancel_one(const Timer &)
Definition: io_context.h:847
void async_wait(native_handle_type fd, impl::socket::wait_type wt, Op &&op)
Definition: io_context.h:483
stdx::expected< void, std::error_code > cancel(native_handle_type fd)
cancel all async-ops of a file-descriptor.
Definition: io_context.h:1087
Definition: expected.h:944
static Bigint * diff(Bigint *a, Bigint *b, Stack_alloc *alloc)
Definition: dtoa.cc:1082
Header for compiler-dependent features.
Definition: buffer.h:45
Definition: gcs_xcom_synode.h:64
Definition: timer.h:144
Definition: timer.h:40
static Clock::duration to_wait_duration(const typename Clock::time_point &tp)
Definition: timer.h:45
static Clock::duration to_wait_duration(const typename Clock::duration &d)
Definition: timer.h:41