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   block_on_overflow.hpp
0009  * \author Andrey Semashev
0010  * \date   04.01.2012
0011  *
0012  * The header contains implementation of \c block_on_overflow strategy for handling
0013  * queue overflows in bounded queues for the asynchronous sink frontend.
0014  */
0015 
0016 #ifndef BOOST_LOG_SINKS_BLOCK_ON_OVERFLOW_HPP_INCLUDED_
0017 #define BOOST_LOG_SINKS_BLOCK_ON_OVERFLOW_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/intrusive/options.hpp>
0030 #include <boost/intrusive/list.hpp>
0031 #include <boost/intrusive/list_hook.hpp>
0032 #include <boost/thread/condition_variable.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 Blocking strategy for handling log record queue overflows
0044  *
0045  * This strategy will cause enqueueing threads to block when the
0046  * log record queue overflows. The blocked threads will be woken as
0047  * soon as there appears free space in the queue, in the same order
0048  * they attempted to enqueue records.
0049  */
0050 class block_on_overflow
0051 {
0052 #ifndef BOOST_LOG_DOXYGEN_PASS
0053 private:
0054     typedef intrusive::list_base_hook<
0055         intrusive::link_mode< intrusive::auto_unlink >
0056     > thread_context_hook_t;
0057 
0058     struct thread_context :
0059         public thread_context_hook_t
0060     {
0061         condition_variable cond;
0062         bool result;
0063 
0064         thread_context() : result(true) {}
0065     };
0066 
0067     typedef intrusive::list<
0068         thread_context,
0069         intrusive::base_hook< thread_context_hook_t >,
0070         intrusive::constant_time_size< false >
0071     > thread_contexts;
0072 
0073 private:
0074     //! Blocked threads
0075     thread_contexts m_thread_contexts;
0076 
0077 public:
0078     /*!
0079      * Default constructor.
0080      */
0081     BOOST_DEFAULTED_FUNCTION(block_on_overflow(), {})
0082 
0083     /*!
0084      * This method is called by the queue when overflow is detected.
0085      *
0086      * \param lock An internal lock that protects the queue
0087      *
0088      * \retval true Attempt to enqueue the record again.
0089      * \retval false Discard the record.
0090      */
0091     template< typename LockT >
0092     bool on_overflow(record_view const&, LockT& lock)
0093     {
0094         thread_context context;
0095         m_thread_contexts.push_back(context);
0096         do
0097         {
0098             context.cond.wait(lock);
0099         }
0100         while (context.is_linked());
0101 
0102         return context.result;
0103     }
0104 
0105     /*!
0106      * This method is called by the queue when there appears a free space.
0107      * The internal lock protecting the queue is locked when calling this method.
0108      */
0109     void on_queue_space_available()
0110     {
0111         if (!m_thread_contexts.empty())
0112         {
0113             m_thread_contexts.front().cond.notify_one();
0114             m_thread_contexts.pop_front();
0115         }
0116     }
0117 
0118     /*!
0119      * This method is called by the queue to interrupt any possible waits in \c on_overflow.
0120      * The internal lock protecting the queue is locked when calling this method.
0121      */
0122     void interrupt()
0123     {
0124         while (!m_thread_contexts.empty())
0125         {
0126             thread_context& context = m_thread_contexts.front();
0127             context.result = false;
0128             context.cond.notify_one();
0129             m_thread_contexts.pop_front();
0130         }
0131     }
0132 
0133     //  Copying prohibited
0134     BOOST_DELETED_FUNCTION(block_on_overflow(block_on_overflow const&))
0135     BOOST_DELETED_FUNCTION(block_on_overflow& operator= (block_on_overflow const&))
0136 #endif // BOOST_LOG_DOXYGEN_PASS
0137 };
0138 
0139 } // namespace sinks
0140 
0141 BOOST_LOG_CLOSE_NAMESPACE // namespace log
0142 
0143 } // namespace boost
0144 
0145 #include <boost/log/detail/footer.hpp>
0146 
0147 #endif // BOOST_LOG_SINKS_BLOCK_ON_OVERFLOW_HPP_INCLUDED_