Chris@16
|
1 /*
|
Chris@101
|
2 * Copyright Andrey Semashev 2007 - 2015.
|
Chris@16
|
3 * Distributed under the Boost Software License, Version 1.0.
|
Chris@16
|
4 * (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
5 * http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
6 */
|
Chris@16
|
7 /*!
|
Chris@16
|
8 * \file bounded_fifo_queue.hpp
|
Chris@16
|
9 * \author Andrey Semashev
|
Chris@16
|
10 * \date 04.01.2012
|
Chris@16
|
11 *
|
Chris@16
|
12 * The header contains implementation of bounded FIFO queueing strategy for
|
Chris@16
|
13 * the asynchronous sink frontend.
|
Chris@16
|
14 */
|
Chris@16
|
15
|
Chris@16
|
16 #ifndef BOOST_LOG_SINKS_BOUNDED_FIFO_QUEUE_HPP_INCLUDED_
|
Chris@16
|
17 #define BOOST_LOG_SINKS_BOUNDED_FIFO_QUEUE_HPP_INCLUDED_
|
Chris@16
|
18
|
Chris@16
|
19 #include <boost/log/detail/config.hpp>
|
Chris@16
|
20
|
Chris@16
|
21 #ifdef BOOST_HAS_PRAGMA_ONCE
|
Chris@16
|
22 #pragma once
|
Chris@16
|
23 #endif
|
Chris@16
|
24
|
Chris@16
|
25 #if defined(BOOST_LOG_NO_THREADS)
|
Chris@16
|
26 #error Boost.Log: This header content is only supported in multithreaded environment
|
Chris@16
|
27 #endif
|
Chris@16
|
28
|
Chris@16
|
29 #include <cstddef>
|
Chris@16
|
30 #include <queue>
|
Chris@16
|
31 #include <boost/thread/locks.hpp>
|
Chris@16
|
32 #include <boost/thread/mutex.hpp>
|
Chris@16
|
33 #include <boost/thread/condition_variable.hpp>
|
Chris@16
|
34 #include <boost/log/core/record_view.hpp>
|
Chris@16
|
35 #include <boost/log/detail/header.hpp>
|
Chris@16
|
36
|
Chris@16
|
37 namespace boost {
|
Chris@16
|
38
|
Chris@16
|
39 BOOST_LOG_OPEN_NAMESPACE
|
Chris@16
|
40
|
Chris@16
|
41 namespace sinks {
|
Chris@16
|
42
|
Chris@16
|
43 /*!
|
Chris@16
|
44 * \brief Bounded FIFO log record queueing strategy
|
Chris@16
|
45 *
|
Chris@16
|
46 * The \c bounded_fifo_queue class is intended to be used with
|
Chris@16
|
47 * the \c asynchronous_sink frontend as a log record queueing strategy.
|
Chris@16
|
48 *
|
Chris@16
|
49 * This strategy describes log record queueing logic.
|
Chris@16
|
50 * The queue has a limited capacity, upon reaching which the enqueue operation will
|
Chris@16
|
51 * invoke the overflow handling strategy specified in the \c OverflowStrategyT
|
Chris@16
|
52 * template parameter to handle the situation. The library provides overflow handling
|
Chris@16
|
53 * strategies for most common cases: \c drop_on_overflow will silently discard the log record,
|
Chris@16
|
54 * and \c block_on_overflow will put the enqueueing thread to wait until there is space
|
Chris@16
|
55 * in the queue.
|
Chris@16
|
56 *
|
Chris@16
|
57 * The log record queue imposes no ordering over the queued
|
Chris@16
|
58 * elements aside from the order in which they are enqueued.
|
Chris@16
|
59 */
|
Chris@16
|
60 template< std::size_t MaxQueueSizeV, typename OverflowStrategyT >
|
Chris@16
|
61 class bounded_fifo_queue :
|
Chris@16
|
62 private OverflowStrategyT
|
Chris@16
|
63 {
|
Chris@16
|
64 private:
|
Chris@16
|
65 typedef OverflowStrategyT overflow_strategy;
|
Chris@16
|
66 typedef std::queue< record_view > queue_type;
|
Chris@16
|
67 typedef boost::mutex mutex_type;
|
Chris@16
|
68
|
Chris@16
|
69 private:
|
Chris@16
|
70 //! Synchronization primitive
|
Chris@16
|
71 mutex_type m_mutex;
|
Chris@16
|
72 //! Condition to block the consuming thread on
|
Chris@16
|
73 condition_variable m_cond;
|
Chris@16
|
74 //! Log record queue
|
Chris@16
|
75 queue_type m_queue;
|
Chris@16
|
76 //! Interruption flag
|
Chris@16
|
77 bool m_interruption_requested;
|
Chris@16
|
78
|
Chris@16
|
79 protected:
|
Chris@16
|
80 //! Default constructor
|
Chris@16
|
81 bounded_fifo_queue() : m_interruption_requested(false)
|
Chris@16
|
82 {
|
Chris@16
|
83 }
|
Chris@16
|
84 //! Initializing constructor
|
Chris@16
|
85 template< typename ArgsT >
|
Chris@16
|
86 explicit bounded_fifo_queue(ArgsT const&) : m_interruption_requested(false)
|
Chris@16
|
87 {
|
Chris@16
|
88 }
|
Chris@16
|
89
|
Chris@16
|
90 //! Enqueues log record to the queue
|
Chris@16
|
91 void enqueue(record_view const& rec)
|
Chris@16
|
92 {
|
Chris@16
|
93 unique_lock< mutex_type > lock(m_mutex);
|
Chris@16
|
94 std::size_t size = m_queue.size();
|
Chris@16
|
95 for (; size >= MaxQueueSizeV; size = m_queue.size())
|
Chris@16
|
96 {
|
Chris@16
|
97 if (!overflow_strategy::on_overflow(rec, lock))
|
Chris@16
|
98 return;
|
Chris@16
|
99 }
|
Chris@16
|
100
|
Chris@16
|
101 m_queue.push(rec);
|
Chris@16
|
102 if (size == 0)
|
Chris@16
|
103 m_cond.notify_one();
|
Chris@16
|
104 }
|
Chris@16
|
105
|
Chris@16
|
106 //! Attempts to enqueue log record to the queue
|
Chris@16
|
107 bool try_enqueue(record_view const& rec)
|
Chris@16
|
108 {
|
Chris@16
|
109 unique_lock< mutex_type > lock(m_mutex, try_to_lock);
|
Chris@16
|
110 if (lock.owns_lock())
|
Chris@16
|
111 {
|
Chris@16
|
112 const std::size_t size = m_queue.size();
|
Chris@16
|
113
|
Chris@16
|
114 // Do not invoke the bounding strategy in case of overflow as it may block
|
Chris@16
|
115 if (size < MaxQueueSizeV)
|
Chris@16
|
116 {
|
Chris@16
|
117 m_queue.push(rec);
|
Chris@16
|
118 if (size == 0)
|
Chris@16
|
119 m_cond.notify_one();
|
Chris@16
|
120 return true;
|
Chris@16
|
121 }
|
Chris@16
|
122 }
|
Chris@16
|
123
|
Chris@16
|
124 return false;
|
Chris@16
|
125 }
|
Chris@16
|
126
|
Chris@16
|
127 //! Attempts to dequeue a log record ready for processing from the queue, does not block if the queue is empty
|
Chris@16
|
128 bool try_dequeue_ready(record_view& rec)
|
Chris@16
|
129 {
|
Chris@16
|
130 return try_dequeue(rec);
|
Chris@16
|
131 }
|
Chris@16
|
132
|
Chris@16
|
133 //! Attempts to dequeue log record from the queue, does not block if the queue is empty
|
Chris@16
|
134 bool try_dequeue(record_view& rec)
|
Chris@16
|
135 {
|
Chris@16
|
136 lock_guard< mutex_type > lock(m_mutex);
|
Chris@16
|
137 const std::size_t size = m_queue.size();
|
Chris@16
|
138 if (size > 0)
|
Chris@16
|
139 {
|
Chris@16
|
140 rec.swap(m_queue.front());
|
Chris@16
|
141 m_queue.pop();
|
Chris@101
|
142 overflow_strategy::on_queue_space_available();
|
Chris@16
|
143 return true;
|
Chris@16
|
144 }
|
Chris@16
|
145
|
Chris@16
|
146 return false;
|
Chris@16
|
147 }
|
Chris@16
|
148
|
Chris@16
|
149 //! Dequeues log record from the queue, blocks if the queue is empty
|
Chris@16
|
150 bool dequeue_ready(record_view& rec)
|
Chris@16
|
151 {
|
Chris@16
|
152 unique_lock< mutex_type > lock(m_mutex);
|
Chris@16
|
153
|
Chris@16
|
154 while (!m_interruption_requested)
|
Chris@16
|
155 {
|
Chris@16
|
156 const std::size_t size = m_queue.size();
|
Chris@16
|
157 if (size > 0)
|
Chris@16
|
158 {
|
Chris@16
|
159 rec.swap(m_queue.front());
|
Chris@16
|
160 m_queue.pop();
|
Chris@101
|
161 overflow_strategy::on_queue_space_available();
|
Chris@16
|
162 return true;
|
Chris@16
|
163 }
|
Chris@16
|
164 else
|
Chris@16
|
165 {
|
Chris@16
|
166 m_cond.wait(lock);
|
Chris@16
|
167 }
|
Chris@16
|
168 }
|
Chris@16
|
169 m_interruption_requested = false;
|
Chris@16
|
170
|
Chris@16
|
171 return false;
|
Chris@16
|
172 }
|
Chris@16
|
173
|
Chris@16
|
174 //! Wakes a thread possibly blocked in the \c dequeue method
|
Chris@16
|
175 void interrupt_dequeue()
|
Chris@16
|
176 {
|
Chris@16
|
177 lock_guard< mutex_type > lock(m_mutex);
|
Chris@16
|
178 m_interruption_requested = true;
|
Chris@16
|
179 overflow_strategy::interrupt();
|
Chris@16
|
180 m_cond.notify_one();
|
Chris@16
|
181 }
|
Chris@16
|
182 };
|
Chris@16
|
183
|
Chris@16
|
184 } // namespace sinks
|
Chris@16
|
185
|
Chris@16
|
186 BOOST_LOG_CLOSE_NAMESPACE // namespace log
|
Chris@16
|
187
|
Chris@16
|
188 } // namespace boost
|
Chris@16
|
189
|
Chris@16
|
190 #include <boost/log/detail/footer.hpp>
|
Chris@16
|
191
|
Chris@16
|
192 #endif // BOOST_LOG_SINKS_BOUNDED_FIFO_QUEUE_HPP_INCLUDED_
|