Skip to content

Commit a504164

Browse files
committed
Simplify LockFreeLinkedList
1 parent 6c418ea commit a504164

File tree

1 file changed

+7
-12
lines changed

1 file changed

+7
-12
lines changed

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

+7-12
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public actual open class LockFreeLinkedListNode {
9191
// prev.next correction, which does not provide linearizable backwards iteration, but can be used to
9292
// resume forward iteration when current node was removed.
9393
public actual val prevNode: Node
94-
get() = correctPrev(null) ?: findPrevNonRemoved(_prev.value)
94+
get() = correctPrev() ?: findPrevNonRemoved(_prev.value)
9595

9696
private tailrec fun findPrevNonRemoved(current: Node): Node {
9797
if (!current.isRemoved) return current
@@ -207,7 +207,7 @@ public actual open class LockFreeLinkedListNode {
207207
val removed = (next as Node).removed()
208208
if (_next.compareAndSet(next, removed)) {
209209
// was removed successfully (linearized remove) -- fixup the list
210-
next.correctPrev(null)
210+
next.correctPrev()
211211
return null
212212
}
213213
}
@@ -247,14 +247,12 @@ public actual open class LockFreeLinkedListNode {
247247
if (next._prev.compareAndSet(nextPrev, this)) {
248248
// This newly added node could have been removed, and the above CAS would have added it physically again.
249249
// Let us double-check for this situation and correct if needed
250-
if (isRemoved) next.correctPrev(null)
250+
if (isRemoved) next.correctPrev()
251251
return
252252
}
253253
}
254254
}
255255

256-
protected open fun nextIfRemoved(): Node? = (next as? Removed)?.ref
257-
258256
/**
259257
* Returns the corrected value of the previous node while also correcting the `prev` pointer
260258
* (so that `this.prev.next === this`) and helps complete node removals to the left ot this node.
@@ -265,7 +263,7 @@ public actual open class LockFreeLinkedListNode {
265263
* remover of this node will ultimately call [correctPrev] on the next node and that will fix all
266264
* the links from this node, too.
267265
*/
268-
private tailrec fun correctPrev(op: OpDescriptor?): Node? {
266+
private tailrec fun correctPrev(): Node? {
269267
val oldPrev = _prev.value
270268
var prev: Node = oldPrev
271269
var last: Node? = null // will be set so that last.next === prev
@@ -278,22 +276,21 @@ public actual open class LockFreeLinkedListNode {
278276
// otherwise need to update prev
279277
if (!this._prev.compareAndSet(oldPrev, prev)) {
280278
// Note: retry from scratch on failure to update prev
281-
return correctPrev(op)
279+
return correctPrev()
282280
}
283281
return prev // return the correct prev
284282
}
285283
// slow path when we need to help remove operations
286284
this.isRemoved -> return null // nothing to do, this node was removed, bail out asap to save time
287-
prevNext === op -> return prev // part of the same op -- don't recurse, didn't correct prev
288285
prevNext is OpDescriptor -> { // help & retry
289286
prevNext.perform(prev)
290-
return correctPrev(op) // retry from scratch
287+
return correctPrev() // retry from scratch
291288
}
292289
prevNext is Removed -> {
293290
if (last !== null) {
294291
// newly added (prev) node is already removed, correct last.next around it
295292
if (!last._next.compareAndSet(prev, prevNext.ref)) {
296-
return correctPrev(op) // retry from scratch on failure to update next
293+
return correctPrev() // retry from scratch on failure to update next
297294
}
298295
prev = last
299296
last = null
@@ -345,6 +342,4 @@ public actual open class LockFreeLinkedListHead : LockFreeLinkedListNode() {
345342

346343
// optimization: because head is never removed, we don't have to read _next.value to check these:
347344
override val isRemoved: Boolean get() = false
348-
349-
override fun nextIfRemoved(): Node? = null
350345
}

0 commit comments

Comments
 (0)