//! Enqueues log record to the queue void enqueue(record_view const& rec) { unique_lock< mutex_type > lock(m_mutex); std::size_t size = m_queue.size(); for (; size >= MaxQueueSizeV; size = m_queue.size()) { if (!overflow_strategy::on_overflow(rec, lock)) return; } m_queue.push(rec); if (size == 0) m_cond.notify_one(); }
inline void enqueue_conditional_signal(const T& elem, size_t signal_at_size) { m_mutex.lock(); m_queue.push_back(elem); // Signal threads waiting on the queue if (sleeping && m_queue.size() >= signal_at_size) m_conditional.signal(); m_mutex.unlock(); }
//! Equality operator. bool operator==(const iterator& it)const { if (m_queue.size() != it.m_queue.size()) { // if the queue size is different return false; // the state of the to iterator are different } if (m_queue.empty()) { // if the queue is empty, we have to check if they are valid and return it.m_valid == m_valid and it.m_cst == m_cst; // belong to the same cst } return (it.m_valid == m_valid) // valid status is equal => for end() iterator and (it.m_cst == m_cst) // iterator belongs to the same cst and (it.m_queue.front() == m_queue.front()) // front element and and (it.m_queue.back() == m_queue.back()); // back element are the same. }
//! Attempts to dequeue log record from the queue, does not block if the queue is empty bool try_dequeue(record_view& rec) { lock_guard< mutex_type > lock(m_mutex); const std::size_t size = m_queue.size(); if (size > 0) { rec.swap(m_queue.front()); m_queue.pop(); if (size == MaxQueueSizeV) overflow_strategy::on_queue_space_available(); return true; } return false; }
void test_pop() { size_t num = 10, count = 0; fill(num); while (!m_queue.empty()) m_queue.pop(); ASSERT_EQ(0, m_queue.size()); for (auto i : m_queue) count++; ASSERT_EQ(0, count); clear(); }
bool dequeue( msg_type & msg, boost::xtime const& xt) { typename boost::mutex::scoped_lock lock( mtx_); if ( active_ == false && empty_() ) return false; not_empty_cond_.timed_wait( lock, xt, boost::bind( & Queue< T, Q >::consumers_activate_, this) ); if ( empty_() ) msg.reset(); else dequeue_( msg); if ( active_ == true && queue_.size() <= low_water_mark_) not_full_cond_.notify_one(); return msg ? true : false; }
bool dequeue( msg_type & msg) { typename boost::mutex::scoped_lock lock( mtx_); if ( active_ == false && empty_() ) return false; while (empty_() ){ not_empty_cond_.wait( lock, boost::bind( & Queue< T, Q >::consumers_activate_, this) ); } dequeue_( msg); if ( active_ == true && queue_.size() <= low_water_mark_) { not_full_cond_.notify_one(); } return msg ? true : false; }
/// Returns immediately of queue size is >= immedeiate_size /// Otherwise, it will poll over 'ns' nanoseconds or on a signal /// until queue is not empty. inline bool try_timed_wait_for_data(size_t ns, size_t immediate_size) { m_mutex.lock(); bool success = false; // Wait while the queue is empty and this queue is alive if (m_queue.size() < immediate_size) { if (m_queue.empty() && m_alive) { sleeping++; m_conditional.timedwait_ns(m_mutex, ns); sleeping--; } } // An element has been added or a signal was raised if(!m_queue.empty()) { success = true; } m_mutex.unlock(); return success; }
//! Attempts to enqueue log record to the queue bool try_enqueue(record_view const& rec) { unique_lock< mutex_type > lock(m_mutex, try_to_lock); if (lock.owns_lock()) { const std::size_t size = m_queue.size(); // Do not invoke the bounding strategy in case of overflow as it may block if (size < MaxQueueSizeV) { m_queue.push(rec); if (size == 0) m_cond.notify_one(); return true; } } return false; }
//! Dequeues log record from the queue, blocks if the queue is empty bool dequeue_ready(record_view& rec) { unique_lock< mutex_type > lock(m_mutex); while (!m_interruption_requested) { const std::size_t size = m_queue.size(); if (size > 0) { rec.swap(m_queue.front()); m_queue.pop(); if (size == MaxQueueSizeV) overflow_strategy::on_queue_space_available(); return true; } else { m_cond.wait(lock); } } m_interruption_requested = false; return false; }
//! Returns the current number of nodes in the queue. size_type size() const { return m_queue.size(); }
bool full_() const { if ( high_water_mark_ == infinity_) return false; return queue_.size() > high_water_mark_; }
std::size_t size() const{ return queue_.size(); }
//! get the current size of the queue inline size_t size() { return m_queue.size(); }
std::size_t size(unsigned int timeout = 5) { boost::shared_lock<boost::shared_mutex> lock(mutex_, boost::get_system_time() + boost::posix_time::seconds(timeout)); if (!lock || queue_.empty()) return 0; return queue_.size(); }