Debian Codegraph @

src/common/threadpool.cpp

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 #include "misc_log_ex.h"
29 #include "common/threadpool.h"
30
31 #include "cryptonote_config.h"
32 #include "common/util.h"
33
34 static __thread int depth = 0;
35 static __thread bool is_leaf = false;
36
37 namespace tools
38 {
39 threadpool::threadpool(unsigned int max_threads) : running(true), active(0) {
40 create(max_threads);
41 }
42
43 threadpool::~threadpool() {
44 destroy();
45 }
46
47 void threadpool::destroy() {
48 try
49 {
50 const boost::unique_lock<boost::mutex> lock(mutex);
51 running = false;
52 has_work.notify_all();
53 }
54 catch (...)
55 {
56 // if the lock throws, we're just do it without a lock and hope,
57 // since the alternative is terminate
58 running = false;
59 has_work.notify_all();
60 }
61 for (size_t i = 0; i<threads.size(); i++) {
62 try { threads[i].join(); }
63 catch (...) { /* ignore */ }
64 }
65 threads.clear();
66 }
67
68 void threadpool::recycle() {
69 destroy();
70 create(max);
71 }
72
73 void threadpool::create(unsigned int max_threads) {
74 const boost::unique_lock<boost::mutex> lock(mutex);
75 boost::thread::attributes attrs;
76 attrs.set_stack_size(THREAD_STACK_SIZE);
77 max = max_threads ? max_threads : tools::get_max_concurrency();
78 size_t i = max ? max - 1 : 0;
79 running = true;
80 while(i--) {
81 threads.push_back(boost::thread(attrs, boost::bind(&threadpool::run, this, false)));
82 }
83 }
84
85 void threadpool::submit(waiter *obj, std::function<void()> f, bool leaf) {
86 CHECK_AND_ASSERT_THROW_MES(!is_leaf, "A leaf routine is using a thread pool");
87 boost::unique_lock<boost::mutex> lock(mutex);
88 if (!leaf && ((active == max && !queue.empty()) || depth > 0)) {
89 // if all available threads are already running
90 // and there's work waiting, just run in current thread
91 lock.unlock();
92 ++depth;
93 is_leaf = leaf;
94 f();
95 --depth;
96 is_leaf = false;
97 } else {
98 if (obj)
99 obj->inc();
100 if (leaf)
101 queue.push_front({obj, f, leaf});
102 else
103 queue.push_back({obj, f, leaf});
104 has_work.notify_one();
105 }
106 }
107
108 unsigned int threadpool::get_max_concurrency() const {
109 return max;
110 }
111
112 threadpool::waiter::~waiter()
113 {
114 try
115 {
116 boost::unique_lock<boost::mutex> lock(mt);
117 if (num)
118 MERROR("wait should have been called before waiter dtor - waiting now");
119 }
120 catch (...) { /* ignore */ }
121 try
122 {
123 wait();
124 }
125 catch (const std::exception &e)
126 {
127 /* ignored */
128 }
129 }
130
131 bool threadpool::waiter::wait() {
132 pool.run(true);
133 boost::unique_lock<boost::mutex> lock(mt);
134 while(num)
135 cv.wait(lock);
136 return !error();
137 }
138
139 void threadpool::waiter::inc() {
140 const boost::unique_lock<boost::mutex> lock(mt);
141 num++;
142 }
143
144 void threadpool::waiter::dec() {
145 const boost::unique_lock<boost::mutex> lock(mt);
146 num--;
147 if (!num)
148 cv.notify_all();
149 }
150
151 void threadpool::run(bool flush) {
152 boost::unique_lock<boost::mutex> lock(mutex);
153 while (running) {
154 entry e;
155 while(queue.empty() && running)
156 {
157 if (flush)
158 return;
159 has_work.wait(lock);
160 }
161 if (!running) break;
162
163 active++;
164 e = std::move(queue.front());
165 queue.pop_front();
166 lock.unlock();
167 ++depth;
168 is_leaf = e.leaf;
169 try { e.f(); }
170 catch (const std::exception &ex) { e.wo->set_error(); try { MERROR("Exception in threadpool job: " << ex.what()); } catch (...) {} }
171 --depth;
172 is_leaf = false;
173
174 if (e.wo)
175 e.wo->dec();
176 lock.lock();
177 active--;
178 }
179 }
180 }