2016-03-29 14:29:50 +02:00
|
|
|
#include "thread-pool.hh"
|
2016-04-22 18:19:17 +02:00
|
|
|
#include "affinity.hh"
|
2016-03-29 14:29:50 +02:00
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
2016-04-22 18:19:17 +02:00
|
|
|
ThreadPool::ThreadPool(size_t _maxThreads)
|
|
|
|
: maxThreads(_maxThreads)
|
2016-03-29 14:29:50 +02:00
|
|
|
{
|
2016-04-22 18:19:17 +02:00
|
|
|
restoreAffinity(); // FIXME
|
|
|
|
|
|
|
|
if (!maxThreads) {
|
|
|
|
maxThreads = std::thread::hardware_concurrency();
|
|
|
|
if (!maxThreads) maxThreads = 1;
|
2016-03-29 14:29:50 +02:00
|
|
|
}
|
2016-04-22 18:19:17 +02:00
|
|
|
|
|
|
|
debug(format("starting pool of %d threads") % maxThreads);
|
|
|
|
}
|
|
|
|
|
|
|
|
ThreadPool::~ThreadPool()
|
|
|
|
{
|
|
|
|
std::vector<std::thread> workers;
|
|
|
|
{
|
|
|
|
auto state(state_.lock());
|
2017-09-08 15:31:24 +02:00
|
|
|
quit = true;
|
2016-04-22 18:19:17 +02:00
|
|
|
std::swap(workers, state->workers);
|
|
|
|
}
|
|
|
|
|
|
|
|
debug(format("reaping %d worker threads") % workers.size());
|
|
|
|
|
|
|
|
work.notify_all();
|
|
|
|
|
|
|
|
for (auto & thr : workers)
|
|
|
|
thr.join();
|
2016-03-29 14:29:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadPool::enqueue(const work_t & t)
|
|
|
|
{
|
2016-04-22 18:19:17 +02:00
|
|
|
auto state(state_.lock());
|
2017-09-08 15:31:24 +02:00
|
|
|
if (quit)
|
2016-07-21 18:14:16 +02:00
|
|
|
throw ThreadPoolShutDown("cannot enqueue a work item while the thread pool is shutting down");
|
2016-04-22 18:19:17 +02:00
|
|
|
state->left.push(t);
|
|
|
|
if (state->left.size() > state->workers.size() && state->workers.size() < maxThreads)
|
|
|
|
state->workers.emplace_back(&ThreadPool::workerEntry, this);
|
|
|
|
work.notify_one();
|
2016-03-29 14:29:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadPool::process()
|
|
|
|
{
|
2017-09-08 14:40:27 +02:00
|
|
|
/* Loop until there are no active work items *and* there either
|
|
|
|
are no queued items or there is an exception. The
|
|
|
|
post-condition is that no new items will become active. */
|
2016-04-22 18:19:17 +02:00
|
|
|
while (true) {
|
|
|
|
auto state(state_.lock());
|
2017-09-08 14:40:27 +02:00
|
|
|
if (!state->active) {
|
|
|
|
if (state->exception)
|
|
|
|
std::rethrow_exception(state->exception);
|
|
|
|
if (state->left.empty())
|
|
|
|
break;
|
|
|
|
}
|
2016-04-22 18:19:17 +02:00
|
|
|
state.wait(done);
|
|
|
|
}
|
|
|
|
}
|
2016-03-29 14:29:50 +02:00
|
|
|
|
2016-04-22 18:19:17 +02:00
|
|
|
void ThreadPool::workerEntry()
|
|
|
|
{
|
2017-09-08 15:31:24 +02:00
|
|
|
interruptCheck = [&]() { return (bool) quit; };
|
|
|
|
|
2016-04-22 18:19:17 +02:00
|
|
|
bool didWork = false;
|
2017-09-08 14:40:27 +02:00
|
|
|
std::exception_ptr exc;
|
2016-03-29 14:29:50 +02:00
|
|
|
|
2016-04-22 18:19:17 +02:00
|
|
|
while (true) {
|
|
|
|
work_t w;
|
|
|
|
{
|
|
|
|
auto state(state_.lock());
|
2017-09-08 14:40:27 +02: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 15:31:24 +02:00
|
|
|
quit = true;
|
2017-09-08 14:40:27 +02: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 (...) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait until a work item is available or another thread
|
|
|
|
had an exception or we're asked to quit. */
|
2016-03-29 14:29:50 +02:00
|
|
|
while (true) {
|
2017-09-08 15:31:24 +02:00
|
|
|
if (quit) {
|
2017-09-08 14:40:27 +02:00
|
|
|
if (!state->active)
|
|
|
|
done.notify_one();
|
|
|
|
return;
|
2016-03-29 14:29:50 +02:00
|
|
|
}
|
2016-04-22 18:19:17 +02:00
|
|
|
if (!state->left.empty()) break;
|
2017-09-08 14:40:27 +02:00
|
|
|
if (!state->active) {
|
|
|
|
done.notify_one();
|
|
|
|
return;
|
|
|
|
}
|
2016-04-22 18:19:17 +02:00
|
|
|
state.wait(work);
|
2016-03-29 14:29:50 +02:00
|
|
|
}
|
2017-09-08 14:40:27 +02:00
|
|
|
|
|
|
|
w = std::move(state->left.front());
|
2016-04-22 18:19:17 +02:00
|
|
|
state->left.pop();
|
2017-09-08 14:40:27 +02:00
|
|
|
state->active++;
|
2016-04-22 18:19:17 +02:00
|
|
|
}
|
2016-03-29 14:29:50 +02:00
|
|
|
|
2016-04-22 18:19:17 +02:00
|
|
|
try {
|
|
|
|
w();
|
2017-09-08 14:40:27 +02:00
|
|
|
} catch (...) {
|
|
|
|
exc = std::current_exception();
|
2016-04-22 18:19:17 +02:00
|
|
|
}
|
2016-03-29 14:29:50 +02:00
|
|
|
|
2016-04-22 18:19:17 +02:00
|
|
|
didWork = true;
|
2016-03-29 14:29:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|