@@ -22,13 +22,16 @@ public interface CancellableContinuation<in T> : Continuation<T>, Job {
22
22
val isCancelled: Boolean
23
23
24
24
/* *
25
- * Tries to resume this continuation with a given value and returns `true` if it was successful,
26
- * or `false` otherwise (it was already resumed or cancelled).
27
- *
28
- * An optional [onSuccess] callback is invoked with [value] as its parameter after the state of this continuation
29
- * is updated (so that is cannot be cancelled anymore), but before it is actually resumed.
25
+ * Tries to resume this continuation with a given value and returns non-null object token if it was successful,
26
+ * or `null` otherwise (it was already resumed or cancelled). When non-null object was returned,
27
+ * [completeResume] must be invoked with it.
30
28
*/
31
- fun tryResume (value : T , onSuccess : ((Any? ) -> Unit )? = null): Boolean
29
+ fun tryResume (value : T ): Any?
30
+
31
+ /* *
32
+ * Completes the execution of [tryResume] on its non-null result.
33
+ */
34
+ fun completeResume (token : Any )
32
35
33
36
/* *
34
37
* Makes this continuation cancellable. Use it with `holdCancellability` optional parameter to
@@ -104,16 +107,20 @@ internal class SafeCancellableContinuation<in T>(
104
107
override val isCancelled: Boolean
105
108
get() = getState() is Cancelled
106
109
107
- override fun tryResume (value : T , onSuccess : (( Any? ) -> Unit ) ? ): Boolean {
110
+ override fun tryResume (value : T ): Any? {
108
111
while (true ) { // lock-free loop on state
109
112
val state = getState() // atomic read
110
113
when (state) {
111
- is Active -> if (updateState (state, value, onSuccess )) return true
112
- else -> return false // cannot resume -- not active anymore
114
+ is Active -> if (tryUpdateState (state, value)) return state
115
+ else -> return null // cannot resume -- not active anymore
113
116
}
114
117
}
115
118
}
116
119
120
+ override fun completeResume (token : Any ) {
121
+ completeUpdateState(token, getState())
122
+ }
123
+
117
124
@Suppress(" UNCHECKED_CAST" )
118
125
override fun afterCompletion (state : Any? ) {
119
126
val decision = this .decision // volatile read
0 commit comments