named_condition.hpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. #if (defined _MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif
  15. #include <boost/interprocess/detail/config_begin.hpp>
  16. #include <boost/interprocess/detail/workaround.hpp>
  17. #include <boost/static_assert.hpp>
  18. #include <boost/interprocess/detail/type_traits.hpp>
  19. #include <boost/interprocess/creation_tags.hpp>
  20. #include <boost/interprocess/exceptions.hpp>
  21. #include <boost/interprocess/shared_memory_object.hpp>
  22. #include <boost/interprocess/sync/interprocess_condition.hpp>
  23. #include <boost/interprocess/detail/managed_open_or_create_impl.hpp>
  24. #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
  25. #include <boost/interprocess/sync/shm/named_creation_functor.hpp>
  26. #include <boost/interprocess/sync/named_mutex.hpp>
  27. #include <boost/interprocess/permissions.hpp>
  28. #if defined (BOOST_INTERPROCESS_NAMED_MUTEX_USES_POSIX_SEMAPHORES)
  29. #include <boost/interprocess/sync/interprocess_mutex.hpp>
  30. #include <boost/interprocess/sync/scoped_lock.hpp>
  31. #include <boost/interprocess/sync/detail/condition_any_algorithm.hpp>
  32. #else
  33. #include <boost/interprocess/sync/detail/locks.hpp>
  34. #endif
  35. //!\file
  36. //!Describes process-shared variables interprocess_condition class
  37. namespace boost {
  38. namespace interprocess {
  39. namespace ipcdetail {
  40. /// @cond
  41. class interprocess_tester;
  42. /// @endcond
  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
  47. {
  48. /// @cond
  49. //Non-copyable
  50. shm_named_condition();
  51. shm_named_condition(const shm_named_condition &);
  52. shm_named_condition &operator=(const shm_named_condition &);
  53. /// @endcond
  54. public:
  55. //!Creates a global condition with a name.
  56. //!If the condition can't be created throws interprocess_exception
  57. shm_named_condition(create_only_t create_only, const char *name, const permissions &perm = permissions());
  58. //!Opens or creates a global condition with a name.
  59. //!If the condition is created, this call is equivalent to
  60. //!shm_named_condition(create_only_t, ... )
  61. //!If the condition is already created, this call is equivalent
  62. //!shm_named_condition(open_only_t, ... )
  63. //!Does not throw
  64. shm_named_condition(open_or_create_t open_or_create, const char *name, const permissions &perm = permissions());
  65. //!Opens a global condition with a name if that condition is previously
  66. //!created. If it is not previously created this function throws
  67. //!interprocess_exception.
  68. shm_named_condition(open_only_t open_only, const char *name);
  69. //!Destroys *this and indicates that the calling process is finished using
  70. //!the resource. The destructor function will deallocate
  71. //!any system resources allocated by the system for use by this process for
  72. //!this resource. The resource can still be opened again calling
  73. //!the open constructor overload. To erase the resource from the system
  74. //!use remove().
  75. ~shm_named_condition();
  76. //!If there is a thread waiting on *this, change that
  77. //!thread's state to ready. Otherwise there is no effect.*/
  78. void notify_one();
  79. //!Change the state of all threads waiting on *this to ready.
  80. //!If there are no waiting threads, notify_all() has no effect.
  81. void notify_all();
  82. //!Releases the lock on the named_mutex object associated with lock, blocks
  83. //!the current thread of execution until readied by a call to
  84. //!this->notify_one() or this->notify_all(), and then reacquires the lock.
  85. template <typename L>
  86. void wait(L& lock);
  87. //!The same as:
  88. //!while (!pred()) wait(lock)
  89. template <typename L, typename Pr>
  90. void wait(L& lock, Pr pred);
  91. //!Releases the lock on the named_mutex object associated with lock, blocks
  92. //!the current thread of execution until readied by a call to
  93. //!this->notify_one() or this->notify_all(), or until time abs_time is reached,
  94. //!and then reacquires the lock.
  95. //!Returns: false if time abs_time is reached, otherwise true.
  96. template <typename L>
  97. bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time);
  98. //!The same as: while (!pred()) {
  99. //! if (!timed_wait(lock, abs_time)) return pred();
  100. //! } return true;
  101. template <typename L, typename Pr>
  102. bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time, Pr pred);
  103. //!Erases a named condition from the system.
  104. //!Returns false on error. Never throws.
  105. static bool remove(const char *name);
  106. /// @cond
  107. private:
  108. #if defined (BOOST_INTERPROCESS_NAMED_MUTEX_USES_POSIX_SEMAPHORES)
  109. class internal_condition_members
  110. {
  111. public:
  112. typedef interprocess_mutex mutex_type;
  113. typedef interprocess_condition condvar_type;
  114. condvar_type& get_condvar() { return m_cond; }
  115. mutex_type& get_mutex() { return m_mtx; }
  116. private:
  117. mutex_type m_mtx;
  118. condvar_type m_cond;
  119. };
  120. typedef ipcdetail::condition_any_wrapper<internal_condition_members> internal_condition;
  121. #else //defined (BOOST_INTERPROCESS_NAMED_MUTEX_USES_POSIX_SEMAPHORES)
  122. typedef interprocess_condition internal_condition;
  123. #endif //defined (BOOST_INTERPROCESS_NAMED_MUTEX_USES_POSIX_SEMAPHORES)
  124. internal_condition &internal_cond()
  125. { return *static_cast<internal_condition*>(m_shmem.get_user_address()); }
  126. friend class boost::interprocess::ipcdetail::interprocess_tester;
  127. void dont_close_on_destruction();
  128. typedef ipcdetail::managed_open_or_create_impl<shared_memory_object, 0, true, false> open_create_impl_t;
  129. open_create_impl_t m_shmem;
  130. template <class T, class Arg> friend class boost::interprocess::ipcdetail::named_creation_functor;
  131. typedef boost::interprocess::ipcdetail::named_creation_functor<internal_condition> construct_func_t;
  132. /// @endcond
  133. };
  134. /// @cond
  135. inline shm_named_condition::~shm_named_condition()
  136. {}
  137. inline shm_named_condition::shm_named_condition(create_only_t, const char *name, const permissions &perm)
  138. : m_shmem (create_only
  139. ,name
  140. ,sizeof(internal_condition) +
  141. open_create_impl_t::ManagedOpenOrCreateUserOffset
  142. ,read_write
  143. ,0
  144. ,construct_func_t(DoCreate)
  145. ,perm)
  146. {}
  147. inline shm_named_condition::shm_named_condition(open_or_create_t, const char *name, const permissions &perm)
  148. : m_shmem (open_or_create
  149. ,name
  150. ,sizeof(internal_condition) +
  151. open_create_impl_t::ManagedOpenOrCreateUserOffset
  152. ,read_write
  153. ,0
  154. ,construct_func_t(DoOpenOrCreate)
  155. ,perm)
  156. {}
  157. inline shm_named_condition::shm_named_condition(open_only_t, const char *name)
  158. : m_shmem (open_only
  159. ,name
  160. ,read_write
  161. ,0
  162. ,construct_func_t(DoOpen))
  163. {}
  164. inline void shm_named_condition::dont_close_on_destruction()
  165. { interprocess_tester::dont_close_on_destruction(m_shmem); }
  166. inline void shm_named_condition::notify_one()
  167. { this->internal_cond().notify_one(); }
  168. inline void shm_named_condition::notify_all()
  169. { this->internal_cond().notify_all(); }
  170. template <typename L>
  171. inline void shm_named_condition::wait(L& lock)
  172. { this->internal_cond().wait(lock); }
  173. template <typename L, typename Pr>
  174. inline void shm_named_condition::wait(L& lock, Pr pred)
  175. { this->internal_cond().wait(lock, pred); }
  176. template <typename L>
  177. inline bool shm_named_condition::timed_wait
  178. (L& lock, const boost::posix_time::ptime &abs_time)
  179. { return this->internal_cond().timed_wait(lock, abs_time); }
  180. template <typename L, typename Pr>
  181. inline bool shm_named_condition::timed_wait
  182. (L& lock, const boost::posix_time::ptime &abs_time, Pr pred)
  183. { return this->internal_cond().timed_wait(lock, abs_time, pred); }
  184. inline bool shm_named_condition::remove(const char *name)
  185. { return shared_memory_object::remove(name); }
  186. /// @endcond
  187. } //namespace ipcdetail
  188. } //namespace interprocess
  189. } //namespace boost
  190. #include <boost/interprocess/detail/config_end.hpp>
  191. #endif // BOOST_INTERPROCESS_SHM_NAMED_CONDITION_HPP