MySQL 26.7.0
Source Code Documentation
condition_variable_wrapper.h
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
24#ifndef MYSQL_CONCURRENCY_CONDITION_VARIABLE_WRAPPER_H
25#define MYSQL_CONCURRENCY_CONDITION_VARIABLE_WRAPPER_H
26
27#include <condition_variable>
28#include <mutex>
31
32namespace mysql::concurrency {
33
34/// @brief MySQL wrapper for a condition variable, using mysql_cond_t as
35/// implementation of a condition variable having interface of
36/// condition_variable provided by the STL
38 public:
41
42 /// @brief Wait until notified or predicate is false
43 /// @tparam Predicate Type of the predicate function
44 /// @param[in,out] lock Unique lock on the mutex to wait on
45 /// @param[in] pred Predicate function to check
46 template <class Predicate>
47 void wait(std::unique_lock<Mutex_wrapper> &lock, Predicate pred);
48
49 /// @brief Wait for a relative timeout or until notified
50 /// @tparam Rep Representation type of the duration
51 /// @tparam Period Period type of the duration
52 /// @tparam Predicate Type of the predicate function
53 /// @param[in,out] lock Unique lock on the mutex to wait on
54 /// @param[in] rel_time Relative time to wait
55 /// @param[in] pred Predicate function to check
56 /// @return True if predicate is true, false if timeout occurred
57 template <class Rep, class Period, class Predicate>
58 bool wait_for(std::unique_lock<Mutex_wrapper> &lock,
59 const std::chrono::duration<Rep, Period> &rel_time,
60 Predicate pred);
61
62 /// @brief Wait for a relative timeout
63 /// @tparam Rep Representation type of the duration
64 /// @tparam Period Period type of the duration
65 /// @param[in,out] lock Unique lock on the mutex to wait on
66 /// @param[in] rel_time Relative time to wait
67 /// @return cv_status indicating whether timeout occurred or not
68 template <class Rep, class Period>
69 std::cv_status wait_for(std::unique_lock<Mutex_wrapper> &lock,
70 const std::chrono::duration<Rep, Period> &rel_time);
71
72 /// @brief Wait until an absolute timeout or until notified
73 /// @tparam Clock Clock type for the time point
74 /// @tparam Duration Duration type for the time point
75 /// @tparam Predicate Type of the predicate function
76 /// @param[in,out] lock Unique lock on the mutex to wait on
77 /// @param[in] abs_time Absolute time to wait until
78 /// @param[in] pred Predicate function to check
79 /// @return True if predicate is true, false if timeout occurred
80 template <class Clock, class Duration, class Predicate>
81 bool wait_until(std::unique_lock<Mutex_wrapper> &lock,
82 const std::chrono::time_point<Clock, Duration> &abs_time,
83 Predicate pred);
84
85 /// @brief Wait until an absolute timeout
86 /// @tparam Clock Clock type for the time point
87 /// @tparam Duration Duration type for the time point
88 /// @param[in,out] lock Unique lock on the mutex to wait on
89 /// @param[in] abs_time Absolute time to wait until
90 /// @return cv_status indicating whether timeout occurred or not
91 template <class Clock, class Duration>
92 std::cv_status wait_until(
93 std::unique_lock<Mutex_wrapper> &lock,
94 const std::chrono::time_point<Clock, Duration> &abs_time);
95
96 /// @brief Notify one waiting thread
97 void notify_one();
98
99 /// @brief Notify all waiting threads
100 void notify_all();
101
102 /// @brief Wait until notified
103 /// @param[in,out] lock Unique lock on the mutex to wait on
104 void wait(std::unique_lock<Mutex_wrapper> &lock);
105
106 // Disable copy-move semantics
110 delete;
112 delete;
113
114 protected:
116};
117
118} // namespace mysql::concurrency
119
121
122#endif // MYSQL_CONCURRENCY_CONDITION_VARIABLE_WRAPPER_H
MySQL wrapper for a condition variable, using mysql_cond_t as implementation of a condition variable ...
Definition: condition_variable_wrapper.h:37
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
Condition_variable_wrapper(PSI_cond_key key)
Definition: condition_variable_wrapper.cpp:30
void notify_one()
Notify one waiting thread.
Definition: condition_variable_wrapper.cpp:38
Condition_variable_wrapper & operator=(Condition_variable_wrapper &&src)=delete
Condition_variable_wrapper(Condition_variable_wrapper &&)=delete
void notify_all()
Notify all waiting threads.
Definition: condition_variable_wrapper.cpp:40
Condition_variable_wrapper & operator=(const Condition_variable_wrapper &src)=delete
void wait(std::unique_lock< Mutex_wrapper > &lock, Predicate pred)
Wait until notified or predicate is false.
Definition: condition_variable_wrapper_impl.hpp:29
~Condition_variable_wrapper()
Definition: condition_variable_wrapper.cpp:34
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
Condition_variable_wrapper(const Condition_variable_wrapper &)=delete
unsigned int PSI_cond_key
Instrumented cond key.
Definition: psi_cond_bits.h:44
Provides atomic access in shared-exclusive modes.
Definition: shared_spin_lock.h:79
Definition: cache_line_size.h:31
Instrumentation helpers for conditions.
required string key
Definition: replication_asynchronous_connection_failover.proto:60
A filter of some sort that is not a join condition (those are stored in JoinPredicate objects).
Definition: access_path.h:138
An instrumented cond structure.
Definition: mysql_cond_bits.h:50