MySQL 9.3.0
Source Code Documentation
waiting_queue_adaptor.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2018, 2025, 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 designed to work 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 either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef MYSQL_HARNESS_WAITING_QUEUE_ADAPTOR_INCLUDED
27#define MYSQL_HARNESS_WAITING_QUEUE_ADAPTOR_INCLUDED
28
29#include <chrono>
30#include <condition_variable>
31#include <mutex>
32
33namespace mysql_harness {
34
35/**
36 * provide waiting pop and push operator to thread-safe queues.
37 *
38 */
39template <class Q>
41 public:
42 using value_type = typename Q::value_type;
43
44 /**
45 * dequeue an item from a queue.
46 *
47 * Waits until item becomes available.
48 *
49 * @returns item
50 */
52 value_type item;
53 {
54 std::unique_lock<std::mutex> lk(dequeueable_cond_mutex_);
55
56 dequeueable_cond_.wait(lk, [this, &item] { return q_.dequeue(item); });
57 }
58
60
61 return item;
62 }
63
64 /**
65 * dequeue an item from a queue if queue is not empty.
66 *
67 * @param item dequeued item if queue was not empty
68 *
69 * @returns item
70 * @retval true item dequeued
71 * @retval false queue was empty
72 */
73 bool try_pop(value_type &item) {
74 if (false == q_.dequeue(item)) {
75 return false;
76 }
78
79 return true;
80 }
81
82 /**
83 * Attempt to dequeue an item from a queue for a given period of time.
84 *
85 * @param item dequeued item if queue was not empty within the specified time.
86 * @param timeout max time to wait for the item to be available.
87 *
88 * @returns item
89 * @retval true item dequeued
90 * @retval false queue was empty
91 */
92 template <class Rep, class Period>
93 bool try_pop(value_type &item, std::chrono::duration<Rep, Period> timeout) {
94 {
95 std::unique_lock<std::mutex> lk(dequeueable_cond_mutex_);
96 if (!dequeueable_cond_.wait_for(
97 lk, timeout, [this, &item] { return q_.dequeue(item); })) {
98 return false;
99 }
100 }
101
103
104 return true;
105 }
106
107 /**
108 * enqueue item into queue.
109 *
110 * waits until queue is not full anymore.
111 *
112 * @param item item to enqueue
113 */
114 void push(const value_type &item) {
115 {
116 std::unique_lock<std::mutex> lk(enqueueable_cond_mutex_);
117
118 enqueueable_cond_.wait(lk, [this, &item] { return q_.enqueue(item); });
119 }
121 }
122
123 void push(value_type &&item) {
124 {
125 std::unique_lock<std::mutex> lk(enqueueable_cond_mutex_);
126
128 lk, [this, &item] { return q_.enqueue(std::move(item)); });
129 }
131 }
132
133 /**
134 * enqueue an item into a queue if queue is not full.
135 *
136 * @param item item to enqueue
137 *
138 * @returns item
139 * @retval true item enqueued
140 * @retval false queue was full
141 */
142 bool try_push(const value_type &item) {
143 if (false == q_.enqueue(item)) {
144 return false;
145 }
146
148 }
149
150 private:
152 std::unique_lock<std::mutex> lk(dequeueable_cond_mutex_);
153
154 dequeueable_cond_.notify_all();
155 }
157 std::unique_lock<std::mutex> lk(enqueueable_cond_mutex_);
158
159 enqueueable_cond_.notify_all();
160 }
161 Q q_;
162
164 std::condition_variable dequeueable_cond_;
165
167 std::condition_variable enqueueable_cond_;
168};
169
170} // namespace mysql_harness
171
172#endif
provide waiting pop and push operator to thread-safe queues.
Definition: waiting_queue_adaptor.h:40
void notify_enqueueable()
Definition: waiting_queue_adaptor.h:156
void push(const value_type &item)
enqueue item into queue.
Definition: waiting_queue_adaptor.h:114
void push(value_type &&item)
Definition: waiting_queue_adaptor.h:123
bool try_push(const value_type &item)
enqueue an item into a queue if queue is not full.
Definition: waiting_queue_adaptor.h:142
std::condition_variable dequeueable_cond_
Definition: waiting_queue_adaptor.h:164
std::condition_variable enqueueable_cond_
Definition: waiting_queue_adaptor.h:167
std::mutex enqueueable_cond_mutex_
Definition: waiting_queue_adaptor.h:166
typename Q::value_type value_type
Definition: waiting_queue_adaptor.h:42
Q q_
Definition: waiting_queue_adaptor.h:161
value_type pop()
dequeue an item from a queue.
Definition: waiting_queue_adaptor.h:51
bool try_pop(value_type &item, std::chrono::duration< Rep, Period > timeout)
Attempt to dequeue an item from a queue for a given period of time.
Definition: waiting_queue_adaptor.h:93
std::mutex dequeueable_cond_mutex_
Definition: waiting_queue_adaptor.h:163
bool try_pop(value_type &item)
dequeue an item from a queue if queue is not empty.
Definition: waiting_queue_adaptor.h:73
void notify_dequeueable()
Definition: waiting_queue_adaptor.h:151
uint16_t value_type
Definition: vt100.h:184
static bool timeout(bool(*wait_condition)())
Timeout function.
Definition: log0meb.cc:498
Definition: common.h:44