named_condition_any.hpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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_ANY_HPP
  11. #define BOOST_INTERPROCESS_SHM_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/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/sync/interprocess_mutex.hpp>
  33. #include <boost/interprocess/sync/scoped_lock.hpp>
  34. #include <boost/interprocess/timed_utils.hpp>
  35. #include <boost/interprocess/sync/detail/condition_any_algorithm.hpp>
  36. //!\file
  37. //!Describes process-shared variables interprocess_condition class
  38. namespace boost {
  39. namespace interprocess {
  40. namespace ipcdetail {
  41. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  42. class interprocess_tester;
  43. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  44. //! A global condition variable that can be created by name.
  45. //! This condition variable is designed to work with named_mutex and
  46. //! can't be placed in shared memory or memory mapped files.
  47. class shm_named_condition_any
  48. {
  49. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  50. //Non-copyable
  51. shm_named_condition_any();
  52. shm_named_condition_any(const shm_named_condition_any &);
  53. shm_named_condition_any &operator=(const shm_named_condition_any &);
  54. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  55. public:
  56. //!Creates a global condition with a name.
  57. //!If the condition can't be created throws interprocess_exception
  58. template <class CharT>
  59. shm_named_condition_any(create_only_t, const CharT *name, const permissions &perm = permissions())
  60. : m_shmem (create_only_t()
  61. ,name
  62. ,sizeof(internal_condition) +
  63. open_create_impl_t::ManagedOpenOrCreateUserOffset
  64. ,read_write
  65. ,0
  66. ,construct_func_t(DoCreate)
  67. ,perm)
  68. {}
  69. //!Opens or creates a global condition with a name.
  70. //!If the condition is created, this call is equivalent to
  71. //!shm_named_condition_any(create_only_t, ... )
  72. //!If the condition is already created, this call is equivalent
  73. //!shm_named_condition_any(open_only_t, ... )
  74. //!Does not throw
  75. template <class CharT>
  76. shm_named_condition_any(open_or_create_t, const CharT *name, const permissions &perm = permissions())
  77. : m_shmem (open_or_create_t()
  78. ,name
  79. ,sizeof(internal_condition) +
  80. open_create_impl_t::ManagedOpenOrCreateUserOffset
  81. ,read_write
  82. ,0
  83. ,construct_func_t(DoOpenOrCreate)
  84. ,perm)
  85. {}
  86. //!Opens a global condition with a name if that condition is previously
  87. //!created. If it is not previously created this function throws
  88. //!interprocess_exception.
  89. template <class CharT>
  90. shm_named_condition_any(open_only_t, const CharT *name)
  91. : m_shmem (open_only_t()
  92. ,name
  93. ,read_write
  94. ,0
  95. ,construct_func_t(DoOpen))
  96. {}
  97. //!Destroys *this and indicates that the calling process is finished using
  98. //!the resource. The destructor function will deallocate
  99. //!any system resources allocated by the system for use by this process for
  100. //!this resource. The resource can still be opened again calling
  101. //!the open constructor overload. To erase the resource from the system
  102. //!use remove().
  103. ~shm_named_condition_any()
  104. {}
  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. { this->internal_cond().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. { this->internal_cond().notify_all(); }
  113. //!Releases the lock on the named_mutex object associated with lock, blocks
  114. //!the current thread of execution until readied by a call to
  115. //!this->notify_one() or this->notify_all(), and then reacquires the lock.
  116. template <typename L>
  117. void wait(L& lock)
  118. { this->internal_cond().wait(lock); }
  119. //!The same as:
  120. //!while (!pred()) wait(lock)
  121. template <typename L, typename Pr>
  122. void wait(L& lock, Pr pred)
  123. { this->internal_cond().wait(lock, pred); }
  124. //!Releases the lock on the named_mutex object associated with lock, blocks
  125. //!the current thread of execution until readied by a call to
  126. //!this->notify_one() or this->notify_all(), or until time abs_time is reached,
  127. //!and then reacquires the lock.
  128. //!Returns: false if time abs_time is reached, otherwise true.
  129. template <typename L, typename TimePoint>
  130. bool timed_wait(L& lock, const TimePoint &abs_time)
  131. { return this->internal_cond().timed_wait(lock, abs_time); }
  132. //!The same as: while (!pred()) {
  133. //! if (!timed_wait(lock, abs_time)) return pred();
  134. //! } return true;
  135. template <typename L, typename TimePoint, typename Pr>
  136. bool timed_wait(L& lock, const TimePoint &abs_time, Pr pred)
  137. { return this->internal_cond().timed_wait(lock, abs_time, pred); }
  138. //!Same as `timed_wait`, but this function is modeled after the
  139. //!standard library interface.
  140. template <typename L, class TimePoint>
  141. cv_status wait_until(L& lock, const TimePoint &abs_time)
  142. { return this->timed_wait(lock, abs_time) ? cv_status::no_timeout : cv_status::timeout; }
  143. //!Same as `timed_wait`, but this function is modeled after the
  144. //!standard library interface.
  145. template <typename L, class TimePoint, typename Pr>
  146. bool wait_until(L& lock, const TimePoint &abs_time, Pr pred)
  147. { return this->timed_wait(lock, abs_time, pred); }
  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>
  151. cv_status wait_for(L& lock, const Duration &dur)
  152. { return this->wait_until(lock, ipcdetail::duration_to_ustime(dur)); }
  153. //!Same as `timed_wait`, but this function is modeled after the
  154. //!standard library interface and uses relative timeouts
  155. template <typename L, class Duration, typename Pr>
  156. bool wait_for(L& lock, const Duration &dur, Pr pred)
  157. { return this->wait_until(lock, ipcdetail::duration_to_ustime(dur), pred); }
  158. //!Erases a named condition from the system.
  159. //!Returns false on error. Never throws.
  160. template <class CharT>
  161. static bool remove(const CharT *name)
  162. { return shared_memory_object::remove(name); }
  163. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  164. private:
  165. class internal_condition_members
  166. {
  167. public:
  168. typedef interprocess_mutex mutex_type;
  169. typedef interprocess_condition condvar_type;
  170. condvar_type& get_condvar() { return m_cond; }
  171. mutex_type& get_mutex() { return m_mtx; }
  172. private:
  173. mutex_type m_mtx;
  174. condvar_type m_cond;
  175. };
  176. typedef ipcdetail::condition_any_wrapper<internal_condition_members> internal_condition;
  177. internal_condition &internal_cond()
  178. { return *static_cast<internal_condition*>(m_shmem.get_user_address()); }
  179. friend class boost::interprocess::ipcdetail::interprocess_tester;
  180. void dont_close_on_destruction()
  181. { interprocess_tester::dont_close_on_destruction(m_shmem); }
  182. typedef ipcdetail::managed_open_or_create_impl<shared_memory_object, 0, true, false> open_create_impl_t;
  183. open_create_impl_t m_shmem;
  184. template <class T, class Arg> friend class boost::interprocess::ipcdetail::named_creation_functor;
  185. typedef boost::interprocess::ipcdetail::named_creation_functor<internal_condition> construct_func_t;
  186. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  187. };
  188. } //namespace ipcdetail
  189. } //namespace interprocess
  190. } //namespace boost
  191. #include <boost/interprocess/detail/config_end.hpp>
  192. #endif // BOOST_INTERPROCESS_SHM_NAMED_CONDITION_ANY_HPP