Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/libexpr/include/nix/expr/parallel-eval.hh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <boost/thread/thread.hpp>

#include "nix/util/move-only-function.hh"
#include "nix/util/sync.hh"
#include "nix/util/logging.hh"
#include "nix/util/environment-variables.hh"
Expand All @@ -21,8 +22,7 @@ namespace nix {

struct Executor
{
// FIXME: support std::moveable_function.
using work_t = std::function<void()>;
using work_t = MoveOnlyFunction<void()>;

struct Item
{
Expand Down Expand Up @@ -84,7 +84,9 @@ struct FutureVector

void spawn(uint8_t prioPrefix, Executor::work_t && work)
{
spawn({{std::move(work), prioPrefix}});
Executor::WorkItems items;
items.emplace_back(std::move(work), prioPrefix);
spawn(std::move(items));
}

void finishAll();
Expand Down
4 changes: 1 addition & 3 deletions src/libexpr/parallel-eval.cc
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,7 @@ static void prim_parallel(EvalState & state, const PosIdx pos, Value ** args, Va
Executor::WorkItems work;
for (auto value : args[0]->listView())
if (!value->isFinished())
state.addWork(work, 0, [value(std::make_shared<RootValue>(value)), &state, pos]() {
state.forceValue(***value, pos);
});
state.addWork(work, 0, [value(RootValue(value)), &state, pos]() { state.forceValue(**value, pos); });
state.executor->spawn(std::move(work));
}

Expand Down
5 changes: 2 additions & 3 deletions src/libexpr/value-to-json.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ static void parallelForceDeep(EvalState & state, Value & v, PosIdx pos)
if (v.attrs()->get(state.s.outPath))
return;
for (auto & a : *v.attrs())
state.addWork(work, 0, [value(std::make_shared<RootValue>(a.value)), pos(a.pos), &state]() {
parallelForceDeep(state, ***value, pos);
});
state.addWork(
work, 0, [value(RootValue(a.value)), pos(a.pos), &state]() { parallelForceDeep(state, **value, pos); });
break;
}

Expand Down
1 change: 1 addition & 0 deletions src/libutil/include/nix/util/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ headers = [ config_pub_h ] + files(
'memo.hh',
'memory-source-accessor.hh',
'mounted-source-accessor.hh',
'move-only-function.hh',
'muxable-pipe.hh',
'nar-accessor.hh',
'nar-cache.hh',
Expand Down
83 changes: 83 additions & 0 deletions src/libutil/include/nix/util/move-only-function.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#pragma once
///@file

#include <functional>
#include <memory>
#include <type_traits>
#include <utility>

namespace nix {

#ifdef __cpp_lib_move_only_function

template<typename Sig>
using MoveOnlyFunction = std::move_only_function<Sig>;

#else

/**
* Fallback implementation of `std::move_only_function` for standard
* libraries that don't provide it yet (e.g. libc++ as of version 21):
* a type-erased callable wrapper that, unlike `std::function`, only
* requires the callable to be move-constructible, so it can hold
* lambdas that capture move-only types.
*/
template<typename Sig>
class MoveOnlyFunction;

template<typename Ret, typename... Args>
class MoveOnlyFunction<Ret(Args...)>
{
struct Base
{
virtual Ret call(Args &&... args) = 0;
virtual ~Base() = default;
};

template<typename F>
struct Impl final : Base
{
F f;

template<typename G>
requires std::is_same_v<std::decay_t<G>, F>
Impl(G && g)
: f(std::forward<G>(g))
{
}

Ret call(Args &&... args) override
{
return f(std::forward<Args>(args)...);
}
};

std::unique_ptr<Base> impl;

public:
MoveOnlyFunction() = default;

template<typename F>
requires(!std::is_same_v<std::decay_t<F>, MoveOnlyFunction> && std::is_invocable_r_v<Ret, F &, Args...>)
MoveOnlyFunction(F && f)
: impl(std::make_unique<Impl<std::decay_t<F>>>(std::forward<F>(f)))
{
}

MoveOnlyFunction(MoveOnlyFunction &&) noexcept = default;
MoveOnlyFunction & operator=(MoveOnlyFunction &&) noexcept = default;

Ret operator()(Args... args)
{
return impl->call(std::forward<Args>(args)...);
}

explicit operator bool() const
{
return (bool) impl;
}
};

#endif

} // namespace nix
Loading