winapi_semaphore_wrapper.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2011-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_WINAPI_SEMAPHORE_WRAPPER_HPP
  11. #define BOOST_INTERPROCESS_DETAIL_WINAPI_SEMAPHORE_WRAPPER_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/creation_tags.hpp>
  22. #include <boost/interprocess/permissions.hpp>
  23. #include <boost/interprocess/detail/win32_api.hpp>
  24. #include <boost/interprocess/sync/windows/winapi_wrapper_common.hpp>
  25. #include <boost/interprocess/errors.hpp>
  26. #include <boost/interprocess/exceptions.hpp>
  27. #include <limits>
  28. namespace boost {
  29. namespace interprocess {
  30. namespace ipcdetail {
  31. class winapi_semaphore_functions
  32. {
  33. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  34. //Non-copyable
  35. winapi_semaphore_functions(const winapi_semaphore_functions &);
  36. winapi_semaphore_functions &operator=(const winapi_semaphore_functions &);
  37. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  38. public:
  39. winapi_semaphore_functions(void *hnd)
  40. : m_sem_hnd(hnd)
  41. {}
  42. void post(long count = 1)
  43. {
  44. long prev_count;
  45. winapi::release_semaphore(m_sem_hnd, count, &prev_count);
  46. }
  47. void wait()
  48. { return winapi_wrapper_wait_for_single_object(m_sem_hnd); }
  49. bool try_wait()
  50. { return winapi_wrapper_try_wait_for_single_object(m_sem_hnd); }
  51. template<class TimePoint>
  52. bool timed_wait(const TimePoint &abs_time)
  53. { return winapi_wrapper_timed_wait_for_single_object(m_sem_hnd, abs_time); }
  54. long value() const
  55. {
  56. long l_count, l_limit;
  57. if(!winapi::get_semaphore_info(m_sem_hnd, l_count, l_limit))
  58. return 0;
  59. return l_count;
  60. }
  61. long limit() const
  62. {
  63. long l_count, l_limit;
  64. if(!winapi::get_semaphore_info(m_sem_hnd, l_count, l_limit))
  65. return 0;
  66. return l_limit;
  67. }
  68. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  69. protected:
  70. void *m_sem_hnd;
  71. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  72. };
  73. //Swappable semaphore wrapper
  74. class winapi_semaphore_wrapper
  75. : public winapi_semaphore_functions
  76. {
  77. winapi_semaphore_wrapper(const winapi_semaphore_wrapper &);
  78. winapi_semaphore_wrapper &operator=(const winapi_semaphore_wrapper &);
  79. public:
  80. //Long is 32 bits in windows
  81. static const long MaxCount = long(0x7FFFFFFF);
  82. winapi_semaphore_wrapper(void *hnd = winapi::invalid_handle_value)
  83. : winapi_semaphore_functions(hnd)
  84. {}
  85. ~winapi_semaphore_wrapper()
  86. { this->close(); }
  87. void *release()
  88. {
  89. void *hnd = m_sem_hnd;
  90. m_sem_hnd = winapi::invalid_handle_value;
  91. return hnd;
  92. }
  93. void *handle() const
  94. { return m_sem_hnd; }
  95. template <class CharT>
  96. bool open_or_create( const CharT *name
  97. , long sem_count
  98. , long max_count
  99. , const permissions &perm
  100. , bool &created)
  101. {
  102. if(m_sem_hnd == winapi::invalid_handle_value){
  103. m_sem_hnd = winapi::open_or_create_semaphore
  104. ( name
  105. , sem_count
  106. , max_count
  107. , (winapi::interprocess_security_attributes*)perm.get_permissions()
  108. );
  109. created = winapi::get_last_error() != winapi::error_already_exists;
  110. return m_sem_hnd != winapi::invalid_handle_value;
  111. }
  112. else{
  113. return false;
  114. }
  115. }
  116. bool open_semaphore(const char *name)
  117. {
  118. if(m_sem_hnd == winapi::invalid_handle_value){
  119. m_sem_hnd = winapi::open_semaphore(name);
  120. return m_sem_hnd != winapi::invalid_handle_value;
  121. }
  122. else{
  123. return false;
  124. }
  125. }
  126. void close()
  127. {
  128. if(m_sem_hnd != winapi::invalid_handle_value){
  129. winapi::close_handle(m_sem_hnd);
  130. m_sem_hnd = winapi::invalid_handle_value;
  131. }
  132. }
  133. void swap(winapi_semaphore_wrapper &other)
  134. { void *tmp = m_sem_hnd; m_sem_hnd = other.m_sem_hnd; other.m_sem_hnd = tmp; }
  135. };
  136. } //namespace ipcdetail {
  137. } //namespace interprocess {
  138. } //namespace boost {
  139. #include <boost/interprocess/detail/config_end.hpp>
  140. #endif //BOOST_INTERPROCESS_DETAIL_WINAPI_SEMAPHORE_WRAPPER_HPP