named_mutex.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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_WINDOWS_NAMED_MUTEX_HPP
  11. #define BOOST_INTERPROCESS_WINDOWS_NAMED_MUTEX_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/interprocess_tester.hpp>
  20. #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
  21. #include <boost/interprocess/sync/windows/sync_utils.hpp>
  22. #include <boost/interprocess/sync/windows/named_sync.hpp>
  23. #include <boost/interprocess/sync/windows/winapi_mutex_wrapper.hpp>
  24. #include <boost/interprocess/errors.hpp>
  25. #include <boost/interprocess/exceptions.hpp>
  26. #include <limits>
  27. namespace boost {
  28. namespace interprocess {
  29. namespace ipcdetail {
  30. class windows_named_mutex
  31. {
  32. /// @cond
  33. //Non-copyable
  34. windows_named_mutex();
  35. windows_named_mutex(const windows_named_mutex &);
  36. windows_named_mutex &operator=(const windows_named_mutex &);
  37. /// @endcond
  38. public:
  39. windows_named_mutex(create_only_t, const char *name, const permissions &perm = permissions());
  40. windows_named_mutex(open_or_create_t, const char *name, const permissions &perm = permissions());
  41. windows_named_mutex(open_only_t, const char *name);
  42. ~windows_named_mutex();
  43. void unlock();
  44. void lock();
  45. bool try_lock();
  46. bool timed_lock(const boost::posix_time::ptime &abs_time);
  47. static bool remove(const char *name);
  48. /// @cond
  49. private:
  50. friend class interprocess_tester;
  51. void dont_close_on_destruction();
  52. winapi_mutex_wrapper m_mtx_wrapper;
  53. windows_named_sync m_named_sync;
  54. class named_mut_callbacks : public windows_named_sync_interface
  55. {
  56. public:
  57. named_mut_callbacks(winapi_mutex_wrapper &mtx_wrapper)
  58. : m_mtx_wrapper(mtx_wrapper)
  59. {}
  60. virtual std::size_t get_data_size() const
  61. { return 0u; }
  62. virtual const void *buffer_with_init_data_to_file()
  63. { return 0; }
  64. virtual const void *buffer_with_final_data_to_file()
  65. { return 0; }
  66. virtual void *buffer_to_store_init_data_from_file()
  67. { return 0; }
  68. virtual bool open(create_enum_t, const char *id_name)
  69. {
  70. std::string aux_str = "Global\\bipc.mut.";
  71. aux_str += id_name;
  72. //
  73. permissions mut_perm;
  74. mut_perm.set_unrestricted();
  75. return m_mtx_wrapper.open_or_create(aux_str.c_str(), mut_perm);
  76. }
  77. virtual void close()
  78. {
  79. m_mtx_wrapper.close();
  80. }
  81. virtual ~named_mut_callbacks()
  82. {}
  83. private:
  84. winapi_mutex_wrapper& m_mtx_wrapper;
  85. };
  86. /// @endcond
  87. };
  88. inline windows_named_mutex::~windows_named_mutex()
  89. {
  90. named_mut_callbacks callbacks(m_mtx_wrapper);
  91. m_named_sync.close(callbacks);
  92. }
  93. inline void windows_named_mutex::dont_close_on_destruction()
  94. {}
  95. inline windows_named_mutex::windows_named_mutex
  96. (create_only_t, const char *name, const permissions &perm)
  97. : m_mtx_wrapper()
  98. {
  99. named_mut_callbacks callbacks(m_mtx_wrapper);
  100. m_named_sync.open_or_create(DoCreate, name, perm, callbacks);
  101. }
  102. inline windows_named_mutex::windows_named_mutex
  103. (open_or_create_t, const char *name, const permissions &perm)
  104. : m_mtx_wrapper()
  105. {
  106. named_mut_callbacks callbacks(m_mtx_wrapper);
  107. m_named_sync.open_or_create(DoOpenOrCreate, name, perm, callbacks);
  108. }
  109. inline windows_named_mutex::windows_named_mutex(open_only_t, const char *name)
  110. : m_mtx_wrapper()
  111. {
  112. named_mut_callbacks callbacks(m_mtx_wrapper);
  113. m_named_sync.open_or_create(DoOpen, name, permissions(), callbacks);
  114. }
  115. inline void windows_named_mutex::unlock()
  116. {
  117. m_mtx_wrapper.unlock();
  118. }
  119. inline void windows_named_mutex::lock()
  120. {
  121. m_mtx_wrapper.lock();
  122. }
  123. inline bool windows_named_mutex::try_lock()
  124. {
  125. return m_mtx_wrapper.try_lock();
  126. }
  127. inline bool windows_named_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
  128. {
  129. return m_mtx_wrapper.timed_lock(abs_time);
  130. }
  131. inline bool windows_named_mutex::remove(const char *name)
  132. {
  133. return windows_named_sync::remove(name);
  134. }
  135. } //namespace ipcdetail {
  136. } //namespace interprocess {
  137. } //namespace boost {
  138. #include <boost/interprocess/detail/config_end.hpp>
  139. #endif //BOOST_INTERPROCESS_WINDOWS_NAMED_MUTEX_HPP