MySQL 8.3.0
Source Code Documentation
timer.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2019, 2023, 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 also distributed 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 included with MySQL.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23*/
24
25#ifndef MYSQL_HARNESS_NET_TS_TIMER_H_
26#define MYSQL_HARNESS_NET_TS_TIMER_H_
27
28#include <chrono>
29#include <thread> // this_thread
30
31#include "my_compiler.h"
35
36namespace net {
37
38template <class Clock>
40 static typename Clock::duration to_wait_duration(
41 const typename Clock::duration &d) {
42 return d;
43 }
44 static typename Clock::duration to_wait_duration(
45 const typename Clock::time_point &tp) {
46 auto diff = tp - Clock::now();
47
48 if (diff > Clock::duration::max()) return Clock::duration::max();
49 if (diff < Clock::duration::min()) return Clock::duration::min();
50
51 return diff;
52 }
53};
54
55template <class Clock, class WaitTraits>
57 public:
59 using clock_type = Clock;
60 using duration = typename clock_type::duration;
61 using time_point = typename clock_type::time_point;
62 using traits_type = WaitTraits;
63
64 // 15.4.1 construct/copy/destroy
66 : basic_waitable_timer(io_ctx, time_point()) {}
68 : executor_{io_ctx.get_executor()}, expiry_{tp} {}
70 : basic_waitable_timer(io_ctx, Clock::now() + d) {}
71
74 : executor_{std::move(rhs.executor_)}, expiry_{std::move(rhs.expiry_)} {
75 id_.swap(rhs.id_);
76
77 rhs.expiry_ = time_point();
78 }
79
81
84 if (this == std::addressof(rhs)) {
85 return *this;
86 }
87 cancel();
88
89 executor_ = std::move(rhs.executor_);
90 expiry_ = std::move(rhs.expiry_);
91 id_.swap(rhs.id_);
92
93 rhs.expiry_ = time_point();
94
95 return *this;
96 }
97
98 // 15.4.4 ops
99 executor_type get_executor() noexcept { return executor_; }
100 size_t cancel() { return executor_.context().cancel(*this); }
101 size_t cancel_one() { return executor_.context().cancel_one(*this); }
102
103 time_point expiry() const { return expiry_; }
104
105 size_t expires_at(const time_point &t) {
106 size_t cancelled = cancel();
107
108 expiry_ = t;
109
110 return cancelled;
111 }
112
113 size_t expires_after(const duration &d) {
114 return expires_at(clock_type::now() + d);
115 }
116
118 while (clock_type::now() < expiry_) {
119 std::this_thread::sleep_for(traits_type::to_wait_duration(expiry_));
120 }
121
122 return {};
123 }
124
125 template <class CompletionToken>
126 auto async_wait(CompletionToken &&token) {
127 async_completion<CompletionToken, void(std::error_code)> init{token};
128
130 std::move(init.completion_handler));
131
132 return init.result.get();
133 }
134
135 private:
138
139 // every timer needs a unique-id to be able to identify it (e.g. to cancel it)
140 //
141 // Note: empty classes like "Id" have a sizeof() > 0. It would perhaps make
142 // sense to use it for something useful.
143 struct Id {};
144
145 Id *id() const { return id_.get(); }
146
147 std::unique_ptr<Id> id_{new Id};
148
149 friend class io_context;
150};
151
156
157} // namespace net
158
159#endif
static mysql_service_status_t init()
Component initialization.
Definition: audit_api_message_emit.cc:570
Definition: executor.h:71
Definition: timer.h:56
basic_waitable_timer(const basic_waitable_timer &)=delete
executor_type executor_
Definition: timer.h:136
~basic_waitable_timer()
Definition: timer.h:80
basic_waitable_timer(io_context &io_ctx)
Definition: timer.h:65
basic_waitable_timer & operator=(const basic_waitable_timer &)=delete
time_point expiry_
Definition: timer.h:137
basic_waitable_timer(io_context &io_ctx, const duration &d)
Definition: timer.h:69
Id * id() const
Definition: timer.h:145
basic_waitable_timer(basic_waitable_timer &&rhs)
Definition: timer.h:73
size_t cancel()
Definition: timer.h:100
time_point expiry() const
Definition: timer.h:103
basic_waitable_timer(io_context &io_ctx, const time_point &tp)
Definition: timer.h:67
typename clock_type::duration duration
Definition: timer.h:60
typename clock_type::time_point time_point
Definition: timer.h:61
auto async_wait(CompletionToken &&token)
Definition: timer.h:126
WaitTraits traits_type
Definition: timer.h:62
size_t expires_at(const time_point &t)
Definition: timer.h:105
executor_type get_executor() noexcept
Definition: timer.h:99
size_t cancel_one()
Definition: timer.h:101
size_t expires_after(const duration &d)
Definition: timer.h:113
basic_waitable_timer & operator=(basic_waitable_timer &&rhs)
Definition: timer.h:83
stdx::expected< void, std::error_code > wait()
Definition: timer.h:117
Clock clock_type
Definition: timer.h:59
std::unique_ptr< Id > id_
Definition: timer.h:147
Definition: io_context.h:989
io_context & context() const noexcept
Definition: io_context.h:1001
Definition: io_context.h:60
size_t cancel_one(const Timer &)
Definition: io_context.h:846
void async_wait(native_handle_type fd, impl::socket::wait_type wt, Op &&op)
Definition: io_context.h:482
stdx::expected< void, std::error_code > cancel(native_handle_type fd)
cancel all async-ops of a file-descriptor.
Definition: io_context.h:1086
Definition: expected.h:943
static Bigint * diff(Bigint *a, Bigint *b, Stack_alloc *alloc)
Definition: dtoa.cc:1076
Header for compiler-dependent features.
Definition: buffer.h:44
Definition: varlen_sort.h:174
Definition: timer.h:143
Definition: timer.h:39
static Clock::duration to_wait_duration(const typename Clock::time_point &tp)
Definition: timer.h:44
static Clock::duration to_wait_duration(const typename Clock::duration &d)
Definition: timer.h:40