From b0c7c1ec664415d451dcd56d2982b369c0c33892 Mon Sep 17 00:00:00 2001 From: eldritch horrors Date: Mon, 30 Sep 2024 01:31:29 +0200 Subject: libstore: turn Worker::run() main loop into a promise Change-Id: Ib112ea9a3e67d5cb3d7d0ded30bbd25c96262470 --- src/libstore/build/worker.cc | 29 +++++++++++++++++------------ src/libstore/build/worker.hh | 2 ++ 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/libstore/build/worker.cc b/src/libstore/build/worker.cc index 7dd75f34a..8ab7fcc86 100644 --- a/src/libstore/build/worker.cc +++ b/src/libstore/build/worker.cc @@ -297,6 +297,18 @@ std::vector Worker::run(std::function req) topGoals.insert(goal); } + auto promise = runImpl(); + promise.wait(aio.waitScope).value(); + + std::vector results; + for (auto & [i, _p] : _topGoals) { + results.push_back(i); + } + return results; +} + +kj::Promise> Worker::runImpl() +try { debug("entered goal loop"); while (1) { @@ -313,12 +325,7 @@ std::vector Worker::run(std::function req) Goals awake2 = std::move(awake); for (auto & goal : awake2) { checkInterrupt(); - auto result = goal->work(); - if (result.poll(aio.waitScope)) { - handleWorkResult(goal, result.wait(aio.waitScope).value()); - } else { - childStarted(goal, std::move(result)); - } + childStarted(goal, goal->work()); if (topGoals.empty()) break; // stuff may have been cancelled } @@ -328,7 +335,7 @@ std::vector Worker::run(std::function req) /* Wait for input. */ if (!children.isEmpty()) - waitForInput().wait(aio.waitScope).value(); + (co_await waitForInput()).value(); else { assert(!awake.empty()); } @@ -344,11 +351,9 @@ std::vector Worker::run(std::function req) assert(!settings.keepGoing || awake.empty()); assert(!settings.keepGoing || children.isEmpty()); - std::vector results; - for (auto & [i, _p] : _topGoals) { - results.push_back(i); - } - return results; + co_return result::success(); +} catch (...) { + co_return result::failure(std::current_exception()); } kj::Promise> Worker::waitForInput() diff --git a/src/libstore/build/worker.hh b/src/libstore/build/worker.hh index 02b98b3aa..6da76fe34 100644 --- a/src/libstore/build/worker.hh +++ b/src/libstore/build/worker.hh @@ -188,6 +188,8 @@ private: statisticsOutdated = true; } + kj::Promise> runImpl(); + public: const Activity act; -- cgit v1.2.3