simplify collector function

looks like the lambda doesn't buy us anything here.
This commit is contained in:
Jörg Thalheim 2023-12-10 17:19:47 +01:00 committed by mergify[bot]
parent 97dd8023e8
commit b24c03e2de

View file

@ -119,9 +119,7 @@ void handleBrokenWorkerPipe(Proc &proc) {
} }
} }
std::function<void()> collector(Sync<State> &state_, void collector(Sync<State> &state_, std::condition_variable &wakeup) {
std::condition_variable &wakeup) {
return [&]() {
try { try {
std::optional<std::unique_ptr<Proc>> proc_; std::optional<std::unique_ptr<Proc>> proc_;
std::optional<std::unique_ptr<LineReader>> fromReader_; std::optional<std::unique_ptr<LineReader>> fromReader_;
@ -129,8 +127,8 @@ std::function<void()> collector(Sync<State> &state_,
while (true) { while (true) {
if (!proc_.has_value()) { if (!proc_.has_value()) {
proc_ = std::make_unique<Proc>(worker); proc_ = std::make_unique<Proc>(worker);
fromReader_ = std::make_unique<LineReader>( fromReader_ =
proc_.value()->from.release()); std::make_unique<LineReader>(proc_.value()->from.release());
} }
auto proc = std::move(proc_.value()); auto proc = std::move(proc_.value());
auto fromReader = std::move(fromReader_.value()); auto fromReader = std::move(fromReader_.value());
@ -146,11 +144,9 @@ std::function<void()> collector(Sync<State> &state_,
} else if (s != "next") { } else if (s != "next") {
try { try {
auto json = json::parse(s); auto json = json::parse(s);
throw Error("worker error: %s", throw Error("worker error: %s", (std::string)json["error"]);
(std::string)json["error"]);
} catch (const json::exception &e) { } catch (const json::exception &e) {
throw Error( throw Error("Received invalid JSON from worker: %s '%s'",
"Received invalid JSON from worker: %s '%s'",
e.what(), s); e.what(), s);
} }
} }
@ -226,10 +222,10 @@ std::function<void()> collector(Sync<State> &state_,
state->exc = std::current_exception(); state->exc = std::current_exception();
wakeup.notify_all(); wakeup.notify_all();
} }
};
} }
int main(int argc, char **argv) { int main(int argc, char **argv) {
/* Prevent undeclared dependencies in the evaluation via /* Prevent undeclared dependencies in the evaluation via
$NIX_PATH. */ $NIX_PATH. */
unsetenv("NIX_PATH"); unsetenv("NIX_PATH");
@ -277,8 +273,9 @@ int main(int argc, char **argv) {
/* Start a collector thread per worker process. */ /* Start a collector thread per worker process. */
std::vector<std::thread> threads; std::vector<std::thread> threads;
std::condition_variable wakeup; std::condition_variable wakeup;
for (size_t i = 0; i < myArgs.nrWorkers; i++) for (size_t i = 0; i < myArgs.nrWorkers; i++) {
threads.emplace_back(std::thread(collector(state_, wakeup))); threads.emplace_back(collector, std::ref(state_), std::ref(wakeup));
}
for (auto &thread : threads) for (auto &thread : threads)
thread.join(); thread.join();