winapi_semaphore_wrapper.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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/detail/posix_time_types_wrk.hpp>
  25. #include <boost/interprocess/sync/windows/winapi_wrapper_common.hpp>
  26. #include <boost/interprocess/errors.hpp>
  27. #include <boost/interprocess/exceptions.hpp>
  28. #include <limits>
  29. namespace boost {
  30. namespace interprocess {
  31. namespace ipcdetail {
  32. class winapi_semaphore_functions
  33. {
  34. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  35. //Non-copyable
  36. winapi_semaphore_functions(const winapi_semaphore_functions &);
  37. winapi_semaphore_functions &operator=(const winapi_semaphore_functions &);
  38. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  39. public:
  40. winapi_semaphore_functions(void *hnd)
  41. : m_sem_hnd(hnd)
  42. {}
  43. void post(long count = 1)
  44. {
  45. long prev_count;
  46. winapi::release_semaphore(m_sem_hnd, count, &prev_count);
  47. }
  48. void wait()
  49. { return winapi_wrapper_wait_for_single_object(m_sem_hnd); }
  50. bool try_wait()
  51. { return winapi_wrapper_try_wait_for_single_object(m_sem_hnd); }
  52. bool timed_wait(const boost::posix_time::ptime &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. bool open_or_create( const char *name
  96. , long sem_count
  97. , long max_count
  98. , const permissions &perm
  99. , bool &created)
  100. {
  101. if(m_sem_hnd == winapi::invalid_handle_value){
  102. m_sem_hnd = winapi::open_or_create_semaphore
  103. ( name
  104. , sem_count
  105. , max_count
  106. , (winapi::interprocess_security_attributes*)perm.get_permissions()
  107. );
  108. created = winapi::get_last_error() != winapi::error_already_exists;
  109. return m_sem_hnd != winapi::invalid_handle_value;
  110. }
  111. else{
  112. return false;
  113. }
  114. }
  115. bool open_semaphore(const char *name)
  116. {
  117. if(m_sem_hnd == winapi::invalid_handle_value){
  118. m_sem_hnd = winapi::open_semaphore(name);
  119. return m_sem_hnd != winapi::invalid_handle_value;
  120. }
  121. else{
  122. return false;
  123. }
  124. }
  125. void close()
  126. {
  127. if(m_sem_hnd != winapi::invalid_handle_value){
  128. winapi::close_handle(m_sem_hnd);
  129. m_sem_hnd = winapi::invalid_handle_value;
  130. }
  131. }
  132. void swap(winapi_semaphore_wrapper &other)
  133. { void *tmp = m_sem_hnd; m_sem_hnd = other.m_sem_hnd; other.m_sem_hnd = tmp; }
  134. };
  135. } //namespace ipcdetail {
  136. } //namespace interprocess {
  137. } //namespace boost {
  138. #include <boost/interprocess/detail/config_end.hpp>
  139. #endif //BOOST_INTERPROCESS_DETAIL_WINAPI_SEMAPHORE_WRAPPER_HPP