@@ -52,11 +52,12 @@ enum RepairFlag : bool;
5252struct MemorySourceAccessor ;
5353struct MountedSourceAccessor ;
5454struct AsyncPathWriter ;
55+ struct Provenance ;
56+ struct Executor ;
5557
5658namespace eval_cache {
5759class EvalCache ;
5860}
59- struct Executor ;
6061
6162/* *
6263 * Increments a count on construction and decrements on destruction.
@@ -1128,6 +1129,45 @@ private:
11281129 friend class ListBuilder ;
11291130
11301131public:
1132+
1133+ /* *
1134+ * Per-thread evaluation context. This context is propagated to worker threads when a value is evaluated
1135+ * asynchronously.
1136+ */
1137+ struct EvalContext
1138+ {
1139+ std::shared_ptr<const Provenance> provenance;
1140+ };
1141+
1142+ thread_local static EvalContext evalContext;
1143+
1144+ /* *
1145+ * Create a work item that propagates the current evaluation context.
1146+ */
1147+ template <typename T>
1148+ auto makeWork (T && t)
1149+ {
1150+ return [this , t{std::move (t)}, evalContext (evalContext)]() {
1151+ this ->evalContext = evalContext;
1152+ t ();
1153+ };
1154+ }
1155+
1156+ /* *
1157+ * Add a work item to the given work vector that propagates the current evaluation context.
1158+ */
1159+ template <typename WorkItems, typename T>
1160+ void addWork (WorkItems & work, uint8_t priority, T && t)
1161+ {
1162+ work.emplace_back (makeWork (std::move (t)), priority);
1163+ }
1164+
1165+ template <typename FuturesVector, typename T>
1166+ void spawn (FuturesVector & futures, uint8_t priority, T && t)
1167+ {
1168+ futures.spawn (priority, makeWork (std::move (t)));
1169+ }
1170+
11311171 /* *
11321172 * Worker threads manager.
11331173 *
@@ -1173,6 +1213,24 @@ SourcePath resolveExprPath(SourcePath path, bool addDefaultNix = true);
11731213 */
11741214bool isAllowedURI (std::string_view uri, const Strings & allowedPaths);
11751215
1216+ struct PushProvenance
1217+ {
1218+ EvalState & state;
1219+ std::shared_ptr<const Provenance> prev;
1220+
1221+ PushProvenance (EvalState & state, std::shared_ptr<const Provenance> prov)
1222+ : state(state)
1223+ {
1224+ state.evalContext .provenance .swap (prev);
1225+ state.evalContext .provenance .swap (prov);
1226+ }
1227+
1228+ ~PushProvenance ()
1229+ {
1230+ state.evalContext .provenance .swap (prev);
1231+ }
1232+ };
1233+
11761234} // namespace nix
11771235
11781236#include " nix/expr/eval-inline.hh"
0 commit comments