Skip to content

Implement P2300 ensure_started algorithm#7241

Open
shivansh023023 wants to merge 3 commits intoTheHPXProject:masterfrom
shivansh023023:feat/p2300-ensure-started
Open

Implement P2300 ensure_started algorithm#7241
shivansh023023 wants to merge 3 commits intoTheHPXProject:masterfrom
shivansh023023:feat/p2300-ensure-started

Conversation

@shivansh023023
Copy link
Copy Markdown
Contributor

P2300 ensure_started Implementation for HPX

Description

This PR implements the ensure_started sender adapter, a critical component of the P2300 (Sender/Receiver) modernization in HPX. Unlike typical lazy adapters, ensure_started transitions an asynchronous operation to eager execution by connecting and starting the predecessor sender immediately upon construction.

Technical Implementation Details:

  • Eager Execution Logic: Automatically triggers the connect and start operations on the predecessor, ensuring work begins without waiting for a downstream consumer.
  • Atomic State Machine: Utilizes a thread-safe state machine (Empty → Started → Completed) to manage the transition and result delivery, preventing race conditions during concurrent access.
  • Shared State Management: Implemented a reference-counted ensure_started_shared_state using hpx::intrusive_ptr. This ensures the asynchronous operation and its results persist until all potential receivers are satisfied.
  • Multi-Shot Support: The resulting sender supports multiple concurrent connect calls, allowing various parts of a program to await the same eager operation result.

Proposed Changes

  • New Header: libs/core/execution/include/hpx/execution/algorithms/ensure_started.hpp containing the core implementation of the shared state, sender, and operation state.
  • Visitor Pattern: Integrated internal value and error visitors to handle result propagation from the predecessor to the shared state.
  • Unit Testing: Added libs/core/execution/tests/unit/algorithm_ensure_started.cpp with test cases for eager starts, multi-receiver value delivery, and error forwarding.

Background context

In a complex asynchronous system like HPX, it is often necessary to start an expensive operation as soon as possible while allowing the program to continue other tasks before eventually "joining" the result. ensure_started provides the standardized P2300 mechanism for this pattern, bridging the gap between lazy senders and eager execution.

Checklist

  • I have added a new feature and have added tests to go along with it.
  • I have verified the build and runtime success of the new unit tests locally.
  • Headers are strictly sorted alphabetically to comply with the HPX inspect tool requirements.

- Implemented eager execution via a reference-counted shared state.
- Used atomic state transitions for thread-safe result delivery.
- Added support for multiple concurrent receivers (multi-shot).
- Integrated with hpx::intrusive_ptr for lifecycle management.
@shivansh023023 shivansh023023 requested a review from hkaiser as a code owner May 2, 2026 22:45
@StellarBot
Copy link
Copy Markdown
Collaborator

Can one of the admins verify this patch?

@codacy-production
Copy link
Copy Markdown

codacy-production Bot commented May 2, 2026

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

- Add missing <hpx/config.hpp> to unit test
- Reorder #includes to be strictly alphabetical
- Remove trailing whitespaces
- Apply clang-format
@@ -1,5 +1,4 @@
// Copyright (c) 2021 ETH Zurich
// Copyright (c) 2022 Hartmut Kaiser
// Copyright (c) 2026 The STE||AR-Group
Copy link
Copy Markdown
Contributor

@arpittkhandelwal arpittkhandelwal May 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change doesn’t look right.

The original copyright lines (ETH Zurich, 2021 and Hartmut Kaiser, 2022) shouldn’t be removed or replaced they represent the actual authorship of the code. Replacing them with “2026, The STE||AR-Group” is misleading unless there’s a valid licensing reason and significant original contribution.

This kind of change often comes from auto-generated (LLM) edits, so it’s worth double-checking before merging since it can accidentally break attribution or licensing rules.

Please keep the original copyright headers intact.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @arpittkhandelwal

Thanks for catching this! You are righton this one ,I was a lil too aggressive with replacing the file during the implementation and accidentally overwrote the original attribution headers. I've pushed a fix that restores the original copyright lines (ETH Zurich & Hartmut Kaiser) while adding my STE||AR-Group line for the new shared-state implementation.

I also realized I had dropped some of the critical run_loop_scheduler and inject_scheduler overloads, which was causing the execution/combinator CI to fail. I've restored those hooks and the HPX_HAVE_STDEXEC guard in the latest commit. The new ensure_started_sender should now correctly dispatch eager starts to the run loop when requested.

@@ -1,374 +1,108 @@
// Copyright (c) 2021 ETH Zurich
// Copyright (c) 2022 Hartmut Kaiser
// Copyright (c) 2026 The STE||AR-Group
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

- Restore original authorship headers (ETH Zurich, Hartmut Kaiser)
- Reintroduce HPX_HAVE_STDEXEC guard
- Restore tag_invoke for run_loop_scheduler integration
- Restore tag_fallback_invoke for inject_scheduler partial application
- Run clang-format to pass HPX inspect checks
@hkaiser hkaiser added category: senders/receivers Implementations of the p0443r14 / p2300 + p1897 proposals type: enhancement type: compatibility issue labels May 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

category: senders/receivers Implementations of the p0443r14 / p2300 + p1897 proposals type: compatibility issue type: enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants