ballerina-platform / nballerina

Ballerina compiler that generates native executables.
https://ballerina.io/
Apache License 2.0
139 stars 46 forks source link

What does BIR need to represent multiple strands? #926

Open jclark opened 2 years ago

jclark commented 2 years ago

Things to consider:

manuranga commented 2 years ago

This is how Ballerina concurrency model interacts with external code that has an async concurrency model, on jBallerina:

1) Library function will get called to perform io-bound operation. 2) A task is scheduled to a native thread (eg: netty). 3) Library function will return with a special return code. Execution returns to scheduler, instead executing anything below the caller (ie: like swapcontext/longjump, but jBallerina implementation don't actually use a longjump). 4) At a later time, native thread notify the scheduler to resume the given strand, and provide a return value. 5) At a later time, strand starts executing code below the caller, as if the value provided by the native thread is the return value.

Currently in jBallerina, this handover is only possible from extern functions. This has been a limiting factor in implementing some connectors fully in Ballerina.

jclark commented 2 years ago

@manuranga How does this impact what is needed in BIR?

jclark commented 2 years ago

IIRC from my @rdhananjaya, named function workers are desugared into start and futures. If we do that, then we need a way to represent inter-worker message send/receive, which isn't available at the language level for futures.

In terms of typing, there is some complexity in figuring the appropriate error type at each message send/receive point.

warunalakshitha commented 2 years ago

wait, inter-worker communication ( worker send and receive), start have been designed as terminator instructions in jballerina bir. Each terminator will be a yield point.

CALL and ASYNC_CALL instruction will be used to differentiate 'start' action.

AFAIK strand is a not included in the jballerina bir. So how multiple strands work, worker data handling through channels are actually part of jballerina runtime implementation.

manuranga commented 2 years ago

How does this impact what is needed in BIR?

I was thinking if we were to continue support such interactions with async code, and support doing it fully in ballerina, we need new instructions, eg: suspend and restart <strand-id>. Other options are: support this via langlib, or keep on doing this in java/native side, or come up with a different model.

Edit: Now that I think about it, instructions will not work since there is no way for the user to insert them, at least they have to be like intrinsic, so langlib is better, but compiler will have to know about them.