Monero
threadpool.h
Go to the documentation of this file.
1 // Copyright (c) 2017-2020, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #pragma once
29 
30 #include <boost/thread/condition_variable.hpp>
31 #include <boost/thread/mutex.hpp>
32 #include <boost/thread/thread.hpp>
33 #include <cstddef>
34 #include <functional>
35 #include <utility>
36 #include <vector>
37 #include <stdexcept>
38 
39 namespace tools
40 {
43 {
44 public:
45  static threadpool& getInstance() {
46  static threadpool instance;
47  return instance;
48  }
49  static threadpool *getNewForUnitTests(unsigned max_threads = 0) {
50  return new threadpool(max_threads);
51  }
52 
53  // The waiter lets the caller know when all of its
54  // tasks are completed.
55  class waiter {
56  boost::mutex mt;
57  boost::condition_variable cv;
59  int num;
60  bool error_flag;
61  public:
62  void inc();
63  void dec();
64  bool wait();
65  void set_error() noexcept { error_flag = true; }
66  bool error() const noexcept { return error_flag; }
68  ~waiter();
69  };
70 
71  // Submit a task to the pool. The waiter pointer may be
72  // NULL if the caller doesn't care to wait for the
73  // task to finish.
74  void submit(waiter *waiter, std::function<void()> f, bool leaf = false);
75 
76  // destroy and recreate threads
77  void recycle();
78 
79  unsigned int get_max_concurrency() const;
80 
81  ~threadpool();
82 
83  private:
84  threadpool(unsigned int max_threads = 0);
85  void destroy();
86  void create(unsigned int max_threads);
87  typedef struct entry {
89  std::function<void()> f;
90  bool leaf;
91  } entry;
92  std::deque<entry> queue;
93  boost::condition_variable has_work;
94  boost::mutex mutex;
95  std::vector<boost::thread> threads;
96  unsigned int active;
97  unsigned int max;
98  bool running;
99  void run(bool flush = false);
100 };
101 
102 }
Definition: threadpool.h:55
waiter(threadpool &pool)
Definition: threadpool.h:67
void set_error() noexcept
Wait for a set of tasks to finish, returns false iff any error.
Definition: threadpool.h:65
int num
Definition: threadpool.h:59
void inc()
Definition: threadpool.cpp:139
bool wait()
Definition: threadpool.cpp:131
bool error() const noexcept
Definition: threadpool.h:66
void dec()
Definition: threadpool.cpp:144
boost::condition_variable cv
Definition: threadpool.h:57
bool error_flag
Definition: threadpool.h:60
threadpool & pool
Definition: threadpool.h:58
~waiter()
Definition: threadpool.cpp:112
boost::mutex mt
Definition: threadpool.h:56
A global thread pool.
Definition: threadpool.h:43
unsigned int active
Definition: threadpool.h:96
void run(bool flush=false)
Definition: threadpool.cpp:151
void recycle()
Definition: threadpool.cpp:68
struct tools::threadpool::entry entry
threadpool(unsigned int max_threads=0)
Definition: threadpool.cpp:39
std::deque< entry > queue
Definition: threadpool.h:92
static threadpool * getNewForUnitTests(unsigned max_threads=0)
Definition: threadpool.h:49
bool running
Definition: threadpool.h:98
~threadpool()
Definition: threadpool.cpp:43
unsigned int max
Definition: threadpool.h:97
boost::condition_variable has_work
Definition: threadpool.h:93
boost::mutex mutex
Definition: threadpool.h:94
static threadpool & getInstance()
Definition: threadpool.h:45
void submit(waiter *waiter, std::function< void()> f, bool leaf=false)
Definition: threadpool.cpp:85
std::vector< boost::thread > threads
Definition: threadpool.h:95
void destroy()
Definition: threadpool.cpp:47
unsigned int get_max_concurrency() const
Definition: threadpool.cpp:108
void create(unsigned int max_threads)
Definition: threadpool.cpp:73
const
Definition: build_protob.py:9
Various Tools.
Definition: apply_permutation.h:40
#define false
Definition: stdbool.h:37
Definition: threadpool.h:87
bool leaf
Definition: threadpool.h:90
waiter * wo
Definition: threadpool.h:88
std::function< void()> f
Definition: threadpool.h:89