named_condition_any.hpp 7.0 KB

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