condition.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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_DETAIL_WINDOWS_CONDITION_HPP
  11. #define BOOST_INTERPROCESS_DETAIL_WINDOWS_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/interprocess/sync/cv_status.hpp>
  22. #include <boost/interprocess/sync/interprocess_mutex.hpp>
  23. #include <boost/interprocess/sync/scoped_lock.hpp>
  24. #include <boost/interprocess/exceptions.hpp>
  25. #include <boost/interprocess/sync/windows/semaphore.hpp>
  26. #include <boost/interprocess/sync/windows/mutex.hpp>
  27. #include <boost/interprocess/timed_utils.hpp>
  28. #include <boost/interprocess/sync/detail/condition_algorithm_8a.hpp>
  29. namespace boost {
  30. namespace interprocess {
  31. namespace ipcdetail {
  32. class winapi_condition
  33. {
  34. winapi_condition(const winapi_condition &);
  35. winapi_condition &operator=(const winapi_condition &);
  36. public:
  37. winapi_condition()
  38. : m_condition_data()
  39. {}
  40. ~winapi_condition()
  41. {
  42. //Notify all waiting threads
  43. //to allow POSIX semantics on condition destruction
  44. this->notify_all();
  45. }
  46. void notify_one()
  47. { m_condition_data.notify_one(); }
  48. void notify_all()
  49. { m_condition_data.notify_all(); }
  50. template <typename L>
  51. void wait(L& lock)
  52. { m_condition_data.wait(lock); }
  53. template <typename L, typename Pr>
  54. void wait(L& lock, Pr pred)
  55. { m_condition_data.wait(lock, pred); }
  56. template <typename L, typename TimePoint>
  57. bool timed_wait(L& lock, const TimePoint &abs_time)
  58. { return m_condition_data.timed_wait(lock, abs_time); }
  59. template <typename L, typename TimePoint, typename Pr>
  60. bool timed_wait(L& lock, const TimePoint &abs_time, Pr pred)
  61. { return m_condition_data.timed_wait(lock, abs_time, pred); }
  62. template <typename L, class TimePoint>
  63. cv_status wait_until(L& lock, const TimePoint &abs_time)
  64. { return this->timed_wait(lock, abs_time) ? cv_status::no_timeout : cv_status::timeout; }
  65. template <typename L, class TimePoint, typename Pr>
  66. bool wait_until(L& lock, const TimePoint &abs_time, Pr pred)
  67. { return this->timed_wait(lock, abs_time, pred); }
  68. template <typename L, class Duration>
  69. cv_status wait_for(L& lock, const Duration &dur)
  70. { return this->wait_until(lock, duration_to_ustime(dur)); }
  71. template <typename L, class Duration, typename Pr>
  72. bool wait_for(L& lock, const Duration &dur, Pr pred)
  73. { return this->wait_until(lock, duration_to_ustime(dur), pred); }
  74. private:
  75. struct condition_data
  76. {
  77. typedef unsigned int integer_type;
  78. typedef winapi_semaphore semaphore_type;
  79. typedef winapi_mutex mutex_type;
  80. condition_data()
  81. : m_nwaiters_blocked(0)
  82. , m_nwaiters_gone(0)
  83. , m_nwaiters_to_unblock(0)
  84. , m_sem_block_queue(0)
  85. , m_sem_block_lock(1)
  86. , m_mtx_unblock_lock()
  87. {}
  88. integer_type &get_nwaiters_blocked()
  89. { return m_nwaiters_blocked; }
  90. integer_type &get_nwaiters_gone()
  91. { return m_nwaiters_gone; }
  92. integer_type &get_nwaiters_to_unblock()
  93. { return m_nwaiters_to_unblock; }
  94. semaphore_type &get_sem_block_queue()
  95. { return m_sem_block_queue; }
  96. semaphore_type &get_sem_block_lock()
  97. { return m_sem_block_lock; }
  98. mutex_type &get_mtx_unblock_lock()
  99. { return m_mtx_unblock_lock; }
  100. integer_type m_nwaiters_blocked;
  101. integer_type m_nwaiters_gone;
  102. integer_type m_nwaiters_to_unblock;
  103. semaphore_type m_sem_block_queue;
  104. semaphore_type m_sem_block_lock;
  105. mutex_type m_mtx_unblock_lock;
  106. };
  107. ipcdetail::condition_8a_wrapper<condition_data> m_condition_data;
  108. };
  109. } //namespace ipcdetail
  110. } //namespace interprocess
  111. } //namespace boost
  112. #include <boost/interprocess/detail/config_end.hpp>
  113. #endif //BOOST_INTERPROCESS_DETAIL_WINDOWS_CONDITION_HPP