message_queue.hpp 45 KB

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