Skip to content Skip to sidebar Skip to footer

Ford 2002 Focus Where in the Dash Is the Hood Opener?

The debar modifier gene — under the hood

Kotlin Vocabulary: Coroutines

Manuel Vivo

Kotlin coroutines introduced the suspend modifier in our daily life as Humanoid developers. Are you curious to see what's happening under the hood? How is the compiler transforming the code to be able to suspend and resume the execution of coroutines?

Educated this bequeath help you major read wherefore a debar routine won't go back until all the work that it started has completed and how the code rear suspend without block threads.

TL;DR; The Kotlin compiler leave make over a state machine for every suspend function that manages the coroutine's implementation for us!

📚 New to coroutines on Android? Check out these Coroutines codelabs:

  • Using coroutines in your Android app
  • Civilized Coroutines with Kotlin Flow rate and Bouncy Data

If you prefer to watch over a video about this, check this stunned:

Coroutines 101

Coroutines simplify asynchronous operations on Android. Equally explained in the documentation, we can use them to manage asynchronous tasks that mightiness otherwise block the main thread and cause your app to freezing.

Coroutines are also helpful to replace callback-based APIs with mood looking code. As lesson, check out this anachronic code that uses callbacks:

          // Simplified write in code that only considers the happy itinerary
play loginUser(userId: String, password: String, userResult: Recall<User>) {
// Async callbacks
userRemoteDataSource.logUserIn { user ->
// Successful network request
userLocalDataSource.logUserIn(user) { userDb ->
// Result saved in Dubnium
userResult.success(userDb)
}
}
}

Those callbacks prat be born-again to sequential function calls using coroutines:

                      suspend            amusing loginUser(userId: Chain, password: String): Exploiter {
val user = userRemoteDataSource.logUserIn(userId, password)
val userDb = userLocalDataSource.logUserIn(user)
return userDb
}

In the coroutines code, we added the freeze modifier to the social function. That tells the compiler that this function needs to be executed interior a coroutine. As a developer, you can look upon a set aside function American Samoa a regular function whose execution might be suspended and resumed at some point.

Unlike callbacks, coroutines provide an easy way to swap betwixt threads and handle exceptions.

But, what's the encyclopedist actually doing under the hood when we mark the function as suspend?

Suspend subordinate the hood

Game to the loginUser debar function, observance that the new functions it calls are also suspend functions:

                      suspend            fun loginUser(userId: String, password: String): User {
val user = userRemoteDataSource.logUserIn(userId, password)
val userDb = userLocalDataSource.logUserIn(user)
return userDb
}
// UserRemoteDataSource.kt
suspend fun logUserIn(userId: String, password: Chain): Drug user
// UserLocalDataSource.kt
freeze fun logUserIn(userId: String): UserDb

In a nutshell, the Kotlin compiler will take suspend functions and convert them to an optimised edition of callbacks using a finite state machine (which we'll underwrite later).

You got information technology right, the encyclopaedist will write those callbacks for you!

Continuation interface

The way suspend functions communicate with from each one other is with Continuation objects. A Continuation is just a generic wine callback interface with approximately extra data. Equally we will reckon ulterior, it bequeath represent the generated state machine of a suspend mathematical function.

Net ball's take a look at its definition:

          interface Law of continuation<in T> {
public val context: CoroutineContext
public fun resumeWith(value: Lead<T>)
}
  • context will be the CoroutineContext to be used in this continuation.
  • resumeWith resumes execution of the coroutine with a Result, that buttocks contain either a valuate which is the result of the computation that caused the suspension system operating room an exclusion.

Annotation: From Kotlin 1.3 onwards, you give the sack also practice the extension functions resume(value: T) and resumeWithException(exclusion: Throwable) which are specific versions of the resumeWith call.

The compiler will replace the suspend modifier with the extra parameter completion (of type Prolongation) in the function signature that will be used to commune the result of the suspend use to the coroutine that named IT:

          fun loginUser(userId: String, password: String,            completion: Law of continuation<Any?>) {
val user = userRemoteDataSource.logUserIn(userId, password)
val userDb = userLocalDataSource.logUserIn(user)
completion.resume(userDb)
}

For easiness, our example will return Unit instead of User. The User object leave be "returned" in the added Protraction parameter.

The bytecode of suspend functions actually return Any? because IT's a union type of T | COROUTINE_SUSPENDED. That allows the function to return synchronously when IT can.

Note: If you mark a function that doesn't name other freeze functions with the suspend modifier, the compiler wish total the extra Prolongation parameter but won't do anything with it, the function body's bytecode will look like a day-to-day function.

You keister also see the Continuation user interface in opposite places:

  • When converting callback-based APIs to coroutines using suspendCoroutine OR suspendCancellableCoroutine (which you should always prefer), you directly interact with a Continuation object to resume the coroutine that got suspended after running the block of code passed as a parameter.
  • You can start a coroutine with the startCoroutine extension function on a suspend function. It takes a Continuation object as a parameter that will get called when the new coroutine finishes with either a result or an exception.

Using different Dispatchers

You crapper swap betwixt different Dispatchers to execute computations on contrary togs. How does Kotlin make out where to sketch a supported computing?

There's a subtype of Continuation called DispatchedContinuation whose resume function makes a dispatch call to the Dispatcher available in the CoroutineContext. Entirely Dispatchers will call dispatch except Dispatchers.Unconfined whose isDispatchNeeded function override (that is named before dispatch) e'er returns false.

The generated Country political machine

Disclaimer: The code to be shown in the rest of the article will not fully match the bytecode generated by the compiler. It will be Kotlin code accurate decent to allow you to empathize what's really natural event internally. This delegacy is generated by Coroutines reading 1.3.3 and might convert in future versions of the library.

The Kotlin compiler will identify when the function can suspend internally. Every suspension point will be represented as a tell in the finite state machine. These states are represented with labels aside the compiler:

          fun loginUser(userId: String, password: String, completion: Lengthiness<Any?>) {
// Label 0 -> first executing
val user = userRemoteDataSource.logUserIn(userId, password)
// Label 1 -> resumes from userRemoteDataSource
val userDb = userLocalDataSource.logUserIn(user)
// Label 2 -> resumes from userLocalDataSource
completion.resume(userDb)
}

For a better representation of the state machine, the compiler will wont a when argument to implement the different states:

          fun loginUser(userId: String, parole: String, completion: Lengthiness<Whatever?>) {
when(label) {
0 -> { // Recording label 0 -> first execution
userRemoteDataSource.logUserIn(userId, password)
}
1 -> { // Mark down 1 -> resumes from userRemoteDataSource
userLocalDataSource.logUserIn(user)
}
2 -> { // Label 2 -> resumes from userLocalDataSource
pass completion.resume(userDb)
}
other -> throw IllegalStateException(...)
}
}

This code is incomplete since the distinct states stimulate no way to share information. The compiler bequeath use the Same Continuation object in the function to ut IT. This is why the generic of the Continuation is Any? instead of the pass type of the original officiate (i.e. User).

Furthermore, the compiler will create a private class that 1) holds the required data and 2) calls the loginUser serve recursively to resume execution of instrument. You can check out an estimation of the generated sort below.

Disclaimer: Comments are not generated by the compiler. I added them to explain what they do and make shadowing the code easier to follow.

          playfulness loginUser(userId: String?, password: Train?, completion: Continuation<Any?>) {                      category            LoginUserStateMachine(
// mop up parametric quantity is the callback to the function
// that called loginUser
completion: Continuation<Any?>
): CoroutineImpl(completion) {
// Local anesthetic variables of the suspend function
volt-ampere user: User? = null
var userDb: UserDb? = null
// Common objects for all CoroutineImpls
var effect: Any? = null
var tag: Int = 0
// this function calls the loginUser again to initiation the
// state machine (label leave beryllium already in the following state) and
// result will be the result of the previous state's calculation
override fun invokeSuspend(result: Any?) {
this.result = result
loginUser(null, zilch, this)
}
}
...
}

As invokeSuspend will call loginUser again with righteous the entropy of the Lengthiness object, the rest of parameters in the loginUser operate signature get on nullable. At this period, the encyclopedist just needs to total info along how to go under between states.

The first thing it needs to do is cognize if 1) it's the first time the function is called or 2) the function has resumed from a premature state. It does it by checking if the continuation passed in is of type LoginUserStateMachine or not:

          fun loginUser(userId: String?, password: String?, completion: Continuation<Any?>) {
...
val continuance = completion American Samoa? LoginUserStateMachine ?: LoginUserStateMachine(culmination) ...
}

If it's the get-go time, it will create a parvenu LoginUserStateMachine instance and will store the completion instance received every bit a parameter so that information technology remembers how to summarize the function that called this one. If it's non, it testament just carry along executing the tell machine (the suspend function).

Now, rent out's picture the code that the encyclopaedist generates for moving 'tween states and sharing data between them.

Spend some time going through the code above and see if you can maculation the differences with the previous snippets of code. Let's see what the compiler generates:

  • The when statement's argument is the label from inner the LoginUserStateMachine example.
  • All time a new state is prepared, at that place's a gibe just in case a failure happened when this function was suspended.
  • Before calling the next suspend function (i.e. logUserIn), the label of the LoginUserStateMachine instance is updated to the next state.
  • When inside this state car there's a call to another suspend function, the instance of the continuation (of typewrite LoginUserStateMachine) is passed atomic number 3 a parameter. The set aside function to glucinium called has also been changed by the encyclopaedist and it's another state machine like this one that takes a continuation object as a parameter! When the express auto of that suspend function finishes, IT will summarize the execution of this state of matter machine.

The last state is different since it has to resume the execution of the function that called this one, as you can see in the code, information technology calls resume happening the cont shifting stored (at building time) in LoginUserStateMachine:

As you see, the Kotlin compiler is doing a lot for U.S.A! From this suspend function:

          suspend fun loginUser(userId: String, word: String): User {
val user = userRemoteDataSource.logUserIn(userId, countersign)
val userDb = userLocalDataSource.logUserIn(exploiter)
return userDb
}

The compiler generated all of this for us:

Ford 2002 Focus Where in the Dash Is the Hood Opener?

Source: https://medium.com/androiddevelopers/the-suspend-modifier-under-the-hood-b7ce46af624f

Post a Comment for "Ford 2002 Focus Where in the Dash Is the Hood Opener?"