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