libstore: use std::async instead of Goal threads

the goals are either already using std::async and merely forgot to
remove std::thread vestiges or they emulate async with threads and
promises. we can simply use async directly everywhere for clarity.

Change-Id: I3f05098310a25984f10fff1e68c573329002b500
This commit is contained in:
eldritch horrors 2024-07-20 21:05:19 +02:00
parent ad36fb43ad
commit 58a91d70c9
3 changed files with 7 additions and 22 deletions

View file

@ -4,7 +4,6 @@
#include "store-api.hh" #include "store-api.hh"
#include "goal.hh" #include "goal.hh"
#include "realisation.hh" #include "realisation.hh"
#include <thread>
#include <future> #include <future>
namespace nix { namespace nix {
@ -41,11 +40,6 @@ class DrvOutputSubstitutionGoal : public Goal {
*/ */
std::shared_ptr<Store> sub; std::shared_ptr<Store> sub;
/**
* The substituter thread.
*/
std::thread thr;
std::unique_ptr<MaintainCount<uint64_t>> maintainRunningSubstitutions; std::unique_ptr<MaintainCount<uint64_t>> maintainRunningSubstitutions;
struct DownloadState struct DownloadState

View file

@ -214,9 +214,7 @@ void PathSubstitutionGoal::tryToRun()
outPipe.create(); outPipe.create();
promise = std::promise<void>(); thr = std::async(std::launch::async, [this]() {
thr = std::thread([this]() {
auto & fetchPath = subPath ? *subPath : storePath; auto & fetchPath = subPath ? *subPath : storePath;
try { try {
ReceiveInterrupts receiveInterrupts; ReceiveInterrupts receiveInterrupts;
@ -230,16 +228,12 @@ void PathSubstitutionGoal::tryToRun()
copyStorePath( copyStorePath(
*sub, worker.store, fetchPath, repair, sub->isTrusted ? NoCheckSigs : CheckSigs *sub, worker.store, fetchPath, repair, sub->isTrusted ? NoCheckSigs : CheckSigs
); );
promise.set_value();
} catch (const EndOfFile &) { } catch (const EndOfFile &) {
promise.set_exception(std::make_exception_ptr(EndOfFile( throw EndOfFile(
"NAR for '%s' fetched from '%s' is incomplete", "NAR for '%s' fetched from '%s' is incomplete",
sub->printStorePath(fetchPath), sub->printStorePath(fetchPath),
sub->getUri() sub->getUri()
))); );
} catch (...) {
promise.set_exception(std::current_exception());
} }
}); });
@ -253,11 +247,10 @@ void PathSubstitutionGoal::finished()
{ {
trace("substitute finished"); trace("substitute finished");
thr.join();
worker.childTerminated(this); worker.childTerminated(this);
try { try {
promise.get_future().get(); thr.get();
} catch (std::exception & e) { } catch (std::exception & e) {
printError(e.what()); printError(e.what());
@ -316,9 +309,9 @@ void PathSubstitutionGoal::handleEOF(int fd)
void PathSubstitutionGoal::cleanup() void PathSubstitutionGoal::cleanup()
{ {
try { try {
if (thr.joinable()) { if (thr.valid()) {
// FIXME: signal worker thread to quit. // FIXME: signal worker thread to quit.
thr.join(); thr.get();
worker.childTerminated(this); worker.childTerminated(this);
} }

View file

@ -50,9 +50,7 @@ struct PathSubstitutionGoal : public Goal
/** /**
* The substituter thread. * The substituter thread.
*/ */
std::thread thr; std::future<void> thr;
std::promise<void> promise;
/** /**
* Whether to try to repair a valid path. * Whether to try to repair a valid path.