Skip to content

Commit

Permalink
Trying to fix segfault in examples
Browse files Browse the repository at this point in the history
  • Loading branch information
hkaiser committed Jun 27, 2024
1 parent f4292fe commit 9cbe23d
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 24 deletions.
4 changes: 2 additions & 2 deletions libs/core/futures/include/hpx/futures/detail/future_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ namespace hpx::lcos::detail {

virtual ~future_data_refcnt_base();

virtual void set_on_completed(completed_callback_type) = 0;
virtual void set_on_completed(completed_callback_type&&) = 0;

HPX_FORCEINLINE bool requires_delete() noexcept
{
Expand Down Expand Up @@ -313,7 +313,7 @@ namespace hpx::lcos::detail {
// Set the callback which needs to be invoked when the future becomes
// ready. If the future is ready the function will be invoked
// immediately.
void set_on_completed(completed_callback_type data_sink) override;
void set_on_completed(completed_callback_type&& data_sink) override;

virtual state wait(error_code& ec = throws);

Expand Down
2 changes: 1 addition & 1 deletion libs/core/futures/include/hpx/futures/futures_factory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ namespace hpx::lcos::local {
using result_type = typename Base::result_type;
using init_no_addref = typename Base::init_no_addref;

F f_;
std::decay_t<F> f_;

explicit task_object(F const& f)
: f_(f)
Expand Down
20 changes: 8 additions & 12 deletions libs/core/futures/include/hpx/futures/packaged_continuation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace hpx::lcos::detail {

template <typename Future, typename SourceState, typename DestinationState>
HPX_FORCEINLINE void transfer_result(
SourceState&& src, DestinationState const& dest)
SourceState&& src, DestinationState& dest)
{
hpx::detail::try_catch_exception_ptr(
[&]() {
Expand Down Expand Up @@ -88,6 +88,8 @@ namespace hpx::lcos::detail {
}
else
{
hpx::intrusive_ptr<Continuation> cont_(&cont);

hpx::detail::try_catch_exception_ptr(
[&]() {
using inner_shared_state_ptr =
Expand All @@ -109,13 +111,12 @@ namespace hpx::lcos::detail {

// Bind an on_completed handler to this future that will
// transfer its result to the new future.
hpx::intrusive_ptr<Continuation> cont_(&cont);
ptr->execute_deferred();
ptr->set_on_completed(
[inner_state = HPX_MOVE(inner_state),
cont_ = HPX_MOVE(cont_)]() mutable -> void {
return transfer_result<inner_future>(
HPX_MOVE(inner_state), HPX_MOVE(cont_));
HPX_MOVE(inner_state), cont_);
});
},
[&](std::exception_ptr ep) {
Expand Down Expand Up @@ -404,13 +405,6 @@ namespace hpx::lcos::detail {
class unwrap_continuation : public future_data<ContResult>
{
private:
template <typename Inner>
void on_inner_ready(
traits::detail::shared_state_ptr_for_t<Inner>&& inner_state)
{
transfer_result<Inner>(HPX_MOVE(inner_state), this);
}

template <typename Outer>
void on_outer_ready(
traits::detail::shared_state_ptr_for_t<Outer>&& outer_state)
Expand Down Expand Up @@ -443,11 +437,13 @@ namespace hpx::lcos::detail {
}

ptr->execute_deferred();

// attach continuation on inner ready
ptr->set_on_completed(
[this_ = HPX_MOVE(this_),
inner = HPX_MOVE(inner_state)]() mutable -> void {
this_->template on_inner_ready<inner_future>(
HPX_MOVE(inner));
transfer_result<inner_future>(
HPX_MOVE(inner), this_);
});
},
[&](std::exception_ptr ep) {
Expand Down
10 changes: 4 additions & 6 deletions libs/core/futures/src/future_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ namespace hpx::lcos::detail {
{
if (runs_child_ != threads::invalid_thread_id)
{
auto* thrd = get_thread_id_data(runs_child_);
(void) thrd;
[[maybe_unused]] auto* thrd = get_thread_id_data(runs_child_);
LTM_(debug).format(
"task_object::~task_object({}), description({}): "
"destroy runs_as_child thread",
Expand Down Expand Up @@ -284,7 +283,7 @@ namespace hpx::lcos::detail {
hpx::detail::try_catch_exception_ptr(
[&]() {
// clang-format off
constexpr void (*p)(Callback&&) noexcept =
constexpr void (*p)(std::decay_t<Callback>&&) noexcept =
&future_data_base::run_on_completed;
// clang-format on
run_on_completed_on_new_thread(util::deferred_call(
Expand Down Expand Up @@ -318,20 +317,19 @@ namespace hpx::lcos::detail {
// Set the callback which needs to be invoked when the future becomes ready.
// If the future is ready the function will be invoked immediately.
void future_data_base<traits::detail::future_data_void>::set_on_completed(
completed_callback_type data_sink)
completed_callback_type&& data_sink)
{
if (!data_sink)
return;

hpx::intrusive_ptr<future_data_base> this_(this); // keep alive
if (is_ready(std::memory_order_relaxed))
{
// invoke the callback (continuation) function right away
handle_on_completed(HPX_MOVE(data_sink));
}
else
{
hpx::intrusive_ptr<future_data_base> this_(this); // keep alive

std::unique_lock l(mtx_);
if (is_ready())
{
Expand Down
5 changes: 2 additions & 3 deletions libs/full/components/include/hpx/components/client_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,9 +335,8 @@ namespace hpx::components {
{
}
explicit client_base(hpx::future<hpx::id_type>&& f) noexcept
: shared_state_(
hpx::traits::future_access<future_type>::get_shared_state(
HPX_MOVE(f)))
: shared_state_(hpx::traits::future_access<
hpx::future<hpx::id_type>>::get_shared_state(HPX_MOVE(f)))
{
}

Expand Down

0 comments on commit 9cbe23d

Please sign in to comment.