Libosmium  2.14.2
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 (https://osmcode.org/libosmium).
7 
8 Copyright 2013-2018 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 
37 #include <osmium/thread/queue.hpp>
38 #include <osmium/thread/util.hpp>
39 #include <osmium/util/config.hpp>
40 
41 #include <cstddef>
42 #include <future>
43 #include <thread>
44 #include <type_traits>
45 #include <utility>
46 #include <vector>
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 = 32;
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 std::size_t get_work_queue_size() noexcept {
80  const std::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 
105  thread_joiner(const thread_joiner&) = delete;
106  thread_joiner& operator=(const thread_joiner&) = delete;
107 
108  thread_joiner(thread_joiner&&) = delete;
110 
112  for (auto& thread : m_threads) {
113  if (thread.joinable()) {
114  thread.join();
115  }
116  }
117  }
118 
119  }; // class thread_joiner
120 
122  std::vector<std::thread> m_threads{};
125 
126  void worker_thread() {
127  osmium::thread::set_thread_name("_osmium_worker");
128  while (true) {
129  function_wrapper task;
130  m_work_queue.wait_and_pop(task);
131  if (task && task()) {
132  // The called tasks returns true only when the
133  // worker thread should shut down.
134  return;
135  }
136  }
137  }
138 
139  public:
140 
141  static constexpr int default_num_threads = 0;
142  static constexpr int default_queue_size = 0;
143 
159  explicit Pool(int num_threads = default_num_threads, std::size_t max_queue_size = default_queue_size) :
160  m_work_queue(max_queue_size > 0 ? max_queue_size : detail::get_work_queue_size(), "work"),
162  m_num_threads(detail::get_pool_size(num_threads, osmium::config::get_pool_threads(), std::thread::hardware_concurrency())) {
163 
164  try {
165  for (int i = 0; i < m_num_threads; ++i) {
166  m_threads.emplace_back(&Pool::worker_thread, this);
167  }
168  } catch (...) {
170  throw;
171  }
172  }
173 
174  static Pool& default_instance() {
175  static Pool pool{};
176  return pool;
177  }
178 
180  for (int i = 0; i < m_num_threads; ++i) {
181  // The special function wrapper makes a worker shut down.
183  }
184  }
185 
186  Pool(const Pool&) = delete;
187  Pool& operator=(const Pool&) = delete;
188 
189  Pool(Pool&&) = delete;
190  Pool& operator=(Pool&&) = delete;
191 
192  ~Pool() {
194  }
195 
196  int num_threads() const noexcept {
197  return m_num_threads;
198  }
199 
200  std::size_t queue_size() const {
201  return m_work_queue.size();
202  }
203 
204  bool queue_empty() const {
205  return m_work_queue.empty();
206  }
207 
208  template <typename TFunction>
210  using result_type = typename std::result_of<TFunction()>::type;
211 
212  std::packaged_task<result_type()> task{std::forward<TFunction>(func)};
213  std::future<result_type> future_result{task.get_future()};
214  m_work_queue.push(std::move(task));
215 
216  return future_result;
217  }
218 
219  }; // class Pool
220 
221  } // namespace thread
222 
223 } // namespace osmium
224 
225 #endif // OSMIUM_THREAD_POOL_HPP
type
Definition: entity_bits.hpp:63
Pool & operator=(const Pool &)=delete
std::size_t get_max_queue_size(const char *queue_name, std::size_t default_value) noexcept
Definition: config.hpp:83
static Pool & default_instance()
Definition: pool.hpp:174
Definition: queue.hpp:57
bool queue_empty() const
Definition: pool.hpp:204
void set_thread_name(const char *name) noexcept
Definition: util.hpp:76
~Pool()
Definition: pool.hpp:192
Definition: location.hpp:550
std::size_t queue_size() const
Definition: pool.hpp:200
std::vector< std::thread > & m_threads
Definition: pool.hpp:97
std::future< typename std::result_of< TFunction()>::type > submit(TFunction &&func)
Definition: pool.hpp:209
static constexpr int default_num_threads
Definition: pool.hpp:141
void worker_thread()
Definition: pool.hpp:126
int get_pool_threads() noexcept
Definition: config.hpp:62
int num_threads() const noexcept
Definition: pool.hpp:196
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
Definition: attr.hpp:333
static constexpr int default_queue_size
Definition: pool.hpp:142
Definition: function_wrapper.hpp:48
std::vector< std::thread > m_threads
Definition: pool.hpp:122
Definition: pool.hpp:89
osmium::thread::Queue< function_wrapper > m_work_queue
Definition: pool.hpp:121
thread_joiner(std::vector< std::thread > &threads)
Definition: pool.hpp:101
int m_num_threads
Definition: pool.hpp:124
~thread_joiner()
Definition: pool.hpp:111
thread_joiner & operator=(const thread_joiner &)=delete
void shutdown_all_workers()
Definition: pool.hpp:179
thread_joiner m_joiner
Definition: pool.hpp:123
Pool(int num_threads=default_num_threads, std::size_t max_queue_size=default_queue_size)
Definition: pool.hpp:159