Libosmium  2.1.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  OSMIUM_CONSTEXPR 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 #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 #ifdef OSMIUM_DEBUG_QUEUE_SIZE
99  ,
100  m_largest_size(0),
101  m_full_counter(0)
102 #endif
103  {
104  }
105 
106  ~Queue() {
107 #ifdef OSMIUM_DEBUG_QUEUE_SIZE
108  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";
109 #endif
110  }
111 
116  void push(T value) {
117  if (m_max_size) {
118  while (size() >= m_max_size) {
119  std::this_thread::sleep_for(full_queue_sleep_duration);
120 #ifdef OSMIUM_DEBUG_QUEUE_SIZE
121  ++m_full_counter;
122 #endif
123  }
124  }
125  std::lock_guard<std::mutex> lock(m_mutex);
126  m_queue.push(std::move(value));
127 #ifdef OSMIUM_DEBUG_QUEUE_SIZE
128  if (m_largest_size < m_queue.size()) {
129  m_largest_size = m_queue.size();
130  }
131 #endif
132  m_data_available.notify_one();
133  }
134 
135  void wait_and_pop(T& value) {
136  std::unique_lock<std::mutex> lock(m_mutex);
137  m_data_available.wait(lock, [this] {
138  return !m_queue.empty();
139  });
140  value=std::move(m_queue.front());
141  m_queue.pop();
142  }
143 
144  void wait_and_pop_with_timeout(T& value) {
145  std::unique_lock<std::mutex> lock(m_mutex);
146  if (!m_data_available.wait_for(lock, std::chrono::seconds(1), [this] {
147  return !m_queue.empty();
148  })) {
149  return;
150  }
151  value=std::move(m_queue.front());
152  m_queue.pop();
153  }
154 
155  bool try_pop(T& value) {
156  std::lock_guard<std::mutex> lock(m_mutex);
157  if (m_queue.empty()) {
158  return false;
159  }
160  value=std::move(m_queue.front());
161  m_queue.pop();
162  return true;
163  }
164 
165  bool empty() const {
166  std::lock_guard<std::mutex> lock(m_mutex);
167  return m_queue.empty();
168  }
169 
170  size_t size() const {
171  std::lock_guard<std::mutex> lock(m_mutex);
172  return m_queue.size();
173  }
174 
175  }; // class Queue
176 
177  } // namespace thread
178 
179 } // namespace osmium
180 
181 #endif // OSMIUM_THREAD_QUEUE_HPP
size_t size() const
Definition: queue.hpp:170
bool empty() const
Definition: queue.hpp:165
Definition: queue.hpp:58
std::mutex m_mutex
Definition: queue.hpp:67
~Queue()
Definition: queue.hpp:106
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:144
bool try_pop(T &value)
Definition: queue.hpp:155
void wait_and_pop(T &value)
Definition: queue.hpp:135
void push(T value)
Definition: queue.hpp:116
#define OSMIUM_CONSTEXPR
Definition: compatibility.hpp:43
Queue(size_t max_size=0, const std::string &name="")
Definition: queue.hpp:92
OSMIUM_CONSTEXPR std::chrono::milliseconds full_queue_sleep_duration
Definition: queue.hpp:52
std::condition_variable m_data_available
Used to signal readers when data is available in the queue.
Definition: queue.hpp:72