Skip to content

Commit a331182

Browse files
committed
~ disposeLockFreeLinkedList
1 parent 7bcbe4a commit a331182

File tree

7 files changed

+84
-0
lines changed

7 files changed

+84
-0
lines changed

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

+2
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ public open class JobSupport constructor(active: Boolean) : Job, ChildJob, Paren
237237
assert { casSuccess }
238238
// And process all post-completion actions
239239
completeStateFinalization(state, finalState)
240+
disposeLockFreeLinkedList { state.list } // only needed on Kotlin/Native
240241
return finalState
241242
}
242243

@@ -292,6 +293,7 @@ public open class JobSupport constructor(active: Boolean) : Job, ChildJob, Paren
292293
onCancelling(null) // simple state is not a failure
293294
onCompletionInternal(update)
294295
completeStateFinalization(state, update)
296+
disposeLockFreeLinkedList { state as? JobNode<*> } // only needed on Kotlin/Native
295297
return true
296298
}
297299

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/*
2+
* Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package kotlinx.coroutines.internal
6+
7+
internal expect fun disposeLockFreeLinkedList(list: () -> LockFreeLinkedListNode?) // only needed on Kotlin/Native

kotlinx-coroutines-core/concurrent/src/internal/LockFreeLinkedList.kt

+14
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,20 @@ public actual open class LockFreeLinkedListNode {
668668
assert { next === this._next.value }
669669
}
670670

671+
/**
672+
* Only needed on Kotlin/Native. See [disposeLockFreeLinkedList].
673+
*/
674+
internal fun unlinkRefs(nullRef: Node) {
675+
_next.value = nullRef
676+
_prev.value = nullRef
677+
_removedRef.value = nullRef.removed()
678+
}
679+
680+
/**
681+
* Only needed on Kotlin/Native. See [disposeLockFreeLinkedList].
682+
*/
683+
internal fun initRemoved() { removed() }
684+
671685
override fun toString(): String = "$classSimpleName@$hexAddress"
672686
}
673687

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/*
2+
* Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package kotlinx.coroutines.internal
6+
7+
@Suppress("NOTHING_TO_INLINE")
8+
internal actual inline fun disposeLockFreeLinkedList(list: () -> LockFreeLinkedListNode?) {} // only needed on Kotlin/Native
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
@file:Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER")
6+
7+
package kotlinx.coroutines.internal
8+
9+
import kotlin.internal.InlineOnly
10+
11+
@InlineOnly
12+
@Suppress("NOTHING_TO_INLINE") // Should be NOP
13+
internal actual inline fun disposeLockFreeLinkedList(list: () -> LockFreeLinkedListNode?) {} // only needed on Kotlin/Native

kotlinx-coroutines-core/jvm/src/internal/Sharing.kt

+14
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,60 @@
22
* Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
33
*/
44

5+
@file:Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER")
6+
57
package kotlinx.coroutines.internal
68

79
import kotlinx.coroutines.*
810
import kotlin.coroutines.*
911
import kotlin.coroutines.intrinsics.*
12+
import kotlin.internal.InlineOnly
1013

14+
@InlineOnly
1115
@Suppress("NOTHING_TO_INLINE") // Should be NOP
1216
internal actual inline fun DisposableHandle.asShareable(): DisposableHandle = this
1317

18+
@InlineOnly
1419
@Suppress("NOTHING_TO_INLINE") // Should be NOP
1520
internal actual inline fun CoroutineDispatcher.asShareable(): CoroutineDispatcher = this
1621

22+
@InlineOnly
1723
@Suppress("NOTHING_TO_INLINE") // Should be NOP
1824
internal actual inline fun <T> Continuation<T>.asShareable() : Continuation<T> = this
1925

26+
@InlineOnly
2027
@Suppress("NOTHING_TO_INLINE") // Should be NOP
2128
internal actual inline fun <T> Continuation<T>.asLocal() : Continuation<T> = this
2229

30+
@InlineOnly
2331
@Suppress("NOTHING_TO_INLINE") // Should be NOP
2432
internal actual inline fun <T> Continuation<T>.asLocalOrNull() : Continuation<T>? = this
2533

34+
@InlineOnly
2635
@Suppress("NOTHING_TO_INLINE") // Should be NOP
2736
internal actual inline fun <T> Continuation<T>.useLocal() : Continuation<T> = this
2837

38+
@InlineOnly
2939
@Suppress("NOTHING_TO_INLINE") // Should be NOP
3040
internal actual inline fun <T> Continuation<T>.shareableInterceptedResumeCancellableWith(result: Result<T>) {
3141
intercepted().resumeCancellableWith(result)
3242
}
3343

44+
@InlineOnly
3445
@Suppress("NOTHING_TO_INLINE") // Save an entry on call stack
3546
internal actual inline fun <T> CancellableContinuationImpl<T>.shareableResume(delegate: Continuation<T>, useMode: Int) =
3647
resumeImpl(delegate, useMode)
3748

49+
@InlineOnly
3850
@Suppress("NOTHING_TO_INLINE") // Save an entry on call stack
3951
internal actual inline fun isReuseSupportedInPlatform() = true
4052

53+
@InlineOnly
4154
internal actual inline fun <T> ArrayList<T>.addOrUpdate(element: T, update: (ArrayList<T>) -> Unit) {
4255
add(element)
4356
}
4457

58+
@InlineOnly
4559
internal actual inline fun <T> ArrayList<T>.addOrUpdate(index: Int, element: T, update: (ArrayList<T>) -> Unit) {
4660
add(index, element)
4761
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package kotlinx.coroutines.internal
6+
7+
import kotlin.native.concurrent.*
8+
9+
@Suppress("NOTHING_TO_INLINE")
10+
internal actual inline fun disposeLockFreeLinkedList(list: () -> LockFreeLinkedListNode?) {
11+
// only needed on Kotlin/Native
12+
val head = list() ?: return
13+
var cur = head
14+
do {
15+
val next = cur.nextNode
16+
cur.unlinkRefs(NullNodeRef)
17+
cur = next
18+
} while (cur !== head)
19+
}
20+
21+
private object NullNodeRef : LockFreeLinkedListNode() {
22+
init {
23+
initRemoved()
24+
freeze()
25+
}
26+
}

0 commit comments

Comments
 (0)