Skip to content

Commit

Permalink
Simpler and faster lock-free linked list (#1565)
Browse files Browse the repository at this point in the history
Lock-free list implementation is considerably simplified, taking into
account a limited number of operations that it needs to support.

* prev pointers in the list are not marked for removal, since we
  don't need to support linearizable backwards iteration.
* helpDelete method is completely removed. All "delete-helping" is
  performed only by correctPrev method.
* correctPrev method bails out when the node it works on is removed to
  reduce contention during concurrent removals.
* Special open methods "isRemoved" and "nextIfRemoved" are introduced
  and are overridden in list head class (which is never removed).
  This ensures that on long list "removeFist" operation (touching head)
  does not interfere with "addLast" (touch tail). There is still
  sharing of cache-lines in this case, but no helping between them.

All in all, this improvement reduces the size of implementation code
and makes it considerably faster. Operations on LinkedListChannel are
now much faster (see timings of ChannelSendReceiveStressTest).
  • Loading branch information
elizarov committed Mar 4, 2020
1 parent c684d04 commit 6862afc
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 197 deletions.
Expand Up @@ -7,12 +7,12 @@ package kotlinx.coroutines.channels
import kotlinx.coroutines.*
import kotlinx.coroutines.internal.*
import kotlinx.coroutines.selects.*
import kotlin.native.concurrent.SharedImmutable
import kotlin.native.concurrent.*

/**
* Channel that buffers at most one element and conflates all subsequent `send` and `offer` invocations,
* so that the receiver always gets the most recently sent element.
* Back-to-send sent elements are _conflated_ -- only the the most recently sent element is received,
* Back-to-send sent elements are _conflated_ -- only the most recently sent element is received,
* while previously sent elements **are lost**.
* Sender to this channel never suspends and [offer] always returns `true`.
*
Expand Down

0 comments on commit 6862afc

Please sign in to comment.