DeepMedia logo
Open Source

Search...

Other versions of this page

Improve this page on GitHub

Search

Suspend Functions

Declaration

All functions that support the @Knee annotation can also be marked as suspend. The developer UX is exactly the same:

kotlin logokotlin
// Kotlin/Native @Knee suspend fun computeNumber(): Int = coroutineScope { val num1 = async { loadFirstNumber() } val num2 = async { loadSecondNumber() } num1.await() + num2.await() } // Kotlin/JVM scope.launch { val number = computeNumber() println("Found number: $number") }

Structured concurrency

The underlying implementation is very complex in order to support two-way cancellation and error propagation. In the example above:

  • If the JVM scope is cancelled, the native coroutines are also cancelled
  • If the native coroutines are cancelled, computeNumber throws a CancellationException
  • Errors are propagated up/down and the exception type, if possible, is preserved

In short, calling a @Knee suspend function is no different than calling a local suspend function and you can expect the same level of support. In particular Knee preserves the hierarchy of coroutines and keeps them connected across the JNI bridge.

Context elements

Knee does no attempt at preserving the CoroutineContext. All context element, most notably the CoroutineDispatcher, will be lost when the JNI bridge is crossed:

  • @Knee suspend functions called from K/JVM are invoked on Dispatchers.Unconfined on the native backend
  • @Knee suspend functions called from K/N are invoked on Dispatchers.Unconfined on the JVM frontend

Subscribe to the DeepMedia Newsletter

The latest news about DeepMedia products, open source projects and software development at our company.

By clicking “Subscribe”, you agree that DeepMedia may use your email address to send you newsletters, including commercial communications, and to process your personal data for this purpose. You agree that DeepMedia may process said data using third-party services for this purpose in accordance with the DeepMedia Privacy Policy. You can revoke this consent at any time using the unsubscribe link included in each email or by writing at contact@deepmedia.io.