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