MySQL 26.7.0
Source Code Documentation
condition_variable_wrapper_impl.hpp
Go to the documentation of this file.
1// Copyright (c) 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, version 2.0, 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
25
26namespace mysql::concurrency {
27
28template <class Predicate>
29void Condition_variable_wrapper::wait(std::unique_lock<Mutex_wrapper> &lock,
30 Predicate pred) {
31 while (!pred()) {
32 mysql_cond_wait(&m_cv, lock.mutex()->native_handle());
33 }
34}
35
36template <class Rep, class Period, class Predicate>
38 std::unique_lock<Mutex_wrapper> &lock,
39 const std::chrono::duration<Rep, Period> &rel_time, Predicate pred) {
40 struct timespec tm;
41 auto end_time = std::chrono::system_clock::now() + rel_time;
42 tm.tv_sec = std::chrono::system_clock::to_time_t(end_time);
43 auto ns = std::chrono::duration_cast<std::chrono::nanoseconds>(
44 end_time.time_since_epoch())
45 .count() %
46 1000000000;
47 tm.tv_nsec = ns;
48 bool last_pred_value{false};
49 bool timeout = false;
50 while (!(last_pred_value = pred()) && !timeout) {
51 timeout = mysql_cond_timedwait(&m_cv, lock.mutex()->native_handle(), &tm);
52 }
53 return last_pred_value;
54}
55
56template <class Rep, class Period>
58 std::unique_lock<Mutex_wrapper> &lock,
59 const std::chrono::duration<Rep, Period> &rel_time) {
60 struct timespec tm;
61 auto end_time = std::chrono::system_clock::now() + rel_time;
62 tm.tv_sec = std::chrono::system_clock::to_time_t(end_time);
63 auto ns = std::chrono::duration_cast<std::chrono::nanoseconds>(
64 end_time.time_since_epoch())
65 .count() %
66 1000000000;
67 tm.tv_nsec = ns;
68 return mysql_cond_timedwait(&m_cv, lock.mutex()->native_handle(), &tm)
70 : std::cv_status::no_timeout;
71}
72
73template <class Clock, class Duration, class Predicate>
75 std::unique_lock<Mutex_wrapper> &lock,
76 const std::chrono::time_point<Clock, Duration> &abs_time, Predicate pred) {
77 struct timespec tm;
78 tm.tv_sec = Clock::to_time_t(abs_time);
79 auto ns = std::chrono::duration_cast<std::chrono::nanoseconds>(
80 abs_time.time_since_epoch())
81 .count() %
82 1000000000;
83 tm.tv_nsec = ns;
84 bool timeout = false;
85 bool last_pred_value = false;
86 while (!(last_pred_value = pred()) && !timeout) {
87 timeout = mysql_cond_timedwait(&m_cv, lock.mutex()->native_handle(), &tm);
88 }
89 return last_pred_value;
90}
91
92template <class Clock, class Duration>
94 std::unique_lock<Mutex_wrapper> &lock,
95 const std::chrono::time_point<Clock, Duration> &abs_time) {
96 struct timespec tm;
97 tm.tv_sec = Clock::to_time_t(abs_time);
98 auto ns = std::chrono::duration_cast<std::chrono::nanoseconds>(
99 abs_time.time_since_epoch())
100 .count() %
101 1000000000;
102 tm.tv_nsec = ns;
103 return mysql_cond_timedwait(&m_cv, lock.mutex()->native_handle(), &tm)
105 : std::cv_status::no_timeout;
106}
107
108} // namespace mysql::concurrency
bool wait_until(std::unique_lock< Mutex_wrapper > &lock, const std::chrono::time_point< Clock, Duration > &abs_time, Predicate pred)
Wait until an absolute timeout or until notified.
Definition: condition_variable_wrapper_impl.hpp:74
mysql_cond_t m_cv
Definition: condition_variable_wrapper.h:115
void wait(std::unique_lock< Mutex_wrapper > &lock, Predicate pred)
Wait until notified or predicate is false.
Definition: condition_variable_wrapper_impl.hpp:29
bool wait_for(std::unique_lock< Mutex_wrapper > &lock, const std::chrono::duration< Rep, Period > &rel_time, Predicate pred)
Wait for a relative timeout or until notified.
Definition: condition_variable_wrapper_impl.hpp:37
#define mysql_cond_wait(C, M)
Definition: mysql_cond.h:48
#define mysql_cond_timedwait(C, M, T)
Definition: mysql_cond.h:51
Provides atomic access in shared-exclusive modes.
Definition: shared_spin_lock.h:79
static bool timeout(bool(*wait_condition)())
Timeout function.
Definition: log0meb.cc:499
Definition: cache_line_size.h:31
A filter of some sort that is not a join condition (those are stored in JoinPredicate objects).
Definition: access_path.h:138