named_condition_any.hpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2011-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_WINDOWS_NAMED_CONDITION_ANY_HPP
  11. #define BOOST_INTERPROCESS_WINDOWS_NAMED_CONDITION_ANY_HPP
  12. #if (defined _MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif
  15. #include <boost/interprocess/detail/config_begin.hpp>
  16. #include <boost/interprocess/detail/workaround.hpp>
  17. #include <boost/interprocess/creation_tags.hpp>
  18. #include <boost/interprocess/permissions.hpp>
  19. #include <boost/interprocess/detail/interprocess_tester.hpp>
  20. #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
  21. #include <boost/interprocess/sync/windows/named_sync.hpp>
  22. #include <boost/interprocess/sync/windows/winapi_semaphore_wrapper.hpp>
  23. #include <boost/interprocess/sync/detail/condition_algorithm_8a.hpp>
  24. namespace boost {
  25. namespace interprocess {
  26. namespace ipcdetail {
  27. class windows_named_condition_any
  28. {
  29. /// @cond
  30. //Non-copyable
  31. windows_named_condition_any();
  32. windows_named_condition_any(const windows_named_condition_any &);
  33. windows_named_condition_any &operator=(const windows_named_condition_any &);
  34. /// @endcond
  35. public:
  36. windows_named_condition_any
  37. (create_only_t, const char *name, const permissions &perm)
  38. : m_condition_data()
  39. {
  40. named_cond_callbacks callbacks(m_condition_data.get_members());
  41. m_named_sync.open_or_create(DoCreate, name, perm, callbacks);
  42. }
  43. windows_named_condition_any
  44. (open_or_create_t, const char *name, const permissions &perm)
  45. : m_condition_data()
  46. {
  47. named_cond_callbacks callbacks(m_condition_data.get_members());
  48. m_named_sync.open_or_create(DoOpenOrCreate, name, perm, callbacks);
  49. }
  50. windows_named_condition_any(open_only_t, const char *name)
  51. : m_condition_data()
  52. {
  53. named_cond_callbacks callbacks(m_condition_data.get_members());
  54. m_named_sync.open_or_create(DoOpen, name, permissions(), callbacks);
  55. }
  56. ~windows_named_condition_any()
  57. {
  58. named_cond_callbacks callbacks(m_condition_data.get_members());
  59. m_named_sync.close(callbacks);
  60. }
  61. void notify_one()
  62. { m_condition_data.notify_one(); }
  63. void notify_all()
  64. { m_condition_data.notify_all(); }
  65. template <typename L>
  66. bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time)
  67. { return m_condition_data.timed_wait(lock, abs_time); }
  68. template <typename L, typename Pr>
  69. bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time, Pr pred)
  70. { return m_condition_data.timed_wait(lock, abs_time, pred); }
  71. template <typename L>
  72. void wait(L& lock)
  73. { m_condition_data.wait(lock); }
  74. template <typename L, typename Pr>
  75. void wait(L& lock, Pr pred)
  76. { m_condition_data.wait(lock, pred); }
  77. static bool remove(const char *name)
  78. { return windows_named_sync::remove(name); }
  79. /// @cond
  80. private:
  81. void windows_named_condition_any::dont_close_on_destruction()
  82. {}
  83. friend class interprocess_tester;
  84. struct condition_data
  85. {
  86. typedef boost::int32_t integer_type;
  87. typedef winapi_semaphore_wrapper semaphore_type;
  88. typedef winapi_mutex_wrapper mutex_type;
  89. integer_type &get_nwaiters_blocked()
  90. { return m_nwaiters_blocked; }
  91. integer_type &get_nwaiters_gone()
  92. { return m_nwaiters_gone; }
  93. integer_type &get_nwaiters_to_unblock()
  94. { return m_nwaiters_to_unblock; }
  95. semaphore_type &get_sem_block_queue()
  96. { return m_sem_block_queue; }
  97. semaphore_type &get_sem_block_lock()
  98. { return m_sem_block_lock; }
  99. mutex_type &get_mtx_unblock_lock()
  100. { return m_mtx_unblock_lock; }
  101. integer_type m_nwaiters_blocked;
  102. integer_type m_nwaiters_gone;
  103. integer_type m_nwaiters_to_unblock;
  104. winapi_semaphore_wrapper m_sem_block_queue;
  105. winapi_semaphore_wrapper m_sem_block_lock;
  106. winapi_mutex_wrapper m_mtx_unblock_lock;
  107. };
  108. ipcdetail::condition_8a_wrapper<condition_data> m_condition_data;
  109. class named_cond_callbacks : public windows_named_sync_interface
  110. {
  111. typedef __int64 sem_count_t;
  112. mutable sem_count_t sem_counts [2];
  113. public:
  114. named_cond_callbacks(condition_data &cond_data)
  115. : m_condition_data(cond_data)
  116. {}
  117. virtual std::size_t get_data_size() const
  118. { return sizeof(sem_counts); }
  119. virtual const void *buffer_with_final_data_to_file()
  120. {
  121. sem_counts[0] = m_condition_data.m_sem_block_queue.value();
  122. sem_counts[1] = m_condition_data.m_sem_block_lock.value();
  123. return &sem_counts;
  124. }
  125. virtual const void *buffer_with_init_data_to_file()
  126. {
  127. sem_counts[0] = 0;
  128. sem_counts[1] = 1;
  129. return &sem_counts;
  130. }
  131. virtual void *buffer_to_store_init_data_from_file()
  132. { return &sem_counts; }
  133. virtual bool open(create_enum_t, const char *id_name)
  134. {
  135. m_condition_data.m_nwaiters_blocked = 0;
  136. m_condition_data.m_nwaiters_gone = 0;
  137. m_condition_data.m_nwaiters_to_unblock = 0;
  138. //Now open semaphores and mutex.
  139. //Use local variables + swap to guarantee consistent
  140. //initialization and cleanup in case any opening fails
  141. permissions perm;
  142. perm.set_unrestricted();
  143. std::string aux_str = "Global\\bipc.cond.";
  144. aux_str += id_name;
  145. std::size_t pos = aux_str.size();
  146. //sem_block_queue
  147. aux_str += "_bq";
  148. winapi_semaphore_wrapper sem_block_queue;
  149. bool created;
  150. if(!sem_block_queue.open_or_create
  151. (aux_str.c_str(), sem_counts[0], winapi_semaphore_wrapper::MaxCount, perm, created))
  152. return false;
  153. aux_str.erase(pos);
  154. //sem_block_lock
  155. aux_str += "_bl";
  156. winapi_semaphore_wrapper sem_block_lock;
  157. if(!sem_block_lock.open_or_create
  158. (aux_str.c_str(), sem_counts[1], winapi_semaphore_wrapper::MaxCount, perm, created))
  159. return false;
  160. aux_str.erase(pos);
  161. //mtx_unblock_lock
  162. aux_str += "_ul";
  163. winapi_mutex_wrapper mtx_unblock_lock;
  164. if(!mtx_unblock_lock.open_or_create(aux_str.c_str(), perm))
  165. return false;
  166. //All ok, commit data
  167. m_condition_data.m_sem_block_queue.swap(sem_block_queue);
  168. m_condition_data.m_sem_block_lock.swap(sem_block_lock);
  169. m_condition_data.m_mtx_unblock_lock.swap(mtx_unblock_lock);
  170. return true;
  171. }
  172. virtual void close()
  173. {
  174. m_condition_data.m_sem_block_queue.close();
  175. m_condition_data.m_sem_block_lock.close();
  176. m_condition_data.m_mtx_unblock_lock.close();
  177. m_condition_data.m_nwaiters_blocked = 0;
  178. m_condition_data.m_nwaiters_gone = 0;
  179. m_condition_data.m_nwaiters_to_unblock = 0;
  180. }
  181. virtual ~named_cond_callbacks()
  182. {}
  183. private:
  184. condition_data &m_condition_data;
  185. };
  186. windows_named_sync m_named_sync;
  187. /// @endcond
  188. };
  189. } //namespace ipcdetail {
  190. } //namespace interprocess {
  191. } //namespace boost {
  192. #include <boost/interprocess/detail/config_end.hpp>
  193. #endif //BOOST_INTERPROCESS_WINDOWS_NAMED_CONDITION_ANY_HPP