123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- #ifndef BOOST_LOG_SINKS_BLOCK_ON_OVERFLOW_HPP_INCLUDED_
- #define BOOST_LOG_SINKS_BLOCK_ON_OVERFLOW_HPP_INCLUDED_
- #include <boost/log/detail/config.hpp>
- #ifdef BOOST_HAS_PRAGMA_ONCE
- #pragma once
- #endif
- #if defined(BOOST_LOG_NO_THREADS)
- #error Boost.Log: This header content is only supported in multithreaded environment
- #endif
- #include <boost/intrusive/options.hpp>
- #include <boost/intrusive/list.hpp>
- #include <boost/intrusive/list_hook.hpp>
- #include <boost/thread/condition_variable.hpp>
- #include <boost/log/core/record_view.hpp>
- #include <boost/log/detail/header.hpp>
- namespace boost {
- BOOST_LOG_OPEN_NAMESPACE
- namespace sinks {
- class block_on_overflow
- {
- #ifndef BOOST_LOG_DOXYGEN_PASS
- private:
- typedef intrusive::list_base_hook<
- intrusive::link_mode< intrusive::auto_unlink >
- > thread_context_hook_t;
- struct thread_context :
- public thread_context_hook_t
- {
- condition_variable cond;
- bool result;
- thread_context() : result(true) {}
- };
- typedef intrusive::list<
- thread_context,
- intrusive::base_hook< thread_context_hook_t >,
- intrusive::constant_time_size< false >
- > thread_contexts;
- private:
-
- thread_contexts m_thread_contexts;
- public:
-
- BOOST_DEFAULTED_FUNCTION(block_on_overflow(), {})
-
- template< typename LockT >
- bool on_overflow(record_view const&, LockT& lock)
- {
- thread_context context;
- m_thread_contexts.push_back(context);
- do
- {
- context.cond.wait(lock);
- }
- while (context.is_linked());
- return context.result;
- }
-
- void on_queue_space_available()
- {
- if (!m_thread_contexts.empty())
- {
- m_thread_contexts.front().cond.notify_one();
- m_thread_contexts.pop_front();
- }
- }
-
- void interrupt()
- {
- while (!m_thread_contexts.empty())
- {
- thread_context& context = m_thread_contexts.front();
- context.result = false;
- context.cond.notify_one();
- m_thread_contexts.pop_front();
- }
- }
-
- BOOST_DELETED_FUNCTION(block_on_overflow(block_on_overflow const&))
- BOOST_DELETED_FUNCTION(block_on_overflow& operator= (block_on_overflow const&))
- #endif
- };
- }
- BOOST_LOG_CLOSE_NAMESPACE
- }
- #include <boost/log/detail/footer.hpp>
- #endif
|