From 7bd9eef772540c37c7a1681c4fa2ed2493906484 Mon Sep 17 00:00:00 2001 From: John Ericson Date: Thu, 15 May 2025 16:21:02 -0400 Subject: [PATCH] Deduplicate the goal creation functions The weak reference logic is the same in both these cases, and if/when I get rid `addWantedOutputs`, also in the `DerivationGoal` case. --- src/libstore/build/worker.cc | 28 ++++++++----------- .../include/nix/store/build/worker.hh | 3 ++ 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/src/libstore/build/worker.cc b/src/libstore/build/worker.cc index dd3692f41..6b1e3c0f4 100644 --- a/src/libstore/build/worker.cc +++ b/src/libstore/build/worker.cc @@ -41,6 +41,16 @@ Worker::~Worker() assert(expectedNarSize == 0); } +template +std::shared_ptr Worker::initGoalIfNeeded(std::weak_ptr & goal_weak, Args && ...args) +{ + if (auto goal = goal_weak.lock()) return goal; + + auto goal = std::make_shared(args...); + goal_weak = goal; + wakeUp(goal); + return goal; +} std::shared_ptr Worker::makeDerivationGoalCommon( const StorePath & drvPath, @@ -79,27 +89,13 @@ std::shared_ptr Worker::makeBasicDerivationGoal(const StorePath std::shared_ptr Worker::makePathSubstitutionGoal(const StorePath & path, RepairFlag repair, std::optional ca) { - std::weak_ptr & goal_weak = substitutionGoals[path]; - auto goal = goal_weak.lock(); // FIXME - if (!goal) { - goal = std::make_shared(path, *this, repair, ca); - goal_weak = goal; - wakeUp(goal); - } - return goal; + return initGoalIfNeeded(substitutionGoals[path], path, *this, repair, ca); } std::shared_ptr Worker::makeDrvOutputSubstitutionGoal(const DrvOutput& id, RepairFlag repair, std::optional ca) { - std::weak_ptr & goal_weak = drvOutputSubstitutionGoals[id]; - auto goal = goal_weak.lock(); // FIXME - if (!goal) { - goal = std::make_shared(id, *this, repair, ca); - goal_weak = goal; - wakeUp(goal); - } - return goal; + return initGoalIfNeeded(drvOutputSubstitutionGoals[id], id, *this, repair, ca); } diff --git a/src/libstore/include/nix/store/build/worker.hh b/src/libstore/include/nix/store/build/worker.hh index 7e03a0c2f..adbaa4027 100644 --- a/src/libstore/include/nix/store/build/worker.hh +++ b/src/libstore/include/nix/store/build/worker.hh @@ -196,6 +196,9 @@ public: * @ref DerivationGoal "derivation goal" */ private: + template + std::shared_ptr initGoalIfNeeded(std::weak_ptr & goal_weak, Args && ...args); + std::shared_ptr makeDerivationGoalCommon( const StorePath & drvPath, const OutputsSpec & wantedOutputs, std::function()> mkDrvGoal);