Chris@16: /* Chris@101: * Copyright Andrey Semashev 2007 - 2015. Chris@16: * Distributed under the Boost Software License, Version 1.0. Chris@16: * (See accompanying file LICENSE_1_0.txt or copy at Chris@16: * http://www.boost.org/LICENSE_1_0.txt) Chris@16: */ Chris@16: /*! Chris@16: * \file unbounded_fifo_queue.hpp Chris@16: * \author Andrey Semashev Chris@16: * \date 24.07.2011 Chris@16: * Chris@16: * The header contains implementation of unbounded FIFO queueing strategy for Chris@16: * the asynchronous sink frontend. Chris@16: */ Chris@16: Chris@16: #ifndef BOOST_LOG_SINKS_UNBOUNDED_FIFO_QUEUE_HPP_INCLUDED_ Chris@16: #define BOOST_LOG_SINKS_UNBOUNDED_FIFO_QUEUE_HPP_INCLUDED_ Chris@16: Chris@16: #include Chris@16: Chris@16: #ifdef BOOST_HAS_PRAGMA_ONCE Chris@16: #pragma once Chris@16: #endif Chris@16: Chris@16: #if defined(BOOST_LOG_NO_THREADS) Chris@16: #error Boost.Log: This header content is only supported in multithreaded environment Chris@16: #endif Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: Chris@16: BOOST_LOG_OPEN_NAMESPACE Chris@16: Chris@16: namespace sinks { Chris@16: Chris@16: /*! Chris@16: * \brief Unbounded FIFO log record queueing strategy Chris@16: * Chris@16: * The \c unbounded_fifo_queue class is intended to be used with Chris@16: * the \c asynchronous_sink frontend as a log record queueing strategy. Chris@16: * Chris@16: * This strategy implements the simplest logic of log record buffering between Chris@16: * threads: the queue has no limits and imposes no ordering over the queued Chris@16: * elements aside from the order in which they are enqueued. Chris@16: * Because of this the queue provides decent performance and scalability, Chris@16: * however if sink backends can't consume log records fast enough the queue Chris@16: * may grow uncontrollably. When this is an issue, it is recommended to Chris@16: * use one of the bounded strategies. Chris@16: */ Chris@16: class unbounded_fifo_queue Chris@16: { Chris@16: private: Chris@16: typedef boost::log::aux::threadsafe_queue< record_view > queue_type; Chris@16: Chris@16: private: Chris@16: //! Thread-safe queue Chris@16: queue_type m_queue; Chris@16: //! Event object to block on Chris@16: boost::log::aux::event m_event; Chris@16: //! Interruption flag Chris@16: volatile bool m_interruption_requested; // TODO: make it atomic Chris@16: Chris@16: protected: Chris@16: //! Default constructor Chris@16: unbounded_fifo_queue() : m_interruption_requested(false) Chris@16: { Chris@16: } Chris@16: //! Initializing constructor Chris@16: template< typename ArgsT > Chris@16: explicit unbounded_fifo_queue(ArgsT const&) : m_interruption_requested(false) Chris@16: { Chris@16: } Chris@16: Chris@16: //! Enqueues log record to the queue Chris@16: void enqueue(record_view const& rec) Chris@16: { Chris@16: m_queue.push(rec); Chris@16: m_event.set_signalled(); Chris@16: } Chris@16: Chris@16: //! Attempts to enqueue log record to the queue Chris@16: bool try_enqueue(record_view const& rec) Chris@16: { Chris@16: // Assume the call never blocks Chris@16: enqueue(rec); Chris@16: return true; Chris@16: } Chris@16: Chris@16: //! Attempts to dequeue a log record ready for processing from the queue, does not block if the queue is empty Chris@16: bool try_dequeue_ready(record_view& rec) Chris@16: { Chris@16: return m_queue.try_pop(rec); Chris@16: } Chris@16: Chris@16: //! Attempts to dequeue log record from the queue, does not block if the queue is empty Chris@16: bool try_dequeue(record_view& rec) Chris@16: { Chris@16: return m_queue.try_pop(rec); Chris@16: } Chris@16: Chris@16: //! Dequeues log record from the queue, blocks if the queue is empty Chris@16: bool dequeue_ready(record_view& rec) Chris@16: { Chris@16: // Try the fast way first Chris@16: if (m_queue.try_pop(rec)) Chris@16: return true; Chris@16: Chris@16: // Ok, we probably have to wait for new records Chris@16: while (true) Chris@16: { Chris@16: m_event.wait(); Chris@16: if (m_interruption_requested) Chris@16: { Chris@16: m_interruption_requested = false; Chris@16: return false; Chris@16: } Chris@16: if (m_queue.try_pop(rec)) Chris@16: return true; Chris@16: } Chris@16: } Chris@16: Chris@16: //! Wakes a thread possibly blocked in the \c dequeue method Chris@16: void interrupt_dequeue() Chris@16: { Chris@16: m_interruption_requested = true; Chris@16: m_event.set_signalled(); Chris@16: } Chris@16: }; Chris@16: Chris@16: } // namespace sinks Chris@16: Chris@16: BOOST_LOG_CLOSE_NAMESPACE // namespace log Chris@16: Chris@16: } // namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #endif // BOOST_LOG_SINKS_UNBOUNDED_FIFO_QUEUE_HPP_INCLUDED_