Nix 2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Loading...
Searching...
No Matches
closure.hh
Go to the documentation of this file.
1#pragma once
3
4#include <set>
5#include <future>
6#include "nix/util/fun.hh"
7#include "nix/util/sync.hh"
8#include "nix/util/async.hh"
9#include "nix/util/ref.hh"
10
11#include <boost/asio/io_context.hpp>
12#include <boost/asio/post.hpp>
13#include <boost/asio/strand.hpp>
14#include <boost/asio/detached.hpp>
15#include <boost/asio/awaitable.hpp>
16#include <boost/asio/co_spawn.hpp>
17#include <boost/asio/bind_executor.hpp>
18#include <boost/asio/executor_work_guard.hpp>
19
20#include <queue>
21#include <set>
22
23namespace nix {
24
25template<typename T>
26using GetEdgesAsync = fun<asio::awaitable<std::set<T>>(const T & elt)>;
27
28template<typename T, typename CompletionToken>
29auto computeClosure(std::set<T> startElts, std::set<T> & res, GetEdgesAsync<T> getEdges, CompletionToken token)
30{
31 auto initiator = [&res, startElts = std::move(startElts), getEdges = std::move(getEdges)](auto handler) {
32 auto executor = asio::make_strand(asio::get_associated_executor(handler));
33
34 using Executor = decltype(executor);
35 using Handler = decltype(handler);
36
37 /* Hand-rolled dynamic async graph traversal. ASIO/Cobalt and standard
38 * C++ will get channels and task groups at some point, but this will
39 * have to suffice for now. */
40 struct State : std::enable_shared_from_this<State>
41 {
42 Executor executor;
43 GetEdgesAsync<T> getEdges;
44 Handler handler;
45 std::set<T> & res;
50 asio::executor_work_guard<decltype(State::executor)> workGuard;
51 std::size_t pending = 0;
55 std::size_t inFlight = 0;
59 std::size_t maxConcurrent = 1024;
63 std::queue<T> todo;
68 std::exception_ptr error;
69
70 State(Executor executor_, GetEdgesAsync<T> getEdges, Handler handler, std::set<T> & res)
71 : executor(executor_)
72 , getEdges(std::move(getEdges))
73 , handler(std::move(handler))
74 , res(res)
75 , workGuard(asio::make_work_guard(executor_))
76 {
77 }
78
79 void complete(std::exception_ptr ex)
80 {
81 workGuard.reset(); /* We are done and we can release the lock. */
82 asio::post(executor, [state = this->shared_from_this(), ex] { state->handler(ex); });
83 }
84
85 void fail(std::exception_ptr ex)
86 {
87 if (!error)
88 error = ex;
89 }
90
91 void enqueue(const std::set<T> & elts)
92 {
93 for (const auto & elt : elts)
94 enqueue(elt);
95 }
96
97 void spawnWorker(const T & elt)
98 {
99 ++inFlight;
100 asio::post(executor, [state = this->shared_from_this(), elt] {
101 asio::co_spawn(
102 state->executor,
103 [state, elt]() -> asio::awaitable<void> {
104 try {
105 state->enqueue(co_await state->getEdges(elt));
106 } catch (...) {
107 state->fail(std::current_exception());
108 }
109 state->onWorkDone();
110 },
111 asio::detached);
112 });
113 }
114
115 void onWorkDone()
116 {
117 --inFlight;
118 --pending;
119
120 if (!todo.empty() && !error) {
121 auto next = std::move(todo.front());
122 todo.pop();
123 asio::post(executor, [state = this->shared_from_this(), next = std::move(next)]() mutable {
124 state->spawnWorker(next);
125 });
126 } else {
127 if (error) {
128 /* Discard all remaining work in case we failed. */
129 pending -= todo.size();
130 todo = {};
131 }
132
133 if (pending == 0)
134 complete(error);
135 }
136 }
137
138 void enqueue(const T & elt)
139 {
140 if (error)
141 return;
142
143 if (!res.insert(elt).second)
144 return;
145
146 ++pending;
147
148 if (inFlight < maxConcurrent) {
149 spawnWorker(elt);
150 } else {
151 todo.push(elt);
152 }
153 }
154 };
155
156 auto state = make_ref<State>(executor, std::move(getEdges), std::move(handler), res);
157 if (startElts.empty()) {
158 /* No work to do. */
159 state->complete(std::exception_ptr{});
160 return;
161 }
162
163 asio::post(executor, [state, startElts = std::move(startElts)] { state->enqueue(startElts); });
164 };
165
166 return asio::async_initiate<CompletionToken, void(std::exception_ptr)>(std::move(initiator), token);
167}
168
169template<typename T>
170void computeClosure(std::set<T> startElts, std::set<T> & res, GetEdgesAsync<T> getEdges)
171{
172 asio::io_context ctx;
173 std::exception_ptr ex = nullptr;
175 std::move(startElts),
176 res,
177 std::move(getEdges),
178 asio::bind_executor(ctx.get_executor(), [&](std::exception_ptr ex2) { ex = ex2; }));
179 ctx.run();
180 if (ex)
181 std::rethrow_exception(ex);
182}
183
184} // namespace nix
Definition fun.hh:19
auto computeClosure(std::set< T > startElts, std::set< T > &res, GetEdgesAsync< T > getEdges, CompletionToken token)
Definition closure.hh:29