winapi_mutex_wrapper.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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_MUTEX_WRAPPER_HPP
  11. #define BOOST_INTERPROCESS_DETAIL_WINAPI_MUTEX_WRAPPER_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/creation_tags.hpp>
  18. #include <boost/interprocess/permissions.hpp>
  19. #include <boost/interprocess/detail/win32_api.hpp>
  20. #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
  21. #include <boost/interprocess/errors.hpp>
  22. #include <boost/interprocess/exceptions.hpp>
  23. #include <limits>
  24. namespace boost {
  25. namespace interprocess {
  26. namespace ipcdetail {
  27. class winapi_mutex_functions
  28. {
  29. /// @cond
  30. //Non-copyable
  31. winapi_mutex_functions(const winapi_mutex_functions &);
  32. winapi_mutex_functions &operator=(const winapi_mutex_functions &);
  33. /// @endcond
  34. public:
  35. winapi_mutex_functions(void *mtx_hnd)
  36. : m_mtx_hnd(mtx_hnd)
  37. {}
  38. void unlock()
  39. {
  40. winapi::release_mutex(m_mtx_hnd);
  41. }
  42. void lock()
  43. {
  44. if(winapi::wait_for_single_object(m_mtx_hnd, winapi::infinite_time) != winapi::wait_object_0){
  45. error_info err = system_error_code();
  46. throw interprocess_exception(err);
  47. }
  48. }
  49. bool try_lock()
  50. {
  51. unsigned long ret = winapi::wait_for_single_object(m_mtx_hnd, 0);
  52. if(ret == winapi::wait_object_0){
  53. return true;
  54. }
  55. else if(ret == winapi::wait_timeout){
  56. return false;
  57. }
  58. else{
  59. error_info err = system_error_code();
  60. throw interprocess_exception(err);
  61. }
  62. }
  63. bool timed_lock(const boost::posix_time::ptime &abs_time)
  64. {
  65. if(abs_time == boost::posix_time::pos_infin){
  66. this->lock();
  67. return true;
  68. }
  69. unsigned long ret = winapi::wait_for_single_object
  70. (m_mtx_hnd, (abs_time - microsec_clock::universal_time()).total_milliseconds());
  71. if(ret == winapi::wait_object_0){
  72. return true;
  73. }
  74. else if(ret == winapi::wait_timeout){
  75. return false;
  76. }
  77. else{
  78. error_info err = system_error_code();
  79. throw interprocess_exception(err);
  80. }
  81. }
  82. /// @cond
  83. protected:
  84. void *m_mtx_hnd;
  85. /// @endcond
  86. };
  87. //Swappable mutex wrapper
  88. class winapi_mutex_wrapper
  89. : public winapi_mutex_functions
  90. {
  91. /// @cond
  92. //Non-copyable
  93. winapi_mutex_wrapper(const winapi_mutex_wrapper &);
  94. winapi_mutex_wrapper &operator=(const winapi_mutex_wrapper &);
  95. /// @endcond
  96. public:
  97. winapi_mutex_wrapper(void *mtx_hnd = winapi::invalid_handle_value)
  98. : winapi_mutex_functions(mtx_hnd)
  99. {}
  100. ~winapi_mutex_wrapper()
  101. { this->close(); }
  102. void *release()
  103. {
  104. void *hnd = m_mtx_hnd;
  105. m_mtx_hnd = winapi::invalid_handle_value;
  106. return hnd;
  107. }
  108. void *handle() const
  109. { return m_mtx_hnd; }
  110. bool open_or_create(const char *name, const permissions &perm)
  111. {
  112. if(m_mtx_hnd == winapi::invalid_handle_value){
  113. m_mtx_hnd = winapi::open_or_create_mutex
  114. ( name
  115. , false
  116. , (winapi::interprocess_security_attributes*)perm.get_permissions()
  117. );
  118. return m_mtx_hnd != winapi::invalid_handle_value;
  119. }
  120. else{
  121. return false;
  122. }
  123. }
  124. void close()
  125. {
  126. if(m_mtx_hnd != winapi::invalid_handle_value){
  127. winapi::close_handle(m_mtx_hnd);
  128. m_mtx_hnd = winapi::invalid_handle_value;
  129. }
  130. }
  131. void swap(winapi_mutex_wrapper &other)
  132. { void *tmp = m_mtx_hnd; m_mtx_hnd = other.m_mtx_hnd; other.m_mtx_hnd = tmp; }
  133. };
  134. } //namespace ipcdetail {
  135. } //namespace interprocess {
  136. } //namespace boost {
  137. #include <boost/interprocess/detail/config_end.hpp>
  138. #endif //BOOST_INTERPROCESS_DETAIL_WINAPI_MUTEX_WRAPPER_HPP