nix-super/src/libstore/build/goal.hh

110 lines
2.6 KiB
C++
Raw Normal View History

2020-10-11 19:17:24 +03:00
#pragma once
2020-10-12 20:16:00 +03:00
#include "types.hh"
#include "store-api.hh"
namespace nix {
/* Forward definition. */
2020-10-12 20:16:00 +03:00
struct Goal;
class Worker;
/* A pointer to a goal. */
2014-03-30 01:49:23 +02:00
typedef std::shared_ptr<Goal> GoalPtr;
typedef std::weak_ptr<Goal> WeakGoalPtr;
struct CompareGoalPtrs {
2017-12-11 20:05:14 +02:00
bool operator() (const GoalPtr & a, const GoalPtr & b) const;
};
/* Set of goals. */
2022-02-21 17:28:23 +02:00
typedef std::set<GoalPtr, CompareGoalPtrs> Goals;
typedef std::set<WeakGoalPtr, std::owner_less<WeakGoalPtr>> WeakGoals;
/* A map of paths to goals (and the other way around). */
typedef std::map<StorePath, WeakGoalPtr> WeakGoalMap;
struct Goal : public std::enable_shared_from_this<Goal>
{
typedef enum {ecBusy, ecSuccess, ecFailed, ecNoSubstituters, ecIncompleteClosure} ExitCode;
2012-07-27 16:59:18 +03:00
/* Backlink to the worker. */
Worker & worker;
/* Goals that this goal is waiting for. */
Goals waitees;
/* Goals waiting for this one to finish. Must use weak pointers
here to prevent cycles. */
WeakGoals waiters;
/* Number of goals we are/were waiting for that have failed. */
unsigned int nrFailed;
/* Number of substitution goals we are/were waiting for that
failed because there are no substituters. */
unsigned int nrNoSubstituters;
/* Number of substitution goals we are/were waiting for that
2020-10-18 15:26:23 +03:00
failed because they had unsubstitutable references. */
unsigned int nrIncompleteClosure;
/* Name of this goal for debugging purposes. */
std::string name;
/* Whether the goal is finished. */
ExitCode exitCode;
/* Exception containing an error message, if any. */
std::optional<Error> ex;
Goal(Worker & worker) : worker(worker)
{
nrFailed = nrNoSubstituters = nrIncompleteClosure = 0;
exitCode = ecBusy;
}
virtual ~Goal()
{
trace("goal destroyed");
}
virtual void work() = 0;
void addWaitee(GoalPtr waitee);
virtual void waiteeDone(GoalPtr waitee, ExitCode result);
virtual void handleChildOutput(int fd, std::string_view data)
{
abort();
}
virtual void handleEOF(int fd)
2004-06-29 12:41:50 +03:00
{
abort();
}
void trace(const FormatOrString & fs);
std::string getName()
{
return name;
}
2012-07-27 16:59:18 +03:00
/* Callback in case of a timeout. It should wake up its waiters,
get rid of any running child processes that are being monitored
by the worker (important!), etc. */
virtual void timedOut(Error && ex) = 0;
virtual std::string key() = 0;
void amDone(ExitCode result, std::optional<Error> ex = {});
virtual void cleanup() { }
};
2020-10-11 19:17:24 +03:00
void addToWeakGoals(WeakGoals & goals, GoalPtr p);
}