Improve coroutines library layout

- Split CoroutinesLibrary into common and JVM parts
- Get rid of startCoroutine duplications
- Make suspendCoroutine implementation to be platform independent
This commit is contained in:
Denis Zharkov
2017-01-31 14:47:05 +03:00
parent 258ee0db75
commit a65d86293b
5 changed files with 203 additions and 211 deletions
@@ -14,18 +14,6 @@ public header fun <R, T> (suspend R.() -> T).createCoroutine(
completion: Continuation<T>
): Continuation<Unit>
/**
* Starts coroutine with receiver type [R] and result type [T].
* This function creates and start a new, fresh instance of suspendable computation every time it is invoked.
* The [completion] continuation is invoked when coroutine completes with result of exception.
* An optional [dispatcher] may be specified to customise dispatch of continuations between suspension points inside the coroutine.
*/
@SinceKotlin("1.1")
public header fun <R, T> (suspend R.() -> T).startCoroutine(
receiver: R,
completion: Continuation<T>
)
/**
* Creates coroutine without receiver and with result type [T].
* This function creates a new, fresh instance of suspendable computation every time it is invoked.
@@ -38,25 +26,17 @@ public header fun <T> (suspend () -> T).createCoroutine(
completion: Continuation<T>
): Continuation<Unit>
/**
* Starts coroutine without receiver and with result type [T].
* This function creates and start a new, fresh instance of suspendable computation every time it is invoked.
* The [completion] continuation is invoked when coroutine completes with result of exception.
* An optional [dispatcher] may be specified to customise dispatch of continuations between suspension points inside the coroutine.
*/
@SinceKotlin("1.1")
@Suppress("UNCHECKED_CAST")
public header fun <T> (suspend () -> T).startCoroutine(
completion: Continuation<T>
)
@PublishedApi
internal header class SafeContinuation<in T> : Continuation<T> {
internal constructor(delegate: Continuation<T>, initialResult: Any?)
/**
* Obtains the current continuation instance inside suspend functions and suspends
* currently running coroutine.
*
* In this function both [Continuation.resume] and [Continuation.resumeWithException] can be used either synchronously in
* the same stack-frame where suspension function is run or asynchronously later in the same thread or
* from a different thread of execution. Repeated invocation of any resume function produces [IllegalStateException].
*/
@SinceKotlin("1.1")
public header inline suspend fun <T> suspendCoroutine(crossinline block: (Continuation<T>) -> Unit): T
@PublishedApi
internal constructor(delegate: Continuation<T>)
@PublishedApi
internal fun getResult(): Any?
override val context: CoroutineContext
override fun resume(value: T): Unit
override fun resumeWithException(exception: Throwable): Unit
}