Debian Codegraph @

src/common/threadpool.h

raw symbols SCIP
1 // Copyright (c) 2017-2022, 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 <deque>
35 #include <functional>
36 #include <utility>
37 #include <vector>
38 #include <stdexcept>
39
40 namespace tools
41 {
42 //! A global thread pool
43 class threadpool
44 {
45 public:
46 static threadpool& getInstanceForCompute() {
47 static threadpool instance;
48 return instance;
49 }
50 static threadpool& getInstanceForIO() {
51 static threadpool instance(8);
52 return instance;
53 }
54 static threadpool *getNewForUnitTests(unsigned max_threads = 0) {
55 return new threadpool(max_threads);
56 }
57
58 // The waiter lets the caller know when all of its
59 // tasks are completed.
60 class waiter {
61 boost::mutex mt;
62 boost::condition_variable cv;
63 threadpool &pool;
64 int num;
65 bool error_flag;
66 public:
67 void inc();
68 void dec();
69 bool wait(); //! Wait for a set of tasks to finish, returns false iff any error
70 void set_error() noexcept { error_flag = true; }
71 bool error() const noexcept { return error_flag; }
72 waiter(threadpool &pool) : pool(pool), num(0), error_flag(false) {}
73 ~waiter();
74 };
75
76 // Submit a task to the pool. The waiter pointer may be
77 // NULL if the caller doesn't care to wait for the
78 // task to finish.
79 void submit(waiter *waiter, std::function<void()> f, bool leaf = false);
80
81 // destroy and recreate threads
82 void recycle();
83
84 unsigned int get_max_concurrency() const;
85
86 ~threadpool();
87
88 private:
89 threadpool(unsigned int max_threads = 0);
90 void destroy();
91 void create(unsigned int max_threads);
92 typedef struct entry {
93 waiter *wo;
94 std::function<void()> f;
95 bool leaf;
96 } entry;
97 std::deque<entry> queue;
98 boost::condition_variable has_work;
99 boost::mutex mutex;
100 std::vector<boost::thread> threads;
101 unsigned int active;
102 unsigned int max;
103 bool running;
104 void run(bool flush = false);
105 };
106
107 }