MCPcopy Create free account
hub / github.com/Snapchat/KeyDB / try_dequeue

Method try_dequeue

deps/concurrentqueue/concurrentqueue.h:1104–1135  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1102 // Never allocates. Thread-safe.
1103 template<typename U>
1104 bool try_dequeue(U& item)
1105 {
1106 // Instead of simply trying each producer in turn (which could cause needless contention on the first
1107 // producer), we score them heuristically.
1108 size_t nonEmptyCount = 0;
1109 ProducerBase* best = nullptr;
1110 size_t bestSize = 0;
1111 for (auto ptr = producerListTail.load(std::memory_order_acquire); nonEmptyCount < 3 && ptr != nullptr; ptr = ptr->next_prod()) {
1112 auto size = ptr->size_approx();
1113 if (size > 0) {
1114 if (size > bestSize) {
1115 bestSize = size;
1116 best = ptr;
1117 }
1118 ++nonEmptyCount;
1119 }
1120 }
1121
1122 // If there was at least one non-empty queue but it appears empty at the time
1123 // we try to dequeue from it, we need to make sure every queue's been tried
1124 if (nonEmptyCount > 0) {
1125 if ((details::likely)(best->dequeue(item))) {
1126 return true;
1127 }
1128 for (auto ptr = producerListTail.load(std::memory_order_acquire); ptr != nullptr; ptr = ptr->next_prod()) {
1129 if (ptr != best && ptr->dequeue(item)) {
1130 return true;
1131 }
1132 }
1133 }
1134 return false;
1135 }
1136
1137 // Attempts to dequeue from the queue.
1138 // Returns false if all producer streams appeared empty at the time they

Callers

nothing calls this directly

Calls 3

next_prodMethod · 0.80
dequeueMethod · 0.80
size_approxMethod · 0.45

Tested by

no test coverage detected