Nix 2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Loading...
Searching...
No Matches
goal-impl.hh
1#pragma once
4
5namespace nix {
6
7template<typename T>
8auto Goal::AwaitableFrameBase::await_transform(AsyncCallback<T> && ac)
9{
10 struct Awaiter
11 {
12 fun<void(Callback<T>)> fn;
13 std::shared_ptr<std::promise<T>> promise;
14
15 bool await_ready()
16 {
17 return false;
18 }
19
20 void await_suspend(std::coroutine_handle<> handle)
21 {
22 auto h = HandleTypeBase::from_address(handle.address());
23 auto goal = h.promise().goal;
24 promise = std::make_shared<std::promise<T>>();
25
26 fn(Callback<T>([promise = promise,
27 maybeWaker = goal->worker.getCrossThreadWaker(),
28 goalWeak = goal->weak_from_this()](std::future<T> res) {
29 try {
30 promise->set_value(res.get());
31 } catch (...) {
32 promise->set_exception(std::current_exception());
33 }
34
35 /* The Worker might have already died (and the waker with it) by
36 the time the callback fired. */
37 if (auto waker = maybeWaker.lock())
38 waker->enqueue(goalWeak);
39 }));
40
41 goal->worker.waitForCompletion(goal->shared_from_this());
42 }
43
44 T await_resume()
45 {
46 return promise->get_future().get();
47 }
48 };
49
50 return Awaiter{.fn = std::move(ac.fn)};
51}
52
53} // namespace nix