* Pushes the item into the queue. If the queue is full, waits until room * is available, for at most timeout microseconds. * \param[in] item An item. * \param[in] timeout a max waiting timeout in microseconds for an item to be pushed. * by default, = 0 means indefinite wait. * \param[in] errorMessage if != nullptr (is nullptr by default) an error message written on std::c
| 80 | * \return true if an item was pushed into the queue, else a timeout has occurred. |
| 81 | */ |
| 82 | bool push(const value_type& item, std::uint64_t timeout = BLOCKING_INFINITE_TIMEOUT,const char* errorMessage = nullptr) { |
| 83 | std::unique_lock < SpinMutex > lock(m_mutex); |
| 84 | |
| 85 | if (timeout == BLOCKING_INFINITE_TIMEOUT) { |
| 86 | m_cond_not_full.wait(lock, [this]() // Lambda funct |
| 87 | { |
| 88 | return m_queue.size() < m_max_num_items; |
| 89 | }); |
| 90 | } else if (timeout <= NON_BLOCKING_TIMEOUT && m_queue.size() >= m_max_num_items) { |
| 91 | // if the value is below a threshold, consider it is a try_push() |
| 92 | return false; |
| 93 | } |
| 94 | else if (false == m_cond_not_full.wait_for(lock, std::chrono::microseconds(timeout), |
| 95 | [this]() { return m_queue.size() < m_max_num_items; })) { |
| 96 | |
| 97 | if (errorMessage != nullptr) { |
| 98 | std::thread::id currentThreadId = std::this_thread::get_id(); |
| 99 | std::cout << "WARNING: Thread 0x" << std::hex << currentThreadId << std::dec << |
| 100 | " (" << currentThreadId << ") executing {" << typeid(*this).name() << "}.push() has failed with timeout > " << |
| 101 | (timeout * 0.001) << " ms, message: '" << errorMessage << "'" << std::endl << std::flush; |
| 102 | } |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | m_queue.push_back(item); |
| 107 | m_cond_not_empty.notify_all(); |
| 108 | return true; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Try to pushes the item into the queue, immediately, without waiting. If the queue is full, the item |
no test coverage detected