named_condition.hpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. //!Destroys *this and indicates that the calling process is finished using
  74. //!the resource. The destructor function will deallocate
  75. //!any system resources allocated by the system for use by this process for
  76. //!this resource. The resource can still be opened again calling
  77. //!the open constructor overload. To erase the resource from the system
  78. //!use remove().
  79. ~shm_named_condition();
  80. //!If there is a thread waiting on *this, change that
  81. //!thread's state to ready. Otherwise there is no effect.*/
  82. void notify_one();
  83. //!Change the state of all threads waiting on *this to ready.
  84. //!If there are no waiting threads, notify_all() has no effect.
  85. void notify_all();
  86. //!Releases the lock on the named_mutex object associated with lock, blocks
  87. //!the current thread of execution until readied by a call to
  88. //!this->notify_one() or this->notify_all(), and then reacquires the lock.
  89. template <typename L>
  90. void wait(L& lock);
  91. //!The same as:
  92. //!while (!pred()) wait(lock)
  93. template <typename L, typename Pr>
  94. void wait(L& lock, Pr pred);
  95. //!Releases the lock on the named_mutex object associated with lock, blocks
  96. //!the current thread of execution until readied by a call to
  97. //!this->notify_one() or this->notify_all(), or until time abs_time is reached,
  98. //!and then reacquires the lock.
  99. //!Returns: false if time abs_time is reached, otherwise true.
  100. template <typename L>
  101. bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time);
  102. //!The same as: while (!pred()) {
  103. //! if (!timed_wait(lock, abs_time)) return pred();
  104. //! } return true;
  105. template <typename L, typename Pr>
  106. bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time, Pr pred);
  107. //!Erases a named condition from the system.
  108. //!Returns false on error. Never throws.
  109. static bool remove(const char *name);
  110. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  111. private:
  112. #if defined (BOOST_INTERPROCESS_NAMED_MUTEX_USES_POSIX_SEMAPHORES)
  113. class internal_condition_members
  114. {
  115. public:
  116. typedef interprocess_mutex mutex_type;
  117. typedef interprocess_condition condvar_type;
  118. condvar_type& get_condvar() { return m_cond; }
  119. mutex_type& get_mutex() { return m_mtx; }
  120. private:
  121. mutex_type m_mtx;
  122. condvar_type m_cond;
  123. };
  124. typedef ipcdetail::condition_any_wrapper<internal_condition_members> internal_condition;
  125. #else //defined (BOOST_INTERPROCESS_NAMED_MUTEX_USES_POSIX_SEMAPHORES)
  126. typedef interprocess_condition internal_condition;
  127. #endif //defined (BOOST_INTERPROCESS_NAMED_MUTEX_USES_POSIX_SEMAPHORES)
  128. internal_condition &internal_cond()
  129. { return *static_cast<internal_condition*>(m_shmem.get_user_address()); }
  130. friend class boost::interprocess::ipcdetail::interprocess_tester;
  131. void dont_close_on_destruction();
  132. typedef ipcdetail::managed_open_or_create_impl<shared_memory_object, 0, true, false> open_create_impl_t;
  133. open_create_impl_t m_shmem;
  134. template <class T, class Arg> friend class boost::interprocess::ipcdetail::named_creation_functor;
  135. typedef boost::interprocess::ipcdetail::named_creation_functor<internal_condition> construct_func_t;
  136. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  137. };
  138. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  139. inline shm_named_condition::~shm_named_condition()
  140. {}
  141. inline shm_named_condition::shm_named_condition(create_only_t, const char *name, const permissions &perm)
  142. : m_shmem (create_only
  143. ,name
  144. ,sizeof(internal_condition) +
  145. open_create_impl_t::ManagedOpenOrCreateUserOffset
  146. ,read_write
  147. ,0
  148. ,construct_func_t(DoCreate)
  149. ,perm)
  150. {}
  151. inline shm_named_condition::shm_named_condition(open_or_create_t, const char *name, const permissions &perm)
  152. : m_shmem (open_or_create
  153. ,name
  154. ,sizeof(internal_condition) +
  155. open_create_impl_t::ManagedOpenOrCreateUserOffset
  156. ,read_write
  157. ,0
  158. ,construct_func_t(DoOpenOrCreate)
  159. ,perm)
  160. {}
  161. inline shm_named_condition::shm_named_condition(open_only_t, const char *name)
  162. : m_shmem (open_only
  163. ,name
  164. ,read_write
  165. ,0
  166. ,construct_func_t(DoOpen))
  167. {}
  168. inline void shm_named_condition::dont_close_on_destruction()
  169. { interprocess_tester::dont_close_on_destruction(m_shmem); }
  170. inline void shm_named_condition::notify_one()
  171. { this->internal_cond().notify_one(); }
  172. inline void shm_named_condition::notify_all()
  173. { this->internal_cond().notify_all(); }
  174. template <typename L>
  175. inline void shm_named_condition::wait(L& lock)
  176. { this->internal_cond().wait(lock); }
  177. template <typename L, typename Pr>
  178. inline void shm_named_condition::wait(L& lock, Pr pred)
  179. { this->internal_cond().wait(lock, pred); }
  180. template <typename L>
  181. inline bool shm_named_condition::timed_wait
  182. (L& lock, const boost::posix_time::ptime &abs_time)
  183. { return this->internal_cond().timed_wait(lock, abs_time); }
  184. template <typename L, typename Pr>
  185. inline bool shm_named_condition::timed_wait
  186. (L& lock, const boost::posix_time::ptime &abs_time, Pr pred)
  187. { return this->internal_cond().timed_wait(lock, abs_time, pred); }
  188. inline bool shm_named_condition::remove(const char *name)
  189. { return shared_memory_object::remove(name); }
  190. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  191. } //namespace ipcdetail
  192. } //namespace interprocess
  193. } //namespace boost
  194. #include <boost/interprocess/detail/config_end.hpp>
  195. #endif // BOOST_INTERPROCESS_SHM_NAMED_CONDITION_HPP