named_condition.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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_SHM_NAMED_CONDITION_HPP
  11. #define BOOST_INTERPROCESS_SHM_NAMED_CONDITION_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/static_assert.hpp>
  22. #include <boost/interprocess/detail/type_traits.hpp>
  23. #include <boost/interprocess/creation_tags.hpp>
  24. #include <boost/interprocess/exceptions.hpp>
  25. #include <boost/interprocess/shared_memory_object.hpp>
  26. #include <boost/interprocess/sync/interprocess_condition.hpp>
  27. #include <boost/interprocess/detail/managed_open_or_create_impl.hpp>
  28. #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
  29. #include <boost/interprocess/sync/shm/named_creation_functor.hpp>
  30. #include <boost/interprocess/sync/named_mutex.hpp>
  31. #include <boost/interprocess/permissions.hpp>
  32. #if defined (BOOST_INTERPROCESS_NAMED_MUTEX_USES_POSIX_SEMAPHORES)
  33. #include <boost/interprocess/sync/interprocess_mutex.hpp>
  34. #include <boost/interprocess/sync/scoped_lock.hpp>
  35. #include <boost/interprocess/sync/detail/condition_any_algorithm.hpp>
  36. #else
  37. #include <boost/interprocess/sync/detail/locks.hpp>
  38. #endif
  39. //!\file
  40. //!Describes process-shared variables interprocess_condition class
  41. namespace boost {
  42. namespace interprocess {
  43. namespace ipcdetail {
  44. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  45. class interprocess_tester;
  46. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  47. //! A global condition variable that can be created by name.
  48. //! This condition variable is designed to work with named_mutex and
  49. //! can't be placed in shared memory or memory mapped files.
  50. class shm_named_condition
  51. {
  52. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  53. //Non-copyable
  54. shm_named_condition();
  55. shm_named_condition(const shm_named_condition &);
  56. shm_named_condition &operator=(const shm_named_condition &);
  57. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  58. public:
  59. //!Creates a global condition with a name.
  60. //!If the condition can't be created throws interprocess_exception
  61. shm_named_condition(create_only_t create_only, const char *name, const permissions &perm = permissions());
  62. //!Opens or creates a global condition with a name.
  63. //!If the condition is created, this call is equivalent to
  64. //!shm_named_condition(create_only_t, ... )
  65. //!If the condition is already created, this call is equivalent
  66. //!shm_named_condition(open_only_t, ... )
  67. //!Does not throw
  68. shm_named_condition(open_or_create_t open_or_create, const char *name, const permissions &perm = permissions());
  69. //!Opens a global condition with a name if that condition is previously
  70. //!created. If it is not previously created this function throws
  71. //!interprocess_exception.
  72. shm_named_condition(open_only_t open_only, const char *name);
  73. #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  74. //!Creates a global condition with a name.
  75. //!If the condition can't be created throws interprocess_exception
  76. //!
  77. //!Note: This function is only available on operating systems with
  78. //! native wchar_t APIs (e.g. Windows).
  79. shm_named_condition(create_only_t create_only, const wchar_t *name, const permissions &perm = permissions());
  80. //!Opens or creates a global condition with a name.
  81. //!If the condition is created, this call is equivalent to
  82. //!shm_named_condition(create_only_t, ... )
  83. //!If the condition is already created, this call is equivalent
  84. //!shm_named_condition(open_only_t, ... )
  85. //!Does not throw
  86. //!
  87. //!Note: This function is only available on operating systems with
  88. //! native wchar_t APIs (e.g. Windows).
  89. shm_named_condition(open_or_create_t open_or_create, const wchar_t *name, const permissions &perm = permissions());
  90. //!Opens a global condition with a name if that condition is previously
  91. //!created. If it is not previously created this function throws
  92. //!interprocess_exception.
  93. //!
  94. //!Note: This function is only available on operating systems with
  95. //! native wchar_t APIs (e.g. Windows).
  96. shm_named_condition(open_only_t open_only, const wchar_t *name);
  97. #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  98. //!Destroys *this and indicates that the calling process is finished using
  99. //!the resource. The destructor function will deallocate
  100. //!any system resources allocated by the system for use by this process for
  101. //!this resource. The resource can still be opened again calling
  102. //!the open constructor overload. To erase the resource from the system
  103. //!use remove().
  104. ~shm_named_condition();
  105. //!If there is a thread waiting on *this, change that
  106. //!thread's state to ready. Otherwise there is no effect.*/
  107. void notify_one();
  108. //!Change the state of all threads waiting on *this to ready.
  109. //!If there are no waiting threads, notify_all() has no effect.
  110. void notify_all();
  111. //!Releases the lock on the named_mutex object associated with lock, blocks
  112. //!the current thread of execution until readied by a call to
  113. //!this->notify_one() or this->notify_all(), and then reacquires the lock.
  114. template <typename L>
  115. void wait(L& lock);
  116. //!The same as:
  117. //!while (!pred()) wait(lock)
  118. template <typename L, typename Pr>
  119. void wait(L& lock, Pr pred);
  120. //!Releases the lock on the named_mutex object associated with lock, blocks
  121. //!the current thread of execution until readied by a call to
  122. //!this->notify_one() or this->notify_all(), or until time abs_time is reached,
  123. //!and then reacquires the lock.
  124. //!Returns: false if time abs_time is reached, otherwise true.
  125. template <typename L>
  126. bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time);
  127. //!The same as: while (!pred()) {
  128. //! if (!timed_wait(lock, abs_time)) return pred();
  129. //! } return true;
  130. template <typename L, typename Pr>
  131. bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time, Pr pred);
  132. //!Erases a named condition from the system.
  133. //!Returns false on error. Never throws.
  134. static bool remove(const char *name);
  135. #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  136. //!Erases a named condition from the system.
  137. //!Returns false on error. Never throws.
  138. //!
  139. //!Note: This function is only available on operating systems with
  140. //! native wchar_t APIs (e.g. Windows).
  141. static bool remove(const wchar_t *name);
  142. #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  143. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  144. private:
  145. #if defined (BOOST_INTERPROCESS_NAMED_MUTEX_USES_POSIX_SEMAPHORES)
  146. class internal_condition_members
  147. {
  148. public:
  149. typedef interprocess_mutex mutex_type;
  150. typedef interprocess_condition condvar_type;
  151. condvar_type& get_condvar() { return m_cond; }
  152. mutex_type& get_mutex() { return m_mtx; }
  153. private:
  154. mutex_type m_mtx;
  155. condvar_type m_cond;
  156. };
  157. typedef ipcdetail::condition_any_wrapper<internal_condition_members> internal_condition;
  158. #else //defined (BOOST_INTERPROCESS_NAMED_MUTEX_USES_POSIX_SEMAPHORES)
  159. typedef interprocess_condition internal_condition;
  160. #endif //defined (BOOST_INTERPROCESS_NAMED_MUTEX_USES_POSIX_SEMAPHORES)
  161. internal_condition &internal_cond()
  162. { return *static_cast<internal_condition*>(m_shmem.get_user_address()); }
  163. friend class boost::interprocess::ipcdetail::interprocess_tester;
  164. void dont_close_on_destruction();
  165. typedef ipcdetail::managed_open_or_create_impl<shared_memory_object, 0, true, false> open_create_impl_t;
  166. open_create_impl_t m_shmem;
  167. template <class T, class Arg> friend class boost::interprocess::ipcdetail::named_creation_functor;
  168. typedef boost::interprocess::ipcdetail::named_creation_functor<internal_condition> construct_func_t;
  169. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  170. };
  171. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  172. inline shm_named_condition::~shm_named_condition()
  173. {}
  174. inline shm_named_condition::shm_named_condition(create_only_t, const char *name, const permissions &perm)
  175. : m_shmem (create_only
  176. ,name
  177. ,sizeof(internal_condition) +
  178. open_create_impl_t::ManagedOpenOrCreateUserOffset
  179. ,read_write
  180. ,0
  181. ,construct_func_t(DoCreate)
  182. ,perm)
  183. {}
  184. inline shm_named_condition::shm_named_condition(open_or_create_t, const char *name, const permissions &perm)
  185. : m_shmem (open_or_create
  186. ,name
  187. ,sizeof(internal_condition) +
  188. open_create_impl_t::ManagedOpenOrCreateUserOffset
  189. ,read_write
  190. ,0
  191. ,construct_func_t(DoOpenOrCreate)
  192. ,perm)
  193. {}
  194. inline shm_named_condition::shm_named_condition(open_only_t, const char *name)
  195. : m_shmem (open_only
  196. ,name
  197. ,read_write
  198. ,0
  199. ,construct_func_t(DoOpen))
  200. {}
  201. #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  202. inline shm_named_condition::shm_named_condition(create_only_t, const wchar_t *name, const permissions &perm)
  203. : m_shmem (create_only
  204. ,name
  205. ,sizeof(internal_condition) +
  206. open_create_impl_t::ManagedOpenOrCreateUserOffset
  207. ,read_write
  208. ,0
  209. ,construct_func_t(DoCreate)
  210. ,perm)
  211. {}
  212. inline shm_named_condition::shm_named_condition(open_or_create_t, const wchar_t *name, const permissions &perm)
  213. : m_shmem (open_or_create
  214. ,name
  215. ,sizeof(internal_condition) +
  216. open_create_impl_t::ManagedOpenOrCreateUserOffset
  217. ,read_write
  218. ,0
  219. ,construct_func_t(DoOpenOrCreate)
  220. ,perm)
  221. {}
  222. inline shm_named_condition::shm_named_condition(open_only_t, const wchar_t *name)
  223. : m_shmem (open_only
  224. ,name
  225. ,read_write
  226. ,0
  227. ,construct_func_t(DoOpen))
  228. {}
  229. #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  230. inline void shm_named_condition::dont_close_on_destruction()
  231. { interprocess_tester::dont_close_on_destruction(m_shmem); }
  232. inline void shm_named_condition::notify_one()
  233. { this->internal_cond().notify_one(); }
  234. inline void shm_named_condition::notify_all()
  235. { this->internal_cond().notify_all(); }
  236. template <typename L>
  237. inline void shm_named_condition::wait(L& lock)
  238. { this->internal_cond().wait(lock); }
  239. template <typename L, typename Pr>
  240. inline void shm_named_condition::wait(L& lock, Pr pred)
  241. { this->internal_cond().wait(lock, pred); }
  242. template <typename L>
  243. inline bool shm_named_condition::timed_wait
  244. (L& lock, const boost::posix_time::ptime &abs_time)
  245. { return this->internal_cond().timed_wait(lock, abs_time); }
  246. template <typename L, typename Pr>
  247. inline bool shm_named_condition::timed_wait
  248. (L& lock, const boost::posix_time::ptime &abs_time, Pr pred)
  249. { return this->internal_cond().timed_wait(lock, abs_time, pred); }
  250. inline bool shm_named_condition::remove(const char *name)
  251. { return shared_memory_object::remove(name); }
  252. #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  253. inline bool shm_named_condition::remove(const wchar_t *name)
  254. { return shared_memory_object::remove(name); }
  255. #endif
  256. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  257. } //namespace ipcdetail
  258. } //namespace interprocess
  259. } //namespace boost
  260. #include <boost/interprocess/detail/config_end.hpp>
  261. #endif // BOOST_INTERPROCESS_SHM_NAMED_CONDITION_HPP