2016-03-29 15:29:50 +03:00
|
|
|
#include "thread-pool.hh"
|
2016-04-22 19:19:17 +03:00
|
|
|
#include "affinity.hh"
|
2016-03-29 15:29:50 +03:00
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
2016-04-22 19:19:17 +03:00
|
|
|
ThreadPool::ThreadPool(size_t _maxThreads)
|
|
|
|
: maxThreads(_maxThreads)
|
2016-03-29 15:29:50 +03:00
|
|
|
{
|
2016-04-22 19:19:17 +03:00
|
|
|
restoreAffinity(); // FIXME
|
|
|
|
|
|
|
|
if (!maxThreads) {
|
|
|
|
maxThreads = std::thread::hardware_concurrency();
|
|
|
|
if (!maxThreads) maxThreads = 1;
|
2016-03-29 15:29:50 +03:00
|
|
|
}
|
2016-04-22 19:19:17 +03:00
|
|
|
|
|
|
|
debug(format("starting pool of %d threads") % maxThreads);
|
|
|
|
}
|
|
|
|
|
|
|
|
ThreadPool::~ThreadPool()
|
|
|
|
{
|
|
|
|
std::vector<std::thread> workers;
|
|
|
|
{
|
|
|
|
auto state(state_.lock());
|
|
|
|
state->quit = true;
|
|
|
|
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 15:29:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadPool::enqueue(const work_t & t)
|
|
|
|
{
|
2016-04-22 19:19:17 +03:00
|
|
|
auto state(state_.lock());
|
2016-07-21 19:14:16 +03:00
|
|
|
if (state->quit)
|
|
|
|
throw ThreadPoolShutDown("cannot enqueue a work item while the thread pool is shutting down");
|
2016-04-22 19:19:17 +03: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 15:29:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadPool::process()
|
|
|
|
{
|
2017-09-08 15:40:27 +03: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 19:19:17 +03:00
|
|
|
while (true) {
|
|
|
|
auto state(state_.lock());
|
2017-09-08 15:40:27 +03:00
|
|
|
if (!state->active) {
|
|
|
|
if (state->exception)
|
|
|
|
std::rethrow_exception(state->exception);
|
|
|
|
if (state->left.empty())
|
|
|
|
break;
|
|
|
|
}
|
2016-04-22 19:19:17 +03:00
|
|
|
state.wait(done);
|
|
|
|
}
|
|
|
|
}
|
2016-03-29 15:29:50 +03:00
|
|
|
|
2016-04-22 19:19:17 +03:00
|
|
|
void ThreadPool::workerEntry()
|
|
|
|
{
|
|
|
|
bool didWork = false;
|
2017-09-08 15:40:27 +03:00
|
|
|
std::exception_ptr exc;
|
2016-03-29 15:29:50 +03:00
|
|
|
|
2016-04-22 19:19:17 +03:00
|
|
|
while (true) {
|
|
|
|
work_t w;
|
|
|
|
{
|
|
|
|
auto state(state_.lock());
|
2017-09-08 15:40:27 +03:00
|
|
|
|
|
|
|
if (didWork) {
|
|
|
|
assert(state->active);
|
|
|
|
state->active--;
|
|
|
|
|
|
|
|
if (exc) {
|
|
|
|
|
|
|
|
if (!state->exception) {
|
|
|
|
state->exception = exc;
|
|
|
|
// Tell the other workers to quit.
|
|
|
|
state->quit = true;
|
|
|
|
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 15:29:50 +03:00
|
|
|
while (true) {
|
2017-09-08 15:40:27 +03:00
|
|
|
if (state->quit) {
|
|
|
|
if (!state->active)
|
|
|
|
done.notify_one();
|
|
|
|
return;
|
2016-03-29 15:29:50 +03:00
|
|
|
}
|
2016-04-22 19:19:17 +03:00
|
|
|
if (!state->left.empty()) break;
|
2017-09-08 15:40:27 +03:00
|
|
|
if (!state->active) {
|
|
|
|
done.notify_one();
|
|
|
|
return;
|
|
|
|
}
|
2016-04-22 19:19:17 +03:00
|
|
|
state.wait(work);
|
2016-03-29 15:29:50 +03:00
|
|
|
}
|
2017-09-08 15:40:27 +03:00
|
|
|
|
|
|
|
w = std::move(state->left.front());
|
2016-04-22 19:19:17 +03:00
|
|
|
state->left.pop();
|
2017-09-08 15:40:27 +03:00
|
|
|
state->active++;
|
2016-04-22 19:19:17 +03:00
|
|
|
}
|
2016-03-29 15:29:50 +03:00
|
|
|
|
2016-04-22 19:19:17 +03:00
|
|
|
try {
|
|
|
|
w();
|
2017-09-08 15:40:27 +03:00
|
|
|
} catch (...) {
|
|
|
|
exc = std::current_exception();
|
2016-04-22 19:19:17 +03:00
|
|
|
}
|
2016-03-29 15:29:50 +03:00
|
|
|
|
2016-04-22 19:19:17 +03:00
|
|
|
didWork = true;
|
2016-03-29 15:29:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|