Git Product home page Git Product logo

Comments (1)

ccotter avatar ccotter commented on July 17, 2024

I have something cooked up that uses task's scheduler affinity to save/restore a context on reschedule. A sketch of the API looks like

template <class T> struct ContextVar {
    T& get(); // Return the value assigned to this variable within the current context.
              // Raise when no value is assigned to the currrent context
    
    void set(T); // Assign the value to the current context.
};

static ContextVar<int> s_data;

task<int> inner() {
    co_await some_io_operation();
    co_return s_data.get();
}

task<void> outer(int i) {
    s_data.set(i+1);
    
    int value = co_await inner();
    assert(value == i+1);
}

task<void> main_event_loop() {
    async_scope scope;
    while (optional<int> data = co_await read_data()) {
        scope.spawn_on(ContextVarsScheduler{Context::copy()}, outer(*data));
    }
    co_await scope.complete();
}

If there's interest, I can share the entire implementation. The quirks/downsides to this approach are

  1. If a child task re-schedules somewhere in the middle of a call chain, that coroutine and subsequently spawned grandchild tasks no longer participates in the ContextVars re-schedule.
  2. Doesn't work with non-scheduler affine coroutines
  3. Spawning with other types like async_scope require explicit code to propagate the ContextVars scheduler. This happens magically for free with task's scheduler affinity, but does not come for free with async_scope etc.
  4. Requires composing the ContextVarsScheduler with the "real" scheduler (e.g., specific thread pool, manual_event_loop, or other). I have a ComposedScheduler(Schedulers...) scheduler that composes 2 or more schedulers into one.

Another thought would be to integrate this directly into the loops (e.g. manual_event_loop) themselves (as Python does), which avoids the downsides above.

from libunifex.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.