Libosmium  2.4.1
Fast and flexible C++ library for working with OpenStreetMap data
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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> // 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 
80  std::atomic<int> m_full_counter;
81 #endif
82 
83  public:
84 
92  Queue(size_t max_size = 0, const std::string& name = "") :
93  m_max_size(max_size),
94  m_name(name),
95  m_mutex(),
96  m_queue(),
97  m_data_available(),
98  m_done(false)
99 #ifdef OSMIUM_DEBUG_QUEUE_SIZE
100  ,
101  m_largest_size(0),
102  m_full_counter(0)
103 #endif
104  {
105  }
106 
107  ~Queue() {
108  shutdown();
109 #ifdef OSMIUM_DEBUG_QUEUE_SIZE
110  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";
111 #endif
112  }
113 
118  void push(T value) {
119  if (m_max_size) {
120  while (size() >= m_max_size) {
121  std::this_thread::sleep_for(full_queue_sleep_duration);
122 #ifdef OSMIUM_DEBUG_QUEUE_SIZE
123  ++m_full_counter;
124 #endif
125  }
126  }
127  std::lock_guard<std::mutex> lock(m_mutex);
128  m_queue.push(std::move(value));
129 #ifdef OSMIUM_DEBUG_QUEUE_SIZE
130  if (m_largest_size < m_queue.size()) {
131  m_largest_size = m_queue.size();
132  }
133 #endif
134  m_data_available.notify_one();
135  }
136 
137  void shutdown() {
138  m_done = true;
139  m_data_available.notify_all();
140  }
141 
142  void wait_and_pop(T& value) {
143  std::unique_lock<std::mutex> lock(m_mutex);
144  m_data_available.wait(lock, [this] {
145  return !m_queue.empty() || m_done;
146  });
147  if (!m_queue.empty()) {
148  value = std::move(m_queue.front());
149  m_queue.pop();
150  }
151  }
152 
153  void wait_and_pop_with_timeout(T& value) {
154  std::unique_lock<std::mutex> lock(m_mutex);
155  if (!m_data_available.wait_for(lock, std::chrono::seconds(1), [this] {
156  return !m_queue.empty() || m_done;
157  })) {
158  return;
159  }
160  if (!m_queue.empty()) {
161  value = std::move(m_queue.front());
162  m_queue.pop();
163  }
164  }
165 
166  bool try_pop(T& value) {
167  std::lock_guard<std::mutex> lock(m_mutex);
168  if (m_queue.empty()) {
169  return false;
170  }
171  value = std::move(m_queue.front());
172  m_queue.pop();
173  return true;
174  }
175 
176  bool empty() const {
177  std::lock_guard<std::mutex> lock(m_mutex);
178  return m_queue.empty();
179  }
180 
181  size_t size() const {
182  std::lock_guard<std::mutex> lock(m_mutex);
183  return m_queue.size();
184  }
185 
186  }; // class Queue
187 
188  } // namespace thread
189 
190 } // namespace osmium
191 
192 #endif // OSMIUM_THREAD_QUEUE_HPP
size_t size() const
Definition: queue.hpp:181
bool empty() const
Definition: queue.hpp:176
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:137
~Queue()
Definition: queue.hpp:107
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:55
void wait_and_pop_with_timeout(T &value)
Definition: queue.hpp:153
bool try_pop(T &value)
Definition: queue.hpp:166
static const std::chrono::milliseconds full_queue_sleep_duration
Definition: queue.hpp:50
void wait_and_pop(T &value)
Definition: queue.hpp:142
void push(T value)
Definition: queue.hpp:118
Queue(size_t max_size=0, const std::string &name="")
Definition: queue.hpp:92
std::condition_variable m_data_available
Used to signal readers when data is available in the queue.
Definition: queue.hpp:70