diff options
author | Vladimir Sadov <vsadov@microsoft.com> | 2022-11-10 20:02:07 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-10 20:02:07 +0300 |
commit | fee618135e5df9d96ea4de242c968839765efcf7 (patch) | |
tree | c15abd0da7bed7a8d91eb2b5a80b57424be368e6 | |
parent | 97643fd6231c2168d6992c31c7f5293544d7e357 (diff) |
Must read `SequenceNumber` before reading the `Item` (#78142)
-rw-r--r-- | src/libraries/System.Private.CoreLib/src/System/Collections/Concurrent/ConcurrentQueue.cs | 10 |
1 files changed, 4 insertions, 6 deletions
diff --git a/src/libraries/System.Private.CoreLib/src/System/Collections/Concurrent/ConcurrentQueue.cs b/src/libraries/System.Private.CoreLib/src/System/Collections/Concurrent/ConcurrentQueue.cs index 3fa2f71ade7..e700a1fb8a5 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Collections/Concurrent/ConcurrentQueue.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Collections/Concurrent/ConcurrentQueue.cs @@ -528,13 +528,11 @@ namespace System.Collections.Concurrent // 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. |