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
19 changes: 13 additions & 6 deletions include/exec/thread_pool_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,8 @@ namespace experimental::execution
bool has_started_{false};
__intrusive_queue<&_task_base::next_> tasks_{};
std::size_t tasks_size_{};
__std::atomic<std::size_t> countdown_{std::ranges::size(range_)};
// Keep start alive until all children have been started and enqueued.
__std::atomic<std::size_t> countdown_{std::ranges::size(range_) + 1};
};

template <class Range, class Receiver, class Pool>
Expand Down Expand Up @@ -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<Receiver&&>(op_->rcvr_));
Expand All @@ -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<Receiver&&>(op_->rcvr_));
Expand Down Expand Up @@ -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:
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_schedule_all_inline_lifetime.cpp
test_bwos_lifo_queue.cpp
test_atomic_intrusive_queue.cpp
test_any_sender.cpp
Expand Down
92 changes: 92 additions & 0 deletions test/exec/test_schedule_all_inline_lifetime.cpp
Original file line number Diff line number Diff line change
@@ -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 <catch2/catch_test_macros.hpp>
#include <cstdio>
#include <exec/sequence_senders.hpp>
#include <exec/static_thread_pool.hpp>
#include <ranges>
#include <stdexec/execution.hpp>

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 <class Item>
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<decltype(sequence), receiver>;
state.destroy = [](void* p)
{
delete static_cast<operation*>(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);
}
2 changes: 1 addition & 1 deletion test/rrd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
48 changes: 48 additions & 0 deletions test/rrd/schedule_all_relacy.cpp
Original file line number Diff line number Diff line change
@@ -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 <stdexec_relacy.hpp>

// Public schedule_all pipeline, original header, tracked independent outputs.
#include <cstdlib>
#include <exec/sequence/ignore_all_values.hpp>
#include <exec/sequence/transform_each.hpp>
#include <exec/static_thread_pool.hpp>
#include <ranges>
#include <stdexec/execution.hpp>

struct schedule_all_publication : rl::test_suite<schedule_all_publication, 1>
{
static constexpr std::size_t dynamic_thread_count = 2;
rl::var<int> 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<schedule_all_publication>(p) ? 0 : 1;
}
Loading