Comments (1)
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
- 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.
- Doesn't work with non-scheduler affine coroutines
- 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.
- 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)
- blocking_kind customization does not compile HOT 1
- let_value decay-copies its final result HOT 4
- Using any_object with any_scheduler
- Fix errors when syncing master back to third-party HOT 1
- How to distinguish between cancellation from stop_when and ouside?
- Improve and rename `type_erased_stream` -> `any_stream_of`
- build libunifex failed with g++ 13.1.0
- `task` resumes after being cancelled causing UB HOT 6
- request_stop() return value is invalid
- (Is there already) support for async stream HOT 7
- How to properly use gtest dependency for building libunifex?
- Compile error with MSVC in default settings HOT 3
- Compile failed In Default Setting HOT 3
- /builddir/build/BUILD/libunifex-0.4.0/include/unifex/v2/async_scope.hpp:76:52: error: βMEM[(const struct scope_reference *)&optFuture].scope_β may be used uninitialized [-Werror=maybe-uninitialized]
- Add documentation for unstoppable
- Document that senders run by at_coroutine_exist must succeed
- error_types is often an invalid type for dematerialize sender HOT 1
- When will the kernel side io_uring passthrough for reading or writing NVMe SSD devices be available?
- When will the kernel side io_uring passthrough for reading or writing NVMe SSD devices be available?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from libunifex.