NVIDIA / NVIDIA/stdexec

How implement a coroutine suspend-running in a multi-thread trampoline scheduler when using a custom sender?

Open
#1,484 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
2.4k
Forks
270
Avg merge
3d 6h
Merged PRs (30d)
39

Description

Something like this

#include <unifex/single_thread_context.hpp>
#include <unifex/trampoline_scheduler.hpp>
#include <unifex/let_value.hpp>
#include <unifex/just.hpp>
#include <unifex/sync_wait.hpp>
#include <unifex/then.hpp>

using namespace unifex;

class counter_sender {
public:
    counter_sender(int start, int end, trampoline_scheduler& scheduler)
        : current_(start), end_(end), scheduler_(scheduler) {}

    template<typename Receiver>
    struct operation {
        operation(int current, int end, 
                 trampoline_scheduler& scheduler, Receiver&& receiver)
            : current_(current)
            , end_(end)
            , scheduler_(scheduler)
            , receiver_((Receiver&&)receiver) {}

        void start() noexcept {
            if (current_ >= end_) {
                unifex::set_value(std::move(receiver_), current_);
                return;
            }

            auto next = current_ + 1;
            
            auto reschedule = 
                scheduler_.schedule() | 
                then([this, next]() {
                    auto new_op = operation{
                        next, 
                        end_, 
                        scheduler_, 
                        (Receiver&&)receiver_
                    };
                    new_op.start();
                });

            // restart
            connect(std::move(reschedule), receiver_).start();
        }

    private:
        int current_;
        int end_;
        trampoline_scheduler& scheduler_;
        Receiver receiver_;
    };

    template<typename Receiver>
    auto connect(Receiver&& receiver) {
        return operation<Receiver>{
            current_, end_, scheduler_, (Receiver&&)receiver
        };
    }

private:
    int current_;
    int end_;
    trampoline_scheduler& scheduler_;
};

void example() {
    trampoline_scheduler scheduler{};
    
    auto result = sync_wait(counter_sender{1, 5, scheduler});
    
    if (result) {
        std::cout << "Final count: " << *result << "\n";
    }
}

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue provides only an inline counter_sender example and names trampoline_scheduler, schedule(), then(), connect(), and sync_wait(); begin by checking those APIs and their operation-state lifetime rules. A useful resolution would define a supported way to suspend and resume the running coroutine across the multi-thread trampoline, demonstrated by a working version of the example with clear completion behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
backend-api-design
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.