-
Notifications
You must be signed in to change notification settings - Fork 188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
task<> scheduler affinity (#290) #506
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
facebook-github-bot
added
the
CLA Signed
This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
label
Apr 28, 2023
janondrusek
force-pushed
the
main
branch
2 times, most recently
from
May 2, 2023 23:14
178f5fc
to
218d50f
Compare
Hooray! 🙌 |
ispeters
approved these changes
May 2, 2023
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two small comments; otherwise, LGTM.
This commit implements scheduler affinity -- aka "sticky" scheduling -- in `unifex::task<>`. The idea is that it is impossible for a child operation to cause the current coroutine to resume on the wrong execution context. * `task<>`-based coroutines track and propagate the current scheduler * `at_coroutine_exit` remembers current scheduler from when the cleanup action is scheduled * `schedule` always returns an instance of `sender_for<schedule, the_sender>`, which is also a `scheduler_provider` * scheduler affinity when co_await-ing senders in a `task<>`-returning coroutine * scheduler affinity when co_await-ing awaitables in a `task<>`-returning coroutine * awaitables and senders that are `blocking_kind::always_inline` don't get a thunk * More senders and awaitables support compile-time blocking queries * `co_await schedule(sched)` is magic in a `task<>`-returning coroutine: it changes execution context and schedules a cleanup action to transition back to the original scheduler Move implementation of special co_await behavior of scheduler senders out of task.hpp Hoist untyped RAII containers for coroutine_handle<> out of task<> and its awaiter (facebookexperimental#329) While looking at the binary size impact of adopting coroutines with `unifex::task<>`, I noticed that a number of operations on `coroutine_handle<T>` are expressed in `unifex::task<>` as if they depend on `T` when they don't. The consequence is extra code. This diff creates a `coro_holder` class that uniquely owns a `coroutine_handle<>` and makes `unifex::task<>` inherit from it. We technically lose some type safety, but it's still correct by construction. This change saves about 1.5 kilobytes in one of our apps. Similar to the above, I noticed binary duplication due to false template parameter dependencies in `unifex::task<>`'s awaiter type. This diff hoists a non-type-specific RAII container for a `coroutine_handle<>` that stores the handle as a `std::uintptr_t` so that `task`'s awaiter can use the low bit as a dirty flag. This change saves another ~1.5 kilobytes in one of our apps. Fix scheduler affinity (facebookexperimental#405) * Fix scheduler affinity We have been storing a `task<>`'s scheduler as an `any_scheduler_ref`, which has proven to be a source of use-after-free bugs. This change switches all the `any_scheduler_ref`s to `any_scheduler`s, fixing the lifetime issues. Make task<>'s thunk-on-resume unstoppable (facebookexperimental#495) * Make task<>'s thunk-on-resume unstoppable When awaiting an async Sender that swallows done signals (such as let_done(never_sender{}, just)), the user-level code looks like it swallows done signals: ``` // never cancels co_await let_done(never_sender{}, just); ``` However, `task<>`'s Scheduler affinity implementation transforms the above code into this: ``` co_await typed_via(let_done(never_sender{}, just), <current scheduler>); ``` The `schedule()` operation inside the injected `typed_via` can emit done if the current stop token has had stop requested, leading to very non-obvious cancellation behaviour that can't be worked around. This diff introduces a pair of regression tests that capture the above scenario, and the analogous scenario of awaiting an async Awaitable that completes with done. The next diff will fix these failing tests. * Change task<>'s thunk-on-resume to be unstoppable This diff fixes the broken tests in the previous diff. Respect blocking_kind in `let_value()` (facebookexperimental#381) * `let_value()` would always assume `blocking_kind::maybe`, which results in potentially unnecessary reschedule on resumption * replicate `blocking_kind` customization from `finally()` fix `variant_sender` blocking kind (facebookexperimental#474) add `unifex::v2::async_scope` (facebookexperimental#463) * simpler than `unifex::v1::async_scope` (`nest()` and `join()`) * does not support cancellation fixing linter error (facebookexperimental#414) move deduction guide to namespace scope for gcc-10 in scheduler concept, check copy_constructability after requiring call to schedule() work around gcc-10 bugs avoid warning about missing braces in initializer back out change to awaiter_type_t Co-authored-by: Eric Niebler <[email protected]> Co-authored-by: Ian Petersen <[email protected]> Co-authored-by: Ondrej Lehecka <[email protected]>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
CLA Signed
This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This commit implements scheduler affinity -- aka "sticky" scheduling -- in
unifex::task<>
. The idea is that it is impossible for a child operation to cause the current coroutine to resume on the wrong execution context.task<>
-based coroutines track and propagate the current schedulerat_coroutine_exit
remembers current scheduler from when the cleanup action is scheduledschedule
always returns an instance ofsender_for<schedule, the_sender>
, which is also ascheduler_provider
task<>
-returning coroutinetask<>
-returning coroutineblocking_kind::always_inline
don't get a thunkco_await schedule(sched)
is magic in atask<>
-returning coroutine: it changes execution context and schedules a cleanup action to transition back to the original schedulerMove implementation of special co_await behavior of scheduler senders out of task.hpp
Hoist untyped RAII containers for coroutine_handle<> out of task<> and its awaiter (#329)
While looking at the binary size impact of adopting coroutines with
unifex::task<>
, I noticed that a number of operations oncoroutine_handle<T>
are expressed inunifex::task<>
as if they depend onT
when they don't. The consequence is extra code.This diff creates a
coro_holder
class that uniquely owns acoroutine_handle<>
and makesunifex::task<>
inherit from it. We technically lose some type safety, but it's still correct by construction. This change saves about 1.5 kilobytes in one of our apps.Similar to the above, I noticed binary duplication due to false template parameter dependencies in
unifex::task<>
's awaiter type.This diff hoists a non-type-specific RAII container for a
coroutine_handle<>
that stores the handle as astd::uintptr_t
so thattask
's awaiter can use the low bit as a dirty flag. This change saves another ~1.5 kilobytes in one of our apps.Fix scheduler affinity (#405)
We have been storing a
task<>
's scheduler as anany_scheduler_ref
, which has proven to be a source of use-after-free bugs. This change switches all theany_scheduler_ref
s toany_scheduler
s, fixing the lifetime issues.Make task<>'s thunk-on-resume unstoppable (#495)
When awaiting an async Sender that swallows done signals (such as let_done(never_sender{}, just)), the user-level code looks like it swallows done signals:
However,
task<>
's Scheduler affinity implementation transforms the above code into this:The
schedule()
operation inside the injectedtyped_via
can emit done if the current stop token has had stop requested, leading to very non-obvious cancellation behaviour that can't be worked around.This diff introduces a pair of regression tests that capture the above scenario, and the analogous scenario of awaiting an async Awaitable that completes with done. The next diff will fix these failing tests.
This diff fixes the broken tests in the previous diff.
Respect blocking_kind in
let_value()
(#381)let_value()
would always assumeblocking_kind::maybe
, which results in potentially unnecessary reschedule on resumptionblocking_kind
customization fromfinally()
fix
variant_sender
blocking kind (#474)add
unifex::v2::async_scope
(#463)unifex::v1::async_scope
(nest()
andjoin()
)fixing linter error (#414)
move deduction guide to namespace scope for gcc-10
in scheduler concept, check copy_constructability after requiring call to schedule()
work around gcc-10 bugs
avoid warning about missing braces in initializer