diff --git a/include/exec/thread_pool_base.hpp b/include/exec/thread_pool_base.hpp index 935e2be5f..d25007a40 100644 --- a/include/exec/thread_pool_base.hpp +++ b/include/exec/thread_pool_base.hpp @@ -728,7 +728,8 @@ namespace experimental::execution bool has_started_{false}; __intrusive_queue<&_task_base::next_> tasks_{}; std::size_t tasks_size_{}; - __std::atomic countdown_{std::ranges::size(range_)}; + // Keep start alive until all children have been started and enqueued. + __std::atomic countdown_{std::ranges::size(range_) + 1}; }; template @@ -822,7 +823,7 @@ namespace experimental::execution void set_value() noexcept { - std::size_t countdown = op_->countdown_.fetch_sub(1, __std::memory_order_relaxed); + std::size_t countdown = op_->countdown_.fetch_sub(1, __std::memory_order_acq_rel); if (countdown == 1) { STDEXEC::set_value(static_cast(op_->rcvr_)); @@ -831,7 +832,7 @@ namespace experimental::execution void set_stopped() noexcept { - std::size_t countdown = op_->countdown_.fetch_sub(1, __std::memory_order_relaxed); + std::size_t countdown = op_->countdown_.fetch_sub(1, __std::memory_order_acq_rel); if (countdown == 1) { STDEXEC::set_value(static_cast(op_->rcvr_)); @@ -918,9 +919,15 @@ namespace experimental::execution STDEXEC::start(items_[i].__get()); } - std::unique_lock lock{this->start_mutex_}; - this->has_started_ = true; - this->pool_.bulk_enqueue(remote_queue, std::move(this->tasks_), this->tasks_size_); + { + std::unique_lock lock{this->start_mutex_}; + this->has_started_ = true; + this->pool_.bulk_enqueue(remote_queue, std::move(this->tasks_), this->tasks_size_); + } + if (this->countdown_.fetch_sub(1, __std::memory_order_acq_rel) == 1) + { + STDEXEC::set_value(std::move(this->rcvr_)); + } } private: diff --git a/test/exec/CMakeLists.txt b/test/exec/CMakeLists.txt index d83ea9c96..d9c5f424a 100644 --- a/test/exec/CMakeLists.txt +++ b/test/exec/CMakeLists.txt @@ -16,6 +16,7 @@ set(exec_test_sources ../test_main.cpp + test_schedule_all_inline_lifetime.cpp test_bwos_lifo_queue.cpp test_atomic_intrusive_queue.cpp test_any_sender.cpp diff --git a/test/exec/test_schedule_all_inline_lifetime.cpp b/test/exec/test_schedule_all_inline_lifetime.cpp new file mode 100644 index 000000000..15f21183b --- /dev/null +++ b/test/exec/test_schedule_all_inline_lifetime.cpp @@ -0,0 +1,92 @@ +/* + * Licensed under the Apache License, Version 2.0 with LLVM Exceptions. + * See https://llvm.org/LICENSE.txt for license information. + */ + +#include +#include +#include +#include +#include +#include + +namespace regression_schedule_all_inline_lifetime +{ + + struct control + { + void* operation{}; + void (*destroy)(void*){}; + bool destroy_on_complete = true; + bool completed = false; + }; + struct receiver + { + using receiver_concept = stdexec::receiver_tag; + control* state; + template + auto set_next(Item&&) noexcept + { + // Declining an item by returning just_stopped is also exercised by + // the repository's sequence/iterate tests. + return stdexec::just_stopped(); + } + void finish() noexcept + { + auto* external = state; + external->completed = true; + if (external->destroy_on_complete) + { + std::fputs("receiver completed; deleting operation state\n", stderr); + external->destroy(external->operation); + external->operation = nullptr; + } + } + void set_value() noexcept + { + finish(); + } + void set_stopped() noexcept + { + finish(); + } + void set_error(std::exception_ptr) noexcept + { + std::terminate(); + } + }; + int run_case(int argc, char**) + { + exec::static_thread_pool pool{1}; + control state; + state.destroy_on_complete = argc == 1; + auto sequence = exec::schedule_all(pool, std::views::iota(0, 1)); + using operation = exec::subscribe_result_t; + state.destroy = [](void* p) + { + delete static_cast(p); + }; + auto* op = new operation(exec::subscribe(std::move(sequence), receiver{&state})); + state.operation = op; + stdexec::start(*op); + if (state.operation) + state.destroy(state.operation); + std::printf("start returned; completed=%d\n", state.completed); + return state.completed ? 0 : 2; + } +} // namespace regression_schedule_all_inline_lifetime + +TEST_CASE("schedule_all_inline_lifetime: default", "[regression][schedule-all-completion]") +{ + char program[] = "regression"; + char* argv[] = {program}; + REQUIRE(regression_schedule_all_inline_lifetime::run_case(1, argv) == 0); +} + +TEST_CASE("schedule_all_inline_lifetime: keepalive", "[regression][schedule-all-completion]") +{ + char program[] = "regression"; + char arg0[] = "keepalive"; + char* argv[] = {program, arg0}; + REQUIRE(regression_schedule_all_inline_lifetime::run_case(2, argv) == 0); +} diff --git a/test/rrd/CMakeLists.txt b/test/rrd/CMakeLists.txt index d8feafc67..11dddea6b 100644 --- a/test/rrd/CMakeLists.txt +++ b/test/rrd/CMakeLists.txt @@ -53,7 +53,7 @@ function(add_relacy_test target_name) set_tests_properties(relacy-${target_name} PROPERTIES TIMEOUT 120) endfunction() -set(relacy_tests async_scope bwos_lifo_queue intrusive_mpsc_queue split +set(relacy_tests schedule_all_relacy async_scope bwos_lifo_queue intrusive_mpsc_queue split static_thread_pool_remote_poll sync_wait) foreach(test ${relacy_tests}) diff --git a/test/rrd/schedule_all_relacy.cpp b/test/rrd/schedule_all_relacy.cpp new file mode 100644 index 000000000..3ae0be40f --- /dev/null +++ b/test/rrd/schedule_all_relacy.cpp @@ -0,0 +1,48 @@ +/* + * Licensed under the Apache License, Version 2.0 with LLVM Exceptions. + * See https://llvm.org/LICENSE.txt for license information. + */ + +#include + +// Public schedule_all pipeline, original header, tracked independent outputs. +#include +#include +#include +#include +#include +#include + +struct schedule_all_publication : rl::test_suite +{ + static constexpr std::size_t dynamic_thread_count = 2; + rl::var outputs[4]; + void before() + { + for (auto& output: outputs) + output($) = 0; + } + void thread(unsigned) + { + exec::static_thread_pool pool{2}; + auto sender = exec::schedule_all(pool, std::views::iota(0, 4)) + | exec::transform_each( + stdexec::then([&](int i) noexcept { outputs[i]($) = i + 1; })) + | exec::ignore_all_values() + | stdexec::then( + [&]() noexcept + { + for (int i = 0; i < 4; ++i) + RL_ASSERT(outputs[i]($) == i + 1); + }); + RL_ASSERT(stdexec::sync_wait(std::move(sender)).has_value()); + } +}; +int main(int argc, char** argv) +{ + rl::test_params p; + p.iteration_count = argc > 1 ? std::strtoul(argv[1], nullptr, 10) : 100000; + p.execution_depth_limit = 20000; + p.search_type = rl::random_scheduler_type; + return rl::simulate(p) ? 0 : 1; +}