Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:39:25

0001 /*
0002  *          Copyright Andrey Semashev 2007 - 2015.
0003  * Distributed under the Boost Software License, Version 1.0.
0004  *    (See accompanying file LICENSE_1_0.txt or copy at
0005  *          http://www.boost.org/LICENSE_1_0.txt)
0006  */
0007 /*!
0008  * \file   unbounded_fifo_queue.hpp
0009  * \author Andrey Semashev
0010  * \date   24.07.2011
0011  *
0012  * The header contains implementation of unbounded FIFO queueing strategy for
0013  * the asynchronous sink frontend.
0014  */
0015 
0016 #ifndef BOOST_LOG_SINKS_UNBOUNDED_FIFO_QUEUE_HPP_INCLUDED_
0017 #define BOOST_LOG_SINKS_UNBOUNDED_FIFO_QUEUE_HPP_INCLUDED_
0018 
0019 #include <boost/log/detail/config.hpp>
0020 
0021 #ifdef BOOST_HAS_PRAGMA_ONCE
0022 #pragma once
0023 #endif
0024 
0025 #if defined(BOOST_LOG_NO_THREADS)
0026 #error Boost.Log: This header content is only supported in multithreaded environment
0027 #endif
0028 
0029 #include <boost/memory_order.hpp>
0030 #include <boost/atomic/atomic.hpp>
0031 #include <boost/log/detail/event.hpp>
0032 #include <boost/log/detail/threadsafe_queue.hpp>
0033 #include <boost/log/core/record_view.hpp>
0034 #include <boost/log/detail/header.hpp>
0035 
0036 namespace boost {
0037 
0038 BOOST_LOG_OPEN_NAMESPACE
0039 
0040 namespace sinks {
0041 
0042 /*!
0043  * \brief Unbounded FIFO log record queueing strategy
0044  *
0045  * The \c unbounded_fifo_queue class is intended to be used with
0046  * the \c asynchronous_sink frontend as a log record queueing strategy.
0047  *
0048  * This strategy implements the simplest logic of log record buffering between
0049  * threads: the queue has no limits and imposes no ordering over the queued
0050  * elements aside from the order in which they are enqueued.
0051  * Because of this the queue provides decent performance and scalability,
0052  * however if sink backends can't consume log records fast enough the queue
0053  * may grow uncontrollably. When this is an issue, it is recommended to
0054  * use one of the bounded strategies.
0055  */
0056 class unbounded_fifo_queue
0057 {
0058 private:
0059     typedef boost::log::aux::threadsafe_queue< record_view > queue_type;
0060 
0061 private:
0062     //! Thread-safe queue
0063     queue_type m_queue;
0064     //! Event object to block on
0065     boost::log::aux::event m_event;
0066     //! Interruption flag
0067     boost::atomic< bool > m_interruption_requested;
0068 
0069 protected:
0070     //! Default constructor
0071     unbounded_fifo_queue() : m_interruption_requested(false)
0072     {
0073     }
0074     //! Initializing constructor
0075     template< typename ArgsT >
0076     explicit unbounded_fifo_queue(ArgsT const&) : m_interruption_requested(false)
0077     {
0078     }
0079 
0080     //! Enqueues log record to the queue
0081     void enqueue(record_view const& rec)
0082     {
0083         m_queue.push(rec);
0084         m_event.set_signalled();
0085     }
0086 
0087     //! Attempts to enqueue log record to the queue
0088     bool try_enqueue(record_view const& rec)
0089     {
0090         // Assume the call never blocks
0091         enqueue(rec);
0092         return true;
0093     }
0094 
0095     //! Attempts to dequeue a log record ready for processing from the queue, does not block if the queue is empty
0096     bool try_dequeue_ready(record_view& rec)
0097     {
0098         return m_queue.try_pop(rec);
0099     }
0100 
0101     //! Attempts to dequeue log record from the queue, does not block if the queue is empty
0102     bool try_dequeue(record_view& rec)
0103     {
0104         return m_queue.try_pop(rec);
0105     }
0106 
0107     //! Dequeues log record from the queue, blocks if the queue is empty
0108     bool dequeue_ready(record_view& rec)
0109     {
0110         // Try the fast way first
0111         if (m_queue.try_pop(rec))
0112             return true;
0113 
0114         // Ok, we probably have to wait for new records
0115         while (true)
0116         {
0117             m_event.wait();
0118             if (m_interruption_requested.exchange(false, boost::memory_order_acquire))
0119                 return false;
0120             if (m_queue.try_pop(rec))
0121                 return true;
0122         }
0123     }
0124 
0125     //! Wakes a thread possibly blocked in the \c dequeue method
0126     void interrupt_dequeue()
0127     {
0128         m_interruption_requested.store(true, boost::memory_order_release);
0129         m_event.set_signalled();
0130     }
0131 };
0132 
0133 } // namespace sinks
0134 
0135 BOOST_LOG_CLOSE_NAMESPACE // namespace log
0136 
0137 } // namespace boost
0138 
0139 #include <boost/log/detail/footer.hpp>
0140 
0141 #endif // BOOST_LOG_SINKS_UNBOUNDED_FIFO_QUEUE_HPP_INCLUDED_