forked from Kotlin/coroutines-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync-optimized.kt
128 lines (110 loc) · 3.91 KB
/
async-optimized.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package coroutines.async
import coroutines.annotations.coroutine
import coroutines.annotations.operator
import coroutines.annotations.suspend
import coroutines.api.Continuation
import java.util.concurrent.CompletableFuture
// TEST CODE
fun foo(): CompletableFuture<String> = CompletableFuture.supplyAsync { "foo" }
fun bar(v: String): CompletableFuture<String> = CompletableFuture.supplyAsync { "bar with $v" }
fun main(args: Array<String>) {
/*
val future = async {
println("start")
val x = await(foo())
println("got '$x'")
val y = await(bar(y))
println("got '$y' after '$x'")
y
}
*/
val future = async(__anonymous__())
future.whenComplete { value, t ->
println("completed with $value")
}
future.join()
}
// LIBRARY CODE
fun <T> async(@coroutine c: FutureController<T>.() -> Continuation<Unit>): CompletableFuture<T> = FutureController<T>().apply {
this.c().resume(Unit)
}
/*
FutureController extends CompletableFuture to economize on allocations
*/
class FutureController<T> : CompletableFuture<T>() {
@suspend fun <V> await(f: CompletableFuture<V>, machine: Continuation<V>) {
f.whenComplete { value, throwable ->
if (throwable == null)
machine.resume(value)
else
machine.resumeWithException(throwable)
}
}
@operator fun handleResult(value: T, c: Continuation<Nothing>) {
complete(value)
}
@operator fun handleException(t: Throwable, c: Continuation<Nothing>) {
completeExceptionally(t)
}
}
class __anonymous__() : Continuation<Any?>,
Function1<FutureController<String>, Continuation<Unit>> {
@Volatile
private var _controller: FutureController<String>? = null
private val controller: FutureController<String>
get() = _controller ?: throw UnsupportedOperationException("Coroutine $this should be initialized before use")
private fun thisOrNew() = if (_controller == null) this else __anonymous__()
override fun invoke(controller: FutureController<String>): Continuation<Unit> {
return thisOrNew().apply {
_controller = controller
}
}
override fun resume(data: Any?) = doResume(data, null)
override fun resumeWithException(exception: Throwable) = doResume(null, exception)
/*
async { v ->
println("start")
val x = await(foo())
println("got '$x'")
val y = await(bar(y))
println("got '$y' after '$x'")
y
}
*/
private lateinit var x: String
private var label = 0
private fun doResume(data: Any?, exception: Throwable?) {
try {
when (label) {
0 -> {
if (exception != null) throw exception
data as Unit
println("start")
label = 1
controller.await(foo(), this)
}
1 -> {
if (exception != null) throw exception
x = data as String
println("got '$x'")
label = 2
controller.await(bar(x), this)
}
2 -> {
if (exception != null) throw exception
val y = data as String
println("got '$y' after '$x'")
label = -1
controller.handleResult(y, this)
}
else -> throw UnsupportedOperationException("Coroutine $this is in an invalid state")
}
} catch(e: Throwable) {
label = -2
controller.handleException(e, this)
}
}
override fun toString(): String {
return "${__anonymous__::_controller.name} coroutine"
}
}