named_condition_any.hpp 7.7 KB

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