async_frontend.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /*
  2. * Copyright Andrey Semashev 2007 - 2013.
  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)
  6. */
  7. /*!
  8. * \file async_frontend.hpp
  9. * \author Andrey Semashev
  10. * \date 14.07.2009
  11. *
  12. * The header contains implementation of asynchronous sink frontend.
  13. */
  14. #ifndef BOOST_LOG_SINKS_ASYNC_FRONTEND_HPP_INCLUDED_
  15. #define BOOST_LOG_SINKS_ASYNC_FRONTEND_HPP_INCLUDED_
  16. #include <boost/log/detail/config.hpp>
  17. #ifdef BOOST_HAS_PRAGMA_ONCE
  18. #pragma once
  19. #endif
  20. #if defined(BOOST_LOG_NO_THREADS)
  21. #error Boost.Log: Asynchronous sink frontend is only supported in multithreaded environment
  22. #endif
  23. #include <boost/bind.hpp>
  24. #include <boost/static_assert.hpp>
  25. #include <boost/smart_ptr/shared_ptr.hpp>
  26. #include <boost/smart_ptr/make_shared_object.hpp>
  27. #include <boost/thread/locks.hpp>
  28. #include <boost/thread/mutex.hpp>
  29. #include <boost/thread/thread.hpp>
  30. #include <boost/thread/condition_variable.hpp>
  31. #include <boost/log/exceptions.hpp>
  32. #include <boost/log/detail/locking_ptr.hpp>
  33. #include <boost/log/detail/parameter_tools.hpp>
  34. #include <boost/log/core/record_view.hpp>
  35. #include <boost/log/sinks/basic_sink_frontend.hpp>
  36. #include <boost/log/sinks/frontend_requirements.hpp>
  37. #include <boost/log/sinks/unbounded_fifo_queue.hpp>
  38. #include <boost/log/keywords/start_thread.hpp>
  39. #include <boost/log/detail/header.hpp>
  40. namespace boost {
  41. BOOST_LOG_OPEN_NAMESPACE
  42. namespace sinks {
  43. #ifndef BOOST_LOG_DOXYGEN_PASS
  44. #define BOOST_LOG_SINK_CTOR_FORWARD_INTERNAL(z, n, types)\
  45. template< BOOST_PP_ENUM_PARAMS(n, typename T) >\
  46. explicit asynchronous_sink(BOOST_PP_ENUM_BINARY_PARAMS(n, T, const& arg)) :\
  47. base_type(true),\
  48. queue_base_type((BOOST_PP_ENUM_PARAMS(n, arg))),\
  49. m_pBackend(boost::make_shared< sink_backend_type >(BOOST_PP_ENUM_PARAMS(n, arg))),\
  50. m_StopRequested(false),\
  51. m_FlushRequested(false)\
  52. {\
  53. if ((BOOST_PP_ENUM_PARAMS(n, arg))[keywords::start_thread | true])\
  54. start_feeding_thread();\
  55. }\
  56. template< BOOST_PP_ENUM_PARAMS(n, typename T) >\
  57. explicit asynchronous_sink(shared_ptr< sink_backend_type > const& backend, BOOST_PP_ENUM_BINARY_PARAMS(n, T, const& arg)) :\
  58. base_type(true),\
  59. queue_base_type((BOOST_PP_ENUM_PARAMS(n, arg))),\
  60. m_pBackend(backend),\
  61. m_StopRequested(false),\
  62. m_FlushRequested(false)\
  63. {\
  64. if ((BOOST_PP_ENUM_PARAMS(n, arg))[keywords::start_thread | true])\
  65. start_feeding_thread();\
  66. }
  67. #endif // BOOST_LOG_DOXYGEN_PASS
  68. /*!
  69. * \brief Asynchronous logging sink frontend
  70. *
  71. * The frontend starts a separate thread on construction. All logging records are passed
  72. * to the backend in this dedicated thread only.
  73. */
  74. template< typename SinkBackendT, typename QueueingStrategyT = unbounded_fifo_queue >
  75. class asynchronous_sink :
  76. public aux::make_sink_frontend_base< SinkBackendT >::type,
  77. private boost::log::aux::locking_ptr_counter_base,
  78. public QueueingStrategyT
  79. {
  80. typedef typename aux::make_sink_frontend_base< SinkBackendT >::type base_type;
  81. typedef QueueingStrategyT queue_base_type;
  82. private:
  83. //! Backend synchronization mutex type
  84. typedef boost::mutex backend_mutex_type;
  85. //! Frontend synchronization mutex type
  86. typedef typename base_type::mutex_type frontend_mutex_type;
  87. //! A scope guard that implements thread ID management
  88. class scoped_thread_id
  89. {
  90. private:
  91. frontend_mutex_type& m_Mutex;
  92. condition_variable_any& m_Cond;
  93. thread::id& m_ThreadID;
  94. bool volatile& m_StopRequested;
  95. public:
  96. //! Initializing constructor
  97. scoped_thread_id(frontend_mutex_type& mut, condition_variable_any& cond, thread::id& tid, bool volatile& sr)
  98. : m_Mutex(mut), m_Cond(cond), m_ThreadID(tid), m_StopRequested(sr)
  99. {
  100. lock_guard< frontend_mutex_type > lock(m_Mutex);
  101. if (m_ThreadID != thread::id())
  102. BOOST_LOG_THROW_DESCR(unexpected_call, "Asynchronous sink frontend already runs a record feeding thread");
  103. m_ThreadID = this_thread::get_id();
  104. }
  105. //! Initializing constructor
  106. scoped_thread_id(unique_lock< frontend_mutex_type >& l, condition_variable_any& cond, thread::id& tid, bool volatile& sr)
  107. : m_Mutex(*l.mutex()), m_Cond(cond), m_ThreadID(tid), m_StopRequested(sr)
  108. {
  109. unique_lock< frontend_mutex_type > lock(move(l));
  110. if (m_ThreadID != thread::id())
  111. BOOST_LOG_THROW_DESCR(unexpected_call, "Asynchronous sink frontend already runs a record feeding thread");
  112. m_ThreadID = this_thread::get_id();
  113. }
  114. //! Destructor
  115. ~scoped_thread_id()
  116. {
  117. try
  118. {
  119. lock_guard< frontend_mutex_type > lock(m_Mutex);
  120. m_StopRequested = false;
  121. m_ThreadID = thread::id();
  122. m_Cond.notify_all();
  123. }
  124. catch (...)
  125. {
  126. }
  127. }
  128. private:
  129. scoped_thread_id(scoped_thread_id const&);
  130. scoped_thread_id& operator= (scoped_thread_id const&);
  131. };
  132. //! A scope guard that resets a flag on destructor
  133. class scoped_flag
  134. {
  135. private:
  136. frontend_mutex_type& m_Mutex;
  137. condition_variable_any& m_Cond;
  138. volatile bool& m_Flag;
  139. public:
  140. explicit scoped_flag(frontend_mutex_type& mut, condition_variable_any& cond, volatile bool& f) :
  141. m_Mutex(mut), m_Cond(cond), m_Flag(f)
  142. {
  143. }
  144. ~scoped_flag()
  145. {
  146. try
  147. {
  148. lock_guard< frontend_mutex_type > lock(m_Mutex);
  149. m_Flag = false;
  150. m_Cond.notify_all();
  151. }
  152. catch (...)
  153. {
  154. }
  155. }
  156. private:
  157. scoped_flag(scoped_flag const&);
  158. scoped_flag& operator= (scoped_flag const&);
  159. };
  160. public:
  161. //! Sink implementation type
  162. typedef SinkBackendT sink_backend_type;
  163. //! \cond
  164. BOOST_STATIC_ASSERT_MSG((has_requirement< typename sink_backend_type::frontend_requirements, synchronized_feeding >::value), "Asynchronous sink frontend is incompatible with the specified backend: thread synchronization requirements are not met");
  165. //! \endcond
  166. #ifndef BOOST_LOG_DOXYGEN_PASS
  167. //! A pointer type that locks the backend until it's destroyed
  168. typedef boost::log::aux::locking_ptr< sink_backend_type > locked_backend_ptr;
  169. #else // BOOST_LOG_DOXYGEN_PASS
  170. //! A pointer type that locks the backend until it's destroyed
  171. typedef implementation_defined locked_backend_ptr;
  172. #endif // BOOST_LOG_DOXYGEN_PASS
  173. private:
  174. //! Synchronization mutex
  175. backend_mutex_type m_BackendMutex;
  176. //! Pointer to the backend
  177. const shared_ptr< sink_backend_type > m_pBackend;
  178. //! Dedicated record feeding thread
  179. thread m_DedicatedFeedingThread;
  180. //! Feeding thread ID
  181. thread::id m_FeedingThreadID;
  182. //! Condition variable to implement blocking operations
  183. condition_variable_any m_BlockCond;
  184. //! The flag indicates that the feeding loop has to be stopped
  185. volatile bool m_StopRequested; // TODO: make it a real atomic
  186. //! The flag indicates that queue flush has been requested
  187. volatile bool m_FlushRequested; // TODO: make it a real atomic
  188. public:
  189. /*!
  190. * Default constructor. Constructs the sink backend instance.
  191. * Requires the backend to be default-constructible.
  192. *
  193. * \param start_thread If \c true, the frontend creates a thread to feed
  194. * log records to the backend. Otherwise no thread is
  195. * started and it is assumed that the user will call
  196. * either \c run or \c feed_records himself.
  197. */
  198. asynchronous_sink(bool start_thread = true) :
  199. base_type(true),
  200. m_pBackend(boost::make_shared< sink_backend_type >()),
  201. m_StopRequested(false),
  202. m_FlushRequested(false)
  203. {
  204. if (start_thread)
  205. start_feeding_thread();
  206. }
  207. /*!
  208. * Constructor attaches user-constructed backend instance
  209. *
  210. * \param backend Pointer to the backend instance.
  211. * \param start_thread If \c true, the frontend creates a thread to feed
  212. * log records to the backend. Otherwise no thread is
  213. * started and it is assumed that the user will call
  214. * either \c run or \c feed_records himself.
  215. *
  216. * \pre \a backend is not \c NULL.
  217. */
  218. explicit asynchronous_sink(shared_ptr< sink_backend_type > const& backend, bool start_thread = true) :
  219. base_type(true),
  220. m_pBackend(backend),
  221. m_StopRequested(false),
  222. m_FlushRequested(false)
  223. {
  224. if (start_thread)
  225. start_feeding_thread();
  226. }
  227. // Constructors that pass arbitrary parameters to the backend constructor
  228. BOOST_LOG_PARAMETRIZED_CONSTRUCTORS_GEN(BOOST_LOG_SINK_CTOR_FORWARD_INTERNAL, ~)
  229. /*!
  230. * Destructor. Implicitly stops the dedicated feeding thread, if one is running.
  231. */
  232. ~asynchronous_sink()
  233. {
  234. boost::this_thread::disable_interruption no_interrupts;
  235. stop();
  236. }
  237. /*!
  238. * Locking accessor to the attached backend
  239. */
  240. locked_backend_ptr locked_backend()
  241. {
  242. return locked_backend_ptr(
  243. m_pBackend,
  244. static_cast< boost::log::aux::locking_ptr_counter_base& >(*this));
  245. }
  246. /*!
  247. * Enqueues the log record to the backend
  248. */
  249. void consume(record_view const& rec)
  250. {
  251. if (m_FlushRequested)
  252. {
  253. unique_lock< frontend_mutex_type > lock(base_type::frontend_mutex());
  254. // Wait until flush is done
  255. while (m_FlushRequested)
  256. m_BlockCond.wait(lock);
  257. }
  258. queue_base_type::enqueue(rec);
  259. }
  260. /*!
  261. * The method attempts to pass logging record to the backend
  262. */
  263. bool try_consume(record_view const& rec)
  264. {
  265. if (!m_FlushRequested)
  266. {
  267. return queue_base_type::try_enqueue(rec);
  268. }
  269. else
  270. return false;
  271. }
  272. /*!
  273. * The method starts record feeding loop and effectively blocks until either of this happens:
  274. *
  275. * \li the thread is interrupted due to either standard thread interruption or a call to \c stop
  276. * \li an exception is thrown while processing a log record in the backend, and the exception is
  277. * not terminated by the exception handler, if one is installed
  278. *
  279. * \pre The sink frontend must be constructed without spawning a dedicated thread
  280. */
  281. void run()
  282. {
  283. // First check that no other thread is running
  284. scoped_thread_id guard(base_type::frontend_mutex(), m_BlockCond, m_FeedingThreadID, m_StopRequested);
  285. // Now start the feeding loop
  286. while (true)
  287. {
  288. do_feed_records();
  289. if (!m_StopRequested)
  290. {
  291. // Block until new record is available
  292. record_view rec;
  293. if (queue_base_type::dequeue_ready(rec))
  294. base_type::feed_record(rec, m_BackendMutex, *m_pBackend);
  295. }
  296. else
  297. break;
  298. }
  299. }
  300. /*!
  301. * The method softly interrupts record feeding loop. This method must be called when the \c run
  302. * method execution has to be interrupted. Unlike regular thread interruption, calling
  303. * \c stop will not interrupt the record processing in the middle. Instead, the sink frontend
  304. * will attempt to finish its business with the record in progress and return afterwards.
  305. * This method can be called either if the sink was created with a dedicated thread,
  306. * or if the feeding loop was initiated by user.
  307. *
  308. * \note Returning from this method does not guarantee that there are no records left buffered
  309. * in the sink frontend. It is possible that log records keep coming during and after this
  310. * method is called. At some point of execution of this method log records stop being processed,
  311. * and all records that come after this point are put into the queue. These records will be
  312. * processed upon further calls to \c run or \c feed_records.
  313. */
  314. void stop()
  315. {
  316. unique_lock< frontend_mutex_type > lock(base_type::frontend_mutex());
  317. if (m_FeedingThreadID != thread::id() || m_DedicatedFeedingThread.joinable())
  318. {
  319. try
  320. {
  321. m_StopRequested = true;
  322. queue_base_type::interrupt_dequeue();
  323. while (m_StopRequested)
  324. m_BlockCond.wait(lock);
  325. }
  326. catch (...)
  327. {
  328. m_StopRequested = false;
  329. throw;
  330. }
  331. lock.unlock();
  332. m_DedicatedFeedingThread.join();
  333. }
  334. }
  335. /*!
  336. * The method feeds log records that may have been buffered to the backend and returns
  337. *
  338. * \pre The sink frontend must be constructed without spawning a dedicated thread
  339. */
  340. void feed_records()
  341. {
  342. // First check that no other thread is running
  343. scoped_thread_id guard(base_type::frontend_mutex(), m_BlockCond, m_FeedingThreadID, m_StopRequested);
  344. // Now start the feeding loop
  345. do_feed_records();
  346. }
  347. /*!
  348. * The method feeds all log records that may have been buffered to the backend and returns.
  349. * Unlike \c feed_records, in case of ordering queueing the method also feeds records
  350. * that were enqueued during the ordering window, attempting to empty the queue completely.
  351. *
  352. * \pre The sink frontend must be constructed without spawning a dedicated thread
  353. */
  354. void flush()
  355. {
  356. unique_lock< frontend_mutex_type > lock(base_type::frontend_mutex());
  357. if (m_FeedingThreadID != thread::id() || m_DedicatedFeedingThread.joinable())
  358. {
  359. // There is already a thread feeding records, let it do the job
  360. m_FlushRequested = true;
  361. queue_base_type::interrupt_dequeue();
  362. while (!m_StopRequested && m_FlushRequested)
  363. m_BlockCond.wait(lock);
  364. // The condition may have been signalled when the feeding thread was finishing.
  365. // In that case records may not have been flushed, and we do the flush ourselves.
  366. if (m_FeedingThreadID != thread::id())
  367. return;
  368. }
  369. m_FlushRequested = true;
  370. // Flush records ourselves. The guard releases the lock.
  371. scoped_thread_id guard(lock, m_BlockCond, m_FeedingThreadID, m_StopRequested);
  372. do_feed_records();
  373. }
  374. private:
  375. #ifndef BOOST_LOG_DOXYGEN_PASS
  376. //! The method spawns record feeding thread
  377. void start_feeding_thread()
  378. {
  379. boost::thread(boost::bind(&asynchronous_sink::run, this)).swap(m_DedicatedFeedingThread);
  380. }
  381. // locking_ptr_counter_base methods
  382. void lock() { m_BackendMutex.lock(); }
  383. bool try_lock() { return m_BackendMutex.try_lock(); }
  384. void unlock() { m_BackendMutex.unlock(); }
  385. //! The record feeding loop
  386. void do_feed_records()
  387. {
  388. while (!m_StopRequested)
  389. {
  390. record_view rec;
  391. register bool dequeued = false;
  392. if (!m_FlushRequested)
  393. dequeued = queue_base_type::try_dequeue_ready(rec);
  394. else
  395. dequeued = queue_base_type::try_dequeue(rec);
  396. if (dequeued)
  397. base_type::feed_record(rec, m_BackendMutex, *m_pBackend);
  398. else
  399. break;
  400. }
  401. if (m_FlushRequested)
  402. {
  403. scoped_flag guard(base_type::frontend_mutex(), m_BlockCond, m_FlushRequested);
  404. base_type::flush_backend(m_BackendMutex, *m_pBackend);
  405. }
  406. }
  407. #endif // BOOST_LOG_DOXYGEN_PASS
  408. };
  409. #undef BOOST_LOG_SINK_CTOR_FORWARD_INTERNAL
  410. } // namespace sinks
  411. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  412. } // namespace boost
  413. #include <boost/log/detail/footer.hpp>
  414. #endif // BOOST_LOG_SINKS_ASYNC_FRONTEND_HPP_INCLUDED_