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
3 changes: 3 additions & 0 deletions include/exec/detail/bwos_lifo_queue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ namespace experimental::execution::bwos

auto push_back(Tp value) noexcept -> bool;

// Pushes items from [first, last) until the queue is full, and returns
// where it stopped. Thieves can steal the pushed items before it returns
// (see GRANTING BLOCKS).
template <class Iterator, class Sentinel>
auto push_back(Iterator first, Sentinel last) noexcept -> Iterator;

Expand Down
11 changes: 8 additions & 3 deletions include/exec/static_thread_pool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -892,9 +892,14 @@ namespace experimental::execution
bwos::lifo_queue<_task_base*, numa_allocator<_task_base*>>& local_queue)
{
auto const last = local_queue.push_back(pending_queue.begin(), pending_queue.end());
__intrusive_queue<&_task_base::next_> tmp{};
tmp.splice(tmp.begin(), pending_queue, pending_queue.begin(), last);
tmp.clear();
// Thieves can steal the tasks that push_back() moved to local_queue, and
// run and destroy them, before it returns. So drop them from
// pending_queue without touching them: splicing them out would write to
// the last one.
_task_base* const head = last == pending_queue.end() ? nullptr : *last;
_task_base* const tail = head == nullptr ? nullptr : pending_queue.back();
pending_queue.clear();
pending_queue = __intrusive_queue<&_task_base::next_>(head, tail);
}

inline auto _static_thread_pool::_thread_state::try_remote(_remote_poll_mode mode)
Expand Down
28 changes: 28 additions & 0 deletions test/exec/test_static_thread_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,34 @@ TEST_CASE("a task on static_thread_pool can wait for a task that it schedules on
}
}

// A worker moves the tasks in its remote queues to its local queue, where
// thieves can steal, run, and destroy them right away when the blocks hold one
// task each. The worker used to write to the last task it moved after that,
// which TSan reported as a data race, and which lost tasks when the write hit
// a new task that reused the memory.
TEST_CASE("static_thread_pool does not touch a task after a thief can steal it",
"[types][static_thread_pool]")
{
constexpr int num_tasks = 20'000;
std::atomic<int> completed{0}; // outlives the pool
exec::static_thread_pool pool{8, exec::bwos_params{.blockSize = 1}};
auto sched = pool.get_scheduler();
for (int i = 0; i < num_tasks; ++i)
{
exec::start_detached(
ex::schedule(sched)
| ex::then([&completed] { completed.fetch_add(1, std::memory_order_relaxed); }));
}

auto const deadline = std::chrono::steady_clock::now() + std::chrono::seconds(10);
while (completed.load(std::memory_order_relaxed) < num_tasks
&& std::chrono::steady_clock::now() < deadline)
{
std::this_thread::yield();
}
CHECK(completed.load(std::memory_order_relaxed) == num_tasks);
}

TEST_CASE("tasks that a task on static_thread_pool schedules run in parallel with it",
"[types][static_thread_pool]")
{
Expand Down
Loading