named_condition.hpp 13 KB

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