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
19 changes: 5 additions & 14 deletions include/beman/execution/detail/parallel_scheduler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import beman.execution.detail.inplace_stop_source;
import beman.execution.detail.operation_state;
import beman.execution.detail.psched_bulk_sender;
import beman.execution.detail.parallel_scheduler_replacement;
import beman.execution.detail.rcvr_proxy_base;
import beman.execution.detail.receiver;
import beman.execution.detail.scheduler_tag;
import beman.execution.detail.sender;
Expand All @@ -58,6 +59,7 @@ import beman.execution.detail.stop_token_of_t;
#include <beman/execution/detail/operation_state.hpp>
#include <beman/execution/detail/psched_bulk_sender.hpp>
#include <beman/execution/detail/parallel_scheduler_replacement.hpp>
#include <beman/execution/detail/rcvr_proxy_base.hpp>
#include <beman/execution/detail/receiver.hpp>
#include <beman/execution/detail/scheduler_tag.hpp>
#include <beman/execution/detail/sender.hpp>
Expand Down Expand Up @@ -163,27 +165,16 @@ class parallel_scheduler::sender {
};

template <typename Rcvr>
struct rcvr_proxy : ::beman::execution::parallel_scheduler_replacement::receiver_proxy {
using stop_token_t = ::beman::execution::stop_token_of_t<::beman::execution::env_of_t<Rcvr>>;
struct rcvr_proxy : ::beman::execution::detail::rcvr_proxy_base<Rcvr, false> {
using base = ::beman::execution::detail::rcvr_proxy_base<Rcvr, false>;

explicit rcvr_proxy(Rcvr rcvr) noexcept
: sp_(::beman::execution::get_stop_token(::beman::execution::get_env(rcvr))), rcvr_(::std::move(rcvr)) {}
explicit rcvr_proxy(Rcvr rcvr) noexcept : base(::std::move(rcvr)) {}

auto set_value() noexcept -> void final { ::beman::execution::set_value(::std::move(this->rcvr_)); }

auto set_error(::std::exception_ptr error) noexcept -> void final {
::beman::execution::set_error(::std::move(this->rcvr_), ::std::move(error));
}

auto set_stopped() noexcept -> void final { ::beman::execution::set_stopped(::std::move(this->rcvr_)); }

private:
auto query_stop_token() const noexcept -> ::std::optional<::beman::execution::inplace_stop_token> final {
return sp_.get_token();
}

[[BEMAN_EXECUTION_NO_UNIQUE_ADDRESS]] ::beman::execution::detail::stop_propagator<stop_token_t> sp_;
Rcvr rcvr_;
};

template <::beman::execution::receiver Rcvr>
Expand Down
50 changes: 21 additions & 29 deletions include/beman/execution/detail/psched_bulk_sender.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import beman.execution.detail.completion_signatures;
import beman.execution.detail.decayed_tuple;
import beman.execution.detail.env_of_t;
import beman.execution.detail.execution_policy;
import beman.execution.detail.fwd_env;
import beman.execution.detail.get_completion_signatures;
import beman.execution.detail.get_env;
import beman.execution.detail.get_stop_token;
Expand All @@ -36,6 +37,7 @@ import beman.execution.detail.meta.prepend;
import beman.execution.detail.meta.unique;
import beman.execution.detail.operation_state;
import beman.execution.detail.parallel_scheduler_replacement;
import beman.execution.detail.rcvr_proxy_base;
import beman.execution.detail.receiver;
import beman.execution.detail.remap_attrs;
import beman.execution.detail.sender;
Expand All @@ -54,6 +56,7 @@ import beman.execution.detail.value_types_of_t;
#include <beman/execution/detail/decayed_tuple.hpp>
#include <beman/execution/detail/env_of_t.hpp>
#include <beman/execution/detail/execution_policy.hpp>
#include <beman/execution/detail/fwd_env.hpp>
#include <beman/execution/detail/get_completion_signatures.hpp>
#include <beman/execution/detail/get_env.hpp>
#include <beman/execution/detail/get_stop_token.hpp>
Expand All @@ -63,6 +66,7 @@ import beman.execution.detail.value_types_of_t;
#include <beman/execution/detail/meta_unique.hpp>
#include <beman/execution/detail/operation_state.hpp>
#include <beman/execution/detail/parallel_scheduler_replacement.hpp>
#include <beman/execution/detail/rcvr_proxy_base.hpp>
#include <beman/execution/detail/receiver.hpp>
#include <beman/execution/detail/remap_attrs.hpp>
#include <beman/execution/detail/sender.hpp>
Expand All @@ -89,20 +93,18 @@ struct psched_bulk_sender {
static constexpr bool is_parallel_policy = ::std::same_as<Policy, ::beman::execution::parallel_policy> ||
::std::same_as<Policy, ::beman::execution::parallel_unsequenced_policy>;
template <typename Rcvr>
struct rcvr_proxy : ::beman::execution::parallel_scheduler_replacement::bulk_item_receiver_proxy {
struct rcvr_proxy : ::beman::execution::detail::rcvr_proxy_base<Rcvr, true> {
using base = ::beman::execution::detail::rcvr_proxy_base<Rcvr, true>;
using result_type = ::beman::execution::detail::meta::
prepend<::std::monostate, ::beman::execution::value_types_of_t<Child, ::beman::execution::env_of_t<Rcvr>>>;
using stop_token_t = ::beman::execution::stop_token_of_t<::beman::execution::env_of_t<Rcvr>>;

rcvr_proxy(::std::shared_ptr<backend_type> backend, Rcvr rcvr, Policy policy, Shape shape, Fn fn) noexcept
: backend(::std::move(backend)),
sp(::beman::execution::get_stop_token(::beman::execution::get_env(rcvr))),
rcvr(::std::move(rcvr)),
policy(::std::move(policy)),
shape(::std::move(shape)),
fn(::std::move(fn)) {}
rcvr_proxy(::std::shared_ptr<backend_type> backend, Rcvr rcvr, Shape shape, Fn fn) noexcept
: base(::std::move(rcvr)), backend(::std::move(backend)), shape(::std::move(shape)), fn(::std::move(fn)) {}

auto get_env() const noexcept { return ::beman::execution::get_env(rcvr); }
auto get_env() const noexcept {
return ::beman::execution::detail::fwd_env(::beman::execution::get_env(this->rcvr_));
}

auto execute(::std::size_t begin, ::std::size_t end) noexcept -> void final {
assert(begin < end);
Expand Down Expand Up @@ -144,7 +146,7 @@ struct psched_bulk_sender {
return;
}
const auto call_set_value = [this]<typename... Args>(Args&&... args) {
::beman::execution::set_value(::std::move(rcvr), ::std::forward<Args>(args)...);
::beman::execution::set_value(::std::move(this->rcvr_), ::std::forward<Args>(args)...);
};
BEMAN_EXECUTION_TRY {
::std::visit(
Expand All @@ -162,29 +164,19 @@ struct psched_bulk_sender {

template <typename E>
auto set_error(E e) noexcept -> void {
::beman::execution::set_error(::std::move(rcvr), ::std::move(e));
::beman::execution::set_error(::std::move(this->rcvr_), ::std::move(e));
}

auto set_error(::std::exception_ptr e) noexcept -> void final { this->set_error<>(::std::move(e)); }

auto set_stopped() noexcept -> void final { ::beman::execution::set_stopped(::std::move(rcvr)); }

private:
auto query_stop_token() const noexcept -> ::std::optional<::beman::execution::inplace_stop_token> final {
return sp.get_token();
}

public:
alignas(psched_storage_alignment)::std::byte storage[psched_storage_size];
::std::shared_ptr<backend_type> backend;
result_type result;
[[BEMAN_EXECUTION_NO_UNIQUE_ADDRESS]] ::beman::execution::detail::stop_propagator<stop_token_t> sp;
Rcvr rcvr;
Policy policy;
Shape shape;
Fn fn;
::std::atomic<bool> has_exception{false};
::std::exception_ptr exception;
::std::shared_ptr<backend_type> backend;
result_type result;
Shape shape;
Fn fn;
::std::atomic<bool> has_exception{false};
::std::exception_ptr exception;
};

template <typename Rcvr>
Expand Down Expand Up @@ -227,8 +219,8 @@ struct psched_bulk_sender {
};
using sub_state_t = ::beman::execution::connect_result_t<Child, receiver_ref>;

state(::std::shared_ptr<backend_type> backend, Child child, Rcvr rcvr, Policy policy, Shape shape, Fn fn)
: proxy(::std::move(backend), ::std::move(rcvr), ::std::move(policy), ::std::move(shape), ::std::move(fn)),
state(::std::shared_ptr<backend_type> backend, Child child, Rcvr rcvr, const Policy&, Shape shape, Fn fn)
: proxy(::std::move(backend), ::std::move(rcvr), ::std::move(shape), ::std::move(fn)),
sub_state(::beman::execution::connect(::std::move(child), receiver_ref{&proxy})) {}

auto start() & noexcept -> void { ::beman::execution::start(sub_state); }
Expand Down
89 changes: 89 additions & 0 deletions include/beman/execution/detail/rcvr_proxy_base.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// include/beman/execution/detail/rcvr_proxy_base.hpp -*-C++-*-
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#ifndef INCLUDED_BEMAN_EXECUTION_DETAIL_RCVR_PROXY_BASE
#define INCLUDED_BEMAN_EXECUTION_DETAIL_RCVR_PROXY_BASE

#include <beman/execution/detail/common.hpp>
#ifdef BEMAN_HAS_IMPORT_STD
import std;
#else
#include <optional>
#include <type_traits>
#include <utility>
#endif
#ifdef BEMAN_HAS_MODULES
import beman.execution.detail.env_of_t;
import beman.execution.detail.get_env;
import beman.execution.detail.get_stop_token;
import beman.execution.detail.inplace_stop_source;
import beman.execution.detail.parallel_scheduler_replacement;
import beman.execution.detail.stop_propagator;
import beman.execution.detail.stop_token_of_t;
import beman.execution.detail.set_stopped;
import beman.execution.detail.unreachable;
import beman.execution.detail.unstoppable_token;
#else
#include <beman/execution/detail/env_of_t.hpp>
#include <beman/execution/detail/get_env.hpp>
#include <beman/execution/detail/get_stop_token.hpp>
#include <beman/execution/detail/inplace_stop_source.hpp>
#include <beman/execution/detail/parallel_scheduler_replacement.hpp>
#include <beman/execution/detail/stop_propagator.hpp>
#include <beman/execution/detail/stop_token_of_t.hpp>
#include <beman/execution/detail/set_stopped.hpp>
#include <beman/execution/detail/unreachable.hpp>
#include <beman/execution/detail/unstoppable_token.hpp>
#endif

// ----------------------------------------------------------------------------

namespace beman::execution::detail {
template <bool Bulk>
using select_rcvr_proxy_base =
::std::conditional_t<Bulk,
::beman::execution::parallel_scheduler_replacement::bulk_item_receiver_proxy,
::beman::execution::parallel_scheduler_replacement::receiver_proxy>;

template <typename Rcvr, bool Bulk>
struct rcvr_proxy_base : select_rcvr_proxy_base<Bulk> {
using stop_token_t = ::beman::execution::stop_token_of_t<::beman::execution::env_of_t<Rcvr>>;

explicit rcvr_proxy_base(Rcvr rcvr) noexcept
: sp_(::beman::execution::get_stop_token(::beman::execution::get_env(rcvr))), rcvr_(::std::move(rcvr)) {}

auto set_stopped() noexcept -> void final { ::beman::execution::set_stopped(::std::move(this->rcvr_)); }

private:
auto query_stop_token() const noexcept -> ::std::optional<::beman::execution::inplace_stop_token> final {
return sp_.get_token();
}

protected:
::beman::execution::detail::stop_propagator<stop_token_t> sp_;
Rcvr rcvr_;
};

template <typename Rcvr, bool Bulk>
requires ::beman::execution::unstoppable_token<
::beman::execution::stop_token_of_t<::beman::execution::env_of_t<Rcvr>>>
struct rcvr_proxy_base<Rcvr, Bulk> : select_rcvr_proxy_base<Bulk> {
using stop_token_t = ::beman::execution::stop_token_of_t<::beman::execution::env_of_t<Rcvr>>;

explicit rcvr_proxy_base(Rcvr rcvr) noexcept : rcvr_(::std::move(rcvr)) {}

auto set_stopped() noexcept -> void final { ::beman::execution::detail::unreachable(); }

private:
auto query_stop_token() const noexcept -> ::std::optional<::beman::execution::inplace_stop_token> final {
return ::std::nullopt;
}

protected:
Rcvr rcvr_;
};
} // namespace beman::execution::detail

// ----------------------------------------------------------------------------

#endif // INCLUDED_BEMAN_EXECUTION_DETAIL_RCVR_PROXY_BASE
104 changes: 104 additions & 0 deletions include/beman/execution/detail/stop_combiner.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// include/beman/execution/detail/stop_combiner.hpp -*-C++-*-
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#ifndef INCLUDED_BEMAN_EXECUTION_DETAIL_STOP_COMBINER
#define INCLUDED_BEMAN_EXECUTION_DETAIL_STOP_COMBINER

#include <beman/execution/detail/common.hpp>
#ifdef BEMAN_HAS_IMPORT_STD
import std;
#else
#include <atomic>
#include <functional>
#include <memory>
#include <type_traits>
#include <utility>
#endif
#ifdef BEMAN_HAS_MODULES
import beman.execution.detail.stoppable_token;
import beman.execution.detail.stop_callback_for_t;
#else
#include <beman/execution/detail/stoppable_token.hpp>
#include <beman/execution/detail/stop_callback_for_t.hpp>
#endif

// ----------------------------------------------------------------------------

namespace beman::execution::detail {
template <typename Fn>
class stop_callback_once {
public:
stop_callback_once(Fn& fn, ::std::atomic<bool>& flag) noexcept : fn_(::std::addressof(fn)), flag_(&flag) {}

auto operator()() const noexcept -> void {
if (flag_->exchange(false, ::std::memory_order_relaxed)) {
::std::invoke(*fn_);
}
}

private:
Fn* fn_;
::std::atomic<bool>* flag_;
};

template <typename First, typename Second>
class stop_combiner {
static_assert(::beman::execution::stoppable_token<First> && ::beman::execution::stoppable_token<Second>);

public:
template <typename Callback>
class callback_type {
private:
using fn_once_t = ::beman::execution::detail::stop_callback_once<Callback>;

public:
template <typename Initializer>
callback_type(const stop_combiner& combiner, Initializer&& init) noexcept(
::std::is_nothrow_constructible_v<Callback, Initializer> &&
::std::is_nothrow_constructible_v<::beman::execution::stop_callback_for_t<First, fn_once_t>,
First,
fn_once_t> &&
::std::is_nothrow_constructible_v<::beman::execution::stop_callback_for_t<Second, fn_once_t>,
Second,
fn_once_t>)
: fn_(::std::forward<Initializer>(init)),
first_cb_(combiner.first_, fn_once_t{fn_, flag_}),
second_cb_(combiner.second_, fn_once_t{fn_, flag_}) {}

callback_type(const callback_type&) = delete;

~callback_type() = default;

auto operator=(const callback_type&) -> callback_type& = delete;

private:
Callback fn_;
::std::atomic<bool> flag_{true};
::beman::execution::stop_callback_for_t<First, fn_once_t> first_cb_;
::beman::execution::stop_callback_for_t<Second, fn_once_t> second_cb_;
};

public:
stop_combiner(First first, Second second) noexcept(::std::is_nothrow_move_constructible_v<First> &&
::std::is_nothrow_move_constructible_v<Second>)
: first_(::std::move(first)), second_(::std::move(second)) {}

[[nodiscard]] auto stop_possible() const noexcept -> bool {
return first_.stop_possible() || second_.stop_possible();
}

[[nodiscard]] auto stop_requested() const noexcept -> bool {
return first_.stop_requested() || second_.stop_requested();
}

friend auto operator==(const stop_combiner& lhs, const stop_combiner& rhs) -> bool = default;

private:
[[BEMAN_EXECUTION_NO_UNIQUE_ADDRESS]] First first_;
[[BEMAN_EXECUTION_NO_UNIQUE_ADDRESS]] Second second_;
};
} // namespace beman::execution::detail

// ----------------------------------------------------------------------------

#endif // INCLUDED_BEMAN_EXECUTION_DETAIL_STOP_COMBINER
Loading