Skip to content
Open
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
14 changes: 12 additions & 2 deletions include/exec/async_scope.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -674,8 +674,18 @@ namespace experimental::execution
public:
using sender_concept = STDEXEC::sender_tag;

__future(__future&&) = default;
auto operator=(__future&&) -> __future& = default;
__future(__future&&) = default;
auto operator=(__future&& __other) noexcept -> __future&
{
if (this != &__other)
{
// Apply the existing destructor's pending-state ownership transfer
// to the old future instead of deleting its unique_ptr directly.
__future __old{std::move(*this)};
__state_ = std::move(__other.__state_);
}
return *this;
}

~__future() noexcept
{
Expand Down
1 change: 1 addition & 0 deletions test/exec/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

set(exec_test_sources
../test_main.cpp
test_spawn_future_move_assignment.cpp
test_bwos_lifo_queue.cpp
test_atomic_intrusive_queue.cpp
test_any_sender.cpp
Expand Down
72 changes: 72 additions & 0 deletions test/exec/test_spawn_future_move_assignment.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Licensed under the Apache License, Version 2.0 with LLVM Exceptions.
* See https://llvm.org/LICENSE.txt for license information.
*/

#include <catch2/catch_test_macros.hpp>
#include <cstdio>
#include <exec/async_scope.hpp>
#include <optional>
#include <stdexec/execution.hpp>
#include <string_view>
#include <utility>

namespace regression_spawn_future_move_assignment
{
// Public APIs only, unmodified production headers. Both futures have already
// started their schedule operations, which are still queued in the run_loop.

int run_case(int argc, char** argv)
{
auto mode = argc > 1 ? std::string_view(argv[1]) : std::string_view("assign");
exec::async_scope scope;
STDEXEC::run_loop loop;
auto task = STDEXEC::schedule(loop.get_scheduler());
using future = decltype(scope.spawn_future(task));
std::optional<future> first{scope.spawn_future(task)};
auto second = scope.spawn_future(task);
if (mode == "completed")
{
loop.finish();
loop.run();
}
if (mode == "drop")
first.reset();
else
*first = std::move(second);
std::fputs("future replacement/drop returned; draining queued operations\n", stderr);
if (mode != "completed")
{
loop.finish();
loop.run();
}
STDEXEC::sync_wait(scope.on_empty());
std::printf("PASS mode=%.*s scope drained\n", int(mode.size()), mode.data());
return 0;
}

} // namespace regression_spawn_future_move_assignment

TEST_CASE("spawn_future_move_assignment: assign", "[regression][async-scope-future]")
{
char program[] = "regression";
char arg0[] = "assign";
char* argv[] = {program, arg0};
REQUIRE(regression_spawn_future_move_assignment::run_case(2, argv) == 0);
}

TEST_CASE("spawn_future_move_assignment: drop", "[regression][async-scope-future]")
{
char program[] = "regression";
char arg0[] = "drop";
char* argv[] = {program, arg0};
REQUIRE(regression_spawn_future_move_assignment::run_case(2, argv) == 0);
}

TEST_CASE("spawn_future_move_assignment: completed", "[regression][async-scope-future]")
{
char program[] = "regression";
char arg0[] = "completed";
char* argv[] = {program, arg0};
REQUIRE(regression_spawn_future_move_assignment::run_case(2, argv) == 0);
}
Loading