Could anyone say a few words about the behavior of...
# kotlin-native
v
Could anyone say a few words about the behavior of kotlin coroutine between Kotlin native and c introp? Say for example, what happens if I call a "C" function from the Kotlin native suspend function? Will it just be considered as a normal thread blocking call ? Because there is no concept of suspend in C isn't it?
yes black 2
h
Would it be possible to put the native call on a separate single thread dispatcher to allow suspending of other coroutines?
v
Have you ever tried this kind of stuff.. bit of lazy to create a setup and try now 😁 maybe I'll try it later .. it sounds like a good suggestion.. "C" call has to be in a separate dispatcher and limit the parallelism to single thread
h
Yeah, me too. Was only an idea but definitely needs some testing. I will try it out tomorrow.
a
what helped with my understanding of coroutines is decompiling Kotlin code to Java code in IntelliJ. I can then see that the
suspend
keyword basically does three things: 1. it adds an extra parameter to the function (a Continuation) which contains the coroutine context (so the current coroutine dispatcher/name/job is accessible) 2. whenever a suspend function is called the Kotlin compiler magically (using the Continuation) converts the code following invocation of a suspend function to a lambda, which will be called when the suspend function completes (this is what’s really clever about coroutines, because it helps prevent callback hell / heavy nesting - everything appears to be flat and in sequential) 3. and some machinery to allow for the Continuation to be suspended/resumed/cancelled, depending on the current state of the coroutine context (this is just my layman’s understanding) And this is all built into Kotiln. kotlinx.coroutines adds some excellent helpers and tools, but it’s only expanding upon the building blocks from Kotlin. So if a function (be it a C interop function, or a regular Kotlin function) doesn’t have the
suspend
keyword then it’s not going to get these under-the-hood changes, so it will just be called sequentially, like a regular function.
h
That’s absolut true, there is also https://github.com/Kotlin/KEEP/blob/master/proposals/coroutines.md and https://github.com/Kotlin/KEEP/blob/master/notes/code-coloring.md But even with this notes I am still curious (or I don’t understand) if using a native thread would allow running other suspending coroutines. Without testing I would say yes it will work out of the box, because it is just another dispatcher like Main/Default/IO.
a
ahh that code colouring thing is cool. I’ve been wondering whether it would be possible to have an IJ plugin that would highlight parts Gradle scripts to help with the configuration cache madness
1