UnboundedQueue: Fix advanceHead
[folly.git] / folly / concurrency / UnboundedQueue.h
index 1d9e5cb4f6b9398a100a6253cbd929a60bd8823d..77bc5b9fa7d005dfb5f022608bffd1685fbfa115 100644 (file)
@@ -500,6 +500,13 @@ class UnboundedQueue {
     auto deadline = std::chrono::steady_clock::time_point::max();
     Segment* next = tryGetNextSegmentUntil(s, deadline);
     DCHECK(next != nullptr);
+    while (head() != s) {
+      // Wait for head to advance to the current segment first before
+      // advancing head to the next segment. Otherwise, a lagging
+      // consumer responsible for advancing head from an earlier
+      // segment may incorrectly set head back.
+      asm_volatile_pause();
+    }
     setHead(next);
     reclaimSegment(s);
   }