MCPcopy Create free account
hub / github.com/dmlc/dmlc-core / enqueue

Method enqueue

include/dmlc/concurrentqueue.h:1799–1892  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1797
1798 template<AllocationMode allocMode, typename U>
1799 inline bool enqueue(U &&element) {
1800 index_t currentTailIndex = this->tailIndex.load(std::memory_order_relaxed);
1801 index_t newTailIndex = 1 + currentTailIndex;
1802 if ((currentTailIndex & static_cast<index_t>(BLOCK_SIZE - 1)) == 0) {
1803 // We reached the end of a block, start a new one
1804 auto startBlock = this->tailBlock;
1805 auto originalBlockIndexSlotsUsed = pr_blockIndexSlotsUsed;
1806 if (this->tailBlock != nullptr &&
1807 this->tailBlock->next->template is_empty<explicit_context>()) {
1808 // We can re-use the block ahead of us, it's empty!
1809 this->tailBlock = this->tailBlock->next;
1810 this->tailBlock->template reset_empty<explicit_context>();
1811
1812 // We'll put the block on the block index (guaranteed to be room since we're conceptually removing the
1813 // last block from it first -- except instead of removing then adding, we can just overwrite).
1814 // Note that there must be a valid block index here, since even if allocation failed in the ctor,
1815 // it would have been re-attempted when adding the first block to the queue; since there is such
1816 // a block, a block index must have been successfully allocated.
1817 } else {
1818 // Whatever head value we see here is >= the last value we saw here (relatively),
1819 // and <= its current value. Since we have the most recent tail, the head must be
1820 // <= to it.
1821 auto head = this->headIndex.load(std::memory_order_relaxed);
1822 assert(!details::circular_less_than<index_t>(currentTailIndex, head));
1823 if (!details::circular_less_than<index_t>(head, currentTailIndex + BLOCK_SIZE)
1824 || (MAX_SUBQUEUE_SIZE != details::const_numeric_max<size_t>::value &&
1825 (MAX_SUBQUEUE_SIZE == 0 ||
1826 MAX_SUBQUEUE_SIZE - BLOCK_SIZE < currentTailIndex - head))) {
1827 // We can't enqueue in another block because there's not enough leeway -- the
1828 // tail could surpass the head by the time the block fills up! (Or we'll exceed
1829 // the size limit, if the second part of the condition was true.)
1830 return false;
1831 }
1832 // We're going to need a new block; check that the block index has room
1833 if (pr_blockIndexRaw == nullptr || pr_blockIndexSlotsUsed == pr_blockIndexSize) {
1834 // Hmm, the circular block index is already full -- we'll need
1835 // to allocate a new index. Note pr_blockIndexRaw can only be nullptr if
1836 // the initial allocation failed in the constructor.
1837
1838 if (allocMode == CannotAlloc || !new_block_index(pr_blockIndexSlotsUsed)) {
1839 return false;
1840 }
1841 }
1842
1843 // Insert a new block in the circular linked list
1844 auto newBlock = this->parent->ConcurrentQueue::template requisition_block<allocMode>();
1845 if (newBlock == nullptr) {
1846 return false;
1847 }
1848#if MCDBGQ_TRACKMEM
1849 newBlock->owner = this;
1850#endif
1851 newBlock->template reset_empty<explicit_context>();
1852 if (this->tailBlock == nullptr) {
1853 newBlock->next = newBlock;
1854 } else {
1855 newBlock->next = this->tailBlock->next;
1856 this->tailBlock->next = newBlock;

Callers

nothing calls this directly

Calls 1

new_block_indexFunction · 0.85

Tested by

no test coverage detected