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