Libosmium  2.6.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 namespace osmium {
47 
48  namespace thread {
49 
50  static const std::chrono::milliseconds full_queue_sleep_duration { 10 }; // XXX
51 
55  template <typename T>
56  class Queue {
57 
60  const size_t m_max_size;
61 
63  const std::string m_name;
64 
65  mutable std::mutex m_mutex;
66 
67  std::queue<T> m_queue;
68 
70  std::condition_variable m_data_available;
71 
72  std::atomic<bool> m_done;
73 
74 #ifdef OSMIUM_DEBUG_QUEUE_SIZE
75  size_t m_largest_size;
77 
79  std::atomic<int> m_push_counter;
80 
83  std::atomic<int> m_full_counter;
84 #endif
85 
86  public:
87 
95  explicit Queue(size_t max_size = 0, const std::string& name = "") :
96  m_max_size(max_size),
97  m_name(name),
98  m_mutex(),
99  m_queue(),
100  m_data_available(),
101  m_done(false)
102 #ifdef OSMIUM_DEBUG_QUEUE_SIZE
103  ,
104  m_largest_size(0),
105  m_push_counter(0),
106  m_full_counter(0)
107 #endif
108  {
109  }
110 
111  ~Queue() {
112  shutdown();
113 #ifdef OSMIUM_DEBUG_QUEUE_SIZE
114  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\n";
115 #endif
116  }
117 
122  void push(T value) {
123 #ifdef OSMIUM_DEBUG_QUEUE_SIZE
124  ++m_push_counter;
125 #endif
126  if (m_max_size) {
127  while (size() >= m_max_size) {
128  std::this_thread::sleep_for(full_queue_sleep_duration);
129 #ifdef OSMIUM_DEBUG_QUEUE_SIZE
130  ++m_full_counter;
131 #endif
132  }
133  }
134  std::lock_guard<std::mutex> lock(m_mutex);
135  m_queue.push(std::move(value));
136 #ifdef OSMIUM_DEBUG_QUEUE_SIZE
137  if (m_largest_size < m_queue.size()) {
138  m_largest_size = m_queue.size();
139  }
140 #endif
141  m_data_available.notify_one();
142  }
143 
144  void shutdown() {
145  m_done = true;
146  m_data_available.notify_all();
147  }
148 
149  void wait_and_pop(T& value) {
150  std::unique_lock<std::mutex> lock(m_mutex);
151  m_data_available.wait(lock, [this] {
152  return !m_queue.empty() || m_done;
153  });
154  if (!m_queue.empty()) {
155  value = std::move(m_queue.front());
156  m_queue.pop();
157  }
158  }
159 
160  void wait_and_pop_with_timeout(T& value) {
161  std::unique_lock<std::mutex> lock(m_mutex);
162  if (!m_data_available.wait_for(lock, std::chrono::seconds(1), [this] {
163  return !m_queue.empty() || m_done;
164  })) {
165  return;
166  }
167  if (!m_queue.empty()) {
168  value = std::move(m_queue.front());
169  m_queue.pop();
170  }
171  }
172 
173  bool try_pop(T& value) {
174  std::lock_guard<std::mutex> lock(m_mutex);
175  if (m_queue.empty()) {
176  return false;
177  }
178  value = std::move(m_queue.front());
179  m_queue.pop();
180  return true;
181  }
182 
183  bool empty() const {
184  std::lock_guard<std::mutex> lock(m_mutex);
185  return m_queue.empty();
186  }
187 
188  size_t size() const {
189  std::lock_guard<std::mutex> lock(m_mutex);
190  return m_queue.size();
191  }
192 
193  }; // class Queue
194 
195  } // namespace thread
196 
197 } // namespace osmium
198 
199 #endif // OSMIUM_THREAD_QUEUE_HPP
size_t size() const
Definition: queue.hpp:188
bool empty() const
Definition: queue.hpp:183
Definition: queue.hpp:56
std::mutex m_mutex
Definition: queue.hpp:65
std::atomic< bool > m_done
Definition: queue.hpp:72
void shutdown()
Definition: queue.hpp:144
~Queue()
Definition: queue.hpp:111
std::queue< T > m_queue
Definition: queue.hpp:67
const std::string m_name
Name of this queue (for debugging only).
Definition: queue.hpp:63
const size_t m_max_size
Definition: queue.hpp:60
Namespace for everything in the Osmium library.
Definition: assembler.hpp:59
void wait_and_pop_with_timeout(T &value)
Definition: queue.hpp:160
bool try_pop(T &value)
Definition: queue.hpp:173
static const std::chrono::milliseconds full_queue_sleep_duration
Definition: queue.hpp:50
void wait_and_pop(T &value)
Definition: queue.hpp:149
void push(T value)
Definition: queue.hpp:122
Queue(size_t max_size=0, const std::string &name="")
Definition: queue.hpp:95
std::condition_variable m_data_available
Used to signal readers when data is available in the queue.
Definition: queue.hpp:70