| 2459 | |
| 2460 | template<AllocationMode allocMode, typename U> |
| 2461 | inline bool enqueue(U &&element) { |
| 2462 | index_t currentTailIndex = this->tailIndex.load(std::memory_order_relaxed); |
| 2463 | index_t newTailIndex = 1 + currentTailIndex; |
| 2464 | if ((currentTailIndex & static_cast<index_t>(BLOCK_SIZE - 1)) == 0) { |
| 2465 | // We reached the end of a block, start a new one |
| 2466 | auto head = this->headIndex.load(std::memory_order_relaxed); |
| 2467 | assert(!details::circular_less_than<index_t>(currentTailIndex, head)); |
| 2468 | if (!details::circular_less_than<index_t>(head, currentTailIndex + BLOCK_SIZE) || |
| 2469 | (MAX_SUBQUEUE_SIZE != details::const_numeric_max<size_t>::value && |
| 2470 | (MAX_SUBQUEUE_SIZE == 0 || |
| 2471 | MAX_SUBQUEUE_SIZE - BLOCK_SIZE < currentTailIndex - head))) { |
| 2472 | return false; |
| 2473 | } |
| 2474 | #if MCDBGQ_NOLOCKFREE_IMPLICITPRODBLOCKINDEX |
| 2475 | debug::DebugLock lock(mutex); |
| 2476 | #endif |
| 2477 | // Find out where we'll be inserting this block in the block index |
| 2478 | BlockIndexEntry *idxEntry; |
| 2479 | if (!insert_block_index_entry<allocMode>(idxEntry, currentTailIndex)) { |
| 2480 | return false; |
| 2481 | } |
| 2482 | |
| 2483 | // Get ahold of a new block |
| 2484 | auto newBlock = this->parent->ConcurrentQueue::template requisition_block<allocMode>(); |
| 2485 | if (newBlock == nullptr) { |
| 2486 | rewind_block_index_tail(); |
| 2487 | idxEntry->value.store(nullptr, std::memory_order_relaxed); |
| 2488 | return false; |
| 2489 | } |
| 2490 | #if MCDBGQ_TRACKMEM |
| 2491 | newBlock->owner = this; |
| 2492 | #endif |
| 2493 | newBlock->template reset_empty<implicit_context>(); |
| 2494 | |
| 2495 | if (!MOODYCAMEL_NOEXCEPT_CTOR(T, U, new(nullptr) T(std::forward<U>(element)))) { |
| 2496 | // May throw, try to insert now before we publish the fact that we have this new block |
| 2497 | MOODYCAMEL_TRY { |
| 2498 | new((*newBlock)[currentTailIndex]) T(std::forward<U>(element)); |
| 2499 | } |
| 2500 | MOODYCAMEL_CATCH (...) { |
| 2501 | rewind_block_index_tail(); |
| 2502 | idxEntry->value.store(nullptr, std::memory_order_relaxed); |
| 2503 | this->parent->add_block_to_free_list(newBlock); |
| 2504 | MOODYCAMEL_RETHROW; |
| 2505 | } |
| 2506 | } |
| 2507 | |
| 2508 | // Insert the new block into the index |
| 2509 | idxEntry->value.store(newBlock, std::memory_order_relaxed); |
| 2510 | |
| 2511 | this->tailBlock = newBlock; |
| 2512 | |
| 2513 | if (!MOODYCAMEL_NOEXCEPT_CTOR(T, U, new(nullptr) T(std::forward<U>(element)))) { |
| 2514 | this->tailIndex.store(newTailIndex, std::memory_order_release); |
| 2515 | return true; |
| 2516 | } |
| 2517 | } |
| 2518 |
nothing calls this directly
no test coverage detected