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