reliable_message_queue.hpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  1. /*
  2. * Copyright Lingxi Li 2015.
  3. * Copyright Andrey Semashev 2016.
  4. * Distributed under the Boost Software License, Version 1.0.
  5. * (See accompanying file LICENSE_1_0.txt or copy at
  6. * http://www.boost.org/LICENSE_1_0.txt)
  7. */
  8. /*!
  9. * \file utility/ipc/reliable_message_queue.hpp
  10. * \author Lingxi Li
  11. * \author Andrey Semashev
  12. * \date 01.01.2016
  13. *
  14. * The header contains declaration of a reliable interprocess message queue.
  15. */
  16. #ifndef BOOST_LOG_UTILITY_IPC_RELIABLE_MESSAGE_QUEUE_HPP_INCLUDED_
  17. #define BOOST_LOG_UTILITY_IPC_RELIABLE_MESSAGE_QUEUE_HPP_INCLUDED_
  18. #include <boost/log/detail/config.hpp>
  19. #include <cstddef>
  20. #include <boost/cstdint.hpp>
  21. #include <boost/move/core.hpp>
  22. #include <boost/log/keywords/open_mode.hpp>
  23. #include <boost/log/keywords/name.hpp>
  24. #include <boost/log/keywords/capacity.hpp>
  25. #include <boost/log/keywords/block_size.hpp>
  26. #include <boost/log/keywords/overflow_policy.hpp>
  27. #include <boost/log/keywords/permissions.hpp>
  28. #include <boost/log/utility/open_mode.hpp>
  29. #include <boost/log/utility/permissions.hpp>
  30. #include <boost/log/utility/ipc/object_name.hpp>
  31. #include <boost/log/detail/parameter_tools.hpp>
  32. #include <boost/log/detail/header.hpp>
  33. #ifdef BOOST_HAS_PRAGMA_ONCE
  34. #pragma once
  35. #endif
  36. namespace boost {
  37. BOOST_LOG_OPEN_NAMESPACE
  38. namespace ipc {
  39. namespace aux {
  40. template< typename T, typename R >
  41. struct enable_if_byte {};
  42. template< typename R >
  43. struct enable_if_byte< char, R > { typedef R type; };
  44. template< typename R >
  45. struct enable_if_byte< signed char, R > { typedef R type; };
  46. template< typename R >
  47. struct enable_if_byte< unsigned char, R > { typedef R type; };
  48. } // namespace aux
  49. /*!
  50. * \brief A reliable interprocess message queue
  51. *
  52. * The queue implements a reliable one-way channel of passing messages from one or multiple writers to a single reader.
  53. * The format of the messages is user-defined and must be consistent across all writers and the reader. The queue does
  54. * not enforce any specific format of the messages, other than they should be supplied as a contiguous array of bytes.
  55. *
  56. * The queue internally uses a process-shared storage identified by an \c object_name (the queue name). Refer to \c object_name
  57. * documentation for details on restrictions imposed on object names.
  58. *
  59. * The queue storage is organized as a fixed number of blocks of a fixed size. The block size must be an integer power of 2 and
  60. * is expressed in bytes. Each written message, together with some metadata added by the queue, consumes an integer number
  61. * of blocks. Each read message received by the reader releases the blocks allocated for that message. As such the maximum size
  62. * of a message is slightly less than block size times capacity of the queue. For efficiency, it is recommended to choose
  63. * block size large enough to accommodate most of the messages to be passed through the queue.
  64. *
  65. * The queue is considered empty when no messages are enqueued (all blocks are free). The queue is considered full at the point
  66. * of enqueueing a message when there is not enough free blocks to accommodate the message.
  67. *
  68. * The queue is reliable in that it will not drop successfully sent messages that are not received by the reader, other than the
  69. * case when a non-empty queue is destroyed by the last user. If a message cannot be enqueued by the writer because the queue is
  70. * full, the queue can either block the writer or return an error or throw an exception, depending on the policy specified at
  71. * the queue creation. The policy is object local, i.e. different writers and the reader can have different overflow policies.
  72. *
  73. * If the queue is empty and the reader attempts to dequeue a message, it will block until a message is enqueued by a writer.
  74. *
  75. * A blocked reader or writer can be unblocked by calling \c stop_local. After this method is called, all threads blocked on
  76. * this particular object are released and return \c operation_result::aborted. The other instances of the queue (in the current
  77. * or other processes) are unaffected. In order to restore the normal functioning of the queue instance after the \c stop_local
  78. * call the user has to invoke \c reset_local.
  79. *
  80. * The queue does not guarantee any particular order of received messages from different writer threads. Messages sent by a
  81. * particular writer thread will be received in the order of sending.
  82. *
  83. * Methods of this class are not thread-safe, unless otherwise specified.
  84. */
  85. class reliable_message_queue
  86. {
  87. public:
  88. //! Result codes for various operations on the queue
  89. enum operation_result
  90. {
  91. succeeded, //!< The operation has completed successfully
  92. no_space, //!< The message could not be sent because the queue is full
  93. aborted //!< The operation has been aborted because the queue method <tt>stop_local()</tt> has been called
  94. };
  95. //! Interprocess queue overflow policies
  96. enum overflow_policy
  97. {
  98. //! Block the send operation when the queue is full
  99. block_on_overflow,
  100. //! Return \c operation_result::no_space when the queue is full
  101. fail_on_overflow,
  102. //! Throw \c capacity_limit_reached exception when the queue is full
  103. throw_on_overflow
  104. };
  105. //! Queue message size type
  106. typedef uint32_t size_type;
  107. #if !defined(BOOST_LOG_DOXYGEN_PASS)
  108. BOOST_MOVABLE_BUT_NOT_COPYABLE(reliable_message_queue)
  109. private:
  110. typedef void (*receive_handler)(void* state, const void* data, size_type size);
  111. struct fixed_buffer_state
  112. {
  113. uint8_t* data;
  114. size_type size;
  115. };
  116. struct implementation;
  117. implementation* m_impl;
  118. #endif // !defined(BOOST_LOG_DOXYGEN_PASS)
  119. public:
  120. /*!
  121. * Default constructor. The method constructs an object that is not associated with any
  122. * message queue.
  123. *
  124. * \post <tt>is_open() == false</tt>
  125. */
  126. BOOST_CONSTEXPR reliable_message_queue() BOOST_NOEXCEPT : m_impl(NULL)
  127. {
  128. }
  129. /*!
  130. * Constructor. The method is used to construct an object and create the associated
  131. * message queue. The constructed object will be in running state if the message queue is
  132. * successfully created.
  133. *
  134. * \post <tt>is_open() == true</tt>
  135. *
  136. * \param name Name of the message queue to be associated with.
  137. * \param capacity Maximum number of allocation blocks the queue can hold.
  138. * \param block_size Size in bytes of allocation block. Must be a power of 2.
  139. * \param oflow_policy Queue behavior policy in case of overflow.
  140. * \param perms Access permissions for the associated message queue.
  141. */
  142. reliable_message_queue
  143. (
  144. open_mode::create_only_tag,
  145. object_name const& name,
  146. uint32_t capacity,
  147. size_type block_size,
  148. overflow_policy oflow_policy = block_on_overflow,
  149. permissions const& perms = permissions()
  150. ) :
  151. m_impl(NULL)
  152. {
  153. this->create(name, capacity, block_size, oflow_policy, perms);
  154. }
  155. /*!
  156. * Constructor. The method is used to construct an object and create or open the associated
  157. * message queue. The constructed object will be in running state if the message queue is
  158. * successfully created or opened. If the message queue that is identified by the name already
  159. * exists then the other queue parameters are ignored. The actual queue parameters can be obtained
  160. * with accessors from the constructed object.
  161. *
  162. * \post <tt>is_open() == true</tt>
  163. *
  164. * \param name Name of the message queue to be associated with.
  165. * \param capacity Maximum number of allocation blocks the queue can hold.
  166. * \param block_size Size in bytes of allocation block. Must be a power of 2.
  167. * \param oflow_policy Queue behavior policy in case of overflow.
  168. * \param perms Access permissions for the associated message queue.
  169. */
  170. reliable_message_queue
  171. (
  172. open_mode::open_or_create_tag,
  173. object_name const& name,
  174. uint32_t capacity,
  175. size_type block_size,
  176. overflow_policy oflow_policy = block_on_overflow,
  177. permissions const& perms = permissions()
  178. ) :
  179. m_impl(NULL)
  180. {
  181. this->open_or_create(name, capacity, block_size, oflow_policy, perms);
  182. }
  183. /*!
  184. * Constructor. The method is used to construct an object and open the existing
  185. * message queue. The constructed object will be in running state if the message queue is
  186. * successfully opened.
  187. *
  188. * \post <tt>is_open() == true</tt>
  189. *
  190. * \param name Name of the message queue to be associated with.
  191. * \param oflow_policy Queue behavior policy in case of overflow.
  192. * \param perms Access permissions for the associated message queue. The permissions will only be used
  193. * if the queue implementation has to create system objects while operating.
  194. * This parameter is currently not used on POSIX systems.
  195. */
  196. reliable_message_queue
  197. (
  198. open_mode::open_only_tag,
  199. object_name const& name,
  200. overflow_policy oflow_policy = block_on_overflow,
  201. permissions const& perms = permissions()
  202. ) :
  203. m_impl(NULL)
  204. {
  205. this->open(name, oflow_policy, perms);
  206. }
  207. /*!
  208. * Constructor with named parameters. The method is used to construct an object and create or open
  209. * the associated message queue. The constructed object will be in running state if the message queue is
  210. * successfully created.
  211. *
  212. * The following named parameters are accepted:
  213. *
  214. * * open_mode - One of the open mode tags: \c open_mode::create_only, \c open_mode::open_only or
  215. * \c open_mode::open_or_create.
  216. * * name - Name of the message queue to be associated with.
  217. * * capacity - Maximum number of allocation blocks the queue can hold. Used only if the queue is created.
  218. * * block_size - Size in bytes of allocation block. Must be a power of 2. Used only if the queue is created.
  219. * * overflow_policy - Queue behavior policy in case of overflow, see \c overflow_policy.
  220. * * permissions - Access permissions for the associated message queue.
  221. *
  222. * \post <tt>is_open() == true</tt>
  223. */
  224. #if !defined(BOOST_LOG_DOXYGEN_PASS)
  225. BOOST_LOG_PARAMETRIZED_CONSTRUCTORS_CALL(reliable_message_queue, construct)
  226. #else
  227. template< typename... Args >
  228. explicit reliable_message_queue(Args const&... args);
  229. #endif
  230. /*!
  231. * Destructor. Calls <tt>close()</tt>.
  232. */
  233. ~reliable_message_queue() BOOST_NOEXCEPT
  234. {
  235. this->close();
  236. }
  237. /*!
  238. * Move constructor. The method move-constructs an object from \c other. After
  239. * the call, the constructed object becomes \c other, while \c other is left in
  240. * default constructed state.
  241. *
  242. * \param that The object to be moved.
  243. */
  244. reliable_message_queue(BOOST_RV_REF(reliable_message_queue) that) BOOST_NOEXCEPT :
  245. m_impl(that.m_impl)
  246. {
  247. that.m_impl = NULL;
  248. }
  249. /*!
  250. * Move assignment operator. If the object is associated with a message queue,
  251. * <tt>close()</tt> is first called and the precondition to calling <tt>close()</tt>
  252. * applies. After the call, the object becomes \a that while \a that is left
  253. * in default constructed state.
  254. *
  255. * \param that The object to be moved.
  256. *
  257. * \return A reference to the assigned object.
  258. */
  259. reliable_message_queue& operator= (BOOST_RV_REF(reliable_message_queue) that) BOOST_NOEXCEPT
  260. {
  261. reliable_message_queue other(static_cast< BOOST_RV_REF(reliable_message_queue) >(that));
  262. this->swap(other);
  263. return *this;
  264. }
  265. /*!
  266. * The method swaps the object with \a that.
  267. *
  268. * \param that The other object to swap with.
  269. */
  270. void swap(reliable_message_queue& that) BOOST_NOEXCEPT
  271. {
  272. implementation* p = m_impl;
  273. m_impl = that.m_impl;
  274. that.m_impl = p;
  275. }
  276. //! Swaps the two \c reliable_message_queue objects.
  277. friend void swap(reliable_message_queue& a, reliable_message_queue& b) BOOST_NOEXCEPT
  278. {
  279. a.swap(b);
  280. }
  281. /*!
  282. * The method creates the message queue to be associated with the object. After the call,
  283. * the object will be in running state if a message queue is successfully created.
  284. *
  285. * \pre <tt>is_open() == false</tt>
  286. * \post <tt>is_open() == true</tt>
  287. *
  288. * \param name Name of the message queue to be associated with.
  289. * \param capacity Maximum number of allocation blocks the queue can hold.
  290. * \param block_size Size in bytes of allocation block. Must be a power of 2.
  291. * \param oflow_policy Queue behavior policy in case of overflow.
  292. * \param perms Access permissions for the associated message queue.
  293. */
  294. BOOST_LOG_API void create
  295. (
  296. object_name const& name,
  297. uint32_t capacity,
  298. size_type block_size,
  299. overflow_policy oflow_policy = block_on_overflow,
  300. permissions const& perms = permissions()
  301. );
  302. /*!
  303. * The method creates or opens the message queue to be associated with the object.
  304. * After the call, the object will be in running state if a message queue is successfully
  305. * created or opened. If the message queue that is identified by the name already exists then
  306. * the other queue parameters are ignored. The actual queue parameters can be obtained
  307. * with accessors from this object after this method returns.
  308. *
  309. * \pre <tt>is_open() == false</tt>
  310. * \post <tt>is_open() == true</tt>
  311. *
  312. * \param name Name of the message queue to be associated with.
  313. * \param capacity Maximum number of allocation blocks the queue can hold.
  314. * \param block_size Size in bytes of allocation block. Must be a power of 2.
  315. * \param oflow_policy Queue behavior policy in case of overflow.
  316. * \param perms Access permissions for the associated message queue.
  317. */
  318. BOOST_LOG_API void open_or_create
  319. (
  320. object_name const& name,
  321. uint32_t capacity,
  322. size_type block_size,
  323. overflow_policy oflow_policy = block_on_overflow,
  324. permissions const& perms = permissions()
  325. );
  326. /*!
  327. * The method opens the existing message queue to be associated with the object.
  328. * After the call, the object will be in running state if a message queue is successfully
  329. * opened.
  330. *
  331. * \pre <tt>is_open() == false</tt>
  332. * \post <tt>is_open() == true</tt>
  333. *
  334. * \param name Name of the message queue to be associated with.
  335. * \param oflow_policy Queue behavior policy in case of overflow.
  336. * \param perms Access permissions for the associated message queue. The permissions will only be used
  337. * if the queue implementation has to create system objects while operating.
  338. * This parameter is currently not used on POSIX systems.
  339. */
  340. BOOST_LOG_API void open
  341. (
  342. object_name const& name,
  343. overflow_policy oflow_policy = block_on_overflow,
  344. permissions const& perms = permissions()
  345. );
  346. /*!
  347. * Tests whether the object is associated with any message queue.
  348. *
  349. * \return \c true if the object is associated with a message queue, and \c false otherwise.
  350. */
  351. bool is_open() const BOOST_NOEXCEPT
  352. {
  353. return m_impl != NULL;
  354. }
  355. /*!
  356. * This method empties the associated message queue. Concurrent calls to this method, <tt>send()</tt>,
  357. * <tt>try_send()</tt>, <tt>receive()</tt>, <tt>try_receive()</tt>, and <tt>stop_local()</tt> are allowed.
  358. *
  359. * \pre <tt>is_open() == true</tt>
  360. */
  361. BOOST_LOG_API void clear();
  362. /*!
  363. * The method returns the name of the associated message queue.
  364. *
  365. * \pre <tt>is_open() == true</tt>
  366. *
  367. * \return Name of the associated message queue
  368. */
  369. BOOST_LOG_API object_name const& name() const;
  370. /*!
  371. * The method returns the maximum number of allocation blocks the associated message queue
  372. * can hold. Note that the returned value may be different from the corresponding
  373. * value passed to the constructor or <tt>open_or_create()</tt>, for the message queue may
  374. * not have been created by this object.
  375. *
  376. * \pre <tt>is_open() == true</tt>
  377. *
  378. * \return Maximum number of allocation blocks the associated message queue can hold.
  379. */
  380. BOOST_LOG_API uint32_t capacity() const;
  381. /*!
  382. * The method returns the allocation block size, in bytes. Each message in the
  383. * associated message queue consumes an integer number of allocation blocks.
  384. * Note that the returned value may be different from the corresponding value passed
  385. * to the constructor or <tt>open_or_create()</tt>, for the message queue may not
  386. * have been created by this object.
  387. *
  388. * \pre <tt>is_open() == true</tt>
  389. *
  390. * \return Allocation block size, in bytes.
  391. */
  392. BOOST_LOG_API size_type block_size() const;
  393. /*!
  394. * The method wakes up all threads that are blocked in calls to <tt>send()</tt> or
  395. * <tt>receive()</tt>. Those calls would then return <tt>operation_result::aborted</tt>.
  396. * Note that, the method does not block until the woken-up threads have actually
  397. * returned from <tt>send()</tt> or <tt>receive()</tt>. Other means is needed to ensure
  398. * that calls to <tt>send()</tt> or <tt>receive()</tt> have returned, e.g., joining the
  399. * threads that might be blocking on the calls.
  400. *
  401. * The method also puts the object in stopped state. When in stopped state, calls to
  402. * <tt>send()</tt> or <tt>receive()</tt> will return immediately with return value
  403. * <tt>operation_result::aborted</tt> when they would otherwise block in running state.
  404. *
  405. * Concurrent calls to this method, <tt>send()</tt>, <tt>try_send()</tt>, <tt>receive()</tt>,
  406. * <tt>try_receive()</tt>, and <tt>clear()</tt> are allowed.
  407. *
  408. * \pre <tt>is_open() == true</tt>
  409. */
  410. BOOST_LOG_API void stop_local();
  411. /*!
  412. * The method puts the object in running state where calls to <tt>send()</tt> or
  413. * <tt>receive()</tt> may block. This method is not thread-safe.
  414. *
  415. * \pre <tt>is_open() == true</tt>
  416. */
  417. BOOST_LOG_API void reset_local();
  418. /*!
  419. * The method disassociates the associated message queue, if any. No other threads
  420. * should be using this object before calling this method. The <tt>stop_local()</tt> method
  421. * can be used to have any threads currently blocked in <tt>send()</tt> or
  422. * <tt>receive()</tt> return, and prevent further calls to them from blocking. Typically,
  423. * before calling this method, one would first call <tt>stop_local()</tt> and then join all
  424. * threads that might be blocking on <tt>send()</tt> or <tt>receive()</tt> to ensure that
  425. * they have returned from the calls. The associated message queue is destroyed if the
  426. * object represents the last outstanding reference to it.
  427. *
  428. * \post <tt>is_open() == false</tt>
  429. */
  430. void close() BOOST_NOEXCEPT
  431. {
  432. if (is_open())
  433. do_close();
  434. }
  435. /*!
  436. * The method sends a message to the associated message queue. When the object is in
  437. * running state and the queue has no free space for the message, the method either blocks
  438. * or throws an exception, depending on the overflow policy that was specified on the queue
  439. * opening/creation. If blocking policy is in effect, the blocking can be interrupted by
  440. * calling <tt>stop_local()</tt>, in which case the method returns \c operation_result::aborted.
  441. * When the object is already in the stopped state, the method does not block but returns
  442. * immediately with return value \c operation_result::aborted.
  443. *
  444. * It is possible to send an empty message by passing \c 0 to the parameter \c message_size.
  445. *
  446. * Concurrent calls to <tt>send()</tt>, <tt>try_send()</tt>, <tt>receive()</tt>, <tt>try_receive()</tt>,
  447. * <tt>stop_local()</tt>, and <tt>clear()</tt> are allowed.
  448. *
  449. * \pre <tt>is_open() == true</tt>
  450. *
  451. * \param message_data The message data to send. Ignored when \c message_size is \c 0.
  452. * \param message_size Size of the message data in bytes. If the size is larger than
  453. * the associated message queue capacity, an <tt>std::logic_error</tt> exception is thrown.
  454. *
  455. * \retval operation_result::succeeded if the operation is successful
  456. * \retval operation_result::no_space if \c overflow_policy::fail_on_overflow is in effect and the queue is full
  457. * \retval operation_result::aborted if the call was interrupted by <tt>stop_local()</tt>
  458. *
  459. * <b>Throws:</b> <tt>std::logic_error</tt> in case if the message size exceeds the queue
  460. * capacity, <tt>system_error</tt> in case if a native OS method fails.
  461. */
  462. BOOST_LOG_API operation_result send(void const* message_data, size_type message_size);
  463. /*!
  464. * The method performs an attempt to send a message to the associated message queue.
  465. * The method is non-blocking, and always returns immediately.
  466. * <tt>boost::system::system_error</tt> is thrown for errors resulting from native
  467. * operating system calls. Note that it is possible to send an empty message by passing
  468. * \c 0 to the parameter \c message_size. Concurrent calls to <tt>send()</tt>,
  469. * <tt>try_send()</tt>, <tt>receive()</tt>, <tt>try_receive()</tt>, <tt>stop_local()</tt>,
  470. * and <tt>clear()</tt> are allowed.
  471. *
  472. * \pre <tt>is_open() == true</tt>
  473. *
  474. * \param message_data The message data to send. Ignored when \c message_size is \c 0.
  475. * \param message_size Size of the message data in bytes. If the size is larger than the
  476. * maximum size allowed by the associated message queue, an
  477. * <tt>std::logic_error</tt> exception is thrown.
  478. *
  479. * \return \c true if the message is successfully sent, and \c false otherwise (e.g.,
  480. * when the queue is full).
  481. *
  482. * <b>Throws:</b> <tt>std::logic_error</tt> in case if the message size exceeds the queue
  483. * capacity, <tt>system_error</tt> in case if a native OS method fails.
  484. */
  485. BOOST_LOG_API bool try_send(void const* message_data, size_type message_size);
  486. /*!
  487. * The method takes a message from the associated message queue. When the object is in
  488. * running state and the queue is empty, the method blocks. The blocking is interrupted
  489. * when <tt>stop_local()</tt> is called, in which case the method returns \c operation_result::aborted.
  490. * When the object is already in the stopped state and the queue is empty, the method
  491. * does not block but returns immediately with return value \c operation_result::aborted.
  492. *
  493. * Concurrent calls to <tt>send()</tt>, <tt>try_send()</tt>, <tt>receive()</tt>,
  494. * <tt>try_receive()</tt>, <tt>stop_local()</tt>, and <tt>clear()</tt> are allowed.
  495. *
  496. * \pre <tt>is_open() == true</tt>
  497. *
  498. * \param buffer The memory buffer to store the received message in.
  499. * \param buffer_size The size of the buffer, in bytes.
  500. * \param message_size Receives the size of the received message, in bytes.
  501. *
  502. * \retval operation_result::succeeded if the operation is successful
  503. * \retval operation_result::aborted if the call was interrupted by <tt>stop_local()</tt>
  504. */
  505. operation_result receive(void* buffer, size_type buffer_size, size_type& message_size)
  506. {
  507. fixed_buffer_state state = { static_cast< uint8_t* >(buffer), buffer_size };
  508. operation_result result = do_receive(&reliable_message_queue::fixed_buffer_receive_handler, &state);
  509. message_size = buffer_size - state.size;
  510. return result;
  511. }
  512. /*!
  513. * The method takes a message from the associated message queue. When the object is in
  514. * running state and the queue is empty, the method blocks. The blocking is interrupted
  515. * when <tt>stop_local()</tt> is called, in which case the method returns \c operation_result::aborted.
  516. * When the object is already in the stopped state and the queue is empty, the method
  517. * does not block but returns immediately with return value \c operation_result::aborted.
  518. *
  519. * Concurrent calls to <tt>send()</tt>, <tt>try_send()</tt>, <tt>receive()</tt>,
  520. * <tt>try_receive()</tt>, <tt>stop_local()</tt>, and <tt>clear()</tt> are allowed.
  521. *
  522. * \pre <tt>is_open() == true</tt>
  523. *
  524. * \param buffer The memory buffer to store the received message in.
  525. * \param message_size Receives the size of the received message, in bytes.
  526. *
  527. * \retval operation_result::succeeded if the operation is successful
  528. * \retval operation_result::aborted if the call was interrupted by <tt>stop_local()</tt>
  529. */
  530. template< typename ElementT, size_type SizeV >
  531. #if !defined(BOOST_LOG_DOXYGEN_PASS)
  532. typename aux::enable_if_byte< ElementT, operation_result >::type
  533. #else
  534. operation_result
  535. #endif
  536. receive(ElementT (&buffer)[SizeV], size_type& message_size)
  537. {
  538. return receive(buffer, SizeV, message_size);
  539. }
  540. /*!
  541. * The method takes a message from the associated message queue. When the object is in
  542. * running state and the queue is empty, the method blocks. The blocking is interrupted
  543. * when <tt>stop_local()</tt> is called, in which case the method returns \c operation_result::aborted.
  544. * When the object is already in the stopped state and the queue is empty, the method
  545. * does not block but returns immediately with return value \c operation_result::aborted.
  546. *
  547. * Concurrent calls to <tt>send()</tt>, <tt>try_send()</tt>, <tt>receive()</tt>,
  548. * <tt>try_receive()</tt>, <tt>stop_local()</tt>, and <tt>clear()</tt> are allowed.
  549. *
  550. * \pre <tt>is_open() == true</tt>
  551. *
  552. * \param container The container to store the received message in. The container should have
  553. * value type of <tt>char</tt>, <tt>signed char</tt> or <tt>unsigned char</tt>
  554. * and support inserting elements at the end.
  555. *
  556. * \retval operation_result::succeeded if the operation is successful
  557. * \retval operation_result::aborted if the call was interrupted by <tt>stop_local()</tt>
  558. */
  559. template< typename ContainerT >
  560. #if !defined(BOOST_LOG_DOXYGEN_PASS)
  561. typename aux::enable_if_byte< typename ContainerT::value_type, operation_result >::type
  562. #else
  563. operation_result
  564. #endif
  565. receive(ContainerT& container)
  566. {
  567. return do_receive(&reliable_message_queue::container_receive_handler< ContainerT >, &container);
  568. }
  569. /*!
  570. * The method performs an attempt to take a message from the associated message queue. The
  571. * method is non-blocking, and always returns immediately.
  572. *
  573. * Concurrent calls to <tt>send()</tt>, <tt>try_send()</tt>, <tt>receive()</tt>,
  574. * <tt>try_receive()</tt>, <tt>stop_local()</tt>, and <tt>clear()</tt> are allowed.
  575. *
  576. * \pre <tt>is_open() == true</tt>
  577. *
  578. * \param buffer The memory buffer to store the received message in.
  579. * \param buffer_size The size of the buffer, in bytes.
  580. * \param message_size Receives the size of the received message, in bytes.
  581. *
  582. * \return \c true if a message is successfully received, and \c false otherwise (e.g.,
  583. * when the queue is empty).
  584. */
  585. bool try_receive(void* buffer, size_type buffer_size, size_type& message_size)
  586. {
  587. fixed_buffer_state state = { static_cast< uint8_t* >(buffer), buffer_size };
  588. bool result = do_try_receive(&reliable_message_queue::fixed_buffer_receive_handler, &state);
  589. message_size = buffer_size - state.size;
  590. return result;
  591. }
  592. /*!
  593. * The method performs an attempt to take a message from the associated message queue. The
  594. * method is non-blocking, and always returns immediately.
  595. *
  596. * Concurrent calls to <tt>send()</tt>, <tt>try_send()</tt>, <tt>receive()</tt>,
  597. * <tt>try_receive()</tt>, <tt>stop_local()</tt>, and <tt>clear()</tt> are allowed.
  598. *
  599. * \pre <tt>is_open() == true</tt>
  600. *
  601. * \param buffer The memory buffer to store the received message in.
  602. * \param message_size Receives the size of the received message, in bytes.
  603. *
  604. * \return \c true if a message is successfully received, and \c false otherwise (e.g.,
  605. * when the queue is empty).
  606. */
  607. template< typename ElementT, size_type SizeV >
  608. #if !defined(BOOST_LOG_DOXYGEN_PASS)
  609. typename aux::enable_if_byte< ElementT, bool >::type
  610. #else
  611. bool
  612. #endif
  613. try_receive(ElementT (&buffer)[SizeV], size_type& message_size)
  614. {
  615. return try_receive(buffer, SizeV, message_size);
  616. }
  617. /*!
  618. * The method performs an attempt to take a message from the associated message queue. The
  619. * method is non-blocking, and always returns immediately.
  620. *
  621. * Concurrent calls to <tt>send()</tt>, <tt>try_send()</tt>, <tt>receive()</tt>,
  622. * <tt>try_receive()</tt>, <tt>stop_local()</tt>, and <tt>clear()</tt> are allowed.
  623. *
  624. * \pre <tt>is_open() == true</tt>
  625. *
  626. * \param container The container to store the received message in. The container should have
  627. * value type of <tt>char</tt>, <tt>signed char</tt> or <tt>unsigned char</tt>
  628. * and support inserting elements at the end.
  629. *
  630. * \return \c true if a message is successfully received, and \c false otherwise (e.g.,
  631. * when the queue is empty).
  632. */
  633. template< typename ContainerT >
  634. #if !defined(BOOST_LOG_DOXYGEN_PASS)
  635. typename aux::enable_if_byte< typename ContainerT::value_type, bool >::type
  636. #else
  637. bool
  638. #endif
  639. try_receive(ContainerT& container)
  640. {
  641. return do_try_receive(&reliable_message_queue::container_receive_handler< ContainerT >, &container);
  642. }
  643. /*!
  644. * The method frees system-wide resources, associated with the interprocess queue with the supplied name.
  645. * The queue referred to by the specified name must not be opened in any process at the point of this call.
  646. * After this call succeeds a new queue with the specified name can be created.
  647. *
  648. * This call can be useful to recover from an earlier process misbehavior (e.g. a crash without properly
  649. * closing the message queue). In this case resources allocated for the interprocess queue may remain
  650. * allocated after the last process closed the queue, which in turn may prevent creating a new queue with
  651. * the same name. By calling this method before creating a queue the application can attempt to ensure
  652. * it starts with a clean slate.
  653. *
  654. * On some platforms resources associated with the queue are automatically reclaimed by the operating system
  655. * when the last process using those resources terminates (even if it terminates abnormally). On these
  656. * platforms this call may be a no-op. However, portable code should still call this method at appropriate
  657. * places to ensure compatibility with other platforms and future library versions, which may change implementation
  658. * of the queue.
  659. *
  660. * \param name Name of the message queue to be removed.
  661. */
  662. static BOOST_LOG_API void remove(object_name const& name);
  663. #if !defined(BOOST_LOG_DOXYGEN_PASS)
  664. private:
  665. //! Implementation of the constructor with named arguments
  666. template< typename ArgsT >
  667. void construct(ArgsT const& args)
  668. {
  669. m_impl = NULL;
  670. construct_dispatch(args[keywords::open_mode], args);
  671. }
  672. //! Implementation of the constructor with named arguments
  673. template< typename ArgsT >
  674. void construct_dispatch(open_mode::create_only_tag, ArgsT const& args)
  675. {
  676. this->create(args[keywords::name], args[keywords::capacity], args[keywords::block_size], args[keywords::overflow_policy | block_on_overflow], args[keywords::permissions | permissions()]);
  677. }
  678. //! Implementation of the constructor with named arguments
  679. template< typename ArgsT >
  680. void construct_dispatch(open_mode::open_or_create_tag, ArgsT const& args)
  681. {
  682. this->open_or_create(args[keywords::name], args[keywords::capacity], args[keywords::block_size], args[keywords::overflow_policy | block_on_overflow], args[keywords::permissions | permissions()]);
  683. }
  684. //! Implementation of the constructor with named arguments
  685. template< typename ArgsT >
  686. void construct_dispatch(open_mode::open_only_tag, ArgsT const& args)
  687. {
  688. this->open(args[keywords::name], args[keywords::overflow_policy | block_on_overflow], args[keywords::permissions | permissions()]);
  689. }
  690. //! Closes the message queue, if it's open
  691. BOOST_LOG_API void do_close() BOOST_NOEXCEPT;
  692. //! Receives the message from the queue and calls the handler to place the data in the user's storage
  693. BOOST_LOG_API operation_result do_receive(receive_handler handler, void* state);
  694. //! Attempts to receives the message from the queue and calls the handler to place the data in the user's storage
  695. BOOST_LOG_API bool do_try_receive(receive_handler handler, void* state);
  696. //! Fixed buffer receive handler
  697. static BOOST_LOG_API void fixed_buffer_receive_handler(void* state, const void* data, size_type size);
  698. //! Receive handler for a container
  699. template< typename ContainerT >
  700. static void container_receive_handler(void* state, const void* data, size_type size)
  701. {
  702. ContainerT* const container = static_cast< ContainerT* >(state);
  703. container->insert
  704. (
  705. container->end(),
  706. static_cast< typename ContainerT::value_type const* >(data),
  707. static_cast< typename ContainerT::value_type const* >(data) + size
  708. );
  709. }
  710. #endif
  711. };
  712. } // namespace ipc
  713. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  714. } // namespace boost
  715. #include <boost/log/detail/footer.hpp>
  716. #endif // BOOST_LOG_UTILITY_IPC_RELIABLE_MESSAGE_QUEUE_HPP_INCLUDED_