Skip to content

Commit

Permalink
Reimplement scheduler affinity (#527)
Browse files Browse the repository at this point in the history
This diff changes how `unifex::task<>` implements *Scheduler* affinity to work more like `folly::coro::Task<>`.  What Folly calls `co_viaIfAsync`, this diff calls `with_scheduler_affinity`, which is a new CPO.

`with_scheduler_affinity` is a *Sender* algorithm that maps the given *Sender* to another *Sender* that must meet new postconditions if the *Receiver* that it's connected to meets certain preconditions.  The new preconditions are:
 - the eventual *Receiver* must provide a "current *Scheduler*";
 - the result of `with_scheduler_affinity` must be started on the *Receiver's* current *Scheduler*; and
 - stop requests delivered to the new *Sender* must be delivered on the *Receiver's* current *Scheduler*.

The *Sender* returned from `with_scheduler_affinity` must complete on its *Receiver's* current *Scheduler* (i.e. must complete where it was started) so long as all the above preconditions are met.

`with_scheduler_affinity` has two default implementations for a _Sender_ type `S`:
 - if `sender_traits<S>::is_always_scheduler_affine` is `true`, the default is the identity;
 - if `sender_traits<S>::is_always_scheduler_affine` is `false`, the default is, essentially, a `typed_via` back to the correct scheduler.
 
Any _Sender_ can customize `with_scheduler_affinity`, regardless of its `is_always_scheduler_affine` property.
  • Loading branch information
ispeters authored Jun 1, 2023
1 parent a4a53ca commit 1067303
Show file tree
Hide file tree
Showing 6 changed files with 602 additions and 108 deletions.
2 changes: 2 additions & 0 deletions include/unifex/at_coroutine_exit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,8 @@ namespace _at_coroutine_exit {
} at_coroutine_exit{};
} // namespace _at_coroutine_exit

// TODO: verify that `at_coroutine_exit()` can't be used to break scheduler
// affinity by running an async task that reschedules
using _at_coroutine_exit::at_coroutine_exit;

} // namespace unifex
Expand Down
Loading

0 comments on commit 1067303

Please sign in to comment.