message_queue.hpp 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/interprocess for documentation.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. #ifndef BOOST_INTERPROCESS_MESSAGE_QUEUE_HPP
  11. #define BOOST_INTERPROCESS_MESSAGE_QUEUE_HPP
  12. #ifndef BOOST_CONFIG_HPP
  13. # include <boost/config.hpp>
  14. #endif
  15. #
  16. #if defined(BOOST_HAS_PRAGMA_ONCE)
  17. # pragma once
  18. #endif
  19. #include <boost/interprocess/detail/config_begin.hpp>
  20. #include <boost/interprocess/detail/workaround.hpp>
  21. #include <boost/interprocess/shared_memory_object.hpp>
  22. #include <boost/interprocess/detail/managed_open_or_create_impl.hpp>
  23. #include <boost/interprocess/sync/interprocess_condition.hpp>
  24. #include <boost/interprocess/sync/interprocess_mutex.hpp>
  25. #include <boost/interprocess/sync/scoped_lock.hpp>
  26. #include <boost/interprocess/detail/utilities.hpp>
  27. #include <boost/interprocess/timed_utils.hpp>
  28. #include <boost/interprocess/offset_ptr.hpp>
  29. #include <boost/interprocess/creation_tags.hpp>
  30. #include <boost/interprocess/exceptions.hpp>
  31. #include <boost/interprocess/permissions.hpp>
  32. #include <boost/core/no_exceptions_support.hpp>
  33. #include <boost/interprocess/detail/type_traits.hpp>
  34. #include <boost/intrusive/pointer_traits.hpp>
  35. #include <boost/move/detail/type_traits.hpp> //make_unsigned, alignment_of
  36. #include <boost/intrusive/pointer_traits.hpp>
  37. #include <boost/move/detail/force_ptr.hpp>
  38. #include <boost/assert.hpp>
  39. #include <algorithm> //std::lower_bound
  40. #include <cstddef> //std::size_t
  41. #include <cstring> //memcpy
  42. //!\file
  43. //!Describes an inter-process message queue. This class allows sending
  44. //!messages between processes and allows blocking, non-blocking and timed
  45. //!sending and receiving.
  46. namespace boost{ namespace interprocess{
  47. namespace ipcdetail
  48. {
  49. template<class VoidPointer>
  50. class msg_queue_initialization_func_t;
  51. }
  52. //Blocking modes
  53. enum mqblock_types { blocking, timed, non_blocking };
  54. //!A class that allows sending messages
  55. //!between processes.
  56. template<class VoidPointer>
  57. class message_queue_t
  58. {
  59. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  60. message_queue_t();
  61. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  62. public:
  63. typedef VoidPointer void_pointer;
  64. typedef typename boost::intrusive::
  65. pointer_traits<void_pointer>::template
  66. rebind_pointer<char>::type char_ptr;
  67. typedef typename boost::intrusive::pointer_traits<char_ptr>::difference_type difference_type;
  68. typedef typename boost::container::dtl::make_unsigned<difference_type>::type size_type;
  69. //!Creates a process shared message queue with name "name". For this message queue,
  70. //!the maximum number of messages will be "max_num_msg" and the maximum message size
  71. //!will be "max_msg_size". Throws on error and if the queue was previously created.
  72. message_queue_t(create_only_t,
  73. const char *name,
  74. size_type max_num_msg,
  75. size_type max_msg_size,
  76. const permissions &perm = permissions());
  77. //!Opens or creates a process shared message queue with name "name".
  78. //!If the queue is created, the maximum number of messages will be "max_num_msg"
  79. //!and the maximum message size will be "max_msg_size". If queue was previously
  80. //!created the queue will be opened and "max_num_msg" and "max_msg_size" parameters
  81. //!are ignored. Throws on error.
  82. message_queue_t(open_or_create_t,
  83. const char *name,
  84. size_type max_num_msg,
  85. size_type max_msg_size,
  86. const permissions &perm = permissions());
  87. //!Opens a previously created process shared message queue with name "name".
  88. //!If the queue was not previously created or there are no free resources,
  89. //!throws an error.
  90. message_queue_t(open_only_t, const char *name);
  91. #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  92. //!Creates a process shared message queue with name "name". For this message queue,
  93. //!the maximum number of messages will be "max_num_msg" and the maximum message size
  94. //!will be "max_msg_size". Throws on error and if the queue was previously created.
  95. //!
  96. //!Note: This function is only available on operating systems with
  97. //! native wchar_t APIs (e.g. Windows).
  98. message_queue_t(create_only_t,
  99. const wchar_t *name,
  100. size_type max_num_msg,
  101. size_type max_msg_size,
  102. const permissions &perm = permissions());
  103. //!Opens or creates a process shared message queue with name "name".
  104. //!If the queue is created, the maximum number of messages will be "max_num_msg"
  105. //!and the maximum message size will be "max_msg_size". If queue was previously
  106. //!created the queue will be opened and "max_num_msg" and "max_msg_size" parameters
  107. //!are ignored. Throws on error.
  108. //!
  109. //!Note: This function is only available on operating systems with
  110. //! native wchar_t APIs (e.g. Windows).
  111. message_queue_t(open_or_create_t,
  112. const wchar_t *name,
  113. size_type max_num_msg,
  114. size_type max_msg_size,
  115. const permissions &perm = permissions());
  116. //!Opens a previously created process shared message queue with name "name".
  117. //!If the queue was not previously created or there are no free resources,
  118. //!throws an error.
  119. //!
  120. //!Note: This function is only available on operating systems with
  121. //! native wchar_t APIs (e.g. Windows).
  122. message_queue_t(open_only_t, const wchar_t *name);
  123. #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  124. //!Creates a process shared message queue in anonymous memory. For this message queue,
  125. //!the maximum number of messages will be "max_num_msg" and the maximum message size
  126. //!will be "max_msg_size". Throws on error.
  127. message_queue_t(size_type max_num_msg,
  128. size_type max_msg_size);
  129. //!Destroys *this and indicates that the calling process is finished using
  130. //!the resource. All opened message queues are still
  131. //!valid after destruction. The destructor function will deallocate
  132. //!any system resources allocated by the system for use by this process for
  133. //!this resource. The resource can still be opened again calling
  134. //!the open constructor overload. To erase the message queue from the system
  135. //!use remove().
  136. ~message_queue_t();
  137. //!Sends a message stored in buffer "buffer" with size "buffer_size" in the
  138. //!message queue with priority "priority". If the message queue is full
  139. //!the sender is blocked. Throws interprocess_error on error.
  140. void send (const void *buffer, size_type buffer_size,
  141. unsigned int priority);
  142. //!Sends a message stored in buffer "buffer" with size "buffer_size" through the
  143. //!message queue with priority "priority". If the message queue is full
  144. //!the sender is not blocked and returns false, otherwise returns true.
  145. //!Throws interprocess_error on error.
  146. bool try_send (const void *buffer, size_type buffer_size,
  147. unsigned int priority);
  148. //!Sends a message stored in buffer "buffer" with size "buffer_size" in the
  149. //!message queue with priority "priority". If the message queue is full
  150. //!the sender retries until time "abs_time" is reached. Returns true if
  151. //!the message has been successfully sent. Returns false if timeout is reached.
  152. //!Throws interprocess_error on error.
  153. template<class TimePoint>
  154. bool timed_send (const void *buffer, size_type buffer_size,
  155. unsigned int priority, const TimePoint& abs_time);
  156. //!Receives a message from the message queue. The message is stored in buffer
  157. //!"buffer", which has size "buffer_size". The received message has size
  158. //!"recvd_size" and priority "priority". If the message queue is empty
  159. //!the receiver is blocked. Throws interprocess_error on error.
  160. void receive (void *buffer, size_type buffer_size,
  161. size_type &recvd_size,unsigned int &priority);
  162. //!Receives a message from the message queue. The message is stored in buffer
  163. //!"buffer", which has size "buffer_size". The received message has size
  164. //!"recvd_size" and priority "priority". If the message queue is empty
  165. //!the receiver is not blocked and returns false, otherwise returns true.
  166. //!Throws interprocess_error on error.
  167. bool try_receive (void *buffer, size_type buffer_size,
  168. size_type &recvd_size,unsigned int &priority);
  169. //!Receives a message from the message queue. The message is stored in buffer
  170. //!"buffer", which has size "buffer_size". The received message has size
  171. //!"recvd_size" and priority "priority". If the message queue is empty
  172. //!the receiver retries until time "abs_time" is reached. Returns true if
  173. //!the message has been successfully sent. Returns false if timeout is reached.
  174. //!Throws interprocess_error on error.
  175. template<class TimePoint>
  176. bool timed_receive (void *buffer, size_type buffer_size,
  177. size_type &recvd_size,unsigned int &priority,
  178. const TimePoint &abs_time);
  179. //!Returns the maximum number of messages allowed by the queue. The message
  180. //!queue must be opened or created previously. Otherwise, returns 0.
  181. //!Never throws
  182. size_type get_max_msg() const;
  183. //!Returns the maximum size of message allowed by the queue. The message
  184. //!queue must be opened or created previously. Otherwise, returns 0.
  185. //!Never throws
  186. size_type get_max_msg_size() const;
  187. //!Returns the number of messages currently stored.
  188. //!Never throws
  189. size_type get_num_msg() const;
  190. //!Removes the message queue from the system.
  191. //!Returns false on error. Never throws
  192. static bool remove(const char *name);
  193. #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  194. //!Removes the message queue from the system.
  195. //!Returns false on error. Never throws
  196. //!
  197. //!Note: This function is only available on operating systems with
  198. //! native wchar_t APIs (e.g. Windows).
  199. static bool remove(const wchar_t *name);
  200. #endif
  201. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  202. private:
  203. friend class ipcdetail::msg_queue_initialization_func_t<VoidPointer>;
  204. template<mqblock_types Block, class TimePoint>
  205. bool do_receive(void *buffer, size_type buffer_size,
  206. size_type &recvd_size, unsigned int &priority,
  207. const TimePoint &abs_time);
  208. template<mqblock_types Block, class TimePoint>
  209. bool do_send(const void *buffer, size_type buffer_size,
  210. unsigned int priority, const TimePoint &abs_time);
  211. //!Returns the needed memory size for the shared message queue.
  212. //!Never throws
  213. static size_type get_mem_size(size_type max_msg_size, size_type max_num_msg);
  214. typedef ipcdetail::managed_open_or_create_impl<shared_memory_object, 0, true, false> open_create_impl_t;
  215. open_create_impl_t m_shmem;
  216. template<class Lock, class TimePoint>
  217. static bool do_cond_wait(ipcdetail::bool_<true>, interprocess_condition &cond, Lock &lock, const TimePoint &abs_time)
  218. { return cond.timed_wait(lock, abs_time); }
  219. template<class Lock, class TimePoint>
  220. static bool do_cond_wait(ipcdetail::bool_<false>, interprocess_condition &cond, Lock &lock, const TimePoint &)
  221. { cond.wait(lock); return true; }
  222. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  223. };
  224. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  225. namespace ipcdetail {
  226. //!This header is the prefix of each message in the queue
  227. template<class VoidPointer>
  228. class msg_hdr_t
  229. {
  230. typedef VoidPointer void_pointer;
  231. typedef typename boost::intrusive::
  232. pointer_traits<void_pointer>::template
  233. rebind_pointer<char>::type char_ptr;
  234. typedef typename boost::intrusive::pointer_traits<char_ptr>::difference_type difference_type;
  235. typedef typename boost::container::dtl::make_unsigned<difference_type>::type size_type;
  236. public:
  237. size_type len; // Message length
  238. unsigned int priority;// Message priority
  239. //!Returns the data buffer associated with this this message
  240. void * data(){ return this+1; } //
  241. };
  242. //!This functor is the predicate to order stored messages by priority
  243. template<class VoidPointer>
  244. class priority_functor
  245. {
  246. typedef typename boost::intrusive::
  247. pointer_traits<VoidPointer>::template
  248. rebind_pointer<msg_hdr_t<VoidPointer> >::type msg_hdr_ptr_t;
  249. public:
  250. bool operator()(const msg_hdr_ptr_t &msg1,
  251. const msg_hdr_ptr_t &msg2) const
  252. { return msg1->priority < msg2->priority; }
  253. };
  254. //!This header is placed in the beginning of the shared memory and contains
  255. //!the data to control the queue. This class initializes the shared memory
  256. //!in the following way: in ascending memory address with proper alignment
  257. //!fillings:
  258. //!
  259. //!-> mq_hdr_t:
  260. //! Main control block that controls the rest of the elements
  261. //!
  262. //!-> offset_ptr<msg_hdr_t> index [max_num_msg]
  263. //! An array of pointers with size "max_num_msg" called index. Each pointer
  264. //! points to a preallocated message. Elements of this array are
  265. //! reordered in runtime in the following way:
  266. //!
  267. //! IF BOOST_INTERPROCESS_MSG_QUEUE_CIRCULAR_INDEX is defined:
  268. //!
  269. //! When the current number of messages is "cur_num_msg", the array
  270. //! is treated like a circular buffer. Starting from position "cur_first_msg"
  271. //! "cur_num_msg" in a circular way, pointers point to inserted messages and the rest
  272. //! point to free messages. Those "cur_num_msg" pointers are
  273. //! ordered by the priority of the pointed message and by insertion order
  274. //! if two messages have the same priority. So the next message to be
  275. //! used in a "receive" is pointed by index [(cur_first_msg + cur_num_msg-1)%max_num_msg]
  276. //! and the first free message ready to be used in a "send" operation is
  277. //! [cur_first_msg] if circular buffer is extended from front,
  278. //! [(cur_first_msg + cur_num_msg)%max_num_msg] otherwise.
  279. //!
  280. //! This transforms the index in a circular buffer with an embedded free
  281. //! message queue.
  282. //!
  283. //! ELSE (BOOST_INTERPROCESS_MSG_QUEUE_CIRCULAR_INDEX is NOT defined):
  284. //!
  285. //! When the current number of messages is "cur_num_msg", the first
  286. //! "cur_num_msg" pointers point to inserted messages and the rest
  287. //! point to free messages. The first "cur_num_msg" pointers are
  288. //! ordered by the priority of the pointed message and by insertion order
  289. //! if two messages have the same priority. So the next message to be
  290. //! used in a "receive" is pointed by index [cur_num_msg-1] and the first free
  291. //! message ready to be used in a "send" operation is index [cur_num_msg].
  292. //!
  293. //! This transforms the index in a fixed size priority queue with an embedded free
  294. //! message queue.
  295. //!
  296. //!-> struct message_t
  297. //! {
  298. //! msg_hdr_t header;
  299. //! char[max_msg_size] data;
  300. //! } messages [max_num_msg];
  301. //!
  302. //! An array of buffers of preallocated messages, each one prefixed with the
  303. //! msg_hdr_t structure. Each of this message is pointed by one pointer of
  304. //! the index structure.
  305. template<class VoidPointer>
  306. class mq_hdr_t
  307. : public ipcdetail::priority_functor<VoidPointer>
  308. {
  309. typedef VoidPointer void_pointer;
  310. typedef msg_hdr_t<void_pointer> msg_header;
  311. typedef typename boost::intrusive::
  312. pointer_traits<void_pointer>::template
  313. rebind_pointer<msg_header>::type msg_hdr_ptr_t;
  314. typedef typename boost::intrusive::pointer_traits
  315. <msg_hdr_ptr_t>::difference_type difference_type;
  316. typedef typename boost::container::
  317. dtl::make_unsigned<difference_type>::type size_type;
  318. typedef typename boost::intrusive::
  319. pointer_traits<void_pointer>::template
  320. rebind_pointer<msg_hdr_ptr_t>::type msg_hdr_ptr_ptr_t;
  321. typedef ipcdetail::managed_open_or_create_impl<shared_memory_object, 0, true, false> open_create_impl_t;
  322. public:
  323. //!Constructor. This object must be constructed in the beginning of the
  324. //!shared memory of the size returned by the function "get_mem_size".
  325. //!This constructor initializes the needed resources and creates
  326. //!the internal structures like the priority index. This can throw.
  327. mq_hdr_t(size_type max_num_msg, size_type max_msg_size)
  328. : m_max_num_msg(max_num_msg),
  329. m_max_msg_size(max_msg_size),
  330. m_cur_num_msg(0)
  331. #if defined(BOOST_INTERPROCESS_MSG_QUEUE_CIRCULAR_INDEX)
  332. ,m_cur_first_msg(0u)
  333. ,m_blocked_senders(0u)
  334. ,m_blocked_receivers(0u)
  335. #endif
  336. { this->initialize_memory(); }
  337. //!Returns true if the message queue is full
  338. bool is_full() const
  339. { return m_cur_num_msg == m_max_num_msg; }
  340. //!Returns true if the message queue is empty
  341. bool is_empty() const
  342. { return !m_cur_num_msg; }
  343. //!Frees the top priority message and saves it in the free message list
  344. void free_top_msg()
  345. { --m_cur_num_msg; }
  346. #if defined(BOOST_INTERPROCESS_MSG_QUEUE_CIRCULAR_INDEX)
  347. typedef msg_hdr_ptr_t *iterator;
  348. size_type end_pos() const
  349. {
  350. const size_type space_until_bufend = m_max_num_msg - m_cur_first_msg;
  351. return space_until_bufend > m_cur_num_msg
  352. ? m_cur_first_msg + m_cur_num_msg : m_cur_num_msg - space_until_bufend;
  353. }
  354. //!Returns the inserted message with top priority
  355. msg_header &top_msg()
  356. {
  357. size_type pos = this->end_pos();
  358. return *mp_index[difference_type(pos ? --pos : m_max_num_msg - 1)];
  359. }
  360. //!Returns the inserted message with bottom priority
  361. msg_header &bottom_msg()
  362. { return *mp_index[difference_type(m_cur_first_msg)]; }
  363. iterator inserted_ptr_begin() const
  364. { return &mp_index[difference_type(m_cur_first_msg)]; }
  365. iterator inserted_ptr_end() const
  366. { return &mp_index[difference_type(this->end_pos())]; }
  367. iterator lower_bound(const msg_hdr_ptr_t & value, priority_functor<VoidPointer> func)
  368. {
  369. iterator begin(this->inserted_ptr_begin()), end(this->inserted_ptr_end());
  370. if(end < begin){
  371. iterator idx_end = &mp_index[difference_type(m_max_num_msg)];
  372. iterator ret = std::lower_bound(begin, idx_end, value, func);
  373. if(idx_end == ret){
  374. iterator idx_beg = &mp_index[0];
  375. ret = std::lower_bound(idx_beg, end, value, func);
  376. //sanity check, these cases should not call lower_bound (optimized out)
  377. BOOST_ASSERT(ret != end);
  378. BOOST_ASSERT(ret != begin);
  379. return ret;
  380. }
  381. else{
  382. return ret;
  383. }
  384. }
  385. else{
  386. return std::lower_bound(begin, end, value, func);
  387. }
  388. }
  389. msg_header & insert_at(iterator where)
  390. {
  391. iterator it_inserted_ptr_end = this->inserted_ptr_end();
  392. iterator it_inserted_ptr_beg = this->inserted_ptr_begin();
  393. if(where == it_inserted_ptr_beg){
  394. //unsigned integer guarantees underflow
  395. m_cur_first_msg = m_cur_first_msg ? m_cur_first_msg : m_max_num_msg;
  396. --m_cur_first_msg;
  397. ++m_cur_num_msg;
  398. return *mp_index[difference_type(m_cur_first_msg)];
  399. }
  400. else if(where == it_inserted_ptr_end){
  401. ++m_cur_num_msg;
  402. return **it_inserted_ptr_end;
  403. }
  404. else{
  405. size_type pos = size_type(where - &mp_index[0]);
  406. size_type circ_pos = pos >= m_cur_first_msg ? pos - m_cur_first_msg : pos + (m_max_num_msg - m_cur_first_msg);
  407. //Check if it's more efficient to move back or move front
  408. if(circ_pos < m_cur_num_msg/2){
  409. //The queue can't be full so m_cur_num_msg == 0 or m_cur_num_msg <= pos
  410. //indicates two step insertion
  411. if(!pos){
  412. pos = m_max_num_msg;
  413. where = &mp_index[difference_type(m_max_num_msg-1u)];
  414. }
  415. else{
  416. --where;
  417. }
  418. const bool unique_segment = m_cur_first_msg && m_cur_first_msg <= pos;
  419. const size_type first_segment_beg = unique_segment ? m_cur_first_msg : 1u;
  420. const size_type first_segment_end = pos;
  421. const size_type second_segment_beg = unique_segment || !m_cur_first_msg ? m_max_num_msg : m_cur_first_msg;
  422. const size_type second_segment_end = m_max_num_msg;
  423. const msg_hdr_ptr_t backup = *(&mp_index[0] + (unique_segment ? first_segment_beg : second_segment_beg) - 1);
  424. //First segment
  425. if(!unique_segment){
  426. std::copy( &mp_index[0] + second_segment_beg
  427. , &mp_index[0] + second_segment_end
  428. , &mp_index[0] + second_segment_beg - 1);
  429. mp_index[difference_type(m_max_num_msg-1u)] = mp_index[0];
  430. }
  431. std::copy( &mp_index[0] + first_segment_beg
  432. , &mp_index[0] + first_segment_end
  433. , &mp_index[0] + first_segment_beg - 1);
  434. *where = backup;
  435. m_cur_first_msg = m_cur_first_msg ? m_cur_first_msg : m_max_num_msg;
  436. --m_cur_first_msg;
  437. ++m_cur_num_msg;
  438. return **where;
  439. }
  440. else{
  441. //The queue can't be full so end_pos < m_cur_first_msg
  442. //indicates two step insertion
  443. const size_type pos_end = this->end_pos();
  444. const bool unique_segment = pos < pos_end;
  445. const size_type first_segment_beg = pos;
  446. const size_type first_segment_end = unique_segment ? pos_end : m_max_num_msg-1;
  447. const size_type second_segment_beg = 0u;
  448. const size_type second_segment_end = unique_segment ? 0u : pos_end;
  449. const msg_hdr_ptr_t backup = *it_inserted_ptr_end;
  450. //First segment
  451. if(!unique_segment){
  452. std::copy_backward( &mp_index[0] + second_segment_beg
  453. , &mp_index[0] + second_segment_end
  454. , &mp_index[0] + second_segment_end + 1u);
  455. mp_index[0] = mp_index[difference_type(m_max_num_msg-1u)];
  456. }
  457. std::copy_backward( &mp_index[0] + first_segment_beg
  458. , &mp_index[0] + first_segment_end
  459. , &mp_index[0] + first_segment_end + 1u);
  460. *where = backup;
  461. ++m_cur_num_msg;
  462. return **where;
  463. }
  464. }
  465. }
  466. #else //BOOST_INTERPROCESS_MSG_QUEUE_CIRCULAR_INDEX
  467. typedef msg_hdr_ptr_t *iterator;
  468. //!Returns the inserted message with top priority
  469. msg_header &top_msg()
  470. { return *mp_index[difference_type(m_cur_num_msg-1u)]; }
  471. //!Returns the inserted message with bottom priority
  472. msg_header &bottom_msg()
  473. { return *mp_index[0]; }
  474. iterator inserted_ptr_begin() const
  475. { return &mp_index[0]; }
  476. iterator inserted_ptr_end() const
  477. { return &mp_index[difference_type(m_cur_num_msg)]; }
  478. iterator lower_bound(const msg_hdr_ptr_t & value, priority_functor<VoidPointer> func)
  479. { return std::lower_bound(this->inserted_ptr_begin(), this->inserted_ptr_end(), value, func); }
  480. msg_header & insert_at(iterator pos)
  481. {
  482. const msg_hdr_ptr_t backup = *inserted_ptr_end();
  483. std::copy_backward(pos, inserted_ptr_end(), inserted_ptr_end()+1);
  484. *pos = backup;
  485. ++m_cur_num_msg;
  486. return **pos;
  487. }
  488. #endif //BOOST_INTERPROCESS_MSG_QUEUE_CIRCULAR_INDEX
  489. //!Inserts the first free message in the priority queue
  490. msg_header & queue_free_msg(unsigned int priority)
  491. {
  492. //Get priority queue's range
  493. iterator it (inserted_ptr_begin()), it_end(inserted_ptr_end());
  494. //Optimize for non-priority usage
  495. if(m_cur_num_msg && priority > this->bottom_msg().priority){
  496. //Check for higher priority than all stored messages
  497. if(priority > this->top_msg().priority){
  498. it = it_end;
  499. }
  500. else{
  501. //Since we don't now which free message we will pick
  502. //build a dummy header for searches
  503. msg_header dummy_hdr;
  504. dummy_hdr.priority = priority;
  505. //Get free msg
  506. msg_hdr_ptr_t dummy_ptr(&dummy_hdr);
  507. //Check where the free message should be placed
  508. it = this->lower_bound(dummy_ptr, static_cast<priority_functor<VoidPointer>&>(*this));
  509. }
  510. }
  511. //Insert the free message in the correct position
  512. return this->insert_at(it);
  513. }
  514. //!Returns the number of bytes needed to construct a message queue with
  515. //!"max_num_size" maximum number of messages and "max_msg_size" maximum
  516. //!message size. Never throws.
  517. static size_type get_mem_size
  518. (size_type max_msg_size, size_type max_num_msg)
  519. {
  520. const size_type
  521. msg_hdr_align = ::boost::container::dtl::alignment_of<msg_header>::value,
  522. index_align = ::boost::container::dtl::alignment_of<msg_hdr_ptr_t>::value,
  523. r_hdr_size = ipcdetail::ct_rounded_size<sizeof(mq_hdr_t), index_align>::value,
  524. r_index_size = ipcdetail::get_rounded_size<size_type>(max_num_msg*sizeof(msg_hdr_ptr_t), msg_hdr_align),
  525. r_max_msg_size = ipcdetail::get_rounded_size<size_type>(max_msg_size, msg_hdr_align) + sizeof(msg_header);
  526. return r_hdr_size + r_index_size + (max_num_msg*r_max_msg_size) +
  527. open_create_impl_t::ManagedOpenOrCreateUserOffset;
  528. }
  529. //!Initializes the memory structures to preallocate messages and constructs the
  530. //!message index. Never throws.
  531. void initialize_memory()
  532. {
  533. const size_type
  534. msg_hdr_align = ::boost::container::dtl::alignment_of<msg_header>::value,
  535. index_align = ::boost::container::dtl::alignment_of<msg_hdr_ptr_t>::value,
  536. r_hdr_size = ipcdetail::ct_rounded_size<sizeof(mq_hdr_t), index_align>::value,
  537. r_index_size = ipcdetail::get_rounded_size<size_type>(m_max_num_msg*sizeof(msg_hdr_ptr_t), msg_hdr_align),
  538. r_max_msg_size = ipcdetail::get_rounded_size<size_type>(m_max_msg_size, msg_hdr_align) + sizeof(msg_header);
  539. //Pointer to the index
  540. msg_hdr_ptr_t *index = move_detail::force_ptr<msg_hdr_ptr_t*>
  541. (reinterpret_cast<char*>(this)+r_hdr_size);
  542. //Pointer to the first message header
  543. msg_header *msg_hdr = move_detail::force_ptr<msg_header*>
  544. (reinterpret_cast<char*>(this)+r_hdr_size+r_index_size);
  545. //Initialize the pointer to the index
  546. mp_index = index;
  547. //Initialize the index so each slot points to a preallocated message
  548. for(size_type i = 0; i < m_max_num_msg; ++i){
  549. index[i] = msg_hdr;
  550. msg_hdr = move_detail::force_ptr<msg_header*>
  551. (reinterpret_cast<char*>(msg_hdr)+r_max_msg_size);
  552. }
  553. }
  554. public:
  555. //Pointer to the index
  556. msg_hdr_ptr_ptr_t mp_index;
  557. //Maximum number of messages of the queue
  558. const size_type m_max_num_msg;
  559. //Maximum size of messages of the queue
  560. const size_type m_max_msg_size;
  561. //Current number of messages
  562. size_type m_cur_num_msg;
  563. //Mutex to protect data structures
  564. interprocess_mutex m_mutex;
  565. //Condition block receivers when there are no messages
  566. interprocess_condition m_cond_recv;
  567. //Condition block senders when the queue is full
  568. interprocess_condition m_cond_send;
  569. #if defined(BOOST_INTERPROCESS_MSG_QUEUE_CIRCULAR_INDEX)
  570. //Current start offset in the circular index
  571. size_type m_cur_first_msg;
  572. size_type m_blocked_senders;
  573. size_type m_blocked_receivers;
  574. #endif
  575. };
  576. //!This is the atomic functor to be executed when creating or opening
  577. //!shared memory. Never throws
  578. template<class VoidPointer>
  579. class msg_queue_initialization_func_t
  580. {
  581. public:
  582. typedef typename boost::intrusive::
  583. pointer_traits<VoidPointer>::template
  584. rebind_pointer<char>::type char_ptr;
  585. typedef typename boost::intrusive::pointer_traits<char_ptr>::
  586. difference_type difference_type;
  587. typedef typename boost::container::dtl::
  588. make_unsigned<difference_type>::type size_type;
  589. msg_queue_initialization_func_t(size_type maxmsg = 0,
  590. size_type maxmsgsize = 0)
  591. : m_maxmsg (maxmsg), m_maxmsgsize(maxmsgsize) {}
  592. bool operator()(void *address, size_type, bool created)
  593. {
  594. char *mptr;
  595. if(created){
  596. mptr = reinterpret_cast<char*>(address);
  597. //Construct the message queue header at the beginning
  598. BOOST_TRY{
  599. new (mptr) mq_hdr_t<VoidPointer>(m_maxmsg, m_maxmsgsize);
  600. }
  601. BOOST_CATCH(...){
  602. return false;
  603. } BOOST_CATCH_END
  604. }
  605. return true;
  606. }
  607. std::size_t get_min_size() const
  608. {
  609. return mq_hdr_t<VoidPointer>::get_mem_size(m_maxmsgsize, m_maxmsg)
  610. - message_queue_t<VoidPointer>::open_create_impl_t::ManagedOpenOrCreateUserOffset;
  611. }
  612. const size_type m_maxmsg;
  613. const size_type m_maxmsgsize;
  614. };
  615. } //namespace ipcdetail {
  616. template<class VoidPointer>
  617. inline message_queue_t<VoidPointer>::~message_queue_t()
  618. {}
  619. template<class VoidPointer>
  620. inline typename message_queue_t<VoidPointer>::size_type message_queue_t<VoidPointer>::get_mem_size
  621. (size_type max_msg_size, size_type max_num_msg)
  622. { return ipcdetail::mq_hdr_t<VoidPointer>::get_mem_size(max_msg_size, max_num_msg); }
  623. template<class VoidPointer>
  624. inline message_queue_t<VoidPointer>::message_queue_t(create_only_t,
  625. const char *name,
  626. size_type max_num_msg,
  627. size_type max_msg_size,
  628. const permissions &perm)
  629. //Create shared memory and execute functor atomically
  630. : m_shmem(create_only,
  631. name,
  632. get_mem_size(max_msg_size, max_num_msg),
  633. read_write,
  634. static_cast<void*>(0),
  635. //Prepare initialization functor
  636. ipcdetail::msg_queue_initialization_func_t<VoidPointer> (max_num_msg, max_msg_size),
  637. perm)
  638. {}
  639. template<class VoidPointer>
  640. inline message_queue_t<VoidPointer>::message_queue_t(open_or_create_t,
  641. const char *name,
  642. size_type max_num_msg,
  643. size_type max_msg_size,
  644. const permissions &perm)
  645. //Create shared memory and execute functor atomically
  646. : m_shmem(open_or_create,
  647. name,
  648. get_mem_size(max_msg_size, max_num_msg),
  649. read_write,
  650. static_cast<void*>(0),
  651. //Prepare initialization functor
  652. ipcdetail::msg_queue_initialization_func_t<VoidPointer> (max_num_msg, max_msg_size),
  653. perm)
  654. {}
  655. template<class VoidPointer>
  656. inline message_queue_t<VoidPointer>::message_queue_t(open_only_t, const char *name)
  657. //Create shared memory and execute functor atomically
  658. : m_shmem(open_only,
  659. name,
  660. read_write,
  661. static_cast<void*>(0),
  662. //Prepare initialization functor
  663. ipcdetail::msg_queue_initialization_func_t<VoidPointer> ())
  664. {}
  665. #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  666. template<class VoidPointer>
  667. inline message_queue_t<VoidPointer>::message_queue_t(create_only_t,
  668. const wchar_t *name,
  669. size_type max_num_msg,
  670. size_type max_msg_size,
  671. const permissions &perm)
  672. //Create shared memory and execute functor atomically
  673. : m_shmem(create_only,
  674. name,
  675. get_mem_size(max_msg_size, max_num_msg),
  676. read_write,
  677. static_cast<void*>(0),
  678. //Prepare initialization functor
  679. ipcdetail::msg_queue_initialization_func_t<VoidPointer> (max_num_msg, max_msg_size),
  680. perm)
  681. {}
  682. template<class VoidPointer>
  683. inline message_queue_t<VoidPointer>::message_queue_t(open_or_create_t,
  684. const wchar_t *name,
  685. size_type max_num_msg,
  686. size_type max_msg_size,
  687. const permissions &perm)
  688. //Create shared memory and execute functor atomically
  689. : m_shmem(open_or_create,
  690. name,
  691. get_mem_size(max_msg_size, max_num_msg),
  692. read_write,
  693. static_cast<void*>(0),
  694. //Prepare initialization functor
  695. ipcdetail::msg_queue_initialization_func_t<VoidPointer> (max_num_msg, max_msg_size),
  696. perm)
  697. {}
  698. template<class VoidPointer>
  699. inline message_queue_t<VoidPointer>::message_queue_t(open_only_t, const wchar_t *name)
  700. //Create shared memory and execute functor atomically
  701. : m_shmem(open_only,
  702. name,
  703. read_write,
  704. static_cast<void*>(0),
  705. //Prepare initialization functor
  706. ipcdetail::msg_queue_initialization_func_t<VoidPointer> ())
  707. {}
  708. #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  709. template <class VoidPointer>
  710. inline message_queue_t<VoidPointer>::message_queue_t(size_type max_num_msg,
  711. size_type max_msg_size)
  712. : m_shmem(get_mem_size(max_msg_size, max_num_msg),
  713. static_cast<void*>(0),
  714. //Prepare initialization functor
  715. ipcdetail::msg_queue_initialization_func_t<VoidPointer> (max_num_msg, max_msg_size))
  716. {}
  717. template<class VoidPointer>
  718. inline void message_queue_t<VoidPointer>::send
  719. (const void *buffer, size_type buffer_size, unsigned int priority)
  720. { this->do_send<blocking>(buffer, buffer_size, priority, 0); }
  721. template<class VoidPointer>
  722. inline bool message_queue_t<VoidPointer>::try_send
  723. (const void *buffer, size_type buffer_size, unsigned int priority)
  724. { return this->do_send<non_blocking>(buffer, buffer_size, priority, 0); }
  725. template<class VoidPointer>
  726. template<class TimePoint>
  727. inline bool message_queue_t<VoidPointer>::timed_send
  728. (const void *buffer, size_type buffer_size
  729. ,unsigned int priority, const TimePoint &abs_time)
  730. {
  731. if(ipcdetail::is_pos_infinity(abs_time)){
  732. this->send(buffer, buffer_size, priority);
  733. return true;
  734. }
  735. return this->do_send<timed>(buffer, buffer_size, priority, abs_time);
  736. }
  737. template<class VoidPointer>
  738. template<mqblock_types Block, class TimePoint>
  739. inline bool message_queue_t<VoidPointer>::do_send(
  740. const void *buffer, size_type buffer_size,
  741. unsigned int priority, const TimePoint &abs_time)
  742. {
  743. ipcdetail::mq_hdr_t<VoidPointer> *p_hdr = static_cast<ipcdetail::mq_hdr_t<VoidPointer>*>(m_shmem.get_user_address());
  744. //Check if buffer is smaller than maximum allowed
  745. if (buffer_size > p_hdr->m_max_msg_size) {
  746. throw interprocess_exception(size_error);
  747. }
  748. #if defined(BOOST_INTERPROCESS_MSG_QUEUE_CIRCULAR_INDEX)
  749. bool notify_blocked_receivers = false;
  750. #endif
  751. //---------------------------------------------
  752. scoped_lock<interprocess_mutex> lock(p_hdr->m_mutex);
  753. //---------------------------------------------
  754. {
  755. //If the queue is full execute blocking logic
  756. if (p_hdr->is_full()) {
  757. BOOST_TRY{
  758. #ifdef BOOST_INTERPROCESS_MSG_QUEUE_CIRCULAR_INDEX
  759. ++p_hdr->m_blocked_senders;
  760. #endif
  761. switch(Block){
  762. case non_blocking :
  763. #ifdef BOOST_INTERPROCESS_MSG_QUEUE_CIRCULAR_INDEX
  764. --p_hdr->m_blocked_senders;
  765. #endif
  766. return false;
  767. break;
  768. case blocking :
  769. do{
  770. (void)do_cond_wait(ipcdetail::bool_<false>(), p_hdr->m_cond_send, lock, abs_time);
  771. }
  772. while (p_hdr->is_full());
  773. break;
  774. case timed :
  775. do{
  776. if(!do_cond_wait(ipcdetail::bool_<Block == timed>(), p_hdr->m_cond_send, lock, abs_time)) {
  777. if(p_hdr->is_full()){
  778. #ifdef BOOST_INTERPROCESS_MSG_QUEUE_CIRCULAR_INDEX
  779. --p_hdr->m_blocked_senders;
  780. #endif
  781. return false;
  782. }
  783. break;
  784. }
  785. }
  786. while (p_hdr->is_full());
  787. break;
  788. default:
  789. break;
  790. }
  791. #ifdef BOOST_INTERPROCESS_MSG_QUEUE_CIRCULAR_INDEX
  792. --p_hdr->m_blocked_senders;
  793. #endif
  794. }
  795. BOOST_CATCH(...){
  796. #ifdef BOOST_INTERPROCESS_MSG_QUEUE_CIRCULAR_INDEX
  797. --p_hdr->m_blocked_senders;
  798. #endif
  799. BOOST_RETHROW;
  800. } BOOST_CATCH_END
  801. }
  802. #if defined(BOOST_INTERPROCESS_MSG_QUEUE_CIRCULAR_INDEX)
  803. notify_blocked_receivers = 0 != p_hdr->m_blocked_receivers;
  804. #endif
  805. //Insert the first free message in the priority queue
  806. ipcdetail::msg_hdr_t<VoidPointer> &free_msg_hdr = p_hdr->queue_free_msg(priority);
  807. //Sanity check, free msgs are always cleaned when received
  808. BOOST_ASSERT(free_msg_hdr.priority == 0);
  809. BOOST_ASSERT(free_msg_hdr.len == 0);
  810. //Copy control data to the free message
  811. free_msg_hdr.priority = priority;
  812. free_msg_hdr.len = buffer_size;
  813. //Copy user buffer to the message
  814. std::memcpy(free_msg_hdr.data(), buffer, buffer_size);
  815. } // Lock end
  816. //Notify outside lock to avoid contention. This might produce some
  817. //spurious wakeups, but it's usually far better than notifying inside.
  818. //If this message changes the queue empty state, notify it to receivers
  819. #if defined(BOOST_INTERPROCESS_MSG_QUEUE_CIRCULAR_INDEX)
  820. if (notify_blocked_receivers){
  821. p_hdr->m_cond_recv.notify_one();
  822. }
  823. #else
  824. p_hdr->m_cond_recv.notify_one();
  825. #endif
  826. return true;
  827. }
  828. template<class VoidPointer>
  829. inline void message_queue_t<VoidPointer>::receive(void *buffer, size_type buffer_size,
  830. size_type &recvd_size, unsigned int &priority)
  831. { this->do_receive<blocking>(buffer, buffer_size, recvd_size, priority, 0); }
  832. template<class VoidPointer>
  833. inline bool
  834. message_queue_t<VoidPointer>::try_receive(void *buffer, size_type buffer_size,
  835. size_type &recvd_size, unsigned int &priority)
  836. { return this->do_receive<non_blocking>(buffer, buffer_size, recvd_size, priority, 0); }
  837. template<class VoidPointer>
  838. template<class TimePoint>
  839. inline bool
  840. message_queue_t<VoidPointer>::timed_receive(void *buffer, size_type buffer_size,
  841. size_type &recvd_size, unsigned int &priority,
  842. const TimePoint &abs_time)
  843. {
  844. if(ipcdetail::is_pos_infinity(abs_time)){
  845. this->receive(buffer, buffer_size, recvd_size, priority);
  846. return true;
  847. }
  848. return this->do_receive<timed>(buffer, buffer_size, recvd_size, priority, abs_time);
  849. }
  850. template<class VoidPointer>
  851. template<mqblock_types Block, class TimePoint>
  852. inline bool
  853. message_queue_t<VoidPointer>::do_receive(
  854. void *buffer, size_type buffer_size,
  855. size_type &recvd_size, unsigned int &priority,
  856. const TimePoint &abs_time)
  857. {
  858. ipcdetail::mq_hdr_t<VoidPointer> *p_hdr = static_cast<ipcdetail::mq_hdr_t<VoidPointer>*>(m_shmem.get_user_address());
  859. //Check if buffer is big enough for any message
  860. if (buffer_size < p_hdr->m_max_msg_size) {
  861. throw interprocess_exception(size_error);
  862. }
  863. #if defined(BOOST_INTERPROCESS_MSG_QUEUE_CIRCULAR_INDEX)
  864. bool notify_blocked_senders = false;
  865. #endif
  866. //---------------------------------------------
  867. scoped_lock<interprocess_mutex> lock(p_hdr->m_mutex);
  868. //---------------------------------------------
  869. {
  870. //If there are no messages execute blocking logic
  871. if (p_hdr->is_empty()) {
  872. BOOST_TRY{
  873. #if defined(BOOST_INTERPROCESS_MSG_QUEUE_CIRCULAR_INDEX)
  874. ++p_hdr->m_blocked_receivers;
  875. #endif
  876. switch(Block){
  877. case non_blocking :
  878. #if defined(BOOST_INTERPROCESS_MSG_QUEUE_CIRCULAR_INDEX)
  879. --p_hdr->m_blocked_receivers;
  880. #endif
  881. return false;
  882. break;
  883. case blocking :
  884. do{
  885. (void)do_cond_wait(ipcdetail::bool_<false>(), p_hdr->m_cond_recv, lock, abs_time);
  886. }
  887. while (p_hdr->is_empty());
  888. break;
  889. case timed :
  890. do{
  891. if(!do_cond_wait(ipcdetail::bool_<Block == timed>(), p_hdr->m_cond_recv, lock, abs_time)) {
  892. if(p_hdr->is_empty()){
  893. #if defined(BOOST_INTERPROCESS_MSG_QUEUE_CIRCULAR_INDEX)
  894. --p_hdr->m_blocked_receivers;
  895. #endif
  896. return false;
  897. }
  898. break;
  899. }
  900. }
  901. while (p_hdr->is_empty());
  902. break;
  903. //Paranoia check
  904. default:
  905. break;
  906. }
  907. #if defined(BOOST_INTERPROCESS_MSG_QUEUE_CIRCULAR_INDEX)
  908. --p_hdr->m_blocked_receivers;
  909. #endif
  910. }
  911. BOOST_CATCH(...){
  912. #if defined(BOOST_INTERPROCESS_MSG_QUEUE_CIRCULAR_INDEX)
  913. --p_hdr->m_blocked_receivers;
  914. #endif
  915. BOOST_RETHROW;
  916. } BOOST_CATCH_END
  917. }
  918. #ifdef BOOST_INTERPROCESS_MSG_QUEUE_CIRCULAR_INDEX
  919. notify_blocked_senders = 0 != p_hdr->m_blocked_senders;
  920. #endif
  921. //There is at least one message ready to pick, get the top one
  922. ipcdetail::msg_hdr_t<VoidPointer> &top_msg = p_hdr->top_msg();
  923. //Get data from the message
  924. recvd_size = top_msg.len;
  925. priority = top_msg.priority;
  926. //Some cleanup to ease debugging
  927. top_msg.len = 0;
  928. top_msg.priority = 0;
  929. //Copy data to receiver's bufers
  930. std::memcpy(buffer, top_msg.data(), recvd_size);
  931. //Free top message and put it in the free message list
  932. p_hdr->free_top_msg();
  933. } //Lock end
  934. //Notify outside lock to avoid contention. This might produce some
  935. //spurious wakeups, but it's usually far better than notifying inside.
  936. //If this reception changes the queue full state, notify senders
  937. #ifdef BOOST_INTERPROCESS_MSG_QUEUE_CIRCULAR_INDEX
  938. if (notify_blocked_senders){
  939. p_hdr->m_cond_send.notify_one();
  940. }
  941. #else
  942. p_hdr->m_cond_send.notify_one();
  943. #endif
  944. return true;
  945. }
  946. template<class VoidPointer>
  947. inline typename message_queue_t<VoidPointer>::size_type message_queue_t<VoidPointer>::get_max_msg() const
  948. {
  949. ipcdetail::mq_hdr_t<VoidPointer> *p_hdr = static_cast<ipcdetail::mq_hdr_t<VoidPointer>*>(m_shmem.get_user_address());
  950. return p_hdr ? p_hdr->m_max_num_msg : 0; }
  951. template<class VoidPointer>
  952. inline typename message_queue_t<VoidPointer>::size_type message_queue_t<VoidPointer>::get_max_msg_size() const
  953. {
  954. ipcdetail::mq_hdr_t<VoidPointer> *p_hdr = static_cast<ipcdetail::mq_hdr_t<VoidPointer>*>(m_shmem.get_user_address());
  955. return p_hdr ? p_hdr->m_max_msg_size : 0;
  956. }
  957. template<class VoidPointer>
  958. inline typename message_queue_t<VoidPointer>::size_type message_queue_t<VoidPointer>::get_num_msg() const
  959. {
  960. ipcdetail::mq_hdr_t<VoidPointer> *p_hdr = static_cast<ipcdetail::mq_hdr_t<VoidPointer>*>(m_shmem.get_user_address());
  961. if(p_hdr){
  962. //---------------------------------------------
  963. scoped_lock<interprocess_mutex> lock(p_hdr->m_mutex);
  964. //---------------------------------------------
  965. return p_hdr->m_cur_num_msg;
  966. }
  967. return 0;
  968. }
  969. template<class VoidPointer>
  970. inline bool message_queue_t<VoidPointer>::remove(const char *name)
  971. { return shared_memory_object::remove(name); }
  972. #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  973. template<class VoidPointer>
  974. inline bool message_queue_t<VoidPointer>::remove(const wchar_t *name)
  975. { return shared_memory_object::remove(name); }
  976. #endif
  977. #else
  978. //!Typedef for a default message queue
  979. //!to be used between processes
  980. typedef message_queue_t<offset_ptr<void> > message_queue;
  981. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  982. }} //namespace boost{ namespace interprocess{
  983. #include <boost/interprocess/detail/config_end.hpp>
  984. #endif //#ifndef BOOST_INTERPROCESS_MESSAGE_QUEUE_HPP