async_frontend.hpp 18 KB

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