Skip to content

Commit

Permalink
Explain why non-constant placeholder is required
Browse files Browse the repository at this point in the history
  • Loading branch information
qwwdfsad committed Jun 15, 2018
1 parent 5f2e94b commit 0cf99bc
Showing 1 changed file with 10 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,17 @@ internal class LockFreeMPSCQueueCore<E : Any>(private val capacity: Int) {

private fun fillPlaceholder(index: Int, element: E): Core<E>? {
val old = array.get(index and mask)
/*
* addLast actions:
* 1) Commit tail slot
* 2) Write element to array slot
* 3) Check for array copy
*
* If copy happened between 2 and 3 then the consumer might have consumed our element,
* then another producer might have written its placeholder in our slot, so we should
* perform *unique* check that current placeholder is our to avoid overwriting another producer placeholder
*/
if (old is Placeholder && old.index == index) {
// that is OUR placeholder and only we can fill it in
array.set(index and mask, element)
// we've corrected missing element, should check if that propagated to further copies, just in case
return this
Expand Down

0 comments on commit 0cf99bc

Please sign in to comment.