Skip to content

Commit 55888f2

Browse files
committed
JobSupport is an internal class (its contracts are quite complex for public use)
1 parent daa7922 commit 55888f2

File tree

5 files changed

+36
-41
lines changed

5 files changed

+36
-41
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ private class StandaloneCoroutine(
7777
}
7878
}
7979

80-
private class InnerCoroutine<T>(
80+
private class InnerCoroutine<in T>(
8181
override val context: CoroutineContext,
8282
continuation: Continuation<T>
8383
) : Continuation<T> by continuation, CoroutineScope {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public interface CoroutineScope {
2525
* It stores the result of continuation in the state of the job.
2626
*/
2727
@Suppress("LeakingThis")
28-
public abstract class AbstractCoroutine<in T>(context: CoroutineContext) : JobSupport(), Continuation<T>, CoroutineScope {
28+
internal abstract class AbstractCoroutine<in T>(context: CoroutineContext) : JobSupport(), Continuation<T>, CoroutineScope {
2929
override val context: CoroutineContext = context + this // merges this job into this context
3030

3131
final override fun resume(value: T) {

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

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,7 @@ public suspend fun Job.join() {
123123
* This is an open class designed for extension by more specific classes that might augment the
124124
* state and mare store addition state information for completed jobs, like their result values.
125125
*/
126-
@Suppress("LeakingThis")
127-
public open class JobSupport : AbstractCoroutineContextElement(Job), Job {
126+
internal open class JobSupport : AbstractCoroutineContextElement(Job), Job {
128127
/*
129128
=== States ===
130129
name state class is Active?
@@ -176,7 +175,7 @@ public open class JobSupport : AbstractCoroutineContextElement(Job), Job {
176175
* Initializes parent job.
177176
* It shall be invoked at most once after construction after all other initialization.
178177
*/
179-
public fun initParentJob(parent: Job?) {
178+
fun initParentJob(parent: Job?) {
180179
if (parent == null) return
181180
check(registration == null)
182181
// directly pass HandlerNode to parent scope to optimize one closure object (see makeNode)
@@ -189,12 +188,12 @@ public open class JobSupport : AbstractCoroutineContextElement(Job), Job {
189188
/**
190189
* Returns current state of this job.
191190
*/
192-
internal fun getState(): Any? = state
191+
fun getState(): Any? = state
193192

194193
/**
195194
* Tries to update current [state][getState] of this job.
196195
*/
197-
protected fun updateState(expect: Any, update: Any?): Boolean {
196+
fun updateState(expect: Any, update: Any?): Boolean {
198197
require(expect is Active && update !is Active) // only active -> inactive transition is allowed
199198
if (!STATE.compareAndSet(this, expect, update)) return false
200199
// #1. Update linked state before invoking completion handlers
@@ -229,9 +228,9 @@ public open class JobSupport : AbstractCoroutineContextElement(Job), Job {
229228
return true
230229
}
231230

232-
public final override val isActive: Boolean get() = state is Active
231+
final override val isActive: Boolean get() = state is Active
233232

234-
public final override fun onCompletion(handler: CompletionHandler): Job.Registration {
233+
final override fun onCompletion(handler: CompletionHandler): Job.Registration {
235234
var nodeCache: JobNode? = null
236235
while (true) { // lock-free loop on state
237236
val state = this.state
@@ -265,7 +264,7 @@ public open class JobSupport : AbstractCoroutineContextElement(Job), Job {
265264
}
266265
}
267266

268-
internal fun removeNode(node: JobNode) {
267+
fun removeNode(node: JobNode) {
269268
// remove logic depends on the state of the job
270269
while (true) { // lock-free loop on job state
271270
val state = this.state
@@ -290,7 +289,7 @@ public open class JobSupport : AbstractCoroutineContextElement(Job), Job {
290289
}
291290
}
292291

293-
public final override fun cancel(reason: Throwable?): Boolean {
292+
final override fun cancel(reason: Throwable?): Boolean {
294293
while (true) { // lock-free loop on state
295294
val state = this.state as? Active ?: return false // quit if not active anymore
296295
if (updateState(state, Cancelled(reason))) return true
@@ -300,19 +299,19 @@ public open class JobSupport : AbstractCoroutineContextElement(Job), Job {
300299
/**
301300
* Override to make linked state changes before completion handlers are invoked.
302301
*/
303-
protected open fun onStateUpdate(update: Any?) {}
302+
open fun onStateUpdate(update: Any?) {}
304303

305304
/**
306305
* Override to process any exceptions that were encountered while invoking [onCompletion] handlers.
307306
*/
308-
protected open fun handleCompletionException(closeException: Throwable) {
307+
open fun handleCompletionException(closeException: Throwable) {
309308
throw closeException
310309
}
311310

312311
/**
313312
* Override for post-completion actions that need to do something with the state.
314313
*/
315-
protected open fun afterCompletion(state: Any?) {}
314+
open fun afterCompletion(state: Any?) {}
316315

317316
private fun makeNode(handler: CompletionHandler): JobNode =
318317
(handler as? JobNode)?.also { require(it.job === this) }
@@ -321,7 +320,7 @@ public open class JobSupport : AbstractCoroutineContextElement(Job), Job {
321320
/**
322321
* Marker interface for active [state][getState] of a job.
323322
*/
324-
public interface Active
323+
internal interface Active
325324

326325
private object Empty : Active
327326

@@ -330,15 +329,15 @@ public open class JobSupport : AbstractCoroutineContextElement(Job), Job {
330329
/**
331330
* Abstract class for a [state][getState] of a job that had completed exceptionally, including cancellation.
332331
*/
333-
public abstract class CompletedExceptionally {
332+
internal abstract class CompletedExceptionally {
334333
abstract val cancelReason: Throwable // original reason or fresh CancellationException
335334
abstract val exception: Throwable // the exception to be thrown in continuation
336335
}
337336

338337
/**
339338
* Represents a [state][getState] of a cancelled job.
340339
*/
341-
public class Cancelled(specifiedReason: Throwable?) : CompletedExceptionally() {
340+
internal class Cancelled(specifiedReason: Throwable?) : CompletedExceptionally() {
342341
@Volatile
343342
private var _cancelReason = specifiedReason // materialize CancellationException on first need
344343

@@ -359,7 +358,7 @@ public open class JobSupport : AbstractCoroutineContextElement(Job), Job {
359358
/**
360359
* Represents a [state][getState] of a failed job.
361360
*/
362-
public class Failed(override val exception: Throwable) : CompletedExceptionally() {
361+
internal class Failed(override val exception: Throwable) : CompletedExceptionally() {
363362
override val cancelReason: Throwable
364363
get() = exception
365364
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import kotlin.coroutines.startCoroutine
1717
* If this lazy deferred value is [cancelled][cancel], then it becomes immediately complete and
1818
* cancels ongoing computation coroutine if it was started.
1919
*/
20-
public interface LazyDeferred<T> : Deferred<T> {
20+
public interface LazyDeferred<out T> : Deferred<T> {
2121
/**
2222
* Returns `true` if the coroutine is computing its value.
2323
*/
@@ -41,7 +41,7 @@ public interface LazyDeferred<T> : Deferred<T> {
4141
* in which case the [Job] of the resulting coroutine is a child of the job of the parent coroutine.
4242
*/
4343
public fun <T> lazyDefer(context: CoroutineContext, block: suspend CoroutineScope.() -> T) : LazyDeferred<T> =
44-
LazyDeferredCoroutine<T>(newCoroutineContext(context), block).apply {
44+
LazyDeferredCoroutine(newCoroutineContext(context), block).apply {
4545
initParentJob(context[Job])
4646
}
4747

kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/internal/LockFreeLinkedList.kt

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ internal open class LockFreeLinkedListNode {
4141
private fun removed(): Removed =
4242
removedRef ?: Removed(this).also { REMOVED_REF.lazySet(this, it) }
4343

44-
@PublishedApi
45-
internal abstract class CondAdd {
44+
abstract class CondAdd {
4645
internal lateinit var newNode: Node
4746
internal lateinit var oldNext: Node
4847
@Volatile
@@ -80,7 +79,7 @@ internal open class LockFreeLinkedListNode {
8079
}
8180
}
8281

83-
public val isRemoved: Boolean get() = _next is Removed
82+
val isRemoved: Boolean get() = _next is Removed
8483

8584
private val isFresh: Boolean get() = _next === this && prev === this
8685

@@ -92,11 +91,9 @@ internal open class LockFreeLinkedListNode {
9291
}
9392
}
9493

95-
@PublishedApi
96-
internal fun next(): Node = next.unwrap()
94+
fun next(): Node = next.unwrap()
9795

98-
@PublishedApi
99-
internal fun addFirstCC(node: Node, condAdd: CondAdd?): Boolean {
96+
fun addFirstCC(node: Node, condAdd: CondAdd?): Boolean {
10097
require(node.isFresh)
10198
condAdd?.newNode = node
10299
while (true) { // lock-free loop on next
@@ -111,7 +108,7 @@ internal open class LockFreeLinkedListNode {
111108
}
112109
}
113110

114-
internal fun addIfEmpty(node: Node): Boolean {
111+
fun addIfEmpty(node: Node): Boolean {
115112
require(node.isFresh)
116113
PREV.lazySet(node, this)
117114
NEXT.lazySet(node, this)
@@ -121,8 +118,7 @@ internal open class LockFreeLinkedListNode {
121118
return true
122119
}
123120

124-
@PublishedApi
125-
internal fun addLastCC(node: Node, condAdd: CondAdd?): Boolean {
121+
fun addLastCC(node: Node, condAdd: CondAdd?): Boolean {
126122
require(node.isFresh)
127123
condAdd?.newNode = node
128124
while (true) { // lock-free loop on prev.next
@@ -158,7 +154,7 @@ internal open class LockFreeLinkedListNode {
158154
/**
159155
* Removes this node from the list. Returns `true` when removed successfully.
160156
*/
161-
public open fun remove(): Boolean {
157+
open fun remove(): Boolean {
162158
while (true) { // lock-free loop on next
163159
val next = this.next
164160
if (next is Removed) return false // was already removed -- don't try to help (original thread will take care)
@@ -171,7 +167,7 @@ internal open class LockFreeLinkedListNode {
171167
}
172168
}
173169

174-
internal fun removeFirstOrNull(): Node? {
170+
fun removeFirstOrNull(): Node? {
175171
while (true) { // try to linearize
176172
val first = next()
177173
if (first == this) return null
@@ -260,19 +256,19 @@ internal open class LockFreeLinkedListNode {
260256

261257
private fun Any.unwrap(): Node = if (this is Removed) ref else this as Node
262258

263-
internal fun validateNode(prev: Node, next: Node) {
259+
fun validateNode(prev: Node, next: Node) {
264260
check(prev === this.prev)
265261
check(next === this.next)
266262
}
267263
}
268264

269265
internal open class LockFreeLinkedListHead : LockFreeLinkedListNode() {
270-
public val isEmpty: Boolean get() = next() == this
266+
val isEmpty: Boolean get() = next() == this
271267

272268
/**
273269
* Iterates over all elements in this list of a specified type.
274270
*/
275-
public inline fun <reified T : Node> forEach(block: (T) -> Unit) {
271+
inline fun <reified T : Node> forEach(block: (T) -> Unit) {
276272
var cur: Node = next()
277273
while (cur != this) {
278274
if (cur is T) block(cur)
@@ -283,32 +279,32 @@ internal open class LockFreeLinkedListHead : LockFreeLinkedListNode() {
283279
/**
284280
* Adds first item to this list.
285281
*/
286-
public fun addFirst(node: Node) { addFirstCC(node, null) }
282+
fun addFirst(node: Node) { addFirstCC(node, null) }
287283

288284
/**
289285
* Adds first item to this list atomically if the [condition] is true.
290286
*/
291-
public inline fun addFirstIf(node: Node, crossinline condition: () -> Boolean): Boolean =
287+
inline fun addFirstIf(node: Node, crossinline condition: () -> Boolean): Boolean =
292288
addFirstCC(node, object : CondAdd() {
293289
override fun isCondition(): Boolean = condition()
294290
})
295291

296292
/**
297293
* Adds last item to this list.
298294
*/
299-
public fun addLast(node: Node) { addLastCC(node, null) }
295+
fun addLast(node: Node) { addLastCC(node, null) }
300296

301297
/**
302298
* Adds last item to this list atomically if the [condition] is true.
303299
*/
304-
public inline fun addLastIf(node: Node, crossinline condition: () -> Boolean): Boolean =
300+
inline fun addLastIf(node: Node, crossinline condition: () -> Boolean): Boolean =
305301
addLastCC(node, object : CondAdd() {
306302
override fun isCondition(): Boolean = condition()
307303
})
308304

309-
public override fun remove() = throw UnsupportedOperationException()
305+
final override fun remove() = throw UnsupportedOperationException()
310306

311-
internal fun validate() {
307+
fun validate() {
312308
var prev: Node = this
313309
var cur: Node = next()
314310
while (cur != this) {

0 commit comments

Comments
 (0)