Libosmium  2.8.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-2016 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 <atomic>
37 #include <chrono>
38 #include <condition_variable>
39 #include <cstddef>
40 #include <mutex>
41 #include <queue>
42 #include <string>
43 #include <thread>
44 #include <utility> // IWYU pragma: keep (for std::move)
45 
46 #ifdef OSMIUM_DEBUG_QUEUE_SIZE
47 # include <iostream>
48 #endif
49 
50 namespace osmium {
51 
52  namespace thread {
53 
54  static const std::chrono::milliseconds full_queue_sleep_duration { 10 }; // XXX
55 
59  template <typename T>
60  class Queue {
61 
64  const size_t m_max_size;
65 
67  const std::string m_name;
68 
69  mutable std::mutex m_mutex;
70 
71  std::queue<T> m_queue;
72 
74  std::condition_variable m_data_available;
75 
76 #ifdef OSMIUM_DEBUG_QUEUE_SIZE
77  size_t m_largest_size;
79 
81  std::atomic<int> m_push_counter;
82 
85  std::atomic<int> m_full_counter;
86 
91  std::atomic<int> m_pop_counter;
92 
95  std::atomic<int> m_empty_counter;
96 #endif
97 
98  public:
99 
107  explicit Queue(size_t max_size = 0, const std::string& name = "") :
108  m_max_size(max_size),
109  m_name(name),
110  m_mutex(),
111  m_queue(),
112  m_data_available()
113 #ifdef OSMIUM_DEBUG_QUEUE_SIZE
114  ,
115  m_largest_size(0),
116  m_push_counter(0),
117  m_full_counter(0),
118  m_pop_counter(0),
119  m_empty_counter(0)
120 #endif
121  {
122  }
123 
124  ~Queue() {
125 #ifdef OSMIUM_DEBUG_QUEUE_SIZE
126  std::cerr << "queue '" << m_name << "' with max_size=" << m_max_size << " had largest size " << m_largest_size << " and was full " << m_full_counter << " times in " << m_push_counter << " push() calls and was empty " << m_empty_counter << " times in " << m_pop_counter << " pop() calls\n";
127 #endif
128  }
129 
134  void push(T value) {
135 #ifdef OSMIUM_DEBUG_QUEUE_SIZE
136  ++m_push_counter;
137 #endif
138  if (m_max_size) {
139  while (size() >= m_max_size) {
140  std::this_thread::sleep_for(full_queue_sleep_duration);
141 #ifdef OSMIUM_DEBUG_QUEUE_SIZE
142  ++m_full_counter;
143 #endif
144  }
145  }
146  std::lock_guard<std::mutex> lock(m_mutex);
147  m_queue.push(std::move(value));
148 #ifdef OSMIUM_DEBUG_QUEUE_SIZE
149  if (m_largest_size < m_queue.size()) {
150  m_largest_size = m_queue.size();
151  }
152 #endif
153  m_data_available.notify_one();
154  }
155 
156  void wait_and_pop(T& value) {
157 #ifdef OSMIUM_DEBUG_QUEUE_SIZE
158  ++m_pop_counter;
159 #endif
160  std::unique_lock<std::mutex> lock(m_mutex);
161 #ifdef OSMIUM_DEBUG_QUEUE_SIZE
162  if (m_queue.empty()) {
163  ++m_empty_counter;
164  }
165 #endif
166  m_data_available.wait(lock, [this] {
167  return !m_queue.empty();
168  });
169  if (!m_queue.empty()) {
170  value = std::move(m_queue.front());
171  m_queue.pop();
172  }
173  }
174 
175  bool try_pop(T& value) {
176 #ifdef OSMIUM_DEBUG_QUEUE_SIZE
177  ++m_pop_counter;
178 #endif
179  std::lock_guard<std::mutex> lock(m_mutex);
180  if (m_queue.empty()) {
181 #ifdef OSMIUM_DEBUG_QUEUE_SIZE
182  ++m_empty_counter;
183 #endif
184  return false;
185  }
186  value = std::move(m_queue.front());
187  m_queue.pop();
188  return true;
189  }
190 
191  bool empty() const {
192  std::lock_guard<std::mutex> lock(m_mutex);
193  return m_queue.empty();
194  }
195 
196  size_t size() const {
197  std::lock_guard<std::mutex> lock(m_mutex);
198  return m_queue.size();
199  }
200 
201  }; // class Queue
202 
203  } // namespace thread
204 
205 } // namespace osmium
206 
207 #endif // OSMIUM_THREAD_QUEUE_HPP
size_t size() const
Definition: queue.hpp:196
bool empty() const
Definition: queue.hpp:191
Definition: queue.hpp:60
std::mutex m_mutex
Definition: queue.hpp:69
~Queue()
Definition: queue.hpp:124
std::queue< T > m_queue
Definition: queue.hpp:71
const std::string m_name
Name of this queue (for debugging only).
Definition: queue.hpp:67
const size_t m_max_size
Definition: queue.hpp:64
Namespace for everything in the Osmium library.
Definition: assembler.hpp:66
bool try_pop(T &value)
Definition: queue.hpp:175
static const std::chrono::milliseconds full_queue_sleep_duration
Definition: queue.hpp:54
void wait_and_pop(T &value)
Definition: queue.hpp:156
void push(T value)
Definition: queue.hpp:134
Queue(size_t max_size=0, const std::string &name="")
Definition: queue.hpp:107
std::condition_variable m_data_available
Used to signal readers when data is available in the queue.
Definition: queue.hpp:74