Libosmium  2.7.2
Fast and flexible C++ library for working with OpenStreetMap data
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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  } // namespace detail
82 
86  class Pool {
87 
92  class thread_joiner {
93 
94  std::vector<std::thread>& m_threads;
95 
96  public:
97 
98  explicit thread_joiner(std::vector<std::thread>& threads) :
99  m_threads(threads) {
100  }
101 
103  for (auto& thread : m_threads) {
104  if (thread.joinable()) {
105  thread.join();
106  }
107  }
108  }
109 
110  }; // class thread_joiner
111 
113  std::vector<std::thread> m_threads;
116 
117  void worker_thread() {
118  osmium::thread::set_thread_name("_osmium_worker");
119  while (true) {
120  function_wrapper task;
121  m_work_queue.wait_and_pop_with_timeout(task);
122  if (task) {
123  if (task()) {
124  // The called tasks returns true only when the
125  // worker thread should shut down.
126  return;
127  }
128  }
129  }
130  }
131 
144  explicit Pool(int num_threads, size_t max_queue_size) :
145  m_work_queue(max_queue_size, "work"),
146  m_threads(),
147  m_joiner(m_threads),
148  m_num_threads(detail::get_pool_size(num_threads, osmium::config::get_pool_threads(), std::thread::hardware_concurrency())) {
149 
150  try {
151  for (int i = 0; i < m_num_threads; ++i) {
152  m_threads.push_back(std::thread(&Pool::worker_thread, this));
153  }
154  } catch (...) {
156  throw;
157  }
158  }
159 
160  public:
161 
162  static constexpr int default_num_threads = 0;
163  static constexpr size_t max_work_queue_size = 10;
164 
165  static Pool& instance() {
166  static Pool pool(default_num_threads, max_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() {
179  m_work_queue.shutdown();
180  }
181 
182  size_t queue_size() const {
183  return m_work_queue.size();
184  }
185 
186  bool queue_empty() const {
187  return m_work_queue.empty();
188  }
189 
190  template <typename TFunction>
192 
193  using result_type = typename std::result_of<TFunction()>::type;
194 
195  std::packaged_task<result_type()> task(std::forward<TFunction>(func));
196  std::future<result_type> future_result(task.get_future());
197  m_work_queue.push(std::move(task));
198 
199  return future_result;
200  }
201 
202  }; // class Pool
203 
204  } // namespace thread
205 
206 } // namespace osmium
207 
208 #endif // OSMIUM_THREAD_POOL_HPP
Pool(int num_threads, size_t max_queue_size)
Definition: pool.hpp:144
type
Definition: entity_bits.hpp:63
size_t size() const
Definition: queue.hpp:188
bool empty() const
Definition: queue.hpp:183
Definition: queue.hpp:56
void set_thread_name(const char *name) noexcept
Definition: util.hpp:74
~Pool()
Definition: pool.hpp:177
Definition: reader_iterator.hpp:39
static constexpr size_t max_work_queue_size
Definition: pool.hpp:163
std::vector< std::thread > & m_threads
Definition: pool.hpp:94
void shutdown()
Definition: queue.hpp:144
std::future< typename std::result_of< TFunction()>::type > submit(TFunction &&func)
Definition: pool.hpp:191
static constexpr int default_num_threads
Definition: pool.hpp:162
void worker_thread()
Definition: pool.hpp:117
static Pool & instance()
Definition: pool.hpp:165
Namespace for everything in the Osmium library.
Definition: assembler.hpp:66
Definition: attr.hpp:298
void wait_and_pop_with_timeout(T &value)
Definition: queue.hpp:160
int get_pool_threads()
Definition: config.hpp:47
Definition: function_wrapper.hpp:48
std::vector< std::thread > m_threads
Definition: pool.hpp:113
Definition: pool.hpp:86
osmium::thread::Queue< function_wrapper > m_work_queue
Definition: pool.hpp:112
thread_joiner(std::vector< std::thread > &threads)
Definition: pool.hpp:98
int m_num_threads
Definition: pool.hpp:115
~thread_joiner()
Definition: pool.hpp:102
void push(T value)
Definition: queue.hpp:122
void shutdown_all_workers()
Definition: pool.hpp:170
bool queue_empty() const
Definition: pool.hpp:186
thread_joiner m_joiner
Definition: pool.hpp:114
size_t queue_size() const
Definition: pool.hpp:182