Skip to content

Commit dbd9e1c

Browse files
committed
Consistent naming of handler base classes and their concrete impls
1 parent 6d9f40f commit dbd9e1c

File tree

7 files changed

+49
-91
lines changed

7 files changed

+49
-91
lines changed

common/kotlinx-coroutines-core-common/src/main/kotlin/kotlinx/coroutines/experimental/AbstractContinuation.kt

+10-20
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ internal abstract class AbstractContinuation<in T>(
7474
name state class public state description
7575
------ ------------ ------------ -----------
7676
ACTIVE Active : Active active, no listeners
77-
SINGLE_A CancellationHandler : Active active, one cancellation listener
77+
SINGLE_A CancelHandler : Active active, one cancellation listener
7878
CANCELLING Cancelling : Active in the process of cancellation due to cancellation of parent job
7979
CANCELLED Cancelled : Cancelled cancelled (final state)
8080
COMPLETED any : Completed produced some result or threw an exception (final state)
@@ -158,7 +158,7 @@ internal abstract class AbstractContinuation<in T>(
158158
resumeImpl(CompletedExceptionally(exception), resumeMode)
159159

160160
public fun invokeOnCancellation(handler: CompletionHandler) {
161-
var handleCache: CancellationHandler? = null
161+
var handleCache: CancelHandler? = null
162162
loopOnState { state ->
163163
when (state) {
164164
is Active -> {
@@ -167,7 +167,7 @@ internal abstract class AbstractContinuation<in T>(
167167
return
168168
}
169169
}
170-
is CancellationHandler -> error("It's prohibited to register multiple handlers, tried to register $handler, already has $state")
170+
is CancelHandler -> error("It's prohibited to register multiple handlers, tried to register $handler, already has $state")
171171
is CancelledContinuation -> {
172172
/*
173173
* Continuation is complete, invoke directly.
@@ -188,18 +188,12 @@ internal abstract class AbstractContinuation<in T>(
188188
}
189189
}
190190

191-
private fun makeHandler(handler: CompletionHandler): CancellationHandlerImpl<*> {
192-
if (handler is CancellationHandlerImpl<*>) {
193-
require(handler.continuation === this) { "Handler has non-matching continuation ${handler.continuation}, current: $this" }
194-
return handler
195-
}
196-
197-
return InvokeOnCancel(this, handler)
198-
}
191+
private fun makeHandler(handler: CompletionHandler): CancelHandler =
192+
if (handler is CancelHandler) handler else InvokeOnCancel(handler)
199193

200194
private fun tryCancel(state: NotCompleted, cause: Throwable?): Boolean {
201195
if (useCancellingState) {
202-
require(state !is CancellationHandler) { "Invariant: 'Cancelling' state and cancellation handlers cannot be used together" }
196+
require(state !is CancelHandler) { "Invariant: 'Cancelling' state and cancellation handlers cannot be used together" }
203197
return _state.compareAndSet(state, Cancelling(CancelledContinuation(this, cause)))
204198
}
205199

@@ -342,7 +336,7 @@ internal abstract class AbstractContinuation<in T>(
342336
onCompletionInternal(mode)
343337

344338
// Invoke cancellation handlers only if necessary
345-
if (update is CancelledContinuation && expect is CancellationHandler) {
339+
if (update is CancelledContinuation && expect is CancelHandler) {
346340
try {
347341
expect.invoke(exceptionally?.cause)
348342
} catch (ex: Throwable) {
@@ -382,18 +376,14 @@ private val ACTIVE: Active = Active()
382376
// In progress of cancellation
383377
internal class Cancelling(@JvmField val cancel: CancelledContinuation) : NotCompleted
384378

385-
internal abstract class CancellationHandlerImpl<out C : AbstractContinuation<*>>(@JvmField val continuation: C) :
386-
CancellationHandler(), NotCompleted
379+
internal abstract class CancelHandler : CancelHandlerBase(), NotCompleted
387380

388-
// Wrapper for lambdas, for the performance sake CancellationHandler can be subclassed directly
381+
// Wrapper for lambdas, for the performance sake CancelHandler can be subclassed directly
389382
private class InvokeOnCancel( // Clashes with InvokeOnCancellation
390-
continuation: AbstractContinuation<*>,
391383
private val handler: CompletionHandler
392-
) : CancellationHandlerImpl<AbstractContinuation<*>>(continuation) {
393-
384+
) : CancelHandler() {
394385
override fun invoke(cause: Throwable?) {
395386
handler.invoke(cause)
396387
}
397-
398388
override fun toString() = "InvokeOnCancel[${handler.classSimpleName}@$hexAddress]"
399389
}

common/kotlinx-coroutines-core-common/src/main/kotlin/kotlinx/coroutines/experimental/CancellableContinuation.kt

+8-38
Original file line numberDiff line numberDiff line change
@@ -219,16 +219,8 @@ public fun CancellableContinuation<*>.removeOnCancel(node: LockFreeLinkedListNod
219219
* Removes a given node on cancellation.
220220
* @suppress **This is unstable API and it is subject to change.**
221221
*/
222-
public fun CancellableContinuation<*>.removeOnCancellation(node: LockFreeLinkedListNode): Unit {
223-
val handler: CompletionHandler
224-
if (this is CancellableContinuationImpl<*>) {
225-
handler = RemoveOnCancel(this, node).asHandler
226-
} else {
227-
handler = { node.remove() }
228-
}
229-
230-
invokeOnCancellation(handler)
231-
}
222+
public fun CancellableContinuation<*>.removeOnCancellation(node: LockFreeLinkedListNode) =
223+
invokeOnCancellation(handler = RemoveOnCancel(node).asHandler)
232224

233225
/**
234226
* Disposes a specified [handle] when this continuation is cancelled.
@@ -255,41 +247,19 @@ public fun CancellableContinuation<*>.disposeOnCompletion(handle: DisposableHand
255247
* invokeOnCancellation { handle.dispose() }
256248
* ```
257249
*/
258-
public fun CancellableContinuation<*>.disposeOnCancellation(handle: DisposableHandle) {
259-
val handler: CompletionHandler
260-
if (this is CancellableContinuationImpl<*>) {
261-
handler = DisposeOnCancellation(this, handle).asHandler
262-
} else {
263-
handler = { handle.dispose() }
264-
}
265-
266-
267-
invokeOnCancellation(handler)
268-
}
250+
public fun CancellableContinuation<*>.disposeOnCancellation(handle: DisposableHandle) =
251+
invokeOnCancellation(handler = DisposeOnCancel(handle).asHandler)
269252

270253
// --------------- implementation details ---------------
271254

272-
// TODO: With separate class IDEA fails
273-
private class RemoveOnCancel(
274-
cont: CancellableContinuationImpl<*>,
275-
@JvmField val node: LockFreeLinkedListNode
276-
) : CancellationHandlerImpl<CancellableContinuationImpl<*>>(cont) {
277-
278-
override fun invoke(cause: Throwable?) {
279-
node.remove()
280-
}
281-
255+
private class RemoveOnCancel(private val node: LockFreeLinkedListNode) : CancelHandler() {
256+
override fun invoke(cause: Throwable?) { node.remove() }
282257
override fun toString() = "RemoveOnCancel[$node]"
283258
}
284259

285-
private class DisposeOnCancellation(
286-
continuation: CancellableContinuationImpl<*>,
287-
private val handle: DisposableHandle
288-
) : CancellationHandlerImpl<CancellableContinuationImpl<*>>(continuation) {
289-
260+
private class DisposeOnCancel(private val handle: DisposableHandle) : CancelHandler() {
290261
override fun invoke(cause: Throwable?) = handle.dispose()
291-
292-
override fun toString(): String = "DisposeOnCancellation[$handle]"
262+
override fun toString(): String = "DisposeOnCancel[$handle]"
293263
}
294264

295265
@PublishedApi

common/kotlinx-coroutines-core-common/src/main/kotlin/kotlinx/coroutines/experimental/CompletionHandler.common.kt

+7-5
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,19 @@ public typealias CompletionHandler = (cause: Throwable?) -> Unit
3939

4040
// We want class that extends LockFreeLinkedListNode & CompletionHandler but we cannot do it on Kotlin/JS,
4141
// so this expect class provides us with the corresponding abstraction in a platform-agnostic way.
42-
internal expect abstract class CompletionHandlerNode() : LockFreeLinkedListNode {
43-
val asHandler: CompletionHandler
42+
internal expect abstract class CompletionHandlerBase() : LockFreeLinkedListNode {
4443
abstract fun invoke(cause: Throwable?)
4544
}
4645

47-
// More compact version of CompletionHandlerNode for CancellableContinuation with same workaround for JS
48-
internal expect abstract class CancellationHandler() {
49-
val asHandler: CompletionHandler
46+
internal expect val CompletionHandlerBase.asHandler: CompletionHandler
47+
48+
// More compact version of CompletionHandlerBase for CancellableContinuation with same workaround for JS
49+
internal expect abstract class CancelHandlerBase() {
5050
abstract fun invoke(cause: Throwable?)
5151
}
5252

53+
internal expect val CancelHandlerBase.asHandler: CompletionHandler
54+
5355
// :KLUDGE: We have to invoke a handler in platform-specific way via `invokeIt` extension,
5456
// because we play type tricks on Kotlin/JS and handler is not necessarily a function there
5557
internal expect fun CompletionHandler.invokeIt(cause: Throwable?)

common/kotlinx-coroutines-core-common/src/main/kotlin/kotlinx/coroutines/experimental/JobSupport.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -853,7 +853,7 @@ internal interface Incomplete {
853853

854854
internal abstract class JobNode<out J : Job>(
855855
@JvmField val job: J
856-
) : CompletionHandlerNode(), DisposableHandle, Incomplete {
856+
) : CompletionHandlerBase(), DisposableHandle, Incomplete {
857857
override val isActive: Boolean get() = true
858858
override val list: NodeList? get() = null
859859
override fun dispose() = (job as JobSupport).removeNode(this)

core/kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/CompletionHandler.kt

+6-4
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,17 @@ package kotlinx.coroutines.experimental
1818

1919
import kotlinx.coroutines.experimental.internal.*
2020

21-
internal actual abstract class CompletionHandlerNode actual constructor() : LockFreeLinkedListNode(), CompletionHandler {
22-
actual inline val asHandler: CompletionHandler get() = this
21+
internal actual abstract class CompletionHandlerBase actual constructor() : LockFreeLinkedListNode(), CompletionHandler {
2322
actual abstract override fun invoke(cause: Throwable?)
2423
}
2524

26-
internal actual abstract class CancellationHandler actual constructor() : CompletionHandler {
27-
actual inline val asHandler: CompletionHandler get() = this
25+
internal actual inline val CompletionHandlerBase.asHandler: CompletionHandler get() = this
26+
27+
internal actual abstract class CancelHandlerBase actual constructor() : CompletionHandler {
2828
actual abstract override fun invoke(cause: Throwable?)
2929
}
3030

31+
internal actual inline val CancelHandlerBase.asHandler: CompletionHandler get() = this
32+
3133
@Suppress("NOTHING_TO_INLINE")
3234
internal actual inline fun CompletionHandler.invokeIt(cause: Throwable?) = invoke(cause)

core/kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/Future.kt

+6-16
Original file line numberDiff line numberDiff line change
@@ -54,36 +54,26 @@ public fun CancellableContinuation<*>.cancelFutureOnCompletion(future: Future<*>
5454
* invokeOnCancellation { future.cancel(false) }
5555
* ```
5656
*/
57-
public fun CancellableContinuation<*>.cancelFutureOnCancellation(future: Future<*>) {
58-
if (this is AbstractContinuation<*>) {
59-
invokeOnCancellation(handler = CancelFutureOnCancellation(this, future))
60-
} else {
61-
// Fallback if someone else implement CancellableContinuation
62-
invokeOnCancellation { future.cancel(false) }
63-
}
64-
}
57+
public fun CancellableContinuation<*>.cancelFutureOnCancellation(future: Future<*>) =
58+
invokeOnCancellation(handler = CancelFutureOnCancel(future))
6559

6660
private class CancelFutureOnCompletion(
6761
job: Job,
6862
private val future: Future<*>
6963
) : JobNode<Job>(job) {
70-
override fun invoke(reason: Throwable?) {
64+
override fun invoke(cause: Throwable?) {
7165
// Don't interrupt when cancelling future on completion, because no one is going to reset this
7266
// interruption flag and it will cause spurious failures elsewhere
7367
future.cancel(false)
7468
}
7569
override fun toString() = "CancelFutureOnCompletion[$future]"
7670
}
7771

78-
private class CancelFutureOnCancellation(
79-
continuation: AbstractContinuation<*>,
80-
private val future: Future<*>
81-
) : CancellationHandlerImpl<AbstractContinuation<*>>(continuation) {
82-
83-
override fun invoke(reason: Throwable?) {
72+
private class CancelFutureOnCancel(private val future: Future<*>) : CancelHandler() {
73+
override fun invoke(cause: Throwable?) {
8474
// Don't interrupt when cancelling future on completion, because no one is going to reset this
8575
// interruption flag and it will cause spurious failures elsewhere
8676
future.cancel(false)
8777
}
88-
override fun toString() = "CancelFutureOnCancellation[$future]"
78+
override fun toString() = "CancelFutureOnCancel[$future]"
8979
}

js/kotlinx-coroutines-core-js/src/main/kotlin/kotlinx/coroutines/experimental/CompletionHandler.kt

+11-7
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,25 @@ package kotlinx.coroutines.experimental
1818

1919
import kotlinx.coroutines.experimental.internal.*
2020

21-
internal actual abstract class CompletionHandlerNode : LinkedListNode() {
22-
@Suppress("UnsafeCastFromDynamic")
23-
actual inline val asHandler: CompletionHandler get() = asDynamic()
21+
internal actual abstract class CompletionHandlerBase : LinkedListNode() {
22+
@JsName("invoke")
2423
actual abstract fun invoke(cause: Throwable?)
2524
}
2625

27-
internal actual abstract class CancellationHandler {
28-
@Suppress("UnsafeCastFromDynamic")
29-
actual inline val asHandler: CompletionHandler get() = asDynamic()
26+
@Suppress("UnsafeCastFromDynamic")
27+
internal actual inline val CompletionHandlerBase.asHandler: CompletionHandler get() = asDynamic()
28+
29+
internal actual abstract class CancelHandlerBase {
30+
@JsName("invoke")
3031
actual abstract fun invoke(cause: Throwable?)
3132
}
3233

34+
@Suppress("UnsafeCastFromDynamic")
35+
internal actual inline val CancelHandlerBase.asHandler: CompletionHandler get() = asDynamic()
36+
3337
internal actual fun CompletionHandler.invokeIt(cause: Throwable?) {
3438
when(jsTypeOf(this)) {
3539
"function" -> invoke(cause)
36-
else -> (this as CompletionHandlerNode).invoke(cause)
40+
else -> asDynamic().invoke(cause)
3741
}
3842
}

0 commit comments

Comments
 (0)