named_condition_any.hpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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/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. #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. shm_named_condition_any(create_only_t create_only, const char *name, const permissions &perm = permissions())
  58. : m_shmem (create_only
  59. ,name
  60. ,sizeof(internal_condition) +
  61. open_create_impl_t::ManagedOpenOrCreateUserOffset
  62. ,read_write
  63. ,0
  64. ,construct_func_t(DoCreate)
  65. ,perm)
  66. {}
  67. //!Opens or creates a global condition with a name.
  68. //!If the condition is created, this call is equivalent to
  69. //!shm_named_condition_any(create_only_t, ... )
  70. //!If the condition is already created, this call is equivalent
  71. //!shm_named_condition_any(open_only_t, ... )
  72. //!Does not throw
  73. shm_named_condition_any(open_or_create_t open_or_create, const char *name, const permissions &perm = permissions())
  74. : m_shmem (open_or_create
  75. ,name
  76. ,sizeof(internal_condition) +
  77. open_create_impl_t::ManagedOpenOrCreateUserOffset
  78. ,read_write
  79. ,0
  80. ,construct_func_t(DoOpenOrCreate)
  81. ,perm)
  82. {}
  83. //!Opens a global condition with a name if that condition is previously
  84. //!created. If it is not previously created this function throws
  85. //!interprocess_exception.
  86. shm_named_condition_any(open_only_t open_only, const char *name)
  87. : m_shmem (open_only
  88. ,name
  89. ,read_write
  90. ,0
  91. ,construct_func_t(DoOpen))
  92. {}
  93. //!Destroys *this and indicates that the calling process is finished using
  94. //!the resource. The destructor function will deallocate
  95. //!any system resources allocated by the system for use by this process for
  96. //!this resource. The resource can still be opened again calling
  97. //!the open constructor overload. To erase the resource from the system
  98. //!use remove().
  99. ~shm_named_condition_any()
  100. {}
  101. //!If there is a thread waiting on *this, change that
  102. //!thread's state to ready. Otherwise there is no effect.*/
  103. void notify_one()
  104. { this->internal_cond().notify_one(); }
  105. //!Change the state of all threads waiting on *this to ready.
  106. //!If there are no waiting threads, notify_all() has no effect.
  107. void notify_all()
  108. { this->internal_cond().notify_all(); }
  109. //!Releases the lock on the named_mutex object associated with lock, blocks
  110. //!the current thread of execution until readied by a call to
  111. //!this->notify_one() or this->notify_all(), and then reacquires the lock.
  112. template <typename L>
  113. void wait(L& lock)
  114. { this->internal_cond().wait(lock); }
  115. //!The same as:
  116. //!while (!pred()) wait(lock)
  117. template <typename L, typename Pr>
  118. void wait(L& lock, Pr pred)
  119. { this->internal_cond().wait(lock, pred); }
  120. //!Releases the lock on the named_mutex object associated with lock, blocks
  121. //!the current thread of execution until readied by a call to
  122. //!this->notify_one() or this->notify_all(), or until time abs_time is reached,
  123. //!and then reacquires the lock.
  124. //!Returns: false if time abs_time is reached, otherwise true.
  125. template <typename L>
  126. bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time)
  127. { return this->internal_cond().timed_wait(lock, 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 Pr>
  132. bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time, Pr pred)
  133. { return this->internal_cond().timed_wait(lock, abs_time, pred); }
  134. //!Erases a named condition from the system.
  135. //!Returns false on error. Never throws.
  136. static bool remove(const char *name)
  137. { return shared_memory_object::remove(name); }
  138. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  139. private:
  140. class internal_condition_members
  141. {
  142. public:
  143. typedef interprocess_mutex mutex_type;
  144. typedef interprocess_condition condvar_type;
  145. condvar_type& get_condvar() { return m_cond; }
  146. mutex_type& get_mutex() { return m_mtx; }
  147. private:
  148. mutex_type m_mtx;
  149. condvar_type m_cond;
  150. };
  151. typedef ipcdetail::condition_any_wrapper<internal_condition_members> internal_condition;
  152. internal_condition &internal_cond()
  153. { return *static_cast<internal_condition*>(m_shmem.get_user_address()); }
  154. friend class boost::interprocess::ipcdetail::interprocess_tester;
  155. void dont_close_on_destruction()
  156. { interprocess_tester::dont_close_on_destruction(m_shmem); }
  157. typedef ipcdetail::managed_open_or_create_impl<shared_memory_object, 0, true, false> open_create_impl_t;
  158. open_create_impl_t m_shmem;
  159. template <class T, class Arg> friend class boost::interprocess::ipcdetail::named_creation_functor;
  160. typedef boost::interprocess::ipcdetail::named_creation_functor<internal_condition> construct_func_t;
  161. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  162. };
  163. } //namespace ipcdetail
  164. } //namespace interprocess
  165. } //namespace boost
  166. #include <boost/interprocess/detail/config_end.hpp>
  167. #endif // BOOST_INTERPROCESS_SHM_NAMED_CONDITION_ANY_HPP