in kotlinx-coroutines-core/common/src/internal/ConcurrentLinkedList.kt [148:166]
fun remove() {
assert { isRemoved || isTail } // The node should be logically removed at first.
// The physical tail cannot be removed. Instead, we remove it when
// a new segment is added and this segment is not the tail one anymore.
if (isTail) return
while (true) {
// Read `next` and `prev` pointers ignoring logically removed nodes.
val prev = aliveSegmentLeft
val next = aliveSegmentRight
// Link `next` and `prev`.
next._prev.update { if (it === null) null else prev }
if (prev !== null) prev._next.value = next
// Checks that prev and next are still alive.
if (next.isRemoved && !next.isTail) continue
if (prev !== null && prev.isRemoved) continue
// This node is removed.
return
}
}