named_condition_any.hpp 9.0 KB

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