Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 1 | /* |
Roman Elizarov | 1f74a2d | 2018-06-29 19:19:45 +0300 | [diff] [blame^] | 2 | * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 3 | */ |
| 4 | |
| 5 | package kotlinx.coroutines.experimental.internal |
| 6 | |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 7 | /** @suppress **This is unstable API and it is subject to change.** */ |
| 8 | public expect open class LockFreeLinkedListNode() { |
| 9 | public val isRemoved: Boolean |
| 10 | public val nextNode: LockFreeLinkedListNode |
| 11 | public val prevNode: LockFreeLinkedListNode |
| 12 | public fun addLast(node: LockFreeLinkedListNode) |
| 13 | public fun addOneIfEmpty(node: LockFreeLinkedListNode): Boolean |
| 14 | public inline fun addLastIf(node: LockFreeLinkedListNode, crossinline condition: () -> Boolean): Boolean |
| 15 | public inline fun addLastIfPrev( |
| 16 | node: LockFreeLinkedListNode, |
| 17 | predicate: (LockFreeLinkedListNode) -> Boolean |
| 18 | ): Boolean |
| 19 | |
| 20 | public inline fun addLastIfPrevAndIf( |
| 21 | node: LockFreeLinkedListNode, |
| 22 | predicate: (LockFreeLinkedListNode) -> Boolean, // prev node predicate |
| 23 | crossinline condition: () -> Boolean // atomically checked condition |
| 24 | ): Boolean |
| 25 | |
| 26 | public open fun remove(): Boolean |
| 27 | public fun removeFirstOrNull(): LockFreeLinkedListNode? |
| 28 | public inline fun <reified T> removeFirstIfIsInstanceOfOrPeekIf(predicate: (T) -> Boolean): T? |
| 29 | } |
| 30 | |
| 31 | /** @suppress **This is unstable API and it is subject to change.** */ |
| 32 | public expect open class LockFreeLinkedListHead() : LockFreeLinkedListNode { |
| 33 | public val isEmpty: Boolean |
| 34 | public inline fun <reified T : LockFreeLinkedListNode> forEach(block: (T) -> Unit) |
| 35 | public final override fun remove(): Nothing |
| 36 | } |
| 37 | |
| 38 | /** @suppress **This is unstable API and it is subject to change.** */ |
| 39 | public expect open class AddLastDesc<T : LockFreeLinkedListNode>( |
| 40 | queue: LockFreeLinkedListNode, |
| 41 | node: T |
| 42 | ) : AbstractAtomicDesc { |
| 43 | val queue: LockFreeLinkedListNode |
| 44 | val node: T |
| 45 | protected override fun onPrepare(affected: LockFreeLinkedListNode, next: LockFreeLinkedListNode): Any? |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 46 | override fun finishOnSuccess(affected: LockFreeLinkedListNode, next: LockFreeLinkedListNode) |
| 47 | } |
| 48 | |
Roman Elizarov | 11d6b5b | 2018-04-26 10:11:50 +0300 | [diff] [blame] | 49 | /** @suppress **This is unstable API and it is subject to change.** */ |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 50 | public expect open class RemoveFirstDesc<T>(queue: LockFreeLinkedListNode): AbstractAtomicDesc { |
| 51 | val queue: LockFreeLinkedListNode |
| 52 | public val result: T |
| 53 | protected open fun validatePrepared(node: T): Boolean |
| 54 | protected final override fun onPrepare(affected: LockFreeLinkedListNode, next: LockFreeLinkedListNode): Any? |
| 55 | final override fun finishOnSuccess(affected: LockFreeLinkedListNode, next: LockFreeLinkedListNode) |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /** @suppress **This is unstable API and it is subject to change.** */ |
| 59 | public expect abstract class AbstractAtomicDesc : AtomicDesc { |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 60 | final override fun prepare(op: AtomicOp<*>): Any? |
| 61 | final override fun complete(op: AtomicOp<*>, failure: Any?) |
Vsevolod Tolstopyatov | 9619134 | 2018-04-20 18:13:33 +0300 | [diff] [blame] | 62 | protected open fun failure(affected: LockFreeLinkedListNode, next: Any): Any? |
| 63 | protected open fun retry(affected: LockFreeLinkedListNode, next: Any): Boolean |
| 64 | protected abstract fun onPrepare(affected: LockFreeLinkedListNode, next: LockFreeLinkedListNode): Any? // non-null on failure |
| 65 | protected abstract fun finishOnSuccess(affected: LockFreeLinkedListNode, next: LockFreeLinkedListNode) |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 66 | } |