2 * Copyright Andrey Semashev 2007 - 2014.
3 * Distributed under the Boost Software License, Version 1.0.
4 * (See accompanying file LICENSE_1_0.txt or copy at
5 * http://www.boost.org/LICENSE_1_0.txt)
8 * \file unbounded_fifo_queue.hpp
9 * \author Andrey Semashev
12 * The header contains implementation of unbounded FIFO queueing strategy for
13 * the asynchronous sink frontend.
16 #ifndef BOOST_LOG_SINKS_UNBOUNDED_FIFO_QUEUE_HPP_INCLUDED_
17 #define BOOST_LOG_SINKS_UNBOUNDED_FIFO_QUEUE_HPP_INCLUDED_
19 #include <boost/log/detail/config.hpp>
21 #ifdef BOOST_HAS_PRAGMA_ONCE
25 #if defined(BOOST_LOG_NO_THREADS)
26 #error Boost.Log: This header content is only supported in multithreaded environment
29 #include <boost/log/detail/event.hpp>
30 #include <boost/log/detail/threadsafe_queue.hpp>
31 #include <boost/log/core/record_view.hpp>
32 #include <boost/log/detail/header.hpp>
36 BOOST_LOG_OPEN_NAMESPACE
41 * \brief Unbounded FIFO log record queueing strategy
43 * The \c unbounded_fifo_queue class is intended to be used with
44 * the \c asynchronous_sink frontend as a log record queueing strategy.
46 * This strategy implements the simplest logic of log record buffering between
47 * threads: the queue has no limits and imposes no ordering over the queued
48 * elements aside from the order in which they are enqueued.
49 * Because of this the queue provides decent performance and scalability,
50 * however if sink backends can't consume log records fast enough the queue
51 * may grow uncontrollably. When this is an issue, it is recommended to
52 * use one of the bounded strategies.
54 class unbounded_fifo_queue
57 typedef boost::log::aux::threadsafe_queue< record_view > queue_type;
62 //! Event object to block on
63 boost::log::aux::event m_event;
65 volatile bool m_interruption_requested; // TODO: make it atomic
68 //! Default constructor
69 unbounded_fifo_queue() : m_interruption_requested(false)
72 //! Initializing constructor
73 template< typename ArgsT >
74 explicit unbounded_fifo_queue(ArgsT const&) : m_interruption_requested(false)
78 //! Enqueues log record to the queue
79 void enqueue(record_view const& rec)
82 m_event.set_signalled();
85 //! Attempts to enqueue log record to the queue
86 bool try_enqueue(record_view const& rec)
88 // Assume the call never blocks
93 //! Attempts to dequeue a log record ready for processing from the queue, does not block if the queue is empty
94 bool try_dequeue_ready(record_view& rec)
96 return m_queue.try_pop(rec);
99 //! Attempts to dequeue log record from the queue, does not block if the queue is empty
100 bool try_dequeue(record_view& rec)
102 return m_queue.try_pop(rec);
105 //! Dequeues log record from the queue, blocks if the queue is empty
106 bool dequeue_ready(record_view& rec)
108 // Try the fast way first
109 if (m_queue.try_pop(rec))
112 // Ok, we probably have to wait for new records
116 if (m_interruption_requested)
118 m_interruption_requested = false;
121 if (m_queue.try_pop(rec))
126 //! Wakes a thread possibly blocked in the \c dequeue method
127 void interrupt_dequeue()
129 m_interruption_requested = true;
130 m_event.set_signalled();
136 BOOST_LOG_CLOSE_NAMESPACE // namespace log
140 #include <boost/log/detail/footer.hpp>
142 #endif // BOOST_LOG_SINKS_UNBOUNDED_FIFO_QUEUE_HPP_INCLUDED_