OdbDesign
OdbDesign ODB++ Parser - C++ library, gRPC/REST server, CLI, tests
 
Loading...
Searching...
No Matches
ThreadSafeQueue.h
Go to the documentation of this file.
1//
2// Created by nam20485 on 5/25/22.
3//
4
5#pragma once
6
7#include <queue>
8#include <mutex>
9#include <condition_variable>
10#include <atomic>
11#include <iostream>
12#include <sstream>
13#include <functional>
14#include <chrono>
15
16
17namespace Utils
18{
23 template <typename T>
25 {
26 public:
27 typedef std::function<void(const std::string&)> internalLogHandlerProc;
28
29 enum class wait_result
30 {
34 };
35
37 : _interrupted(false)
38 {
39 }
40
45 void push(T&& item);
46
52 T& front(bool wait);
53
57 void pop();
58
62 void interrupt();
63
69
75 wait_result wait(std::chrono::milliseconds wait_ms);
76
81 bool empty();
82
88
89 private:
90 std::mutex _m;
91 std::condition_variable _cv;
92 std::queue<T> _queue;
93 std::atomic_bool _interrupted;
94
95 internalLogHandlerProc _internalLogHandler;
96
97 void logInternal(const std::string& message);
98 };
99
100
101 template<typename T>
103 {
104 {
105 logInternal("[ThreadSafeQueue<TResults>::push] enter, taking lock...");
106
107 // unlock once scope is exited
108 std::lock_guard lock(_m);
109
110 std::stringstream ss;
111 ss << "[ThreadSafeQueue<TResults>::push] lock taken, pushing item (_queue.size(): " << _queue.size() << ')';
112 logInternal(ss.str());
113
114 _queue.push(item);
115
116 ss.str(""); // clear the stream out
117 ss << "[ThreadSafeQueue<TResults>::push] item pushed, _queue.size(): " << _queue.size();
118 logInternal(ss.str());
119 }
120
121 logInternal("[ThreadSafeQueue<TResults>::push] lock given up, notifying...");
122
123 // unlock before notifying
124 _cv.notify_one();
125
126 logInternal("[ThreadSafeQueue<TResults>::push] _cv notified, exit");
127 }
128
129 template<typename T>
131 {
132 std::unique_lock lock(_m);
133 if (wait)
134 {
135 _cv.wait(lock,
136 [&]
137 {
138 return _queue.empty() == false;
139 });
140 }
141 return _queue.front();
142 }
143
144 template<typename T>
146 {
147 std::lock_guard lock(_m);
148 _queue.pop();
149 }
150
151 template<typename T>
153 {
154 // lock and notify
155 {
156 std::unique_lock lock(_m);
157
158 // set interrupt to return from wait even if _queue is empty
159 _interrupted.store(true);
160 }
161 _cv.notify_one();
162 }
163
164 template<typename T>
166 {
167 logInternal("[ThreadSafeQueue<TResults>::wait] enter, taking lock...");
168
169 // wait until _queue is not empty _OR_ interrupt has been set
170 std::unique_lock lock(_m);
171
172 logInternal("[ThreadSafeQueue<TResults>::wait] lock taken, begin waiting...");
173
174 _cv.wait(lock,
175 [&]
176 {
177 return _queue.empty() == false || _interrupted.load();
178 });
179
180 std::stringstream ss;
181 ss << "[ThreadSafeQueue<TResults>::wait] wait returned, exiting (_queue.size() => " << _queue.size() << ", _interrupted = " << _interrupted.load() << ')';
182 logInternal(ss.str());
183
184 // return whether _interrupted was tripped
185 if (_interrupted.load())
186 {
188 }
189 else
190 {
192 }
193 }
194
195 template<typename T>
196 typename ThreadSafeQueue<T>::wait_result ThreadSafeQueue<T>::wait(std::chrono::milliseconds wait_ms)
197 {
198 logInternal("[ThreadSafeQueue<TResults>::wait] enter, taking lock...");
199
200 // wait until _queue is not empty _OR_ interrupt has been set
201 std::unique_lock lock(_m);
202
203 logInternal("[ThreadSafeQueue<TResults>::wait] lock taken, begin waiting...");
204
205 auto expired = !_cv.wait_for(lock, wait_ms, // NOLINT(clion-misra-cpp2008-5-3-1)
206 [&]
207 {
208 return _queue.empty() == false || _interrupted.load();
209 });
210
211 std::stringstream ss;
212 ss << "[ThreadSafeQueue<TResults>::wait] wait returned, exiting (_queue.size() => " << _queue.size() << ", _interrupted = " << _interrupted.load() << ')';
213 logInternal(ss.str());
214
216 if (expired)
217 {
219 }
220 else if (_interrupted.load())
221 {
223 }
224 else
225 {
227 }
228 return wait_result;
229 }
230
231 template<typename T>
233 {
234 std::unique_lock lock(_m);
235 return _queue.empty();
236 }
237
238 template<typename T>
239 void ThreadSafeQueue<T>::logInternal(const std::string& message)
240 {
241 if (_internalLogHandler)
242 {
243 _internalLogHandler(message);
244 }
245 }
246
247 template<typename T>
249 {
250 _internalLogHandler = std::move(internalLogHandler);
251 }
252
253} // namespace Lib::Utils
void registerInternalLogHandler(internalLogHandlerProc internalLogHandler)
std::function< void(const std::string &)> internalLogHandlerProc
wait_result wait(std::chrono::milliseconds wait_ms)