Libosmium  2.2.0
Fast and flexible C++ library for working with OpenStreetMap data
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
sorted_queue.hpp
Go to the documentation of this file.
1 #ifndef OSMIUM_THREAD_SORTED_QUEUE_HPP
2 #define OSMIUM_THREAD_SORTED_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 <condition_variable>
37 #include <cstddef>
38 #include <deque>
39 #include <mutex>
40 
41 namespace osmium {
42 
43  namespace thread {
44 
56  template <typename T>
57  class SortedQueue {
58 
59  typedef typename std::deque<T>::size_type size_type;
60 
61  mutable std::mutex m_mutex;
62  std::deque<T> m_queue;
63  std::condition_variable m_data_available;
64 
65  size_type m_offset;
66 
67  // this method expects that we already have the lock
68  bool empty_intern() const {
69  return m_queue.front() == T();
70  }
71 
72  public:
73 
75  m_mutex(),
76  m_queue(1),
77  m_data_available(),
78  m_offset(0) {
79  }
80 
88  void push(T value, size_type num) {
89  std::lock_guard<std::mutex> lock(m_mutex);
90 
91  num -= m_offset;
92  if (m_queue.size() <= num + 1) {
93  m_queue.resize(num + 2);
94  }
95  m_queue[num] = std::move(value);
96 
97  m_data_available.notify_one();
98  }
99 
104  void wait_and_pop(T& value) {
105  std::unique_lock<std::mutex> lock(m_mutex);
106 
107  m_data_available.wait(lock, [this] {
108  return !empty_intern();
109  });
110  value = std::move(m_queue.front());
111  m_queue.pop_front();
112  ++m_offset;
113  }
114 
119  bool try_pop(T& value) {
120  std::lock_guard<std::mutex> lock(m_mutex);
121 
122  if (empty_intern()) {
123  return false;
124  }
125  value = std::move(m_queue.front());
126  m_queue.pop_front();
127  ++m_offset;
128  return true;
129  }
130 
137  bool empty() const {
138  std::lock_guard<std::mutex> lock(m_mutex);
139 
140  return empty_intern();
141  }
142 
148  size_t size() const {
149  std::lock_guard<std::mutex> lock(m_mutex);
150  return m_queue.size();
151  }
152 
153  }; // class SortedQueue
154 
155  } // namespace thread
156 
157 } // namespace osmium
158 
159 #endif // OSMIUM_THREAD_SORTED_QUEUE_HPP
void wait_and_pop(T &value)
Definition: sorted_queue.hpp:104
void push(T value, size_type num)
Definition: sorted_queue.hpp:88
std::deque< T >::size_type size_type
Definition: sorted_queue.hpp:59
Namespace for everything in the Osmium library.
Definition: assembler.hpp:55
std::condition_variable m_data_available
Definition: sorted_queue.hpp:63
std::mutex m_mutex
Definition: sorted_queue.hpp:61
size_type m_offset
Definition: sorted_queue.hpp:65
Definition: sorted_queue.hpp:57
bool empty() const
Definition: sorted_queue.hpp:137
bool empty_intern() const
Definition: sorted_queue.hpp:68
SortedQueue()
Definition: sorted_queue.hpp:74
bool try_pop(T &value)
Definition: sorted_queue.hpp:119
size_t size() const
Definition: sorted_queue.hpp:148
std::deque< T > m_queue
Definition: sorted_queue.hpp:62