Libosmium  2.11.3
Fast and flexible C++ library for working with OpenStreetMap data
pool.hpp
Go to the documentation of this file.
1 #ifndef OSMIUM_THREAD_POOL_HPP
2 #define OSMIUM_THREAD_POOL_HPP
3 
4 /*
5 
6 This file is part of Osmium (http://osmcode.org/libosmium).
7 
8 Copyright 2013-2017 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 <algorithm>
37 #include <cstddef>
38 #include <future>
39 #include <thread>
40 #include <type_traits>
41 #include <vector>
42 
44 #include <osmium/thread/queue.hpp>
45 #include <osmium/thread/util.hpp>
46 #include <osmium/util/config.hpp>
47 
48 namespace osmium {
49 
53  namespace thread {
54 
55  namespace detail {
56 
57  // Maximum number of allowed pool threads (just to keep the user
58  // from setting something silly).
59  constexpr const int max_pool_threads = 256;
60 
61  inline int get_pool_size(int num_threads, int user_setting, unsigned hardware_concurrency) {
62  if (num_threads == 0) {
63  num_threads = user_setting ? user_setting : -2;
64  }
65 
66  if (num_threads < 0) {
67  num_threads += int(hardware_concurrency);
68  }
69 
70  if (num_threads < 1) {
71  num_threads = 1;
72  } else if (num_threads > max_pool_threads) {
73  num_threads = max_pool_threads;
74  }
75 
76  return num_threads;
77  }
78 
79  inline size_t get_work_queue_size() noexcept {
80  const size_t n = osmium::config::get_max_queue_size("WORK", 10);
81  return n > 2 ? n : 2;
82  }
83 
84  } // namespace detail
85 
89  class Pool {
90 
95  class thread_joiner {
96 
97  std::vector<std::thread>& m_threads;
98 
99  public:
100 
101  explicit thread_joiner(std::vector<std::thread>& threads) :
102  m_threads(threads) {
103  }
104 
106  for (auto& thread : m_threads) {
107  if (thread.joinable()) {
108  thread.join();
109  }
110  }
111  }
112 
113  }; // class thread_joiner
114 
116  std::vector<std::thread> m_threads;
119 
120  void worker_thread() {
121  osmium::thread::set_thread_name("_osmium_worker");
122  while (true) {
123  function_wrapper task;
124  m_work_queue.wait_and_pop(task);
125  if (task && task()) {
126  // The called tasks returns true only when the
127  // worker thread should shut down.
128  return;
129  }
130  }
131  }
132 
145  explicit Pool(int num_threads, size_t max_queue_size) :
146  m_work_queue(max_queue_size, "work"),
147  m_threads(),
148  m_joiner(m_threads),
149  m_num_threads(detail::get_pool_size(num_threads, osmium::config::get_pool_threads(), std::thread::hardware_concurrency())) {
150 
151  try {
152  for (int i = 0; i < m_num_threads; ++i) {
153  m_threads.push_back(std::thread(&Pool::worker_thread, this));
154  }
155  } catch (...) {
156  shutdown_all_workers();
157  throw;
158  }
159  }
160 
161  public:
162 
163  static constexpr int default_num_threads = 0;
164 
165  static Pool& instance() {
166  static Pool pool(default_num_threads, detail::get_work_queue_size());
167  return pool;
168  }
169 
171  for (int i = 0; i < m_num_threads; ++i) {
172  // The special function wrapper makes a worker shut down.
173  m_work_queue.push(function_wrapper{0});
174  }
175  }
176 
177  ~Pool() {
178  shutdown_all_workers();
179  }
180 
181  size_t queue_size() const {
182  return m_work_queue.size();
183  }
184 
185  bool queue_empty() const {
186  return m_work_queue.empty();
187  }
188 
189  template <typename TFunction>
191 
192  using result_type = typename std::result_of<TFunction()>::type;
193 
194  std::packaged_task<result_type()> task(std::forward<TFunction>(func));
195  std::future<result_type> future_result(task.get_future());
196  m_work_queue.push(std::move(task));
197 
198  return future_result;
199  }
200 
201  }; // class Pool
202 
203  } // namespace thread
204 
205 } // namespace osmium
206 
207 #endif // OSMIUM_THREAD_POOL_HPP
Pool(int num_threads, size_t max_queue_size)
Definition: pool.hpp:145
type
Definition: entity_bits.hpp:63
size_t size() const
Definition: queue.hpp:219
Definition: queue.hpp:60
bool queue_empty() const
Definition: pool.hpp:185
void set_thread_name(const char *name) noexcept
Definition: util.hpp:76
~Pool()
Definition: pool.hpp:177
Definition: reader_iterator.hpp:39
std::vector< std::thread > & m_threads
Definition: pool.hpp:97
std::future< typename std::result_of< TFunction()>::type > submit(TFunction &&func)
Definition: pool.hpp:190
void worker_thread()
Definition: pool.hpp:120
int get_pool_threads() noexcept
Definition: config.hpp:48
static Pool & instance()
Definition: pool.hpp:165
Namespace for everything in the Osmium library.
Definition: assembler.hpp:73
Definition: attr.hpp:333
Definition: function_wrapper.hpp:48
std::vector< std::thread > m_threads
Definition: pool.hpp:116
Definition: pool.hpp:89
osmium::thread::Queue< function_wrapper > m_work_queue
Definition: pool.hpp:115
thread_joiner(std::vector< std::thread > &threads)
Definition: pool.hpp:101
int m_num_threads
Definition: pool.hpp:118
void wait_and_pop(T &value)
Definition: queue.hpp:170
~thread_joiner()
Definition: pool.hpp:105
size_t queue_size() const
Definition: pool.hpp:181
void push(T value)
Definition: queue.hpp:145
size_t get_max_queue_size(const char *queue_name, size_t default_value) noexcept
Definition: config.hpp:69
bool empty() const
Definition: queue.hpp:214
void shutdown_all_workers()
Definition: pool.hpp:170
thread_joiner m_joiner
Definition: pool.hpp:117