Skip to content

Commit

Permalink
Get rid of an additional parent field from ScopeCoroutine
Browse files Browse the repository at this point in the history
    * Leverage already presented information in our implementation, just expose it via already present internal interface
  • Loading branch information
qwwdfsad committed Mar 22, 2021
1 parent d7a56eb commit 45e0652
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 1 deletion.
2 changes: 2 additions & 0 deletions kotlinx-coroutines-core/api/kotlinx-coroutines-core.api
Expand Up @@ -86,6 +86,7 @@ public final class kotlinx/coroutines/CancellableContinuationKt {

public abstract interface class kotlinx/coroutines/ChildHandle : kotlinx/coroutines/DisposableHandle {
public abstract fun childCancelled (Ljava/lang/Throwable;)Z
public abstract fun getParent ()Lkotlinx/coroutines/Job;
}

public abstract interface class kotlinx/coroutines/ChildJob : kotlinx/coroutines/Job {
Expand Down Expand Up @@ -471,6 +472,7 @@ public final class kotlinx/coroutines/NonDisposableHandle : kotlinx/coroutines/C
public static final field INSTANCE Lkotlinx/coroutines/NonDisposableHandle;
public fun childCancelled (Ljava/lang/Throwable;)Z
public fun dispose ()V
public fun getParent ()Lkotlinx/coroutines/Job;
public fun toString ()Ljava/lang/String;
}

Expand Down
11 changes: 11 additions & 0 deletions kotlinx-coroutines-core/common/src/Job.kt
Expand Up @@ -466,6 +466,14 @@ public interface ParentJob : Job {
@InternalCoroutinesApi
@Deprecated(level = DeprecationLevel.ERROR, message = "This is internal API and may be removed in the future releases")
public interface ChildHandle : DisposableHandle {

/**
* Returns the parent of the current parent-child relationship.
* @suppress **This is unstable API and it is subject to change.**
*/
@InternalCoroutinesApi
public val parent: Job?

/**
* Child is cancelling its parent by invoking this method.
* This method is invoked by the child twice. The first time child report its root cause as soon as possible,
Expand Down Expand Up @@ -659,6 +667,9 @@ private fun Throwable?.orCancellation(job: Job): Throwable = this ?: JobCancella
*/
@InternalCoroutinesApi
public object NonDisposableHandle : DisposableHandle, ChildHandle {

override val parent: Job? get() = null

/**
* Does not do anything.
* @suppress
Expand Down
1 change: 1 addition & 0 deletions kotlinx-coroutines-core/common/src/JobSupport.kt
Expand Up @@ -1459,6 +1459,7 @@ private class InvokeOnCancelling(
internal class ChildHandleNode(
@JvmField val childJob: ChildJob
) : JobCancellingNode(), ChildHandle {
override val parent: Job get() = job
override fun invoke(cause: Throwable?) = childJob.parentCancelled(job)
override fun childCancelled(cause: Throwable): Boolean = job.childCancelled(cause)
}
Expand Down
2 changes: 1 addition & 1 deletion kotlinx-coroutines-core/common/src/internal/Scopes.kt
Expand Up @@ -21,7 +21,7 @@ internal open class ScopeCoroutine<in T>(
final override fun getStackTraceElement(): StackTraceElement? = null

final override val isScopedCoroutine: Boolean get() = true
internal val parent: Job? = context[Job]
internal val parent: Job? get() = parentHandle?.parent

override fun afterCompletion(state: Any?) {
// Resume in a cancellable way by default when resuming from another context
Expand Down

0 comments on commit 45e0652

Please sign in to comment.