semaphore.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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_SEMAPHORE_HPP
  11. #define BOOST_INTERPROCESS_DETAIL_WINDOWS_SEMAPHORE_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/win32_api.hpp>
  22. #include <boost/interprocess/detail/windows_intermodule_singleton.hpp>
  23. #include <boost/interprocess/sync/windows/sync_utils.hpp>
  24. #include <boost/interprocess/sync/windows/winapi_semaphore_wrapper.hpp>
  25. #include <boost/interprocess/exceptions.hpp>
  26. #include <boost/assert.hpp>
  27. namespace boost {
  28. namespace interprocess {
  29. namespace ipcdetail {
  30. class winapi_semaphore
  31. {
  32. winapi_semaphore(const winapi_semaphore &);
  33. winapi_semaphore &operator=(const winapi_semaphore &);
  34. public:
  35. winapi_semaphore(unsigned int initialCount);
  36. ~winapi_semaphore();
  37. void post(unsigned int release_count = 1);
  38. void wait();
  39. bool try_wait();
  40. template<class TimePoint> bool timed_wait(const TimePoint &abs_time);
  41. private:
  42. const sync_id id_;
  43. const unsigned initial_count_;
  44. };
  45. inline winapi_semaphore::winapi_semaphore(unsigned int initialCount)
  46. : id_(), initial_count_(initialCount)
  47. {
  48. sync_handles &handles =
  49. windows_intermodule_singleton<sync_handles>::get();
  50. //Force smeaphore creation with the initial count
  51. bool open_or_created;
  52. handles.obtain_semaphore(this->id_, this, initialCount, &open_or_created);
  53. //The semaphore must be created, never opened
  54. BOOST_ASSERT(open_or_created);
  55. BOOST_ASSERT(open_or_created && winapi::get_last_error() != winapi::error_already_exists);
  56. (void)open_or_created;
  57. }
  58. inline winapi_semaphore::~winapi_semaphore()
  59. {
  60. sync_handles &handles =
  61. windows_intermodule_singleton<sync_handles>::get();
  62. handles.destroy_handle(this->id_, this);
  63. }
  64. inline void winapi_semaphore::wait()
  65. {
  66. sync_handles &handles =
  67. windows_intermodule_singleton<sync_handles>::get();
  68. //This can throw
  69. winapi_semaphore_functions sem(handles.obtain_semaphore(this->id_, this, initial_count_));
  70. sem.wait();
  71. }
  72. inline bool winapi_semaphore::try_wait()
  73. {
  74. sync_handles &handles =
  75. windows_intermodule_singleton<sync_handles>::get();
  76. //This can throw
  77. winapi_semaphore_functions sem(handles.obtain_semaphore(this->id_, this, initial_count_));
  78. return sem.try_wait();
  79. }
  80. template<class TimePoint>
  81. inline bool winapi_semaphore::timed_wait(const TimePoint &abs_time)
  82. {
  83. sync_handles &handles =
  84. windows_intermodule_singleton<sync_handles>::get();
  85. //This can throw
  86. winapi_semaphore_functions sem(handles.obtain_semaphore(this->id_, this, initial_count_));
  87. return sem.timed_wait(abs_time);
  88. }
  89. inline void winapi_semaphore::post(unsigned release_count)
  90. {
  91. sync_handles &handles =
  92. windows_intermodule_singleton<sync_handles>::get();
  93. winapi_semaphore_functions sem(handles.obtain_semaphore(this->id_, this, initial_count_));
  94. sem.post(static_cast<long>(release_count));
  95. }
  96. } //namespace ipcdetail {
  97. } //namespace interprocess {
  98. } //namespace boost {
  99. #include <boost/interprocess/detail/config_end.hpp>
  100. #endif //BOOST_INTERPROCESS_DETAIL_WINDOWS_SEMAPHORE_HPP