MySQL 9.7.0
Source Code Documentation
wait_variable.h
Go to the documentation of this file.
1/* Copyright (c) 2023, 2026, 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 MYSQL_HARNESS_UTILITY_WAIT_VARIABLE_H_
25#define MYSQL_HARNESS_UTILITY_WAIT_VARIABLE_H_
26
27#include <initializer_list>
28
32
33namespace mysql_harness {
34namespace utility {
35
36template <typename ValueType>
38 public:
41
42 class DoNothing {
43 public:
44 void operator()() const {}
45 };
46
47 template <typename Container = std::initializer_list<ValueType>,
48 typename Callback = DoNothing>
49 bool exchange(const Container &expected, const ValueType &v,
50 const Callback &after_set_callback = DoNothing()) {
51 bool result{false};
52 monitor_with_value_.serialize_with_cv(
53 [&expected, &v, &after_set_callback, &result](auto &value, auto &cv) {
55 value = v;
56 result = true;
57 after_set_callback();
58 cv.notify_all();
59 }
60 });
61
62 return result;
63 }
64
65 template <typename Callback = DoNothing>
66 bool exchange(const ValueType &expected, const ValueType &v,
67 const Callback &after_set_callback = DoNothing()) {
68 bool result{false};
69 monitor_with_value_.serialize_with_cv(
70 [&expected, &v, &after_set_callback, &result](auto &value, auto &cv) {
71 if (expected == value) {
72 value = v;
73 result = true;
74 after_set_callback();
75 cv.notify_all();
76 }
77 });
78
79 return result;
80 }
81
82 template <typename Callback = DoNothing>
83 ValueType get(const Callback &after_get_callback = DoNothing()) const {
84 ValueType result;
85 monitor_with_value_.serialize_with_cv(
86 [&result, &after_get_callback](auto &current_value, auto &) {
87 result = current_value;
88 after_get_callback();
89 });
90
91 return result;
92 }
93
94 template <typename Callback = DoNothing>
95 void set(const ValueType &v,
96 const Callback &after_set_callback = DoNothing()) {
97 monitor_with_value_.serialize_with_cv(
98 [&v, &after_set_callback](auto &value, auto &cv) {
99 value = v;
100 after_set_callback();
101 cv.notify_all();
102 });
103 }
104
105 template <typename Callback>
106 void change(const Callback &set_callback) {
107 monitor_with_value_.serialize_with_cv(
108 [&set_callback](auto &value, auto &cv) {
109 set_callback(value);
110 cv.notify_all();
111 });
112 }
113
114 template <typename Callback = DoNothing>
115 bool is(std::initializer_list<ValueType> expected_values,
116 const Callback &after_is_callback = Callback()) {
117 bool result{false};
118 monitor_with_value_.serialize_with_cv(
119 [&result, &expected_values, &after_is_callback](auto &current_value,
120 auto &) {
122 current_value);
123 if (result) after_is_callback();
124 });
125 return result;
126 }
127
128 template <typename Callback = DoNothing>
129 bool is(const ValueType &expected_value,
130 const Callback &after_is_callback = Callback()) {
131 bool result{false};
132 monitor_with_value_.serialize_with_cv(
133 [&result, &expected_value, &after_is_callback](auto &current_value,
134 auto &) {
135 result = (expected_value == current_value);
136 if (result) after_is_callback();
137 });
138 return result;
139 }
140
141 template <typename Callback = DoNothing>
142 void wait(const ValueType &expected_value,
143 const Callback &callback = Callback()) {
145 [&expected_value, &callback](const auto &current_value) {
146 if (expected_value == current_value) {
147 callback();
148 return true;
149 }
150 return false;
151 });
152 }
153
154 template <typename Callback = DoNothing>
155 ValueType wait(std::initializer_list<ValueType> expected_values,
156 const Callback &callback = Callback()) {
157 ValueType result;
159 [&expected_values, &callback, &result](const auto &current_value) {
160 if (mysql_harness::utility::container::has(expected_values,
161 current_value)) {
162 result = current_value;
163 callback();
164 return true;
165 }
166 return false;
167 });
168
169 return result;
170 }
171
172 template <class Rep, class Period, typename Callback = DoNothing>
173 bool wait_for(const std::chrono::duration<Rep, Period> &rel_time,
174 const ValueType &expected_value,
175 const Callback &callback = Callback()) {
176 return monitor_with_value_.wait_for(
177 rel_time, [&expected_value, &callback](auto &current_value) {
178 if (current_value == expected_value) {
179 callback();
180 return true;
181 }
182 return false;
183 });
184 }
185
186 template <class Rep, class Period, typename Callback = DoNothing>
188 const std::chrono::duration<Rep, Period> &rel_time,
189 std::initializer_list<ValueType> expected_values,
190 const Callback &callback = Callback()) {
191 ValueType result;
192 if (monitor_with_value_.wait_for(
193 rel_time,
194 [&expected_values, &result, &callback](const auto &current_value) {
195 if (mysql_harness::utility::container::has(expected_values,
196 current_value)) {
197 result = current_value;
198 callback();
199 return true;
200 }
201 return false;
202 })) {
203 return {result};
204 }
205
206 return stdx::unexpected(true);
207 }
208
209 private:
210 WaitableMonitor<ValueType> monitor_with_value_{};
211};
212
213} // namespace utility
214} // namespace mysql_harness
215
216#endif // MYSQL_HARNESS_UTILITY_WAIT_VARIABLE_H_
Class that stores callback function reference as well as the result of the callback function call (in...
Definition: keyring_service.cc:44
Monitor can be waited for.
Definition: monitor.h:62
void operator()() const
Definition: wait_variable.h:44
Definition: wait_variable.h:37
bool is(const ValueType &expected_value, const Callback &after_is_callback=Callback())
Definition: wait_variable.h:129
void set(const ValueType &v, const Callback &after_set_callback=DoNothing())
Definition: wait_variable.h:95
bool wait_for(const std::chrono::duration< Rep, Period > &rel_time, const ValueType &expected_value, const Callback &callback=Callback())
Definition: wait_variable.h:173
void change(const Callback &set_callback)
Definition: wait_variable.h:106
bool exchange(const ValueType &expected, const ValueType &v, const Callback &after_set_callback=DoNothing())
Definition: wait_variable.h:66
WaitableVariable()
Definition: wait_variable.h:39
ValueType wait(std::initializer_list< ValueType > expected_values, const Callback &callback=Callback())
Definition: wait_variable.h:155
bool exchange(const Container &expected, const ValueType &v, const Callback &after_set_callback=DoNothing())
Definition: wait_variable.h:49
void wait(const ValueType &expected_value, const Callback &callback=Callback())
Definition: wait_variable.h:142
WaitableVariable(const ValueType &value)
Definition: wait_variable.h:40
WaitableMonitor< ValueType > monitor_with_value_
Definition: wait_variable.h:210
ValueType get(const Callback &after_get_callback=DoNothing()) const
Definition: wait_variable.h:83
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:187
bool is(std::initializer_list< ValueType > expected_values, const Callback &after_is_callback=Callback())
Definition: wait_variable.h:115
Definition: expected.h:286
ValueType value(const std::optional< ValueType > &v)
Definition: gtid.h:83
bool has(const Container &c, Value &&val)
Definition: generic.h:89
Definition: common.h:44
unexpected(E) -> unexpected< E >
struct result result
Definition: result.h:34
Definition: result.h:30