MySQL 8.4.2
Source Code Documentation
waiting_queue_adaptor.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2018, 2024, 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 <condition_variable>
30#include <mutex>
31
32namespace mysql_harness {
33
34/**
35 * provide waiting pop and push operator to thread-safe queues.
36 *
37 */
38template <class Q>
40 public:
41 using value_type = typename Q::value_type;
42
43 /**
44 * dequeue an item from a queue.
45 *
46 * Waits until item becomes available.
47 *
48 * @returns item
49 */
51 value_type item;
52 {
53 std::unique_lock<std::mutex> lk(dequeueable_cond_mutex_);
54
55 dequeueable_cond_.wait(lk, [this, &item] { return q_.dequeue(item); });
56 }
57
59
60 return item;
61 }
62
63 /**
64 * dequeue an item from a queue if queue is not empty.
65 *
66 * @param item dequeued item if queue was not empty
67 *
68 * @returns item
69 * @retval true item dequeued
70 * @retval false queue was empty
71 */
72 bool try_pop(value_type &item) {
73 if (false == q_.dequeue(item)) {
74 return false;
75 }
77
78 return true;
79 }
80
81 /**
82 * enqueue item into queue.
83 *
84 * waits until queue is not full anymore.
85 *
86 * @param item item to enqueue
87 */
88 void push(const value_type &item) {
89 {
90 std::unique_lock<std::mutex> lk(enqueueable_cond_mutex_);
91
92 enqueueable_cond_.wait(lk, [this, &item] { return q_.enqueue(item); });
93 }
95 }
96
97 void push(value_type &&item) {
98 {
99 std::unique_lock<std::mutex> lk(enqueueable_cond_mutex_);
100
102 lk, [this, &item] { return q_.enqueue(std::move(item)); });
103 }
105 }
106
107 /**
108 * enqueue an item into a queue if queue is not full.
109 *
110 * @param item item to enqueue
111 *
112 * @returns item
113 * @retval true item enqueued
114 * @retval false queue was full
115 */
116 bool try_push(const value_type &item) {
117 if (false == q_.enqueue(item)) {
118 return false;
119 }
120
122 }
123
124 private:
126 std::unique_lock<std::mutex> lk(dequeueable_cond_mutex_);
127
128 dequeueable_cond_.notify_all();
129 }
131 std::unique_lock<std::mutex> lk(enqueueable_cond_mutex_);
132
133 enqueueable_cond_.notify_all();
134 }
135 Q q_;
136
138 std::condition_variable dequeueable_cond_;
139
141 std::condition_variable enqueueable_cond_;
142};
143
144} // namespace mysql_harness
145
146#endif
provide waiting pop and push operator to thread-safe queues.
Definition: waiting_queue_adaptor.h:39
void notify_enqueueable()
Definition: waiting_queue_adaptor.h:130
void push(const value_type &item)
enqueue item into queue.
Definition: waiting_queue_adaptor.h:88
void push(value_type &&item)
Definition: waiting_queue_adaptor.h:97
bool try_push(const value_type &item)
enqueue an item into a queue if queue is not full.
Definition: waiting_queue_adaptor.h:116
std::condition_variable dequeueable_cond_
Definition: waiting_queue_adaptor.h:138
std::condition_variable enqueueable_cond_
Definition: waiting_queue_adaptor.h:141
std::mutex enqueueable_cond_mutex_
Definition: waiting_queue_adaptor.h:140
typename Q::value_type value_type
Definition: waiting_queue_adaptor.h:41
Q q_
Definition: waiting_queue_adaptor.h:135
value_type pop()
dequeue an item from a queue.
Definition: waiting_queue_adaptor.h:50
std::mutex dequeueable_cond_mutex_
Definition: waiting_queue_adaptor.h:137
bool try_pop(value_type &item)
dequeue an item from a queue if queue is not empty.
Definition: waiting_queue_adaptor.h:72
void notify_dequeueable()
Definition: waiting_queue_adaptor.h:125
uint16_t value_type
Definition: vt100.h:184
Definition: common.h:42