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 block_on_overflow.hpp Chris@16: * \author Andrey Semashev Chris@16: * \date 04.01.2012 Chris@16: * Chris@16: * The header contains implementation of \c block_on_overflow strategy for handling Chris@16: * queue overflows in bounded queues for the asynchronous sink frontend. Chris@16: */ Chris@16: Chris@16: #ifndef BOOST_LOG_SINKS_BLOCK_ON_OVERFLOW_HPP_INCLUDED_ Chris@16: #define BOOST_LOG_SINKS_BLOCK_ON_OVERFLOW_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: #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 Blocking strategy for handling log record queue overflows Chris@16: * Chris@16: * This strategy will cause enqueueing threads to block when the Chris@16: * log record queue overflows. The blocked threads will be woken as Chris@16: * soon as there appears free space in the queue, in the same order Chris@16: * they attempted to enqueue records. Chris@16: */ Chris@16: class block_on_overflow Chris@16: { Chris@16: #ifndef BOOST_LOG_DOXYGEN_PASS Chris@16: private: Chris@16: typedef intrusive::list_base_hook< Chris@16: intrusive::link_mode< intrusive::auto_unlink > Chris@16: > thread_context_hook_t; Chris@16: Chris@16: struct thread_context : Chris@16: public thread_context_hook_t Chris@16: { Chris@16: condition_variable cond; Chris@16: bool result; Chris@16: Chris@16: thread_context() : result(true) {} Chris@16: }; Chris@16: Chris@16: typedef intrusive::list< Chris@16: thread_context, Chris@16: intrusive::base_hook< thread_context_hook_t >, Chris@16: intrusive::constant_time_size< false > Chris@16: > thread_contexts; Chris@16: Chris@16: private: Chris@16: //! Blocked threads Chris@16: thread_contexts m_thread_contexts; Chris@16: Chris@16: public: Chris@16: /*! Chris@16: * Default constructor. Chris@16: */ Chris@101: BOOST_DEFAULTED_FUNCTION(block_on_overflow(), {}) Chris@16: Chris@16: /*! Chris@16: * This method is called by the queue when overflow is detected. Chris@16: * Chris@16: * \param lock An internal lock that protects the queue Chris@16: * Chris@16: * \retval true Attempt to enqueue the record again. Chris@16: * \retval false Discard the record. Chris@16: */ Chris@16: template< typename LockT > Chris@16: bool on_overflow(record_view const&, LockT& lock) Chris@16: { Chris@16: thread_context context; Chris@16: m_thread_contexts.push_back(context); Chris@16: do Chris@16: { Chris@16: context.cond.wait(lock); Chris@16: } Chris@16: while (context.is_linked()); Chris@16: Chris@16: return context.result; Chris@16: } Chris@16: Chris@16: /*! Chris@16: * This method is called by the queue when there appears a free space. Chris@16: * The internal lock protecting the queue is locked when calling this method. Chris@16: */ Chris@16: void on_queue_space_available() Chris@16: { Chris@16: if (!m_thread_contexts.empty()) Chris@16: { Chris@16: m_thread_contexts.front().cond.notify_one(); Chris@16: m_thread_contexts.pop_front(); Chris@16: } Chris@16: } Chris@16: Chris@16: /*! Chris@16: * This method is called by the queue to interrupt any possible waits in \c on_overflow. Chris@16: * The internal lock protecting the queue is locked when calling this method. Chris@16: */ Chris@16: void interrupt() Chris@16: { Chris@16: while (!m_thread_contexts.empty()) Chris@16: { Chris@16: thread_context& context = m_thread_contexts.front(); Chris@16: context.result = false; Chris@16: context.cond.notify_one(); Chris@16: m_thread_contexts.pop_front(); Chris@16: } Chris@16: } Chris@101: Chris@101: // Copying prohibited Chris@101: BOOST_DELETED_FUNCTION(block_on_overflow(block_on_overflow const&)) Chris@101: BOOST_DELETED_FUNCTION(block_on_overflow& operator= (block_on_overflow const&)) Chris@16: #endif // BOOST_LOG_DOXYGEN_PASS 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_BLOCK_ON_OVERFLOW_HPP_INCLUDED_