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_ordering_queue.hpp
9 * \author Andrey Semashev
12 * The header contains implementation of unbounded ordering record queueing strategy for
13 * the asynchronous sink frontend.
16 #ifndef BOOST_LOG_SINKS_UNBOUNDED_ORDERING_QUEUE_HPP_INCLUDED_
17 #define BOOST_LOG_SINKS_UNBOUNDED_ORDERING_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
31 #include <boost/cstdint.hpp>
32 #include <boost/thread/locks.hpp>
33 #include <boost/thread/mutex.hpp>
34 #include <boost/thread/condition_variable.hpp>
35 #include <boost/thread/thread_time.hpp>
36 #include <boost/date_time/posix_time/posix_time_types.hpp>
37 #include <boost/log/detail/timestamp.hpp>
38 #include <boost/log/detail/enqueued_record.hpp>
39 #include <boost/log/keywords/order.hpp>
40 #include <boost/log/keywords/ordering_window.hpp>
41 #include <boost/log/core/record_view.hpp>
42 #include <boost/log/detail/header.hpp>
46 BOOST_LOG_OPEN_NAMESPACE
51 * \brief Unbounded ordering log record queueing strategy
53 * The \c unbounded_ordering_queue class is intended to be used with
54 * the \c asynchronous_sink frontend as a log record queueing strategy.
56 * This strategy provides the following properties to the record queueing mechanism:
58 * \li The queue has no size limits.
59 * \li The queue has a fixed latency window. This means that each log record put
60 * into the queue will normally not be dequeued for a certain period of time.
61 * \li The queue performs stable record ordering within the latency window.
62 * The ordering predicate can be specified in the \c OrderT template parameter.
64 * Since this queue has no size limits, it may grow uncontrollably if sink backends
65 * dequeue log records not fast enough. When this is an issue, it is recommended to
66 * use one of the bounded strategies.
68 template< typename OrderT >
69 class unbounded_ordering_queue
72 typedef boost::mutex mutex_type;
73 typedef sinks::aux::enqueued_record enqueued_record;
75 typedef std::priority_queue<
77 std::vector< enqueued_record >,
78 enqueued_record::order< OrderT >
82 //! Ordering window duration, in milliseconds
83 const uint64_t m_ordering_window;
84 //! Synchronization mutex
86 //! Condition for blocking
87 condition_variable m_cond;
91 bool m_interruption_requested;
95 * Returns ordering window size specified during initialization
97 posix_time::time_duration get_ordering_window() const
99 return posix_time::milliseconds(m_ordering_window);
103 * Returns default ordering window size.
104 * The default window size is specific to the operating system thread scheduling mechanism.
106 static posix_time::time_duration get_default_ordering_window()
108 // The main idea behind this parameter is that the ordering window should be large enough
109 // to allow the frontend to order records from different threads on an attribute
110 // that contains system time. Thus this value should be:
111 // * No less than the minimum time resolution quant that Boost.DateTime provides on the current OS.
112 // For instance, on Windows it defaults to around 15-16 ms.
113 // * No less than thread switching quant on the current OS. For now 30 ms is large enough window size to
114 // switch threads on any known OS. It can be tuned for other platforms as needed.
115 return posix_time::milliseconds(30);
119 //! Initializing constructor
120 template< typename ArgsT >
121 explicit unbounded_ordering_queue(ArgsT const& args) :
122 m_ordering_window(args[keywords::ordering_window || &unbounded_ordering_queue::get_default_ordering_window].total_milliseconds()),
123 m_queue(args[keywords::order]),
124 m_interruption_requested(false)
128 //! Enqueues log record to the queue
129 void enqueue(record_view const& rec)
131 lock_guard< mutex_type > lock(m_mutex);
132 enqueue_unlocked(rec);
135 //! Attempts to enqueue log record to the queue
136 bool try_enqueue(record_view const& rec)
138 unique_lock< mutex_type > lock(m_mutex, try_to_lock);
139 if (lock.owns_lock())
141 enqueue_unlocked(rec);
148 //! Attempts to dequeue a log record ready for processing from the queue, does not block if no log records are ready to be processed
149 bool try_dequeue_ready(record_view& rec)
151 lock_guard< mutex_type > lock(m_mutex);
152 if (!m_queue.empty())
154 const boost::log::aux::timestamp now = boost::log::aux::get_timestamp();
155 enqueued_record const& elem = m_queue.top();
156 if (static_cast< uint64_t >((now - elem.m_timestamp).milliseconds()) >= m_ordering_window)
158 // We got a new element
168 //! Attempts to dequeue log record from the queue, does not block.
169 bool try_dequeue(record_view& rec)
171 lock_guard< mutex_type > lock(m_mutex);
172 if (!m_queue.empty())
174 enqueued_record const& elem = m_queue.top();
183 //! Dequeues log record from the queue, blocks if no log records are ready to be processed
184 bool dequeue_ready(record_view& rec)
186 unique_lock< mutex_type > lock(m_mutex);
187 while (!m_interruption_requested)
189 if (!m_queue.empty())
191 const boost::log::aux::timestamp now = boost::log::aux::get_timestamp();
192 enqueued_record const& elem = m_queue.top();
193 const uint64_t difference = (now - elem.m_timestamp).milliseconds();
194 if (difference >= m_ordering_window)
196 // We got a new element
203 // Wait until the element becomes ready to be processed
204 m_cond.timed_wait(lock, posix_time::milliseconds(m_ordering_window - difference));
209 // Wait for an element to come
213 m_interruption_requested = false;
218 //! Wakes a thread possibly blocked in the \c dequeue method
219 void interrupt_dequeue()
221 lock_guard< mutex_type > lock(m_mutex);
222 m_interruption_requested = true;
227 //! Enqueues a log record
228 void enqueue_unlocked(record_view const& rec)
230 const bool was_empty = m_queue.empty();
231 m_queue.push(enqueued_record(rec));
239 BOOST_LOG_CLOSE_NAMESPACE // namespace log
243 #include <boost/log/detail/footer.hpp>
245 #endif // BOOST_LOG_SINKS_UNBOUNDED_ORDERING_QUEUE_HPP_INCLUDED_