Skip to content

Commit b37fb22

Browse files
committed
Simplify LockFreeLinkedList
1 parent cb79909 commit b37fb22

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

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

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

9999
private tailrec fun findPrevNonRemoved(current: Node): Node {
100100
if (!current.isRemoved) return current
@@ -210,7 +210,7 @@ public actual open class LockFreeLinkedListNode {
210210
val removed = (next as Node).removed()
211211
if (_next.compareAndSet(next, removed)) {
212212
// was removed successfully (linearized remove) -- fixup the list
213-
next.correctPrev(null)
213+
next.correctPrev()
214214
return null
215215
}
216216
}
@@ -250,7 +250,7 @@ public actual open class LockFreeLinkedListNode {
250250
if (next._prev.compareAndSet(nextPrev, this)) {
251251
// This newly added node could have been removed, and the above CAS would have added it physically again.
252252
// Let us double-check for this situation and correct if needed
253-
if (isRemoved) next.correctPrev(null)
253+
if (isRemoved) next.correctPrev()
254254
return
255255
}
256256
}
@@ -268,7 +268,7 @@ public actual open class LockFreeLinkedListNode {
268268
* remover of this node will ultimately call [correctPrev] on the next node and that will fix all
269269
* the links from this node, too.
270270
*/
271-
private tailrec fun correctPrev(op: OpDescriptor?): Node? {
271+
private tailrec fun correctPrev(): Node? {
272272
val oldPrev = _prev.value
273273
var prev: Node = oldPrev
274274
var last: Node? = null // will be set so that last.next === prev
@@ -281,22 +281,21 @@ public actual open class LockFreeLinkedListNode {
281281
// otherwise need to update prev
282282
if (!this._prev.compareAndSet(oldPrev, prev)) {
283283
// Note: retry from scratch on failure to update prev
284-
return correctPrev(op)
284+
return correctPrev()
285285
}
286286
return prev // return the correct prev
287287
}
288288
// slow path when we need to help remove operations
289289
this.isRemoved -> return null // nothing to do, this node was removed, bail out asap to save time
290-
prevNext === op -> return prev // part of the same op -- don't recurse, didn't correct prev
291290
prevNext is OpDescriptor -> { // help & retry
292291
prevNext.perform(prev)
293-
return correctPrev(op) // retry from scratch
292+
return correctPrev() // retry from scratch
294293
}
295294
prevNext is Removed -> {
296295
if (last !== null) {
297296
// newly added (prev) node is already removed, correct last.next around it
298297
if (!last._next.compareAndSet(prev, prevNext.ref)) {
299-
return correctPrev(op) // retry from scratch on failure to update next
298+
return correctPrev() // retry from scratch on failure to update next
300299
}
301300
prev = last
302301
last = null

0 commit comments

Comments
 (0)