2016-03-29 12:29:50 +00:00
|
|
|
#include "thread-pool.hh"
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
2016-04-22 16:19:17 +00:00
|
|
|
ThreadPool::ThreadPool(size_t _maxThreads)
|
|
|
|
: maxThreads(_maxThreads)
|
2016-03-29 12:29:50 +00:00
|
|
|
{
|
2016-04-22 16:19:17 +00:00
|
|
|
if (!maxThreads) {
|
|
|
|
maxThreads = std::thread::hardware_concurrency();
|
|
|
|
if (!maxThreads) maxThreads = 1;
|
2016-03-29 12:29:50 +00:00
|
|
|
}
|
2016-04-22 16:19:17 +00:00
|
|
|
|
2017-10-09 13:07:07 +00:00
|
|
|
debug("starting pool of %d threads", maxThreads - 1);
|
2016-04-22 16:19:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ThreadPool::~ThreadPool()
|
2017-10-09 13:07:07 +00:00
|
|
|
{
|
|
|
|
shutdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadPool::shutdown()
|
2016-04-22 16:19:17 +00:00
|
|
|
{
|
|
|
|
std::vector<std::thread> workers;
|
|
|
|
{
|
|
|
|
auto state(state_.lock());
|
2017-09-08 13:31:24 +00:00
|
|
|
quit = true;
|
2016-04-22 16:19:17 +00:00
|
|
|
std::swap(workers, state->workers);
|
|
|
|
}
|
|
|
|
|
2017-10-09 13:07:07 +00:00
|
|
|
if (workers.empty()) return;
|
|
|
|
|
|
|
|
debug("reaping %d worker threads", workers.size());
|
2016-04-22 16:19:17 +00:00
|
|
|
|
|
|
|
work.notify_all();
|
|
|
|
|
|
|
|
for (auto & thr : workers)
|
|
|
|
thr.join();
|
2016-03-29 12:29:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadPool::enqueue(const work_t & t)
|
|
|
|
{
|
2016-04-22 16:19:17 +00:00
|
|
|
auto state(state_.lock());
|
2017-09-08 13:31:24 +00:00
|
|
|
if (quit)
|
2016-07-21 16:14:16 +00:00
|
|
|
throw ThreadPoolShutDown("cannot enqueue a work item while the thread pool is shutting down");
|
2017-10-09 13:07:07 +00:00
|
|
|
state->pending.push(t);
|
|
|
|
/* Note: process() also executes items, so count it as a worker. */
|
|
|
|
if (state->pending.size() > state->workers.size() + 1 && state->workers.size() + 1 < maxThreads)
|
|
|
|
state->workers.emplace_back(&ThreadPool::doWork, this, false);
|
2016-04-22 16:19:17 +00:00
|
|
|
work.notify_one();
|
2016-03-29 12:29:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadPool::process()
|
|
|
|
{
|
2017-10-09 13:07:07 +00:00
|
|
|
state_.lock()->draining = true;
|
|
|
|
|
|
|
|
/* Do work until no more work is pending or active. */
|
|
|
|
try {
|
|
|
|
doWork(true);
|
|
|
|
|
2016-04-22 16:19:17 +00:00
|
|
|
auto state(state_.lock());
|
2017-10-09 13:07:07 +00:00
|
|
|
|
|
|
|
assert(quit);
|
|
|
|
|
|
|
|
if (state->exception)
|
|
|
|
std::rethrow_exception(state->exception);
|
|
|
|
|
|
|
|
} catch (...) {
|
|
|
|
/* In the exceptional case, some workers may still be
|
|
|
|
active. They may be referencing the stack frame of the
|
|
|
|
caller. So wait for them to finish. (~ThreadPool also does
|
|
|
|
this, but it might be destroyed after objects referenced by
|
|
|
|
the work item lambdas.) */
|
|
|
|
shutdown();
|
|
|
|
throw;
|
2016-04-22 16:19:17 +00:00
|
|
|
}
|
|
|
|
}
|
2016-03-29 12:29:50 +00:00
|
|
|
|
2017-10-09 13:07:07 +00:00
|
|
|
void ThreadPool::doWork(bool mainThread)
|
2016-04-22 16:19:17 +00:00
|
|
|
{
|
2017-10-09 13:07:07 +00:00
|
|
|
if (!mainThread)
|
|
|
|
interruptCheck = [&]() { return (bool) quit; };
|
2017-09-08 13:31:24 +00:00
|
|
|
|
2016-04-22 16:19:17 +00:00
|
|
|
bool didWork = false;
|
2017-09-08 12:40:27 +00:00
|
|
|
std::exception_ptr exc;
|
2016-03-29 12:29:50 +00:00
|
|
|
|
2016-04-22 16:19:17 +00:00
|
|
|
while (true) {
|
|
|
|
work_t w;
|
|
|
|
{
|
|
|
|
auto state(state_.lock());
|
2017-09-08 12:40:27 +00:00
|
|
|
|
|
|
|
if (didWork) {
|
|
|
|
assert(state->active);
|
|
|
|
state->active--;
|
|
|
|
|
|
|
|
if (exc) {
|
|
|
|
|
|
|
|
if (!state->exception) {
|
|
|
|
state->exception = exc;
|
|
|
|
// Tell the other workers to quit.
|
2017-09-08 13:31:24 +00:00
|
|
|
quit = true;
|
2017-09-08 12:40:27 +00:00
|
|
|
work.notify_all();
|
|
|
|
} else {
|
|
|
|
/* Print the exception, since we can't
|
|
|
|
propagate it. */
|
|
|
|
try {
|
|
|
|
std::rethrow_exception(exc);
|
|
|
|
} catch (std::exception & e) {
|
|
|
|
if (!dynamic_cast<Interrupted*>(&e) &&
|
|
|
|
!dynamic_cast<ThreadPoolShutDown*>(&e))
|
|
|
|
ignoreException();
|
|
|
|
} catch (...) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-09 13:07:07 +00:00
|
|
|
/* Wait until a work item is available or we're asked to
|
|
|
|
quit. */
|
2016-03-29 12:29:50 +00:00
|
|
|
while (true) {
|
2017-10-09 13:07:07 +00:00
|
|
|
if (quit) return;
|
|
|
|
|
|
|
|
if (!state->pending.empty()) break;
|
|
|
|
|
|
|
|
/* If there are no active or pending items, and the
|
|
|
|
main thread is running process(), then no new items
|
|
|
|
can be added. So exit. */
|
|
|
|
if (!state->active && state->draining) {
|
|
|
|
quit = true;
|
|
|
|
work.notify_all();
|
2017-09-08 12:40:27 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-10-09 13:07:07 +00:00
|
|
|
|
2016-04-22 16:19:17 +00:00
|
|
|
state.wait(work);
|
2016-03-29 12:29:50 +00:00
|
|
|
}
|
2017-09-08 12:40:27 +00:00
|
|
|
|
2017-10-09 13:07:07 +00:00
|
|
|
w = std::move(state->pending.front());
|
|
|
|
state->pending.pop();
|
2017-09-08 12:40:27 +00:00
|
|
|
state->active++;
|
2016-04-22 16:19:17 +00:00
|
|
|
}
|
2016-03-29 12:29:50 +00:00
|
|
|
|
2016-04-22 16:19:17 +00:00
|
|
|
try {
|
|
|
|
w();
|
2017-09-08 12:40:27 +00:00
|
|
|
} catch (...) {
|
|
|
|
exc = std::current_exception();
|
2016-04-22 16:19:17 +00:00
|
|
|
}
|
2016-03-29 12:29:50 +00:00
|
|
|
|
2016-04-22 16:19:17 +00:00
|
|
|
didWork = true;
|
2016-03-29 12:29:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|