Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
duckdb_moodycamel::ConcurrentQueue< T, Traits >::ExplicitProducer Struct Reference
Inheritance diagram for duckdb_moodycamel::ConcurrentQueue< T, Traits >::ExplicitProducer:
Collaboration diagram for duckdb_moodycamel::ConcurrentQueue< T, Traits >::ExplicitProducer:

Classes

struct  BlockIndexEntry
struct  BlockIndexHeader

Public Member Functions

 ExplicitProducer (ConcurrentQueue *parent_)
template<AllocationMode allocMode, typename U>
bool enqueue (U &&element)
template<typename U>
bool dequeue (U &element)
template<AllocationMode allocMode, typename It>
bool enqueue_bulk (It itemFirst, size_t count)
template<typename It>
size_t dequeue_bulk (It &itemFirst, size_t max)
Public Member Functions inherited from duckdb_moodycamel::ConcurrentQueue< T, Traits >::ProducerBase
 ProducerBase (ConcurrentQueue *parent_, bool isExplicit_)
template<typename U>
bool dequeue (U &element)
template<typename It>
size_t dequeue_bulk (It &itemFirst, size_t max)
ProducerBase * next_prod () const
size_t size_approx () const
index_t getTail () const

Private Member Functions

bool new_block_index (size_t numberOfFilledSlotsToExpose)

Private Attributes

std::atomic< BlockIndexHeader * > blockIndex
size_t pr_blockIndexSlotsUsed
size_t pr_blockIndexSize
size_t pr_blockIndexFront
BlockIndexEntry * pr_blockIndexEntries
void * pr_blockIndexRaw

Additional Inherited Members

Public Attributes inherited from duckdb_moodycamel::ConcurrentQueue< T, Traits >::ProducerBase
bool isExplicit
ConcurrentQueue * parent
Public Attributes inherited from duckdb_moodycamel::details::ConcurrentQueueProducerTypelessBase
ConcurrentQueueProducerTypelessBase * next
std::atomic< bool > inactive
ProducerToken * token
Protected Attributes inherited from duckdb_moodycamel::ConcurrentQueue< T, Traits >::ProducerBase
std::atomic< index_t > tailIndex
std::atomic< index_t > headIndex
std::atomic< index_t > dequeueOptimisticCount
std::atomic< index_t > dequeueOvercommit
Block * tailBlock

Constructor & Destructor Documentation

◆ ExplicitProducer()

template<typename T, typename Traits = ConcurrentQueueDefaultTraits>
duckdb_moodycamel::ConcurrentQueue< T, Traits >::ExplicitProducer::ExplicitProducer ( ConcurrentQueue * parent_)
inlineexplicit
332518 :
332519 ProducerBase(parent_, true),
332520 blockIndex(nullptr),
332521 pr_blockIndexSlotsUsed(0),
332522 pr_blockIndexSize(EXPLICIT_INITIAL_INDEX_SIZE >> 1),
332523 pr_blockIndexFront(0),
332524 pr_blockIndexEntries(nullptr),
332525 pr_blockIndexRaw(nullptr)
332526 {
332527 size_t poolBasedIndexSize = details::ceil_to_pow_2(parent_->initialBlockPoolSize) >> 1;
332528 if (poolBasedIndexSize > pr_blockIndexSize) {
332529 pr_blockIndexSize = poolBasedIndexSize;
332530 }
332531
332532 new_block_index(0); // This creates an index with double the number of current entries, i.e. EXPLICIT_INITIAL_INDEX_SIZE
332533 }
Definition duckdb.cpp:331497

◆ ~ExplicitProducer()

template<typename T, typename Traits = ConcurrentQueueDefaultTraits>
duckdb_moodycamel::ConcurrentQueue< T, Traits >::ExplicitProducer::~ExplicitProducer ( )
inline
332536 {
332537 // Destruct any elements not yet dequeued.
332538 // Since we're in the destructor, we can assume all elements
332539 // are either completely dequeued or completely not (no halfways).
332540 if (this->tailBlock != nullptr) { // Note this means there must be a block index too
332541 // First find the block that's partially dequeued, if any
332542 Block* halfDequeuedBlock = nullptr;
332543 if ((this->headIndex.load(std::memory_order_relaxed) & static_cast<index_t>(BLOCK_SIZE - 1)) != 0) {
332544 // The head's not on a block boundary, meaning a block somewhere is partially dequeued
332545 // (or the head block is the tail block and was fully dequeued, but the head/tail are still not on a boundary)
332546 size_t i = (pr_blockIndexFront - pr_blockIndexSlotsUsed) & (pr_blockIndexSize - 1);
332547 while (details::circular_less_than<index_t>(pr_blockIndexEntries[i].base + BLOCK_SIZE, this->headIndex.load(std::memory_order_relaxed))) {
332548 i = (i + 1) & (pr_blockIndexSize - 1);
332549 }
332550 assert(details::circular_less_than<index_t>(pr_blockIndexEntries[i].base, this->headIndex.load(std::memory_order_relaxed)));
332551 halfDequeuedBlock = pr_blockIndexEntries[i].block;
332552 }
332553
332554 // Start at the head block (note the first line in the loop gives us the head from the tail on the first iteration)
332555 auto block = this->tailBlock;
332556 do {
332557 block = block->next;
332558 if (block->ConcurrentQueue::Block::template is_empty<explicit_context>()) {
332559 continue;
332560 }
332561
332562 size_t i = 0; // Offset into block
332563 if (block == halfDequeuedBlock) {
332564 i = static_cast<size_t>(this->headIndex.load(std::memory_order_relaxed) & static_cast<index_t>(BLOCK_SIZE - 1));
332565 }
332566
332567 // Walk through all the items in the block; if this is the tail block, we need to stop when we reach the tail index
332568 auto lastValidIndex = (this->tailIndex.load(std::memory_order_relaxed) & static_cast<index_t>(BLOCK_SIZE - 1)) == 0 ? BLOCK_SIZE : static_cast<size_t>(this->tailIndex.load(std::memory_order_relaxed) & static_cast<index_t>(BLOCK_SIZE - 1));
332569 while (i != BLOCK_SIZE && (block != this->tailBlock || i != lastValidIndex)) {
332570 (*block)[i++]->~T();
332571 }
332572 } while (block != this->tailBlock);
332573 }
332574
332575 // Destroy all blocks that we own
332576 if (this->tailBlock != nullptr) {
332577 auto block = this->tailBlock;
332578 do {
332579 auto nextBlock = block->next;
332580 if (block->dynamicallyAllocated) {
332581 destroy(block);
332582 }
332583 else {
332584 this->parent->add_block_to_free_list(block);
332585 }
332586 block = nextBlock;
332587 } while (block != this->tailBlock);
332588 }
332589
332590 // Destroy the block indices
332591 auto header = static_cast<BlockIndexHeader*>(pr_blockIndexRaw);
332592 while (header != nullptr) {
332593 auto prev = static_cast<BlockIndexHeader*>(header->prev);
332594 header->~BlockIndexHeader();
332595 (Traits::free)(header);
332596 header = prev;
332597 }
332598 }
#define assert(x)
Definition duckdb.cpp:486071

Member Function Documentation

◆ enqueue()

template<typename T, typename Traits = ConcurrentQueueDefaultTraits>
template<AllocationMode allocMode, typename U>
bool duckdb_moodycamel::ConcurrentQueue< T, Traits >::ExplicitProducer::enqueue ( U && element)
inline
332602 {
332603 index_t currentTailIndex = this->tailIndex.load(std::memory_order_relaxed);
332605 if ((currentTailIndex & static_cast<index_t>(BLOCK_SIZE - 1)) == 0) {
332606 // We reached the end of a block, start a new one
332607 auto startBlock = this->tailBlock;
332608 auto originalBlockIndexSlotsUsed = pr_blockIndexSlotsUsed;
332609 if (this->tailBlock != nullptr && this->tailBlock->next->ConcurrentQueue::Block::template is_empty<explicit_context>()) {
332610 // We can re-use the block ahead of us, it's empty!
332611 this->tailBlock = this->tailBlock->next;
332612 this->tailBlock->ConcurrentQueue::Block::template reset_empty<explicit_context>();
332613
332614 // We'll put the block on the block index (guaranteed to be room since we're conceptually removing the
332615 // last block from it first -- except instead of removing then adding, we can just overwrite).
332616 // Note that there must be a valid block index here, since even if allocation failed in the ctor,
332617 // it would have been re-attempted when adding the first block to the queue; since there is such
332618 // a block, a block index must have been successfully allocated.
332619 }
332620 else {
332621 // Whatever head value we see here is >= the last value we saw here (relatively),
332622 // and <= its current value. Since we have the most recent tail, the head must be
332623 // <= to it.
332624 auto head = this->headIndex.load(std::memory_order_relaxed);
332628 // We can't enqueue in another block because there's not enough leeway -- the
332629 // tail could surpass the head by the time the block fills up! (Or we'll exceed
332630 // the size limit, if the second part of the condition was true.)
332631 return false;
332632 }
332633 // We're going to need a new block; check that the block index has room
332634 if (pr_blockIndexRaw == nullptr || pr_blockIndexSlotsUsed == pr_blockIndexSize) {
332635 // Hmm, the circular block index is already full -- we'll need
332636 // to allocate a new index. Note pr_blockIndexRaw can only be nullptr if
332637 // the initial allocation failed in the constructor.
332638
332640 return false;
332641 }
332642 else if (!new_block_index(pr_blockIndexSlotsUsed)) {
332643 return false;
332644 }
332645 }
332646
332647 // Insert a new block in the circular linked list
332648 auto newBlock = this->parent->ConcurrentQueue::template requisition_block<allocMode>();
332649 if (newBlock == nullptr) {
332650 return false;
332651 }
332652#ifdef MCDBGQ_TRACKMEM
332653 newBlock->owner = this;
332654#endif
332655 newBlock->ConcurrentQueue::Block::template reset_empty<explicit_context>();
332656 if (this->tailBlock == nullptr) {
332657 newBlock->next = newBlock;
332658 }
332659 else {
332660 newBlock->next = this->tailBlock->next;
332661 this->tailBlock->next = newBlock;
332662 }
332663 this->tailBlock = newBlock;
332664 ++pr_blockIndexSlotsUsed;
332665 }
332666
332667 if (!MOODYCAMEL_NOEXCEPT_CTOR(T, U, new ((T*)nullptr) T(std::forward<U>(element)))) {
332668 // The constructor may throw. We want the element not to appear in the queue in
332669 // that case (without corrupting the queue):
332671 new ((*this->tailBlock)[currentTailIndex]) T(std::forward<U>(element));
332672 }
332673 MOODYCAMEL_CATCH (...) {
332674 // Revert change to the current block, but leave the new block available
332675 // for next time
332676 pr_blockIndexSlotsUsed = originalBlockIndexSlotsUsed;
332677 this->tailBlock = startBlock == nullptr ? this->tailBlock : startBlock;
332679 }
332680 }
332681 else {
332682 (void)startBlock;
332684 }
332685
332686 // Add block to block index
332687 auto& entry = blockIndex.load(std::memory_order_relaxed)->entries[pr_blockIndexFront];
332688 entry.base = currentTailIndex;
332689 entry.block = this->tailBlock;
332690 blockIndex.load(std::memory_order_relaxed)->front.store(pr_blockIndexFront, std::memory_order_release);
332691 pr_blockIndexFront = (pr_blockIndexFront + 1) & (pr_blockIndexSize - 1);
332692
332693 if (!MOODYCAMEL_NOEXCEPT_CTOR(T, U, new ((T*)nullptr) T(std::forward<U>(element)))) {
332694 this->tailIndex.store(newTailIndex, std::memory_order_release);
332695 return true;
332696 }
332697 }
332698
332699 // Enqueue
332700 new ((*this->tailBlock)[currentTailIndex]) T(std::forward<U>(element));
332701
332702 this->tailIndex.store(newTailIndex, std::memory_order_release);
332703 return true;
332704 }

◆ dequeue()

template<typename T, typename Traits = ConcurrentQueueDefaultTraits>
template<typename U>
bool duckdb_moodycamel::ConcurrentQueue< T, Traits >::ExplicitProducer::dequeue ( U & element)
inline
332708 {
332709 auto tail = this->tailIndex.load(std::memory_order_relaxed);
332710 auto overcommit = this->dequeueOvercommit.load(std::memory_order_relaxed);
332711 if (details::circular_less_than<index_t>(this->dequeueOptimisticCount.load(std::memory_order_relaxed) - overcommit, tail)) {
332712 // Might be something to dequeue, let's give it a try
332713
332714 // Note that this if is purely for performance purposes in the common case when the queue is
332715 // empty and the values are eventually consistent -- we may enter here spuriously.
332716
332717 // Note that whatever the values of overcommit and tail are, they are not going to change (unless we
332718 // change them) and must be the same value at this point (inside the if) as when the if condition was
332719 // evaluated.
332720
332721 // We insert an acquire fence here to synchronize-with the release upon incrementing dequeueOvercommit below.
332722 // This ensures that whatever the value we got loaded into overcommit, the load of dequeueOptisticCount in
332723 // the fetch_add below will result in a value at least as recent as that (and therefore at least as large).
332724 // Note that I believe a compiler (signal) fence here would be sufficient due to the nature of fetch_add (all
332725 // read-modify-write operations are guaranteed to work on the latest value in the modification order), but
332726 // unfortunately that can't be shown to be correct using only the C++11 standard.
332727 // See http://stackoverflow.com/questions/18223161/what-are-the-c11-memory-ordering-guarantees-in-this-corner-case
332729
332730 // Increment optimistic counter, then check if it went over the boundary
332731 auto myDequeueCount = this->dequeueOptimisticCount.fetch_add(1, std::memory_order_relaxed);
332732
332733 // Note that since dequeueOvercommit must be <= dequeueOptimisticCount (because dequeueOvercommit is only ever
332734 // incremented after dequeueOptimisticCount -- this is enforced in the `else` block below), and since we now
332735 // have a version of dequeueOptimisticCount that is at least as recent as overcommit (due to the release upon
332736 // incrementing dequeueOvercommit and the acquire above that synchronizes with it), overcommit <= myDequeueCount.
332737 // However, we can't assert this since both dequeueOptimisticCount and dequeueOvercommit may (independently)
332738 // overflow; in such a case, though, the logic still holds since the difference between the two is maintained.
332739
332740 // Note that we reload tail here in case it changed; it will be the same value as before or greater, since
332741 // this load is sequenced after (happens after) the earlier load above. This is supported by read-read
332742 // coherency (as defined in the standard), explained here: http://en.cppreference.com/w/cpp/atomic/memory_order
332743 tail = this->tailIndex.load(std::memory_order_acquire);
332745 // Guaranteed to be at least one element to dequeue!
332746
332747 // Get the index. Note that since there's guaranteed to be at least one element, this
332748 // will never exceed tail. We need to do an acquire-release fence here since it's possible
332749 // that whatever condition got us to this point was for an earlier enqueued element (that
332750 // we already see the memory effects for), but that by the time we increment somebody else
332751 // has incremented it, and we need to see the memory effects for *that* element, which is
332752 // in such a case is necessarily visible on the thread that incremented it in the first
332753 // place with the more current condition (they must have acquired a tail that is at least
332754 // as recent).
332755 auto index = this->headIndex.fetch_add(1, std::memory_order_acq_rel);
332756
332757
332758 // Determine which block the element is in
332759
332760 auto localBlockIndex = blockIndex.load(std::memory_order_acquire);
332762
332763 // We need to be careful here about subtracting and dividing because of index wrap-around.
332764 // When an index wraps, we need to preserve the sign of the offset when dividing it by the
332765 // block size (in order to get a correct signed block count offset in all cases):
332766 auto headBase = localBlockIndex->entries[localBlockIndexHead].base;
332769 auto block = localBlockIndex->entries[(localBlockIndexHead + offset) & (localBlockIndex->size - 1)].block;
332770
332771 // Dequeue
332772 auto& el = *((*block)[index]);
332774 // Make sure the element is still fully dequeued and destroyed even if the assignment
332775 // throws
332776 struct Guard {
332777 Block* block;
332778 index_t index;
332779
332780 ~Guard()
332781 {
332782 (*block)[index]->~T();
332783 block->ConcurrentQueue::Block::template set_empty<explicit_context>(index);
332784 }
332785 } guard = { block, index };
332786
332787 element = std::move(el); // NOLINT
332788 }
332789 else {
332790 element = std::move(el); // NOLINT
332791 el.~T(); // NOLINT
332792 block->ConcurrentQueue::Block::template set_empty<explicit_context>(index);
332793 }
332794
332795 return true;
332796 }
332797 else {
332798 // Wasn't anything to dequeue after all; make the effective dequeue count eventually consistent
332799 this->dequeueOvercommit.fetch_add(1, std::memory_order_release); // Release so that the fetch_add on dequeueOptimisticCount is guaranteed to happen before this write
332800 }
332801 }
332802
332803 return false;
332804 }

◆ enqueue_bulk()

template<typename T, typename Traits = ConcurrentQueueDefaultTraits>
template<AllocationMode allocMode, typename It>
bool duckdb_moodycamel::ConcurrentQueue< T, Traits >::ExplicitProducer::enqueue_bulk ( It itemFirst,
size_t count )
inline
332808 {
332809 // First, we need to make sure we have enough room to enqueue all of the elements;
332810 // this means pre-allocating blocks and putting them in the block index (but only if
332811 // all the allocations succeeded).
332812 index_t startTailIndex = this->tailIndex.load(std::memory_order_relaxed);
332813 auto startBlock = this->tailBlock;
332814 auto originalBlockIndexFront = pr_blockIndexFront;
332815 auto originalBlockIndexSlotsUsed = pr_blockIndexSlotsUsed;
332816
332817 Block* firstAllocatedBlock = nullptr;
332818
332819 // Figure out how many blocks we'll need to allocate, and do so
332820 size_t blockBaseDiff = ((startTailIndex + count - 1) & ~static_cast<index_t>(BLOCK_SIZE - 1)) - ((startTailIndex - 1) & ~static_cast<index_t>(BLOCK_SIZE - 1));
332821 index_t currentTailIndex = (startTailIndex - 1) & ~static_cast<index_t>(BLOCK_SIZE - 1);
332822 if (blockBaseDiff > 0) {
332823 // Allocate as many blocks as possible from ahead
332824 while (blockBaseDiff > 0 && this->tailBlock != nullptr && this->tailBlock->next != firstAllocatedBlock && this->tailBlock->next->ConcurrentQueue::Block::template is_empty<explicit_context>()) {
332825 blockBaseDiff -= static_cast<index_t>(BLOCK_SIZE);
332826 currentTailIndex += static_cast<index_t>(BLOCK_SIZE);
332827
332828 this->tailBlock = this->tailBlock->next;
332829 firstAllocatedBlock = firstAllocatedBlock == nullptr ? this->tailBlock : firstAllocatedBlock;
332830
332831 auto& entry = blockIndex.load(std::memory_order_relaxed)->entries[pr_blockIndexFront];
332832 entry.base = currentTailIndex;
332833 entry.block = this->tailBlock;
332834 pr_blockIndexFront = (pr_blockIndexFront + 1) & (pr_blockIndexSize - 1);
332835 }
332836
332837 // Now allocate as many blocks as necessary from the block pool
332838 while (blockBaseDiff > 0) {
332839 blockBaseDiff -= static_cast<index_t>(BLOCK_SIZE);
332840 currentTailIndex += static_cast<index_t>(BLOCK_SIZE);
332841
332842 auto head = this->headIndex.load(std::memory_order_relaxed);
332845 if (pr_blockIndexRaw == nullptr || pr_blockIndexSlotsUsed == pr_blockIndexSize || full) {
332847 // Failed to allocate, undo changes (but keep injected blocks)
332848 pr_blockIndexFront = originalBlockIndexFront;
332849 pr_blockIndexSlotsUsed = originalBlockIndexSlotsUsed;
332850 this->tailBlock = startBlock == nullptr ? firstAllocatedBlock : startBlock;
332851 return false;
332852 }
332853 else if (full || !new_block_index(originalBlockIndexSlotsUsed)) {
332854 // Failed to allocate, undo changes (but keep injected blocks)
332855 pr_blockIndexFront = originalBlockIndexFront;
332856 pr_blockIndexSlotsUsed = originalBlockIndexSlotsUsed;
332857 this->tailBlock = startBlock == nullptr ? firstAllocatedBlock : startBlock;
332858 return false;
332859 }
332860
332861 // pr_blockIndexFront is updated inside new_block_index, so we need to
332862 // update our fallback value too (since we keep the new index even if we
332863 // later fail)
332865 }
332866
332867 // Insert a new block in the circular linked list
332868 auto newBlock = this->parent->ConcurrentQueue::template requisition_block<allocMode>();
332869 if (newBlock == nullptr) {
332870 pr_blockIndexFront = originalBlockIndexFront;
332871 pr_blockIndexSlotsUsed = originalBlockIndexSlotsUsed;
332872 this->tailBlock = startBlock == nullptr ? firstAllocatedBlock : startBlock;
332873 return false;
332874 }
332875
332876#ifdef MCDBGQ_TRACKMEM
332877 newBlock->owner = this;
332878#endif
332879 newBlock->ConcurrentQueue::Block::template set_all_empty<explicit_context>();
332880 if (this->tailBlock == nullptr) {
332881 newBlock->next = newBlock;
332882 }
332883 else {
332884 newBlock->next = this->tailBlock->next;
332885 this->tailBlock->next = newBlock;
332886 }
332887 this->tailBlock = newBlock;
332888 firstAllocatedBlock = firstAllocatedBlock == nullptr ? this->tailBlock : firstAllocatedBlock;
332889
332890 ++pr_blockIndexSlotsUsed;
332891
332892 auto& entry = blockIndex.load(std::memory_order_relaxed)->entries[pr_blockIndexFront];
332893 entry.base = currentTailIndex;
332894 entry.block = this->tailBlock;
332895 pr_blockIndexFront = (pr_blockIndexFront + 1) & (pr_blockIndexSize - 1);
332896 }
332897
332898 // Excellent, all allocations succeeded. Reset each block's emptiness before we fill them up, and
332899 // publish the new block index front
332900 auto block = firstAllocatedBlock;
332901 while (true) {
332902 block->ConcurrentQueue::Block::template reset_empty<explicit_context>();
332903 if (block == this->tailBlock) {
332904 break;
332905 }
332906 block = block->next;
332907 }
332908
332909 if (MOODYCAMEL_NOEXCEPT_CTOR(T, decltype(*itemFirst), new ((T*)nullptr) T(details::deref_noexcept(itemFirst)))) {
332910 blockIndex.load(std::memory_order_relaxed)->front.store((pr_blockIndexFront - 1) & (pr_blockIndexSize - 1), std::memory_order_release);
332911 }
332912 }
332913
332914 // Enqueue, one block at a time
332915 index_t newTailIndex = startTailIndex + static_cast<index_t>(count);
332917 auto endBlock = this->tailBlock;
332918 this->tailBlock = startBlock;
332919 assert((startTailIndex & static_cast<index_t>(BLOCK_SIZE - 1)) != 0 || firstAllocatedBlock != nullptr || count == 0);
332920 if ((startTailIndex & static_cast<index_t>(BLOCK_SIZE - 1)) == 0 && firstAllocatedBlock != nullptr) {
332921 this->tailBlock = firstAllocatedBlock;
332922 }
332923 while (true) {
332927 }
332928 if (MOODYCAMEL_NOEXCEPT_CTOR(T, decltype(*itemFirst), new ((T*)nullptr) T(details::deref_noexcept(itemFirst)))) {
332929 while (currentTailIndex != stopIndex) {
332930 new ((*this->tailBlock)[currentTailIndex++]) T(*itemFirst++);
332931 }
332932 }
332933 else {
332935 while (currentTailIndex != stopIndex) {
332936 // Must use copy constructor even if move constructor is available
332937 // because we may have to revert if there's an exception.
332938 // Sorry about the horrible templated next line, but it was the only way
332939 // to disable moving *at compile time*, which is important because a type
332940 // may only define a (noexcept) move constructor, and so calls to the
332941 // cctor will not compile, even if they are in an if branch that will never
332942 // be executed
332943 new ((*this->tailBlock)[currentTailIndex]) T(details::nomove_if<(bool)!MOODYCAMEL_NOEXCEPT_CTOR(T, decltype(*itemFirst), new ((T*)nullptr) T(details::deref_noexcept(itemFirst)))>::eval(*itemFirst));
332945 ++itemFirst;
332946 }
332947 }
332948 MOODYCAMEL_CATCH (...) {
332949 // Oh dear, an exception's been thrown -- destroy the elements that
332950 // were enqueued so far and revert the entire bulk operation (we'll keep
332951 // any allocated blocks in our linked list for later, though).
332953 auto lastBlockEnqueued = this->tailBlock;
332954
332955 pr_blockIndexFront = originalBlockIndexFront;
332956 pr_blockIndexSlotsUsed = originalBlockIndexSlotsUsed;
332957 this->tailBlock = startBlock == nullptr ? firstAllocatedBlock : startBlock;
332958
332960 auto block = startBlock;
332961 if ((startTailIndex & static_cast<index_t>(BLOCK_SIZE - 1)) == 0) {
332963 }
332965 while (true) {
332969 }
332970 while (currentTailIndex != stopIndex) {
332971 (*block)[currentTailIndex++]->~T();
332972 }
332973 if (block == lastBlockEnqueued) {
332974 break;
332975 }
332976 block = block->next;
332977 }
332978 }
332980 }
332981 }
332982
332983 if (this->tailBlock == endBlock) {
332985 break;
332986 }
332987 this->tailBlock = this->tailBlock->next;
332988 }
332989
332990 if (!MOODYCAMEL_NOEXCEPT_CTOR(T, decltype(*itemFirst), new ((T*)nullptr) T(details::deref_noexcept(itemFirst))) && firstAllocatedBlock != nullptr) {
332991 blockIndex.load(std::memory_order_relaxed)->front.store((pr_blockIndexFront - 1) & (pr_blockIndexSize - 1), std::memory_order_release);
332992 }
332993
332994 this->tailIndex.store(newTailIndex, std::memory_order_release);
332995 return true;
332996 }

◆ dequeue_bulk()

template<typename T, typename Traits = ConcurrentQueueDefaultTraits>
template<typename It>
size_t duckdb_moodycamel::ConcurrentQueue< T, Traits >::ExplicitProducer::dequeue_bulk ( It & itemFirst,
size_t max )
inline
333000 {
333001 auto tail = this->tailIndex.load(std::memory_order_relaxed);
333002 auto overcommit = this->dequeueOvercommit.load(std::memory_order_relaxed);
333003 auto desiredCount = static_cast<size_t>(tail - (this->dequeueOptimisticCount.load(std::memory_order_relaxed) - overcommit));
333007
333008 auto myDequeueCount = this->dequeueOptimisticCount.fetch_add(desiredCount, std::memory_order_relaxed);;
333009
333010 tail = this->tailIndex.load(std::memory_order_acquire);
333011 auto actualCount = static_cast<size_t>(tail - (myDequeueCount - overcommit));
333014 if (actualCount < desiredCount) {
333015 this->dequeueOvercommit.fetch_add(desiredCount - actualCount, std::memory_order_release);
333016 }
333017
333018 // Get the first index. Note that since there's guaranteed to be at least actualCount elements, this
333019 // will never exceed tail.
333020 auto firstIndex = this->headIndex.fetch_add(actualCount, std::memory_order_acq_rel);
333021
333022 // Determine which block the first element is in
333023 auto localBlockIndex = blockIndex.load(std::memory_order_acquire);
333025
333026 auto headBase = localBlockIndex->entries[localBlockIndexHead].base;
333029 auto indexIndex = (localBlockIndexHead + offset) & (localBlockIndex->size - 1);
333030
333031 // Iterate the blocks and dequeue
333032 auto index = firstIndex;
333033 do {
333034 auto firstIndexInBlock = index;
333035 auto endIndex = (index & ~static_cast<index_t>(BLOCK_SIZE - 1)) + static_cast<index_t>(BLOCK_SIZE);
333037 auto block = localBlockIndex->entries[indexIndex].block;
333039 while (index != endIndex) {
333040 auto& el = *((*block)[index]);
333041 *itemFirst++ = std::move(el);
333042 el.~T();
333043 ++index;
333044 }
333045 }
333046 else {
333048 while (index != endIndex) {
333049 auto& el = *((*block)[index]);
333050 *itemFirst = std::move(el);
333051 ++itemFirst;
333052 el.~T();
333053 ++index;
333054 }
333055 }
333056 MOODYCAMEL_CATCH (...) {
333057 // It's too late to revert the dequeue, but we can make sure that all
333058 // the dequeued objects are properly destroyed and the block index
333059 // (and empty count) are properly updated before we propagate the exception
333060 do {
333061 block = localBlockIndex->entries[indexIndex].block;
333062 while (index != endIndex) {
333063 (*block)[index++]->~T();
333064 }
333065 block->ConcurrentQueue::Block::template set_many_empty<explicit_context>(firstIndexInBlock, static_cast<size_t>(endIndex - firstIndexInBlock));
333066 indexIndex = (indexIndex + 1) & (localBlockIndex->size - 1);
333067
333071 } while (index != firstIndex + actualCount);
333072
333074 }
333075 }
333076 block->ConcurrentQueue::Block::template set_many_empty<explicit_context>(firstIndexInBlock, static_cast<size_t>(endIndex - firstIndexInBlock));
333077 indexIndex = (indexIndex + 1) & (localBlockIndex->size - 1);
333078 } while (index != firstIndex + actualCount);
333079
333080 return actualCount;
333081 }
333082 else {
333083 // Wasn't anything to dequeue after all; make the effective dequeue count eventually consistent
333084 this->dequeueOvercommit.fetch_add(desiredCount, std::memory_order_release);
333085 }
333086 }
333087
333088 return 0;
333089 }

◆ new_block_index()

template<typename T, typename Traits = ConcurrentQueueDefaultTraits>
bool duckdb_moodycamel::ConcurrentQueue< T, Traits >::ExplicitProducer::new_block_index ( size_t numberOfFilledSlotsToExpose)
inlineprivate
333108 {
333109 auto prevBlockSizeMask = pr_blockIndexSize - 1;
333110
333111 // Create the new block
333112 pr_blockIndexSize <<= 1;
333113 auto newRawPtr = static_cast<char*>((Traits::malloc)(sizeof(BlockIndexHeader) + std::alignment_of<BlockIndexEntry>::value - 1 + sizeof(BlockIndexEntry) * pr_blockIndexSize));
333114 if (newRawPtr == nullptr) {
333115 pr_blockIndexSize >>= 1; // Reset to allow graceful retry
333116 return false;
333117 }
333118
333120
333121 // Copy in all the old indices, if any
333122 size_t j = 0;
333123 if (pr_blockIndexSlotsUsed != 0) {
333124 auto i = (pr_blockIndexFront - pr_blockIndexSlotsUsed) & prevBlockSizeMask;
333125 do {
333126 newBlockIndexEntries[j++] = pr_blockIndexEntries[i];
333127 i = (i + 1) & prevBlockSizeMask;
333128 } while (i != pr_blockIndexFront);
333129 }
333130
333131 // Update everything
333132 auto header = new (newRawPtr) BlockIndexHeader;
333133 header->size = pr_blockIndexSize;
333135 header->entries = newBlockIndexEntries;
333136 header->prev = pr_blockIndexRaw; // we link the new block to the old one so we can free it later
333137
333138 pr_blockIndexFront = j;
333139 pr_blockIndexEntries = newBlockIndexEntries;
333140 pr_blockIndexRaw = newRawPtr;
333141 blockIndex.store(header, std::memory_order_release);
333142
333143 return true;
333144 }

The documentation for this struct was generated from the following file: