@@ -108,6 +108,9 @@ public actual open class LockFreeLinkedListNode {
108
108
public actual val prevNode: Node
109
109
get() = correctPrev(null ) ? : findPrevNonRemoved(_prev .value)
110
110
111
+ public actual val prevNodeIfNotRemoved: LockFreeLinkedListNode ?
112
+ get() = correctPrev(null )
113
+
111
114
private tailrec fun findPrevNonRemoved (current : Node ): Node {
112
115
if (! current.isRemoved) return current
113
116
return findPrevNonRemoved(current._prev .value)
@@ -258,21 +261,13 @@ public actual open class LockFreeLinkedListNode {
258
261
// Helps with removal of this node
259
262
public actual fun helpRemove () {
260
263
// Note: this node must be already removed
261
- (next as Removed ).ref.helpRemovePrev( )
264
+ (next as Removed ).ref.correctPrev( null )
262
265
}
263
266
264
267
// Helps with removal of nodes that are previous to this
265
- private fun helpRemovePrev () {
266
- // We need to call correctPrev on a non-removed node to ensure progress, since correctPrev bails out when
267
- // called on a removed node. There's always at least one non-removed node (list head).
268
- var node = this
269
- while (true ) {
270
- val next = node.next
271
- if (next !is Removed ) break
272
- node = next.ref
273
- }
274
- // Found a non-removed node
275
- node.correctPrev(null )
268
+ @PublishedApi
269
+ internal fun helpRemovePrev () {
270
+ correctPrev(null )
276
271
}
277
272
278
273
public actual fun removeFirstOrNull (): Node ? {
@@ -309,13 +304,14 @@ public actual open class LockFreeLinkedListNode {
309
304
* However, we limit the number of attempts to move to the next node and help with removal at the end
310
305
* to avoid repeatedly scanning very long lists in LinkedListChannel.
311
306
*/
312
- if (moveNextAttempts++ < 32 ) {
307
+ if (moveNextAttempts++ < 4 ) {
313
308
first = next
314
309
} else {
315
- break // help and start from the beginning
310
+ // help and start from the beginning
311
+ next.helpRemovePrev()
312
+ break
316
313
}
317
314
}
318
- first.helpRemove() // help remove this one and retry from the beginning of the list
319
315
}
320
316
}
321
317
0 commit comments