Nix 2.26.3
Nix, the purely functional package manager; unstable internal interfaces
 
Loading...
Searching...
No Matches
goal.hh
Go to the documentation of this file.
1#pragma once
3
4#include "store-api.hh"
5#include "build-result.hh"
6
7#include <coroutine>
8
9namespace nix {
10
14struct Goal;
15class Worker;
16
20typedef std::shared_ptr<Goal> GoalPtr;
21typedef std::weak_ptr<Goal> WeakGoalPtr;
22
24 bool operator() (const GoalPtr & a, const GoalPtr & b) const;
25};
26
30typedef std::set<GoalPtr, CompareGoalPtrs> Goals;
31typedef std::set<WeakGoalPtr, std::owner_less<WeakGoalPtr>> WeakGoals;
32
36typedef std::map<StorePath, WeakGoalPtr> WeakGoalMap;
37
54
55struct Goal : public std::enable_shared_from_this<Goal>
56{
57 typedef enum {ecBusy, ecSuccess, ecFailed, ecNoSubstituters, ecIncompleteClosure} ExitCode;
58
63
68
73 WeakGoals waiters;
74
78 size_t nrFailed = 0;
79
84 size_t nrNoSubstituters = 0;
85
91
95 std::string name;
96
100 ExitCode exitCode = ecBusy;
101
102protected:
107public:
108
113 struct Suspend {};
114
120 struct Return {};
121
126 struct [[nodiscard]] Done {
127 private:
128 Done(){}
129
130 friend Goal;
131 };
132
133 // forward declaration of promise_type, see below
134 struct promise_type;
135
139 using handle_type = std::coroutine_handle<promise_type>;
140
182 struct [[nodiscard]] Co {
187
188 explicit Co(handle_type handle) : handle(handle) {};
189 void operator=(Co&&);
190 Co(Co&& rhs);
191 ~Co();
192
193 bool await_ready() { return false; };
206 std::coroutine_handle<> await_suspend(handle_type handle);
207 void await_resume() {};
208 };
209
220
221 bool await_ready() { return false; };
222 void await_suspend(handle_type handle_) {
223 handle = handle_;
224 }
225 void await_resume() {
226 assert(handle);
227 assert(handle.promise().goal); // goal must be set
228 assert(handle.promise().goal->top_co); // top_co of goal must be set
229 assert(handle.promise().goal->top_co->handle == handle); // top_co of goal must be us
230 }
231 };
232
242 std::optional<Co> continuation;
243
248 Goal* goal = nullptr;
249
254 bool alive = true;
255
260 bool await_ready() noexcept { return false; };
267 std::coroutine_handle<> await_suspend(handle_type h) noexcept;
268 void await_resume() noexcept { assert(false); };
269 };
270
276
283
288 final_awaiter final_suspend() noexcept { return {}; };
289
295
301
315 void return_value(Co&&);
316
321 void unhandled_exception() { throw; };
322
326 Co&& await_transform(Co&& co) { return static_cast<Co&&>(co); }
327
332 std::suspend_always await_transform(Suspend) { return {}; };
333 };
334
342 std::optional<Co> top_co;
343
347 virtual Co init() = 0;
348
353 inline Co init_wrapper();
354
360 Done amDone(ExitCode result, std::optional<Error> ex = {});
361
362 virtual void cleanup() { }
363
374 BuildResult getBuildResult(const DerivedPath &) const;
375
379 std::optional<Error> ex;
380
381 Goal(Worker & worker, DerivedPath path)
383 {
384 // top_co shouldn't have a goal already, should be nullptr.
385 assert(!top_co->handle.promise().goal);
386 // we set it such that top_co can pass it down to its subcoroutines.
387 top_co->handle.promise().goal = this;
388 }
389
390 virtual ~Goal()
391 {
392 trace("goal destroyed");
393 }
394
395 void work();
396
397 void addWaitee(GoalPtr waitee);
398
399 virtual void waiteeDone(GoalPtr waitee, ExitCode result);
400
401 virtual void handleChildOutput(Descriptor fd, std::string_view data)
402 {
403 unreachable();
404 }
405
406 virtual void handleEOF(Descriptor fd)
407 {
408 unreachable();
409 }
410
411 void trace(std::string_view s);
412
413 std::string getName() const
414 {
415 return name;
416 }
417
423 virtual void timedOut(Error && ex) = 0;
424
425 virtual std::string key() = 0;
426
431 virtual JobCategory jobCategory() const = 0;
432};
433
434void addToWeakGoals(WeakGoals & goals, GoalPtr p);
435
436}
437
438template<typename... ArgTypes>
439struct std::coroutine_traits<nix::Goal::Co, ArgTypes...> {
440 using promise_type = nix::Goal::promise_type;
441};
442
Definition worker.hh:65
JobCategory
Definition goal.hh:44
@ Substitution
Definition goal.hh:52
@ Build
Definition goal.hh:48
std::shared_ptr< Goal > GoalPtr
Definition goal.hh:20
std::set< GoalPtr, CompareGoalPtrs > Goals
Definition goal.hh:30
std::map< StorePath, WeakGoalPtr > WeakGoalMap
Definition goal.hh:36
return s
Definition lexer.l:459
std::shared_ptr< T > p
Definition lexer.l:1269
std::variant< std::string, std::string_view > data
Definition lexer.l:177
return fd
Definition lexer.l:2948
Definition build-result.hh:14
Definition goal.hh:23
Definition derived-path.hh:229
Definition goal.hh:182
handle_type handle
Definition goal.hh:186
Definition goal.hh:126
Definition goal.hh:214
handle_type handle
Definition goal.hh:219
Definition goal.hh:120
Definition goal.hh:113
std::coroutine_handle await_suspend(handle_type h) noexcept
Definition goal.cc:31
Definition goal.hh:237
void return_value(Return)
Definition goal.hh:294
Goal * goal
Definition goal.hh:248
void return_value(Done)
Definition goal.hh:300
Co && await_transform(Co &&co)
Definition goal.hh:326
void unhandled_exception()
Definition goal.hh:321
InitialSuspend initial_suspend()
Definition goal.hh:282
std::optional< Co > continuation
Definition goal.hh:242
std::suspend_always await_transform(Suspend)
Definition goal.hh:332
Co get_return_object()
Definition goal.cc:26
final_awaiter final_suspend() noexcept
Definition goal.hh:288
bool alive
Definition goal.hh:254
Definition goal.hh:56
virtual void timedOut(Error &&ex)=0
BuildResult buildResult
Definition goal.hh:106
size_t nrFailed
Definition goal.hh:78
std::string name
Definition goal.hh:95
std::optional< Error > ex
Definition goal.hh:379
std::optional< Co > top_co
Definition goal.hh:342
virtual JobCategory jobCategory() const =0
Hint for the scheduler, which concurrency limit applies.
Co init_wrapper()
Definition goal.hh:443
virtual Co init()=0
Goals waitees
Definition goal.hh:67
std::coroutine_handle< promise_type > handle_type
Definition goal.hh:139
ExitCode exitCode
Definition goal.hh:100
size_t nrIncompleteClosure
Definition goal.hh:90
size_t nrNoSubstituters
Definition goal.hh:84
Done amDone(ExitCode result, std::optional< Error > ex={})
Definition goal.cc:169
Worker & worker
Definition goal.hh:62
WeakGoals waiters
Definition goal.hh:73
BuildResult getBuildResult(const DerivedPath &) const
Definition goal.cc:105