winapi_mutex_wrapper.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. #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_mutex_functions
  33. {
  34. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  35. //Non-copyable
  36. winapi_mutex_functions(const winapi_mutex_functions &);
  37. winapi_mutex_functions &operator=(const winapi_mutex_functions &);
  38. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  39. public:
  40. winapi_mutex_functions(void *mtx_hnd)
  41. : m_mtx_hnd(mtx_hnd)
  42. {}
  43. void unlock()
  44. { winapi::release_mutex(m_mtx_hnd); }
  45. void lock()
  46. { return winapi_wrapper_wait_for_single_object(m_mtx_hnd); }
  47. bool try_lock()
  48. { return winapi_wrapper_try_wait_for_single_object(m_mtx_hnd); }
  49. bool timed_lock(const boost::posix_time::ptime &abs_time)
  50. { return winapi_wrapper_timed_wait_for_single_object(m_mtx_hnd, abs_time); }
  51. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  52. protected:
  53. void *m_mtx_hnd;
  54. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  55. };
  56. //Swappable mutex wrapper
  57. class winapi_mutex_wrapper
  58. : public winapi_mutex_functions
  59. {
  60. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  61. //Non-copyable
  62. winapi_mutex_wrapper(const winapi_mutex_wrapper &);
  63. winapi_mutex_wrapper &operator=(const winapi_mutex_wrapper &);
  64. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  65. //Note that Windows API does not return winapi::invalid_handle_value
  66. //when failing to create/open a mutex, but a nullptr
  67. public:
  68. winapi_mutex_wrapper(void *mtx_hnd = 0)
  69. : winapi_mutex_functions(mtx_hnd)
  70. {}
  71. ~winapi_mutex_wrapper()
  72. { this->close(); }
  73. void *release()
  74. {
  75. void *hnd = m_mtx_hnd;
  76. m_mtx_hnd = 0;
  77. return hnd;
  78. }
  79. void *handle() const
  80. { return m_mtx_hnd; }
  81. bool open_or_create(const char *name, const permissions &perm)
  82. {
  83. if(m_mtx_hnd == 0){
  84. m_mtx_hnd = winapi::open_or_create_mutex
  85. ( name
  86. , false
  87. , (winapi::interprocess_security_attributes*)perm.get_permissions()
  88. );
  89. return m_mtx_hnd != 0;
  90. }
  91. else{
  92. return false;
  93. }
  94. }
  95. void close()
  96. {
  97. if(m_mtx_hnd != 0){
  98. winapi::close_handle(m_mtx_hnd);
  99. m_mtx_hnd = 0;
  100. }
  101. }
  102. void swap(winapi_mutex_wrapper &other)
  103. { void *tmp = m_mtx_hnd; m_mtx_hnd = other.m_mtx_hnd; other.m_mtx_hnd = tmp; }
  104. };
  105. } //namespace ipcdetail {
  106. } //namespace interprocess {
  107. } //namespace boost {
  108. #include <boost/interprocess/detail/config_end.hpp>
  109. #endif //BOOST_INTERPROCESS_DETAIL_WINAPI_MUTEX_WRAPPER_HPP