Skip to content

Commit

Permalink
Must read SequenceNumber before reading the Item (#78178)
Browse files Browse the repository at this point in the history
Co-authored-by: vsadov <8218165+VSadov@users.noreply.github.com>
  • Loading branch information
github-actions[bot] and VSadov authored Nov 14, 2022
1 parent 89161f4 commit af314f3
Showing 1 changed file with 4 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -528,13 +528,11 @@ private static T GetItemWhenAvailable(ConcurrentQueueSegment<T> segment, int i)

// If the expected sequence number is not yet written, we're still waiting for
// an enqueuer to finish storing it. Spin until it's there.
if ((segment._slots[i].SequenceNumber & segment._slotsMask) != expectedSequenceNumberAndMask)
SpinWait spinner = default;
// Must read SequenceNumber before reading Item, thus Volatile.Read
while ((Volatile.Read(ref segment._slots[i].SequenceNumber) & segment._slotsMask) != expectedSequenceNumberAndMask)
{
SpinWait spinner = default;
while ((Volatile.Read(ref segment._slots[i].SequenceNumber) & segment._slotsMask) != expectedSequenceNumberAndMask)
{
spinner.SpinOnce();
}
spinner.SpinOnce();
}

// Return the value from the slot.
Expand Down

0 comments on commit af314f3

Please sign in to comment.