AaronRobinsonMSFT / DNNE

Prototype native exports for a .NET Assembly.
MIT License
394 stars 41 forks source link

How could one use async functions? #171

Closed ThaDaVos closed 1 year ago

ThaDaVos commented 1 year ago

In my current project, the need has come to call async methods from other libraries - any guidance if we can mark the exported method as async? Or should one simply follow the default "calling async from sync" practices inside C#?

AaronRobinsonMSFT commented 1 year ago

The async model is merely a syntactic sugar state machine around the classic callback mechanism. There is integration with the .NET thread-pool and a bit of clever magic going on but it all boils down to callbacks. In this case the Roslyn compiler generates a large callback state machine and there is no simple way to express that across the interop boundary. The C++ language has co_await, which represents a similar state machine approach but reconciling them would be tough and is definitely beyond the scope of DNNE since its primary contract is C.

Or should one simply follow the default "calling async from sync" practices inside C#?

This is correct. One could accept a callback in the signature exported function signature and handle the state machine transition, but that wouldn't really achieve the semantics I assume is being expected on the call side of the boundary.