Libosmium  2.13.0
Fast and flexible C++ library for working with OpenStreetMap data
queue.hpp
Go to the documentation of this file.
1 #ifndef OSMIUM_THREAD_QUEUE_HPP
2 #define OSMIUM_THREAD_QUEUE_HPP
3 
4 /*
5 
6 This file is part of Osmium (http://osmcode.org/libosmium).
7 
8 Copyright 2013-2017 Jochen Topf <jochen@topf.org> and others (see README).
9 
10 Boost Software License - Version 1.0 - August 17th, 2003
11 
12 Permission is hereby granted, free of charge, to any person or organization
13 obtaining a copy of the software and accompanying documentation covered by
14 this license (the "Software") to use, reproduce, display, distribute,
15 execute, and transmit the Software, and to prepare derivative works of the
16 Software, and to permit third-parties to whom the Software is furnished to
17 do so, all subject to the following:
18 
19 The copyright notices in the Software and this entire statement, including
20 the above license grant, this restriction and the following disclaimer,
21 must be included in all copies of the Software, in whole or in part, and
22 all derivative works of the Software, unless such copies or derivative
23 works are solely in the form of machine-executable object code generated by
24 a source language processor.
25 
26 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32 DEALINGS IN THE SOFTWARE.
33 
34 */
35 
36 #include <chrono>
37 #include <condition_variable>
38 #include <cstddef>
39 #include <mutex>
40 #include <queue>
41 #include <string>
42 #include <utility> // IWYU pragma: keep
43 
44 #ifdef OSMIUM_DEBUG_QUEUE_SIZE
45 # include <atomic>
46 # include <iostream>
47 #endif
48 
49 namespace osmium {
50 
51  namespace thread {
52 
53  static const std::chrono::milliseconds max_wait{10};
54 
58  template <typename T>
59  class Queue {
60 
63  const std::size_t m_max_size;
64 
66  const std::string m_name;
67 
68  mutable std::mutex m_mutex;
69 
70  std::queue<T> m_queue;
71 
73  std::condition_variable m_data_available;
74 
76  std::condition_variable m_space_available;
77 
78 #ifdef OSMIUM_DEBUG_QUEUE_SIZE
79  std::size_t m_largest_size;
81 
83  std::atomic<int> m_push_counter;
84 
87  std::atomic<int> m_full_counter;
88 
93  std::atomic<int> m_pop_counter;
94 
97  std::atomic<int> m_empty_counter;
98 #endif
99 
100  public:
101 
109  explicit Queue(std::size_t max_size = 0, const std::string& name = "") :
110  m_max_size(max_size),
111  m_name(name),
112  m_mutex(),
113  m_queue(),
114  m_data_available(),
115  m_space_available()
116 #ifdef OSMIUM_DEBUG_QUEUE_SIZE
117  ,
118  m_largest_size(0),
119  m_push_counter(0),
120  m_full_counter(0),
121  m_pop_counter(0),
122  m_empty_counter(0)
123 #endif
124  {
125  }
126 
127  ~Queue() {
128 #ifdef OSMIUM_DEBUG_QUEUE_SIZE
129  std::cerr << "queue '" << m_name
130  << "' with max_size=" << m_max_size
131  << " had largest size " << m_largest_size
132  << " and was full " << m_full_counter
133  << " times in " << m_push_counter
134  << " push() calls and was empty " << m_empty_counter
135  << " times in " << m_pop_counter
136  << " pop() calls\n";
137 #endif
138  }
139 
144  void push(T value) {
145 #ifdef OSMIUM_DEBUG_QUEUE_SIZE
146  ++m_push_counter;
147 #endif
148  if (m_max_size) {
149  while (size() >= m_max_size) {
150  std::unique_lock<std::mutex> lock{m_mutex};
151  m_space_available.wait_for(lock, max_wait, [this] {
152  return m_queue.size() < m_max_size;
153  });
154 #ifdef OSMIUM_DEBUG_QUEUE_SIZE
155  ++m_full_counter;
156 #endif
157  }
158  }
159  std::lock_guard<std::mutex> lock{m_mutex};
160  m_queue.push(std::move(value));
161 #ifdef OSMIUM_DEBUG_QUEUE_SIZE
162  if (m_largest_size < m_queue.size()) {
163  m_largest_size = m_queue.size();
164  }
165 #endif
166  m_data_available.notify_one();
167  }
168 
169  void wait_and_pop(T& value) {
170 #ifdef OSMIUM_DEBUG_QUEUE_SIZE
171  ++m_pop_counter;
172 #endif
173  std::unique_lock<std::mutex> lock{m_mutex};
174 #ifdef OSMIUM_DEBUG_QUEUE_SIZE
175  if (m_queue.empty()) {
176  ++m_empty_counter;
177  }
178 #endif
179  m_data_available.wait(lock, [this] {
180  return !m_queue.empty();
181  });
182  if (!m_queue.empty()) {
183  value = std::move(m_queue.front());
184  m_queue.pop();
185  lock.unlock();
186  if (m_max_size) {
187  m_space_available.notify_one();
188  }
189  }
190  }
191 
192  bool try_pop(T& value) {
193 #ifdef OSMIUM_DEBUG_QUEUE_SIZE
194  ++m_pop_counter;
195 #endif
196  {
197  std::lock_guard<std::mutex> lock{m_mutex};
198  if (m_queue.empty()) {
199 #ifdef OSMIUM_DEBUG_QUEUE_SIZE
200  ++m_empty_counter;
201 #endif
202  return false;
203  }
204  value = std::move(m_queue.front());
205  m_queue.pop();
206  }
207  if (m_max_size) {
208  m_space_available.notify_one();
209  }
210  return true;
211  }
212 
213  bool empty() const {
214  std::lock_guard<std::mutex> lock{m_mutex};
215  return m_queue.empty();
216  }
217 
218  std::size_t size() const {
219  std::lock_guard<std::mutex> lock{m_mutex};
220  return m_queue.size();
221  }
222 
223  }; // class Queue
224 
225  } // namespace thread
226 
227 } // namespace osmium
228 
229 #endif // OSMIUM_THREAD_QUEUE_HPP
Definition: queue.hpp:59
std::mutex m_mutex
Definition: queue.hpp:68
~Queue()
Definition: queue.hpp:127
static const std::chrono::milliseconds max_wait
Definition: queue.hpp:53
std::queue< T > m_queue
Definition: queue.hpp:70
const std::string m_name
Name of this queue (for debugging only).
Definition: queue.hpp:66
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
std::condition_variable m_space_available
Used to signal producers when queue is not full.
Definition: queue.hpp:76
bool try_pop(T &value)
Definition: queue.hpp:192
Queue(std::size_t max_size=0, const std::string &name="")
Definition: queue.hpp:109
void wait_and_pop(T &value)
Definition: queue.hpp:169
void push(T value)
Definition: queue.hpp:144
bool empty() const
Definition: queue.hpp:213
std::size_t size() const
Definition: queue.hpp:218
const std::size_t m_max_size
Definition: queue.hpp:63
std::condition_variable m_data_available
Used to signal consumers when data is available in the queue.
Definition: queue.hpp:73