Skip to content

Commit 45e0652

Browse files
committed
Get rid of an additional parent field from ScopeCoroutine
* Leverage already presented information in our implementation, just expose it via already present internal interface
1 parent d7a56eb commit 45e0652

File tree

4 files changed

+15
-1
lines changed

4 files changed

+15
-1
lines changed

kotlinx-coroutines-core/api/kotlinx-coroutines-core.api

+2
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ public final class kotlinx/coroutines/CancellableContinuationKt {
8686

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

9192
public abstract interface class kotlinx/coroutines/ChildJob : kotlinx/coroutines/Job {
@@ -471,6 +472,7 @@ public final class kotlinx/coroutines/NonDisposableHandle : kotlinx/coroutines/C
471472
public static final field INSTANCE Lkotlinx/coroutines/NonDisposableHandle;
472473
public fun childCancelled (Ljava/lang/Throwable;)Z
473474
public fun dispose ()V
475+
public fun getParent ()Lkotlinx/coroutines/Job;
474476
public fun toString ()Ljava/lang/String;
475477
}
476478

kotlinx-coroutines-core/common/src/Job.kt

+11
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,14 @@ public interface ParentJob : Job {
466466
@InternalCoroutinesApi
467467
@Deprecated(level = DeprecationLevel.ERROR, message = "This is internal API and may be removed in the future releases")
468468
public interface ChildHandle : DisposableHandle {
469+
470+
/**
471+
* Returns the parent of the current parent-child relationship.
472+
* @suppress **This is unstable API and it is subject to change.**
473+
*/
474+
@InternalCoroutinesApi
475+
public val parent: Job?
476+
469477
/**
470478
* Child is cancelling its parent by invoking this method.
471479
* This method is invoked by the child twice. The first time child report its root cause as soon as possible,
@@ -659,6 +667,9 @@ private fun Throwable?.orCancellation(job: Job): Throwable = this ?: JobCancella
659667
*/
660668
@InternalCoroutinesApi
661669
public object NonDisposableHandle : DisposableHandle, ChildHandle {
670+
671+
override val parent: Job? get() = null
672+
662673
/**
663674
* Does not do anything.
664675
* @suppress

kotlinx-coroutines-core/common/src/JobSupport.kt

+1
Original file line numberDiff line numberDiff line change
@@ -1459,6 +1459,7 @@ private class InvokeOnCancelling(
14591459
internal class ChildHandleNode(
14601460
@JvmField val childJob: ChildJob
14611461
) : JobCancellingNode(), ChildHandle {
1462+
override val parent: Job get() = job
14621463
override fun invoke(cause: Throwable?) = childJob.parentCancelled(job)
14631464
override fun childCancelled(cause: Throwable): Boolean = job.childCancelled(cause)
14641465
}

kotlinx-coroutines-core/common/src/internal/Scopes.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ internal open class ScopeCoroutine<in T>(
2121
final override fun getStackTraceElement(): StackTraceElement? = null
2222

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

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

0 commit comments

Comments
 (0)