1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-24 22:11:15 +02:00

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.
This commit is contained in:
John Ericson 2025-05-15 16:21:02 -04:00
parent 0d3750e902
commit 7bd9eef772
2 changed files with 15 additions and 16 deletions

View file

@ -41,6 +41,16 @@ Worker::~Worker()
assert(expectedNarSize == 0);
}
template<class G, typename... Args>
std::shared_ptr<G> Worker::initGoalIfNeeded(std::weak_ptr<G> & goal_weak, Args && ...args)
{
if (auto goal = goal_weak.lock()) return goal;
auto goal = std::make_shared<G>(args...);
goal_weak = goal;
wakeUp(goal);
return goal;
}
std::shared_ptr<DerivationGoal> Worker::makeDerivationGoalCommon(
const StorePath & drvPath,
@ -79,27 +89,13 @@ std::shared_ptr<DerivationGoal> Worker::makeBasicDerivationGoal(const StorePath
std::shared_ptr<PathSubstitutionGoal> Worker::makePathSubstitutionGoal(const StorePath & path, RepairFlag repair, std::optional<ContentAddress> ca)
{
std::weak_ptr<PathSubstitutionGoal> & goal_weak = substitutionGoals[path];
auto goal = goal_weak.lock(); // FIXME
if (!goal) {
goal = std::make_shared<PathSubstitutionGoal>(path, *this, repair, ca);
goal_weak = goal;
wakeUp(goal);
}
return goal;
return initGoalIfNeeded(substitutionGoals[path], path, *this, repair, ca);
}
std::shared_ptr<DrvOutputSubstitutionGoal> Worker::makeDrvOutputSubstitutionGoal(const DrvOutput& id, RepairFlag repair, std::optional<ContentAddress> ca)
{
std::weak_ptr<DrvOutputSubstitutionGoal> & goal_weak = drvOutputSubstitutionGoals[id];
auto goal = goal_weak.lock(); // FIXME
if (!goal) {
goal = std::make_shared<DrvOutputSubstitutionGoal>(id, *this, repair, ca);
goal_weak = goal;
wakeUp(goal);
}
return goal;
return initGoalIfNeeded(drvOutputSubstitutionGoals[id], id, *this, repair, ca);
}

View file

@ -196,6 +196,9 @@ public:
* @ref DerivationGoal "derivation goal"
*/
private:
template<class G, typename... Args>
std::shared_ptr<G> initGoalIfNeeded(std::weak_ptr<G> & goal_weak, Args && ...args);
std::shared_ptr<DerivationGoal> makeDerivationGoalCommon(
const StorePath & drvPath, const OutputsSpec & wantedOutputs,
std::function<std::shared_ptr<DerivationGoal>()> mkDrvGoal);