semaphore.hpp 3.4 KB

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