Libosmium  2.5.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-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 <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 
60  class Pool {
61 
66  class thread_joiner {
67 
68  std::vector<std::thread>& m_threads;
69 
70  public:
71 
72  explicit thread_joiner(std::vector<std::thread>& threads) :
73  m_threads(threads) {
74  }
75 
77  for (auto& thread : m_threads) {
78  if (thread.joinable()) {
79  thread.join();
80  }
81  }
82  }
83 
84  }; // class thread_joiner
85 
87  std::vector<std::thread> m_threads;
90 
91  void worker_thread() {
92  osmium::thread::set_thread_name("_osmium_worker");
93  while (true) {
94  function_wrapper task;
95  m_work_queue.wait_and_pop_with_timeout(task);
96  if (task) {
97  if (task()) {
98  // The called tasks returns true only when the
99  // worker thread should shut down.
100  return;
101  }
102  }
103  }
104  }
105 
118  explicit Pool(int num_threads, size_t max_queue_size) :
119  m_work_queue(max_queue_size, "work"),
120  m_threads(),
121  m_joiner(m_threads),
122  m_num_threads(num_threads) {
123 
124  if (m_num_threads == 0) {
125  m_num_threads = osmium::config::get_pool_threads();
126  }
127 
128  if (m_num_threads <= 0) {
129  m_num_threads = std::max(1, static_cast<int>(std::thread::hardware_concurrency()) + m_num_threads);
130  }
131 
132  try {
133  for (int i = 0; i < m_num_threads; ++i) {
134  m_threads.push_back(std::thread(&Pool::worker_thread, this));
135  }
136  } catch (...) {
138  throw;
139  }
140  }
141 
142  public:
143 
144  static constexpr int default_num_threads = 0;
145  static constexpr size_t max_work_queue_size = 10;
146 
147  static Pool& instance() {
148  static Pool pool(default_num_threads, max_work_queue_size);
149  return pool;
150  }
151 
153  for (int i = 0; i < m_num_threads; ++i) {
154  // The special function wrapper makes a worker shut down.
155  m_work_queue.push(function_wrapper{0});
156  }
157  }
158 
159  ~Pool() {
161  m_work_queue.shutdown();
162  }
163 
164  size_t queue_size() const {
165  return m_work_queue.size();
166  }
167 
168  bool queue_empty() const {
169  return m_work_queue.empty();
170  }
171 
172  template <typename TFunction>
174 
175  typedef typename std::result_of<TFunction()>::type result_type;
176 
177  std::packaged_task<result_type()> task(std::forward<TFunction>(func));
178  std::future<result_type> future_result(task.get_future());
179  m_work_queue.push(std::move(task));
180 
181  return future_result;
182  }
183 
184  }; // class Pool
185 
186  } // namespace thread
187 
188 } // namespace osmium
189 
190 #endif // OSMIUM_THREAD_POOL_HPP
Pool(int num_threads, size_t max_queue_size)
Definition: pool.hpp:118
type
Definition: entity_bits.hpp:60
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:159
static constexpr size_t max_work_queue_size
Definition: pool.hpp:145
std::vector< std::thread > & m_threads
Definition: pool.hpp:68
void shutdown()
Definition: queue.hpp:144
std::future< typename std::result_of< TFunction()>::type > submit(TFunction &&func)
Definition: pool.hpp:173
static constexpr int default_num_threads
Definition: pool.hpp:144
void worker_thread()
Definition: pool.hpp:91
static Pool & instance()
Definition: pool.hpp:147
Namespace for everything in the Osmium library.
Definition: assembler.hpp:59
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:87
Definition: pool.hpp:60
osmium::thread::Queue< function_wrapper > m_work_queue
Definition: pool.hpp:86
thread_joiner(std::vector< std::thread > &threads)
Definition: pool.hpp:72
int m_num_threads
Definition: pool.hpp:89
~thread_joiner()
Definition: pool.hpp:76
void push(T value)
Definition: queue.hpp:122
void shutdown_all_workers()
Definition: pool.hpp:152
bool queue_empty() const
Definition: pool.hpp:168
thread_joiner m_joiner
Definition: pool.hpp:88
size_t queue_size() const
Definition: pool.hpp:164