Alex-Gamper / Ada-WinRT

Ada bindings for the Microsft WinRT Api
GNU Lesser General Public License v3.0
12 stars 2 forks source link

C#'s await is not a busy-wait loop retrying periodically #4

Open optikos opened 6 years ago

optikos commented 6 years ago

This issue is food for thought as a design for Ada's analogue of C#'s await. Since Roslyn generates a lambda (anonymous local function) to implement an await, it is conceivable that the Ada-WinRT binding-generator tool could generate an analogous •named• (i.e., not-lambda) Ada subroutine to be called (back) when the operation completes. The lack of name is mere convenience in the lambda. Indeed even being a local function seems to be mere convenience in the lambda at the heart of await because that generated lambda seems to not absolutely crucially need to bind to any data allocated on the call-stack, other than to provide some sort of context to identify multiple concurrent invocations of the same async subroutine.

This posting on StackOverflow seems to be a lucid description of how C#'s await works (well) under the hood. Since Ada-WinRT has a code-generator at its disposal, some of the same tricks that Roslyn does as a compiler with an AST, the Ada-WinRT could conceivably (at least partially, with generated named subroutines, lay similar groundwork to) do for each async method in WinRT components.

Also, this blog posting is Kenny Kerr's intro to 1) a callback interface-mixin solution and 2) a co-routine solution to how the analogue of C#'s await could appear in C++/WinRT for an async subroutine. In variant 1, Kenny Kerr's implements<MyAsync, IAsyncAction, IAsyncInfo> seems to be similar to the callback alternative that you mention on the README.md regarding your current busy-wait-loop approach to an analogue of await in Ada.

Alex-Gamper commented 6 years ago

I had researched C# implementation of await/async and it seems to (as you correctly pointed out) use Roslyn to generate a internal FSM ? As an alternative I simply setup a busy-wait in the OCR sample, just to demonstrate the simplest method. In the Network example I use a different approach, ie a Callback, but this is still currently a busy wait. A better approach would be to do a blocking wait on a signal, which is raised by the completion Callback/routine