condition.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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/detail/posix_time_types_wrk.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/sync/detail/condition_algorithm_8a.hpp>
  28. namespace boost {
  29. namespace interprocess {
  30. namespace ipcdetail {
  31. class winapi_condition
  32. {
  33. winapi_condition(const winapi_condition &);
  34. winapi_condition &operator=(const winapi_condition &);
  35. public:
  36. winapi_condition()
  37. : m_condition_data()
  38. {}
  39. ~winapi_condition()
  40. {
  41. //Notify all waiting threads
  42. //to allow POSIX semantics on condition destruction
  43. this->notify_all();
  44. }
  45. void notify_one()
  46. { m_condition_data.notify_one(); }
  47. void notify_all()
  48. { m_condition_data.notify_all(); }
  49. template <typename L>
  50. bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time)
  51. { return m_condition_data.timed_wait(lock, abs_time); }
  52. template <typename L, typename Pr>
  53. bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time, Pr pred)
  54. { return m_condition_data.timed_wait(lock, abs_time, pred); }
  55. template <typename L>
  56. void wait(L& lock)
  57. { m_condition_data.wait(lock); }
  58. template <typename L, typename Pr>
  59. void wait(L& lock, Pr pred)
  60. { m_condition_data.wait(lock, pred); }
  61. private:
  62. struct condition_data
  63. {
  64. typedef boost::int32_t integer_type;
  65. typedef winapi_semaphore semaphore_type;
  66. typedef winapi_mutex mutex_type;
  67. condition_data()
  68. : m_nwaiters_blocked(0)
  69. , m_nwaiters_gone(0)
  70. , m_nwaiters_to_unblock(0)
  71. , m_sem_block_queue(0)
  72. , m_sem_block_lock(1)
  73. , m_mtx_unblock_lock()
  74. {}
  75. integer_type &get_nwaiters_blocked()
  76. { return m_nwaiters_blocked; }
  77. integer_type &get_nwaiters_gone()
  78. { return m_nwaiters_gone; }
  79. integer_type &get_nwaiters_to_unblock()
  80. { return m_nwaiters_to_unblock; }
  81. semaphore_type &get_sem_block_queue()
  82. { return m_sem_block_queue; }
  83. semaphore_type &get_sem_block_lock()
  84. { return m_sem_block_lock; }
  85. mutex_type &get_mtx_unblock_lock()
  86. { return m_mtx_unblock_lock; }
  87. integer_type m_nwaiters_blocked;
  88. integer_type m_nwaiters_gone;
  89. integer_type m_nwaiters_to_unblock;
  90. semaphore_type m_sem_block_queue;
  91. semaphore_type m_sem_block_lock;
  92. mutex_type m_mtx_unblock_lock;
  93. };
  94. ipcdetail::condition_8a_wrapper<condition_data> m_condition_data;
  95. };
  96. } //namespace ipcdetail
  97. } //namespace interprocess
  98. } //namespace boost
  99. #include <boost/interprocess/detail/config_end.hpp>
  100. #endif //BOOST_INTERPROCESS_DETAIL_WINDOWS_CONDITION_HPP