mirror of
https://github.com/privatevoid-net/nix-super.git
synced 2024-11-23 06:26:15 +02:00
ThreadPool: Start doing work as soon as work items are enqueued
This commit is contained in:
parent
58c84cda3b
commit
b2ce6fde5a
2 changed files with 84 additions and 59 deletions
|
@ -1,79 +1,99 @@
|
||||||
#include "thread-pool.hh"
|
#include "thread-pool.hh"
|
||||||
|
#include "affinity.hh"
|
||||||
|
|
||||||
namespace nix {
|
namespace nix {
|
||||||
|
|
||||||
ThreadPool::ThreadPool(size_t _nrThreads)
|
ThreadPool::ThreadPool(size_t _maxThreads)
|
||||||
: nrThreads(_nrThreads)
|
: maxThreads(_maxThreads)
|
||||||
{
|
{
|
||||||
if (!nrThreads) {
|
restoreAffinity(); // FIXME
|
||||||
nrThreads = std::thread::hardware_concurrency();
|
|
||||||
if (!nrThreads) nrThreads = 1;
|
if (!maxThreads) {
|
||||||
|
maxThreads = std::thread::hardware_concurrency();
|
||||||
|
if (!maxThreads) maxThreads = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ThreadPool::enqueue(const work_t & t)
|
void ThreadPool::enqueue(const work_t & t)
|
||||||
{
|
{
|
||||||
auto state_(state.lock());
|
auto state(state_.lock());
|
||||||
state_->left.push(t);
|
assert(!state->quit);
|
||||||
wakeup.notify_one();
|
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ThreadPool::process()
|
void ThreadPool::process()
|
||||||
{
|
{
|
||||||
printMsg(lvlDebug, format("starting pool of %d threads") % nrThreads);
|
while (true) {
|
||||||
|
auto state(state_.lock());
|
||||||
|
if (state->exception)
|
||||||
|
std::rethrow_exception(state->exception);
|
||||||
|
if (state->left.empty() && !state->pending) break;
|
||||||
|
state.wait(done);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
std::vector<std::thread> workers;
|
void ThreadPool::workerEntry()
|
||||||
|
{
|
||||||
for (size_t n = 0; n < nrThreads; n++)
|
bool didWork = false;
|
||||||
workers.push_back(std::thread([&]() {
|
|
||||||
bool first = true;
|
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
work_t work;
|
work_t w;
|
||||||
{
|
{
|
||||||
auto state_(state.lock());
|
auto state(state_.lock());
|
||||||
if (state_->exception) return;
|
while (true) {
|
||||||
if (!first) {
|
if (state->quit || state->exception) return;
|
||||||
assert(state_->pending);
|
if (didWork) {
|
||||||
state_->pending--;
|
assert(state->pending);
|
||||||
|
state->pending--;
|
||||||
|
didWork = false;
|
||||||
}
|
}
|
||||||
first = false;
|
if (!state->left.empty()) break;
|
||||||
while (state_->left.empty()) {
|
if (!state->pending)
|
||||||
if (!state_->pending) {
|
done.notify_all();
|
||||||
wakeup.notify_all();
|
state.wait(work);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
if (state_->exception) return;
|
w = state->left.front();
|
||||||
state_.wait(wakeup);
|
state->left.pop();
|
||||||
}
|
state->pending++;
|
||||||
work = state_->left.front();
|
|
||||||
state_->left.pop();
|
|
||||||
state_->pending++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
work();
|
w();
|
||||||
} catch (std::exception & e) {
|
} catch (std::exception & e) {
|
||||||
auto state_(state.lock());
|
auto state(state_.lock());
|
||||||
if (state_->exception) {
|
if (state->exception) {
|
||||||
if (!dynamic_cast<Interrupted*>(&e))
|
if (!dynamic_cast<Interrupted*>(&e))
|
||||||
printMsg(lvlError, format("error: %s") % e.what());
|
printMsg(lvlError, format("error: %s") % e.what());
|
||||||
} else {
|
} else {
|
||||||
state_->exception = std::current_exception();
|
state->exception = std::current_exception();
|
||||||
wakeup.notify_all();
|
work.notify_all();
|
||||||
}
|
done.notify_all();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}));
|
didWork = true;
|
||||||
|
|
||||||
for (auto & thr : workers)
|
|
||||||
thr.join();
|
|
||||||
|
|
||||||
{
|
|
||||||
auto state_(state.lock());
|
|
||||||
if (state_->exception)
|
|
||||||
std::rethrow_exception(state_->exception);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -15,7 +15,9 @@ class ThreadPool
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
ThreadPool(size_t nrThreads = 0);
|
ThreadPool(size_t maxThreads = 0);
|
||||||
|
|
||||||
|
~ThreadPool();
|
||||||
|
|
||||||
// FIXME: use std::packaged_task?
|
// FIXME: use std::packaged_task?
|
||||||
typedef std::function<void()> work_t;
|
typedef std::function<void()> work_t;
|
||||||
|
@ -34,19 +36,22 @@ public:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
size_t nrThreads;
|
size_t maxThreads;
|
||||||
|
|
||||||
struct State
|
struct State
|
||||||
{
|
{
|
||||||
std::queue<work_t> left;
|
std::queue<work_t> left;
|
||||||
size_t pending = 0;
|
size_t pending = 0;
|
||||||
std::exception_ptr exception;
|
std::exception_ptr exception;
|
||||||
|
std::vector<std::thread> workers;
|
||||||
|
bool quit = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
Sync<State> state;
|
Sync<State> state_;
|
||||||
|
|
||||||
std::condition_variable wakeup;
|
std::condition_variable work, done;
|
||||||
|
|
||||||
|
void workerEntry();
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue