Refactor folly::coro::Task to eliminate unnecessary synchronisation
Summary: This ability to eagerly start a folly::coro::Task by calling .scheduleVia() introduced a potential race between the operation completing concurrently on another thread and the current thread then subsequently co_awaiting the returned folly::coro::Future. Thus the implementation of coro::Task's promise_type required the use of an atomic variable to decide this race. This diff eliminates the need for that synchronisation by separating the step of associating the task with an executor from the step of launching the task so that the Task is always lazily started. The folly::coro::Task<T>::scheduleVia() method has now been replaced with a .scheduleOn() method that returns a folly::coro::TaskWithExecutor<T>. The TaskWithExecutor<T> type is awaitable and will schedule the Task's execution on the bound executor when it is awaited. Also fixes a cyclic dependency issue between Promise.h and Task.h/Future.h by merging them all into Task.h. folly/futures/Future.h now defines the `folly::coro::toSemiFuture()` function for wrapping an Awaitable up into a `folly::SemiFuture<T>`. The `folly::coro::Future<T>` type and folly/experimental/coro/Future.h header has been removed. Reviewed By: andriigrynenko Differential Revision: D9948564 fbshipit-source-id: 21c9ee1ec0e67d56321629b5901c3eb7cddcfe9b
Showing
Please register or sign in to comment