forked from Kotlin/kotlinx.coroutines
/
LockFreeLinkedList.common.kt
89 lines (77 loc) · 3.62 KB
/
LockFreeLinkedList.common.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
@file:Suppress("NO_EXPLICIT_VISIBILITY_IN_API_MODE")
package kotlinx.coroutines.internal
import kotlin.jvm.*
import kotlin.native.concurrent.*
/** @suppress **This is unstable API and it is subject to change.** */
public expect open class LockFreeLinkedListNode() {
public val isRemoved: Boolean
public val nextNode: LockFreeLinkedListNode
public val prevNode: LockFreeLinkedListNode
public fun addLast(node: LockFreeLinkedListNode)
public fun addOneIfEmpty(node: LockFreeLinkedListNode): Boolean
public inline fun addLastIfPrev(
node: LockFreeLinkedListNode,
predicate: (LockFreeLinkedListNode) -> Boolean
): Boolean
public inline fun addLastIfPrevAndIf(
node: LockFreeLinkedListNode,
predicate: (LockFreeLinkedListNode) -> Boolean, // prev node predicate
crossinline condition: () -> Boolean // atomically checked condition
): Boolean
public open fun remove(): Boolean
/**
* Helps fully finish [remove] operation, must be invoked after [remove] if needed.
* Ensures that traversing the list via prev pointers sees this node as removed.
* No-op on JS
*/
public fun helpRemove()
public fun removeFirstOrNull(): LockFreeLinkedListNode?
public inline fun <reified T> removeFirstIfIsInstanceOfOrPeekIf(predicate: (T) -> Boolean): T?
}
/** @suppress **This is unstable API and it is subject to change.** */
public expect open class LockFreeLinkedListHead() : LockFreeLinkedListNode {
public val isEmpty: Boolean
public inline fun <reified T : LockFreeLinkedListNode> forEach(block: (T) -> Unit)
public final override fun remove(): Nothing
}
/** @suppress **This is unstable API and it is subject to change.** */
public expect open class AddLastDesc<T : LockFreeLinkedListNode>(
queue: LockFreeLinkedListNode,
node: T
) : AbstractAtomicDesc {
val queue: LockFreeLinkedListNode
val node: T
override fun finishPrepare(prepareOp: PrepareOp)
override fun finishOnSuccess(affected: LockFreeLinkedListNode, next: LockFreeLinkedListNode)
}
/** @suppress **This is unstable API and it is subject to change.** */
public expect open class RemoveFirstDesc<T>(queue: LockFreeLinkedListNode): AbstractAtomicDesc {
val queue: LockFreeLinkedListNode
public val result: T
override fun finishPrepare(prepareOp: PrepareOp)
final override fun finishOnSuccess(affected: LockFreeLinkedListNode, next: LockFreeLinkedListNode)
}
/** @suppress **This is unstable API and it is subject to change.** */
public expect abstract class AbstractAtomicDesc : AtomicDesc {
final override fun prepare(op: AtomicOp<*>): Any?
final override fun complete(op: AtomicOp<*>, failure: Any?)
protected open fun failure(affected: LockFreeLinkedListNode): Any?
protected open fun retry(affected: LockFreeLinkedListNode, next: Any): Boolean
public abstract fun finishPrepare(prepareOp: PrepareOp) // non-null on failure
public open fun onPrepare(prepareOp: PrepareOp): Any? // non-null on failure
public open fun onRemoved(affected: LockFreeLinkedListNode) // non-null on failure
protected abstract fun finishOnSuccess(affected: LockFreeLinkedListNode, next: LockFreeLinkedListNode)
}
/** @suppress **This is unstable API and it is subject to change.** */
public expect class PrepareOp: OpDescriptor {
val affected: LockFreeLinkedListNode
override val atomicOp: AtomicOp<*>
val desc: AbstractAtomicDesc
fun finishPrepare()
}
@JvmField
@SharedImmutable
internal val REMOVE_PREPARED: Any = Symbol("REMOVE_PREPARED")