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