MySQL 8.4.0
Source Code Documentation
wait_variable.h
Go to the documentation of this file.
1/* Copyright (c) 2023, 2024, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
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#ifndef ROUTER_SRC_ROUTER_INCLUDE_HELPER_WAIT_VARIABLE_H_
25#define ROUTER_SRC_ROUTER_INCLUDE_HELPER_WAIT_VARIABLE_H_
26
27#include <initializer_list>
28
29#include "container/generic.h"
32
33template <typename ValueType>
35 public:
37 WaitableVariable(const ValueType &value) : monitor_with_value_{value} {}
38
39 class DoNothing {
40 public:
41 void operator()() const {}
42 };
43
44 template <typename Container = std::initializer_list<ValueType>,
45 typename Callback = DoNothing>
46 bool exchange(const Container &expected, const ValueType &v,
47 const Callback &after_set_callback = DoNothing()) {
48 bool result{false};
49 monitor_with_value_.serialize_with_cv(
50 [&expected, &v, &after_set_callback, &result](auto &value, auto &cv) {
51 if (helper::container::has(expected, value)) {
52 value = v;
53 result = true;
54 after_set_callback();
55 cv.notify_all();
56 }
57 });
58
59 return result;
60 }
61
62 template <typename Callback = DoNothing>
63 bool exchange(const ValueType &expected, const ValueType &v,
64 const Callback &after_set_callback = DoNothing()) {
65 bool result{false};
66 monitor_with_value_.serialize_with_cv(
67 [&expected, &v, &after_set_callback, &result](auto &value, auto &cv) {
68 if (expected == value) {
69 value = v;
70 result = true;
71 after_set_callback();
72 cv.notify_all();
73 }
74 });
75
76 return result;
77 }
78
79 template <typename Callback = DoNothing>
80 ValueType get(const Callback &after_get_callback = DoNothing()) {
81 ValueType result;
82 monitor_with_value_.serialize_with_cv(
83 [&result, &after_get_callback](auto &current_value, auto &) {
84 result = current_value;
85 after_get_callback();
86 });
87
88 return result;
89 }
90
91 template <typename Callback = DoNothing>
92 void set(const ValueType &v,
93 const Callback &after_set_callback = DoNothing()) {
94 monitor_with_value_.serialize_with_cv(
95 [&v, &after_set_callback](auto &value, auto &cv) {
96 value = v;
97 after_set_callback();
98 cv.notify_all();
99 });
100 }
101
102 template <typename Callback>
103 void change(const Callback &set_callback) {
104 monitor_with_value_.serialize_with_cv(
105 [&set_callback](auto &value, auto &cv) {
106 set_callback(value);
107 cv.notify_all();
108 });
109 }
110
111 template <typename Callback = DoNothing>
112 bool is(std::initializer_list<ValueType> expected_values,
113 const Callback &after_is_callback = Callback()) {
114 bool result{false};
115 monitor_with_value_.serialize_with_cv(
116 [this, &result, &expected_values, &after_is_callback](
117 auto &current_value, auto &) {
118 result = helper::container::has(expected_values, current_value);
119 if (result) after_is_callback();
120 });
121 return result;
122 }
123
124 template <typename Callback = DoNothing>
125 bool is(const ValueType &expected_value,
126 const Callback &after_is_callback = Callback()) {
127 bool result{false};
128 monitor_with_value_.serialize_with_cv(
129 [&result, &expected_value, &after_is_callback](auto &current_value,
130 auto &) {
131 result = (expected_value == current_value);
132 if (result) after_is_callback();
133 });
134 return result;
135 }
136
137 template <typename Callback = DoNothing>
138 void wait(const ValueType &expected_value,
139 const Callback &callback = Callback()) {
141 [&expected_value, &callback](const auto &current_value) {
142 if (expected_value == current_value) {
143 callback();
144 return true;
145 }
146 return false;
147 });
148 }
149
150 template <typename Callback = DoNothing>
151 ValueType wait(std::initializer_list<ValueType> expected_values,
152 const Callback &callback = Callback()) {
153 ValueType result;
155 [&expected_values, &callback, &result](const auto &current_value) {
156 if (helper::container::has(expected_values, current_value)) {
157 result = current_value;
158 callback();
159 return true;
160 }
161 return false;
162 });
163
164 return result;
165 }
166
167 template <class Rep, class Period, typename Callback = DoNothing>
168 bool wait_for(const std::chrono::duration<Rep, Period> &rel_time,
169 const ValueType &expected_value,
170 const Callback &callback = Callback()) {
171 return monitor_with_value_.wait_for(
172 rel_time, [this, expected_value, &callback](auto &current_value) {
173 if (current_value == expected_value) {
174 callback();
175 return true;
176 }
177 return false;
178 });
179 }
180
181 template <class Rep, class Period, typename Callback = DoNothing>
183 const std::chrono::duration<Rep, Period> &rel_time,
184 std::initializer_list<ValueType> expected_values,
185 const Callback &callback = Callback()) {
186 ValueType result;
187 if (monitor_with_value_.wait_for(
188 rel_time, [this, &expected_values, &result,
189 &callback](const auto &current_value) {
190 if (helper::container::has(expected_values, current_value)) {
191 result = current_value;
192 callback();
193 return true;
194 }
195 return false;
196 })) {
197 return {result};
198 }
199
200 return {stdx::unexpect, true};
201 }
202
203 private:
204 WaitableMonitor<ValueType> monitor_with_value_{};
205};
206
207#endif // ROUTER_SRC_ROUTER_INCLUDE_HELPER_WAIT_VARIABLE_H_
Class that stores callback function reference as well as the result of the callback function call (in...
Definition: keyring_service.cc:43
Monitor can be waited for.
Definition: monitor.h:62
Definition: wait_variable.h:39
void operator()() const
Definition: wait_variable.h:41
Definition: wait_variable.h:34
WaitableMonitor< ValueType > monitor_with_value_
Definition: wait_variable.h:204
bool is(std::initializer_list< ValueType > expected_values, const Callback &after_is_callback=Callback())
Definition: wait_variable.h:112
void change(const Callback &set_callback)
Definition: wait_variable.h:103
WaitableVariable()
Definition: wait_variable.h:36
WaitableVariable(const ValueType &value)
Definition: wait_variable.h:37
ValueType get(const Callback &after_get_callback=DoNothing())
Definition: wait_variable.h:80
void wait(const ValueType &expected_value, const Callback &callback=Callback())
Definition: wait_variable.h:138
ValueType wait(std::initializer_list< ValueType > expected_values, const Callback &callback=Callback())
Definition: wait_variable.h:151
stdx::expected< ValueType, bool > wait_for(const std::chrono::duration< Rep, Period > &rel_time, std::initializer_list< ValueType > expected_values, const Callback &callback=Callback())
Definition: wait_variable.h:182
bool exchange(const Container &expected, const ValueType &v, const Callback &after_set_callback=DoNothing())
Definition: wait_variable.h:46
bool is(const ValueType &expected_value, const Callback &after_is_callback=Callback())
Definition: wait_variable.h:125
bool wait_for(const std::chrono::duration< Rep, Period > &rel_time, const ValueType &expected_value, const Callback &callback=Callback())
Definition: wait_variable.h:168
bool exchange(const ValueType &expected, const ValueType &v, const Callback &after_set_callback=DoNothing())
Definition: wait_variable.h:63
void set(const ValueType &v, const Callback &after_set_callback=DoNothing())
Definition: wait_variable.h:92
Definition: expected.h:284
bool has(const Container &c, Value &&val)
Definition: generic.h:97
constexpr unexpect_t unexpect
Definition: expected.h:109
struct result result
Definition: result.h:34
Definition: result.h:30