semaphore.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 windows_semaphore
  32. {
  33. windows_semaphore(const windows_semaphore &);
  34. windows_semaphore &operator=(const windows_semaphore &);
  35. public:
  36. windows_semaphore(unsigned int initialCount);
  37. ~windows_semaphore();
  38. void post(long 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. };
  45. inline windows_semaphore::windows_semaphore(unsigned int initialCount)
  46. : id_(this)
  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_, 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 windows_semaphore::~windows_semaphore()
  59. {
  60. sync_handles &handles =
  61. windows_intermodule_singleton<sync_handles>::get();
  62. handles.destroy_handle(this->id_);
  63. }
  64. inline void windows_semaphore::wait(void)
  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_, 0));
  70. sem.wait();
  71. }
  72. inline bool windows_semaphore::try_wait(void)
  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_, 0));
  78. return sem.try_wait();
  79. }
  80. inline bool windows_semaphore::timed_wait(const boost::posix_time::ptime &abs_time)
  81. {
  82. sync_handles &handles =
  83. windows_intermodule_singleton<sync_handles>::get();
  84. //This can throw
  85. winapi_semaphore_functions sem(handles.obtain_semaphore(this->id_, 0));
  86. return sem.timed_wait(abs_time);
  87. }
  88. inline void windows_semaphore::post(long release_count)
  89. {
  90. sync_handles &handles =
  91. windows_intermodule_singleton<sync_handles>::get();
  92. winapi_semaphore_functions sem(handles.obtain_semaphore(this->id_, 0));
  93. sem.post(release_count);
  94. }
  95. } //namespace ipcdetail {
  96. } //namespace interprocess {
  97. } //namespace boost {
  98. #include <boost/interprocess/detail/config_end.hpp>
  99. #endif //BOOST_INTERPROCESS_DETAIL_WINDOWS_SEMAPHORE_HPP