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