From 3225ec8935075f59a151023dcb43438458240eeb Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Thu, 12 Jan 2017 03:05:54 +0300 Subject: [PATCH] Provide headers for coroutine building blocks --- .../stdlib/common/src/kotlin/CoroutinesH.kt | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 libraries/stdlib/common/src/kotlin/CoroutinesH.kt diff --git a/libraries/stdlib/common/src/kotlin/CoroutinesH.kt b/libraries/stdlib/common/src/kotlin/CoroutinesH.kt new file mode 100644 index 00000000000..290855f9737 --- /dev/null +++ b/libraries/stdlib/common/src/kotlin/CoroutinesH.kt @@ -0,0 +1,62 @@ +package kotlin.coroutines + + +/** + * Creates coroutine with receiver type [R] and result type [T]. + * This function creates a new, fresh instance of suspendable computation every time it is invoked. + * To start executing the created coroutine, invoke `resume(Unit)` on the returned [Continuation] instance. + * 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 (suspend R.() -> T).createCoroutine( + receiver: R, + completion: Continuation +): Continuation + +/** + * 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 (suspend R.() -> T).startCoroutine( + receiver: R, + completion: Continuation +) + +/** + * Creates coroutine without receiver and with result type [T]. + * This function creates a new, fresh instance of suspendable computation every time it is invoked. + * To start executing the created coroutine, invoke `resume(Unit)` on the returned [Continuation] instance. + * 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 (suspend () -> T).createCoroutine( + completion: Continuation +): Continuation + +/** + * 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 (suspend () -> T).startCoroutine( + completion: Continuation +) + +/** + * 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 suspendCoroutine(crossinline block: (Continuation) -> Unit): T