| 191 | // Can be called by any thread at any time. |
| 192 | template<bool NeedSizeEstimate> |
| 193 | unsigned SizeOrNotEmpty() const { |
| 194 | // Emptiness plays critical role in thread pool blocking. So we go to great |
| 195 | // effort to not produce false positives (claim non-empty queue as empty). |
| 196 | unsigned front = front_.load(std::memory_order_acquire); |
| 197 | for (;;) { |
| 198 | // Capture a consistent snapshot of front/tail. |
| 199 | unsigned back = back_.load(std::memory_order_acquire); |
| 200 | unsigned front1 = front_.load(std::memory_order_relaxed); |
| 201 | if (front != front1) { |
| 202 | front = front1; |
| 203 | std::atomic_thread_fence(std::memory_order_acquire); |
| 204 | continue; |
| 205 | } |
| 206 | if (NeedSizeEstimate) { |
| 207 | return CalculateSize(front, back); |
| 208 | } else { |
| 209 | // This value will be 0 if the queue is empty, and undefined otherwise. |
| 210 | unsigned maybe_zero = ((front ^ back) & kMask2); |
| 211 | // Queue size estimate must agree with maybe zero check on the queue |
| 212 | // empty/non-empty state. |
| 213 | eigen_assert((CalculateSize(front, back) == 0) == (maybe_zero == 0)); |
| 214 | return maybe_zero; |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | EIGEN_ALWAYS_INLINE |
| 220 | unsigned CalculateSize(unsigned front, unsigned back) const { |