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

Method enqueue

deps/concurrentqueue/concurrentqueue.h:1834–1930  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1832
1833 template<AllocationMode allocMode, typename U>
1834 inline bool enqueue(U&& element)
1835 {
1836 index_t currentTailIndex = this->tailIndex.load(std::memory_order_relaxed);
1837 index_t newTailIndex = 1 + currentTailIndex;
1838 if ((currentTailIndex & static_cast<index_t>(BLOCK_SIZE - 1)) == 0) {
1839 // We reached the end of a block, start a new one
1840 auto startBlock = this->tailBlock;
1841 auto originalBlockIndexSlotsUsed = pr_blockIndexSlotsUsed;
1842 if (this->tailBlock != nullptr && this->tailBlock->next->ConcurrentQueue::Block::template is_empty<explicit_context>()) {
1843 // We can re-use the block ahead of us, it's empty!
1844 this->tailBlock = this->tailBlock->next;
1845 this->tailBlock->ConcurrentQueue::Block::template reset_empty<explicit_context>();
1846
1847 // We'll put the block on the block index (guaranteed to be room since we're conceptually removing the
1848 // last block from it first -- except instead of removing then adding, we can just overwrite).
1849 // Note that there must be a valid block index here, since even if allocation failed in the ctor,
1850 // it would have been re-attempted when adding the first block to the queue; since there is such
1851 // a block, a block index must have been successfully allocated.
1852 }
1853 else {
1854 // Whatever head value we see here is >= the last value we saw here (relatively),
1855 // and <= its current value. Since we have the most recent tail, the head must be
1856 // <= to it.
1857 auto head = this->headIndex.load(std::memory_order_relaxed);
1858 assert(!details::circular_less_than<index_t>(currentTailIndex, head));
1859 if (!details::circular_less_than<index_t>(head, currentTailIndex + BLOCK_SIZE)
1860 || (MAX_SUBQUEUE_SIZE != details::const_numeric_max<size_t>::value && (MAX_SUBQUEUE_SIZE == 0 || MAX_SUBQUEUE_SIZE - BLOCK_SIZE < currentTailIndex - head))) {
1861 // We can't enqueue in another block because there's not enough leeway -- the
1862 // tail could surpass the head by the time the block fills up! (Or we'll exceed
1863 // the size limit, if the second part of the condition was true.)
1864 return false;
1865 }
1866 // We're going to need a new block; check that the block index has room
1867 if (pr_blockIndexRaw == nullptr || pr_blockIndexSlotsUsed == pr_blockIndexSize) {
1868 // Hmm, the circular block index is already full -- we'll need
1869 // to allocate a new index. Note pr_blockIndexRaw can only be nullptr if
1870 // the initial allocation failed in the constructor.
1871
1872 MOODYCAMEL_CONSTEXPR_IF (allocMode == CannotAlloc) {
1873 return false;
1874 }
1875 else if (!new_block_index(pr_blockIndexSlotsUsed)) {
1876 return false;
1877 }
1878 }
1879
1880 // Insert a new block in the circular linked list
1881 auto newBlock = this->parent->ConcurrentQueue::template requisition_block<allocMode>();
1882 if (newBlock == nullptr) {
1883 return false;
1884 }
1885#ifdef MCDBGQ_TRACKMEM
1886 newBlock->owner = this;
1887#endif
1888 newBlock->ConcurrentQueue::Block::template reset_empty<explicit_context>();
1889 if (this->tailBlock == nullptr) {
1890 newBlock->next = newBlock;
1891 }

Callers

nothing calls this directly

Calls 2

MOODYCAMEL_CONSTEXPR_IFFunction · 0.85
new_block_indexFunction · 0.85

Tested by

no test coverage detected