named_condition_any.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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_NAMED_CONDITION_ANY_HPP
  11. #define BOOST_INTERPROCESS_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/interprocess/creation_tags.hpp>
  18. #include <boost/interprocess/exceptions.hpp>
  19. #include <boost/interprocess/detail/interprocess_tester.hpp>
  20. #include <boost/interprocess/permissions.hpp>
  21. #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
  22. #include <boost/interprocess/sync/detail/locks.hpp>
  23. #if !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_WINDOWS)
  24. #include <boost/interprocess/sync/windows/named_condition_any.hpp>
  25. #define BOOST_INTERPROCESS_USE_WINDOWS
  26. #else
  27. #include <boost/interprocess/sync/shm/named_condition_any.hpp>
  28. #endif
  29. //!\file
  30. //!Describes a named condition class for inter-process synchronization
  31. namespace boost {
  32. namespace interprocess {
  33. /// @cond
  34. namespace ipcdetail{ class interprocess_tester; }
  35. /// @endcond
  36. //! A global condition variable that can be created by name.
  37. //! This condition variable is designed to work with named_mutex and
  38. //! can't be placed in shared memory or memory mapped files.
  39. class named_condition_any
  40. {
  41. /// @cond
  42. //Non-copyable
  43. named_condition_any();
  44. named_condition_any(const named_condition_any &);
  45. named_condition_any &operator=(const named_condition_any &);
  46. /// @endcond
  47. public:
  48. //!Creates a global condition with a name.
  49. //!If the condition can't be created throws interprocess_exception
  50. named_condition_any(create_only_t, const char *name, const permissions &perm = permissions())
  51. : m_cond(create_only_t(), name, perm)
  52. {}
  53. //!Opens or creates a global condition with a name.
  54. //!If the condition is created, this call is equivalent to
  55. //!named_condition_any(create_only_t, ... )
  56. //!If the condition is already created, this call is equivalent
  57. //!named_condition_any(open_only_t, ... )
  58. //!Does not throw
  59. named_condition_any(open_or_create_t, const char *name, const permissions &perm = permissions())
  60. : m_cond(open_or_create_t(), name, perm)
  61. {}
  62. //!Opens a global condition with a name if that condition is previously
  63. //!created. If it is not previously created this function throws
  64. //!interprocess_exception.
  65. named_condition_any(open_only_t, const char *name)
  66. : m_cond(open_only_t(), name)
  67. {}
  68. //!Destroys *this and indicates that the calling process is finished using
  69. //!the resource. The destructor function will deallocate
  70. //!any system resources allocated by the system for use by this process for
  71. //!this resource. The resource can still be opened again calling
  72. //!the open constructor overload. To erase the resource from the system
  73. //!use remove().
  74. ~named_condition_any()
  75. {}
  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. { m_cond.notify_one(); }
  80. //!Change the state of all threads waiting on *this to ready.
  81. //!If there are no waiting threads, notify_all() has no effect.
  82. void notify_all()
  83. { m_cond.notify_all(); }
  84. //!Releases the lock on the named_mutex object associated with lock, blocks
  85. //!the current thread of execution until readied by a call to
  86. //!this->notify_one() or this->notify_all(), and then reacquires the lock.
  87. template <typename L>
  88. void wait(L& lock)
  89. { return m_cond.wait(lock); }
  90. //!The same as:
  91. //!while (!pred()) wait(lock)
  92. template <typename L, typename Pr>
  93. void wait(L& lock, Pr pred)
  94. { return m_cond.wait(lock, 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. { return m_cond.timed_wait(lock, abs_time); }
  103. //!The same as: while (!pred()) {
  104. //! if (!timed_wait(lock, abs_time)) return pred();
  105. //! } return true;
  106. template <typename L, typename Pr>
  107. bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time, Pr pred)
  108. { return m_cond.timed_wait(lock, abs_time, pred); }
  109. //!Erases a named condition from the system.
  110. //!Returns false on error. Never throws.
  111. static bool remove(const char *name)
  112. { return condition_any_type::remove(name); }
  113. /// @cond
  114. private:
  115. #if defined(BOOST_INTERPROCESS_USE_WINDOWS)
  116. typedef ipcdetail::windows_named_condition_any condition_any_type;
  117. #else
  118. typedef ipcdetail::shm_named_condition_any condition_any_type;
  119. #endif
  120. condition_any_type m_cond;
  121. friend class ipcdetail::interprocess_tester;
  122. void dont_close_on_destruction()
  123. { ipcdetail::interprocess_tester::dont_close_on_destruction(m_cond); }
  124. /// @endcond
  125. };
  126. } //namespace interprocess
  127. } //namespace boost
  128. #include <boost/interprocess/detail/config_end.hpp>
  129. #endif // BOOST_INTERPROCESS_NAMED_CONDITION_ANY_HPP