| 1145 | // Never allocates. Thread-safe. |
| 1146 | template<typename U> |
| 1147 | bool try_dequeue(U& item) |
| 1148 | { |
| 1149 | // Instead of simply trying each producer in turn (which could cause needless contention on the first |
| 1150 | // producer), we score them heuristically. |
| 1151 | size_t nonEmptyCount = 0; |
| 1152 | ProducerBase* best = nullptr; |
| 1153 | size_t bestSize = 0; |
| 1154 | for (auto ptr = producerListTail.load(std::memory_order_acquire); nonEmptyCount < 3 && ptr != nullptr; ptr = ptr->next_prod()) { |
| 1155 | auto size = ptr->size_approx(); |
| 1156 | if (size > 0) { |
| 1157 | if (size > bestSize) { |
| 1158 | bestSize = size; |
| 1159 | best = ptr; |
| 1160 | } |
| 1161 | ++nonEmptyCount; |
| 1162 | } |
| 1163 | } |
| 1164 | |
| 1165 | // If there was at least one non-empty queue but it appears empty at the time |
| 1166 | // we try to dequeue from it, we need to make sure every queue's been tried |
| 1167 | if (nonEmptyCount > 0) { |
| 1168 | if ((details::likely)(best->dequeue(item))) { |
| 1169 | return true; |
| 1170 | } |
| 1171 | for (auto ptr = producerListTail.load(std::memory_order_acquire); ptr != nullptr; ptr = ptr->next_prod()) { |
| 1172 | if (ptr != best && ptr->dequeue(item)) { |
| 1173 | return true; |
| 1174 | } |
| 1175 | } |
| 1176 | } |
| 1177 | return false; |
| 1178 | } |
| 1179 | |
| 1180 | // Attempts to dequeue from the queue. |
| 1181 | // Returns false if all producer streams appeared empty at the time they |
no test coverage detected