Skip to content

Commit c12123e

Browse files
committed
AbsractCoroutine moved to common module
1 parent 2adf8bc commit c12123e

File tree

16 files changed

+112
-187
lines changed

16 files changed

+112
-187
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ public expect object Unconfined : CoroutineDispatcher {
2424
}
2525

2626
internal expect inline fun <T> withCoroutineContext(context: CoroutineContext, block: () -> T): T
27-
internal expect fun Continuation<*>.toDebugString(): String
27+
internal expect fun Continuation<*>.toDebugString(): String
28+
internal expect val CoroutineContext.coroutineName: String?

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,31 @@ public expect suspend fun Job.joinChildren()
5959
public expect object NonDisposableHandle : DisposableHandle {
6060
override fun dispose()
6161
}
62+
63+
internal expect open class JobSupport(active: Boolean) : Job {
64+
public final override val key: CoroutineContext.Key<*>
65+
public final override val isActive: Boolean
66+
public final override val isCompleted: Boolean
67+
public final override val isCancelled: Boolean
68+
69+
public final override fun getCancellationException(): CancellationException
70+
public final override fun start(): Boolean
71+
public final override fun cancel(cause: Throwable?): Boolean
72+
public final override val children: Sequence<Job>
73+
74+
public final override fun attachChild(child: Job): DisposableHandle
75+
public final override suspend fun join()
76+
public final override fun invokeOnCompletion(
77+
onCancelling: Boolean,
78+
invokeImmediately: Boolean,
79+
handler: CompletionHandler
80+
): DisposableHandle
81+
82+
internal fun initParentJobInternal(parent: Job?)
83+
internal fun makeCompletingOnce(proposedUpdate: Any?, mode: Int): Boolean
84+
internal open fun afterCompletion(state: Any?, mode: Int)
85+
internal open fun onStartInternal()
86+
internal open fun onCancellationInternal(exceptionally: CompletedExceptionally?)
87+
internal open fun nameString(): String
88+
internal open fun handleException(exception: Throwable)
89+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ internal fun CoroutineContext.updateThreadContext(): String? {
130130
return oldName
131131
}
132132

133-
internal val CoroutineContext.coroutineName: String? get() {
133+
internal actual val CoroutineContext.coroutineName: String? get() {
134134
if (!DEBUG) return null
135135
val coroutineId = this[CoroutineId] ?: return null
136136
val coroutineName = this[CoroutineName]?.name ?: "coroutine"

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

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import kotlinx.coroutines.experimental.internal.OpDescriptor
2424
import kotlinx.coroutines.experimental.internal.unwrap
2525
import kotlinx.coroutines.experimental.intrinsics.startCoroutineUndispatched
2626
import kotlinx.coroutines.experimental.selects.SelectClause0
27-
import kotlinx.coroutines.experimental.selects.SelectClause1
2827
import kotlinx.coroutines.experimental.selects.SelectInstance
2928
import kotlinx.coroutines.experimental.selects.select
3029
import java.util.concurrent.Future
@@ -525,8 +524,8 @@ public actual object NonDisposableHandle : DisposableHandle {
525524
* @param active when `true` the job is created in _active_ state, when `false` in _new_ state. See [Job] for details.
526525
* @suppress **This is unstable API and it is subject to change.**
527526
*/
528-
internal open class JobSupport(active: Boolean) : Job, SelectClause0 {
529-
override val key: CoroutineContext.Key<*> get() = Job
527+
internal actual open class JobSupport actual constructor(active: Boolean) : Job, SelectClause0 {
528+
actual final override val key: CoroutineContext.Key<*> get() = Job
530529

531530
/*
532531
=== Internal states ===
@@ -598,7 +597,7 @@ internal open class JobSupport(active: Boolean) : Job, SelectClause0 {
598597
* It shall be invoked at most once after construction after all other initialization.
599598
* @suppress **This is unstable API and it is subject to change.**
600599
*/
601-
internal fun initParentJobInternal(parent: Job?) {
600+
internal actual fun initParentJobInternal(parent: Job?) {
602601
check(parentHandle == null)
603602
if (parent == null) {
604603
parentHandle = NonDisposableHandle
@@ -637,14 +636,14 @@ internal open class JobSupport(active: Boolean) : Job, SelectClause0 {
637636
}
638637
}
639638

640-
public final override val isActive: Boolean get() {
639+
public actual final override val isActive: Boolean get() {
641640
val state = this.state
642641
return state is Incomplete && state.isActive
643642
}
644643

645-
public final override val isCompleted: Boolean get() = state !is Incomplete
644+
public actual final override val isCompleted: Boolean get() = state !is Incomplete
646645

647-
public final override val isCancelled: Boolean get() {
646+
public actual final override val isCancelled: Boolean get() {
648647
val state = this.state
649648
return state is Cancelled || (state is Finishing && state.cancelled != null)
650649
}
@@ -741,7 +740,7 @@ internal open class JobSupport(active: Boolean) : Job, SelectClause0 {
741740
private fun notifyCancellation(list: NodeList, cause: Throwable?) =
742741
notifyHandlers<JobCancellationNode<*>>(list, cause)
743742

744-
public final override fun start(): Boolean {
743+
public actual final override fun start(): Boolean {
745744
loopOnState { state ->
746745
when (startInternal(state)) {
747746
FALSE -> return false
@@ -775,9 +774,9 @@ internal open class JobSupport(active: Boolean) : Job, SelectClause0 {
775774
* Override to provide the actual [start] action.
776775
* This function is invoked exactly once when non-active coroutine is [started][start].
777776
*/
778-
internal open fun onStartInternal() {}
777+
internal actual open fun onStartInternal() {}
779778

780-
public final override fun getCancellationException(): CancellationException {
779+
public actual final override fun getCancellationException(): CancellationException {
781780
val state = this.state
782781
return when {
783782
state is Finishing && state.cancelled != null ->
@@ -822,7 +821,7 @@ internal open class JobSupport(active: Boolean) : Job, SelectClause0 {
822821
public final override fun invokeOnCompletion(onCancelling_: Boolean, handler: CompletionHandler): DisposableHandle =
823822
invokeOnCompletion(onCancelling = onCancelling_, invokeImmediately = true, handler = handler)
824823

825-
public final override fun invokeOnCompletion(
824+
public actual final override fun invokeOnCompletion(
826825
onCancelling: Boolean,
827826
invokeImmediately: Boolean,
828827
handler: CompletionHandler
@@ -888,7 +887,7 @@ internal open class JobSupport(active: Boolean) : Job, SelectClause0 {
888887
_state.compareAndSet(state, list)
889888
}
890889

891-
public final override suspend fun join() {
890+
public actual final override suspend fun join() {
892891
if (!joinInternal()) { // fast-path no wait
893892
return suspendCoroutineOrReturn { cont ->
894893
cont.context.checkCompletion()
@@ -960,7 +959,7 @@ internal open class JobSupport(active: Boolean) : Job, SelectClause0 {
960959
*/
961960
internal open val onCancelMode: Int get() = ON_CANCEL_MAKE_CANCELLING
962961

963-
public override fun cancel(cause: Throwable?): Boolean = when (onCancelMode) {
962+
public actual override fun cancel(cause: Throwable?): Boolean = when (onCancelMode) {
964963
ON_CANCEL_MAKE_CANCELLED -> makeCancelled(cause)
965964
ON_CANCEL_MAKE_CANCELLING -> makeCancelling(cause)
966965
ON_CANCEL_MAKE_COMPLETING -> makeCompletingOnCancel(cause)
@@ -1045,7 +1044,7 @@ internal open class JobSupport(active: Boolean) : Job, SelectClause0 {
10451044
* @throws IllegalStateException if job is already complete or completing
10461045
* @suppress **This is unstable API and it is subject to change.**
10471046
*/
1048-
internal fun makeCompletingOnce(proposedUpdate: Any?, mode: Int): Boolean =
1047+
internal actual fun makeCompletingOnce(proposedUpdate: Any?, mode: Int): Boolean =
10491048
when (makeCompletingInternal(proposedUpdate, mode)) {
10501049
COMPLETING_COMPLETED -> true
10511050
COMPLETING_WAITING_CHILDREN -> false
@@ -1128,7 +1127,7 @@ internal open class JobSupport(active: Boolean) : Job, SelectClause0 {
11281127
}
11291128
}
11301129

1131-
public final override val children: Sequence<Job> get() = buildSequence {
1130+
public actual final override val children: Sequence<Job> get() = buildSequence {
11321131
val state = this@JobSupport.state
11331132
when (state) {
11341133
is Child -> yield(state.childJob)
@@ -1139,7 +1138,7 @@ internal open class JobSupport(active: Boolean) : Job, SelectClause0 {
11391138
}
11401139

11411140
@Suppress("OverridingDeprecatedMember")
1142-
public final override fun attachChild(child: Job): DisposableHandle =
1141+
public actual final override fun attachChild(child: Job): DisposableHandle =
11431142
invokeOnCompletion(onCancelling = true, handler = Child(this, child))
11441143

11451144
@Suppress("OverridingDeprecatedMember")
@@ -1150,8 +1149,9 @@ internal open class JobSupport(active: Boolean) : Job, SelectClause0 {
11501149
/**
11511150
* Override to process any exceptions that were encountered while invoking completion handlers
11521151
* installed via [invokeOnCompletion].
1152+
* @suppress **This is unstable API and it is subject to change.**
11531153
*/
1154-
internal open fun handleException(exception: Throwable) {
1154+
internal actual open fun handleException(exception: Throwable) {
11551155
throw exception
11561156
}
11571157

@@ -1162,23 +1162,23 @@ internal open class JobSupport(active: Boolean) : Job, SelectClause0 {
11621162
* null when it has completed normally.
11631163
* @suppress **This is unstable API and it is subject to change.**
11641164
*/
1165-
internal open fun onCancellationInternal(exceptionally: CompletedExceptionally?) {}
1165+
internal actual open fun onCancellationInternal(exceptionally: CompletedExceptionally?) {}
11661166

11671167
/**
11681168
* Override for post-completion actions that need to do something with the state.
11691169
* @param mode completion mode.
11701170
* @suppress **This is unstable API and it is subject to change.**
11711171
*/
1172-
internal open fun afterCompletion(state: Any?, mode: Int) {}
1172+
internal actual open fun afterCompletion(state: Any?, mode: Int) {}
11731173

11741174
// for nicer debugging
1175-
override fun toString(): String =
1175+
public override fun toString(): String =
11761176
"${nameString()}{${stateString()}}@$hexAddress"
11771177

11781178
/**
11791179
* @suppress **This is unstable API and it is subject to change.**
11801180
*/
1181-
internal open fun nameString(): String = this::class.java.simpleName
1181+
internal actual open fun nameString(): String = this::class.java.simpleName
11821182

11831183
private fun stateString(): String {
11841184
val state = this.state

js/kotlinx-coroutines-core-js/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
"mocha": "^4.1.0"
55
},
66
"dependencies": {
7-
"source-map-support": "^0.5.0"
7+
"source-map-support": "^0.5.2"
88
}
99
}

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

Lines changed: 0 additions & 55 deletions
This file was deleted.

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ public actual fun launch(
5959
val coroutine = if (start.isLazy)
6060
LazyStandaloneCoroutine(newContext, block) else
6161
StandaloneCoroutine(newContext, active = true)
62-
coroutine.initParentJob(newContext[Job])
63-
start(block, coroutine, coroutine)
62+
coroutine.start(start, coroutine, block)
6463
return coroutine
6564
}
6665

@@ -106,7 +105,7 @@ public actual suspend fun <T> withContext(
106105
context = newContext,
107106
delegate = cont,
108107
resumeMode = if (start == CoroutineStart.ATOMIC) MODE_ATOMIC_DEFAULT else MODE_CANCELLABLE)
109-
completion.initParentJob(newContext[Job]) // attach to job
108+
completion.initParentJobInternal(newContext[Job]) // attach to job
110109
start(block, completion)
111110
completion.getResult()
112111
}
@@ -117,9 +116,9 @@ private open class StandaloneCoroutine(
117116
private val parentContext: CoroutineContext,
118117
active: Boolean
119118
) : AbstractCoroutine<Unit>(parentContext, active) {
120-
override fun onCancellation(exceptionally: CompletedExceptionally?) {
119+
override fun onCancellation(cause: Throwable?) {
121120
// note the use of the parent's job context below!
122-
if (exceptionally != null) handleCoroutineException(parentContext, exceptionally.exception)
121+
if (cause != null) handleCoroutineException(parentContext, cause)
123122
}
124123
}
125124

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ internal class CancellableContinuationImpl<in T>(
202202
get() = _context ?: (delegate.context + this).also { _context = it }
203203

204204
override fun initCancellability() {
205-
initParentJob(delegate.context[Job])
205+
initParentJobInternal(delegate.context[Job])
206206
}
207207

208208
override val onCancelMode: Int get() = ON_CANCEL_MAKE_CANCELLED
@@ -259,8 +259,8 @@ internal class CancellableContinuationImpl<in T>(
259259
override fun <T> getSuccessfulResult(state: Any?): T =
260260
if (state is CompletedIdempotentResult) state.result as T else state as T
261261

262-
override fun toString(): String =
263-
"CancellableContinuation{${stateString()}}[$delegate]"
262+
override fun nameString(): String =
263+
"CancellableContinuation(${delegate.toDebugString()})"
264264

265265
// todo: This workaround for KT-21968, should be removed in the future
266266
public override fun cancel(cause: Throwable?): Boolean =

0 commit comments

Comments
 (0)