named_mutex.hpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. #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/interprocess_tester.hpp>
  24. #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
  25. #include <boost/interprocess/sync/windows/sync_utils.hpp>
  26. #include <boost/interprocess/sync/windows/named_sync.hpp>
  27. #include <boost/interprocess/sync/windows/winapi_mutex_wrapper.hpp>
  28. #include <boost/interprocess/errors.hpp>
  29. #include <boost/interprocess/exceptions.hpp>
  30. #include <limits>
  31. namespace boost {
  32. namespace interprocess {
  33. namespace ipcdetail {
  34. class winapi_named_mutex
  35. {
  36. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  37. //Non-copyable
  38. winapi_named_mutex();
  39. winapi_named_mutex(const winapi_named_mutex &);
  40. winapi_named_mutex &operator=(const winapi_named_mutex &);
  41. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  42. public:
  43. winapi_named_mutex(create_only_t, const char *name, const permissions &perm = permissions());
  44. winapi_named_mutex(open_or_create_t, const char *name, const permissions &perm = permissions());
  45. winapi_named_mutex(open_only_t, const char *name);
  46. winapi_named_mutex(create_only_t, const wchar_t *name, const permissions &perm = permissions());
  47. winapi_named_mutex(open_or_create_t, const wchar_t *name, const permissions &perm = permissions());
  48. winapi_named_mutex(open_only_t, const wchar_t *name);
  49. ~winapi_named_mutex();
  50. void unlock();
  51. void lock();
  52. bool try_lock();
  53. bool timed_lock(const boost::posix_time::ptime &abs_time);
  54. static bool remove(const char *name);
  55. static bool remove(const wchar_t *name);
  56. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  57. private:
  58. friend class interprocess_tester;
  59. void dont_close_on_destruction();
  60. winapi_mutex_wrapper m_mtx_wrapper;
  61. windows_named_sync m_named_sync;
  62. class named_mut_callbacks : public windows_named_sync_interface
  63. {
  64. public:
  65. named_mut_callbacks(winapi_mutex_wrapper &mtx_wrapper)
  66. : m_mtx_wrapper(mtx_wrapper)
  67. {}
  68. virtual std::size_t get_data_size() const
  69. { return 0u; }
  70. virtual const void *buffer_with_init_data_to_file()
  71. { return 0; }
  72. virtual const void *buffer_with_final_data_to_file()
  73. { return 0; }
  74. virtual void *buffer_to_store_init_data_from_file()
  75. { return 0; }
  76. virtual bool open(create_enum_t, const char *id_name)
  77. {
  78. std::string aux_str = "Global\\bipc.mut.";
  79. aux_str += id_name;
  80. //
  81. permissions mut_perm;
  82. mut_perm.set_unrestricted();
  83. return m_mtx_wrapper.open_or_create(aux_str.c_str(), mut_perm);
  84. }
  85. virtual bool open(create_enum_t, const wchar_t *id_name)
  86. {
  87. std::wstring aux_str = L"Global\\bipc.mut.";
  88. aux_str += id_name;
  89. //
  90. permissions mut_perm;
  91. mut_perm.set_unrestricted();
  92. return m_mtx_wrapper.open_or_create(aux_str.c_str(), mut_perm);
  93. }
  94. virtual void close()
  95. {
  96. m_mtx_wrapper.close();
  97. }
  98. virtual ~named_mut_callbacks()
  99. {}
  100. private:
  101. winapi_mutex_wrapper& m_mtx_wrapper;
  102. };
  103. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  104. };
  105. inline winapi_named_mutex::~winapi_named_mutex()
  106. {
  107. named_mut_callbacks callbacks(m_mtx_wrapper);
  108. m_named_sync.close(callbacks);
  109. }
  110. inline void winapi_named_mutex::dont_close_on_destruction()
  111. {}
  112. inline winapi_named_mutex::winapi_named_mutex
  113. (create_only_t, const char *name, const permissions &perm)
  114. : m_mtx_wrapper()
  115. {
  116. named_mut_callbacks callbacks(m_mtx_wrapper);
  117. m_named_sync.open_or_create(DoCreate, name, perm, callbacks);
  118. }
  119. inline winapi_named_mutex::winapi_named_mutex
  120. (open_or_create_t, const char *name, const permissions &perm)
  121. : m_mtx_wrapper()
  122. {
  123. named_mut_callbacks callbacks(m_mtx_wrapper);
  124. m_named_sync.open_or_create(DoOpenOrCreate, name, perm, callbacks);
  125. }
  126. inline winapi_named_mutex::winapi_named_mutex(open_only_t, const char *name)
  127. : m_mtx_wrapper()
  128. {
  129. named_mut_callbacks callbacks(m_mtx_wrapper);
  130. m_named_sync.open_or_create(DoOpen, name, permissions(), callbacks);
  131. }
  132. inline winapi_named_mutex::winapi_named_mutex
  133. (create_only_t, const wchar_t *name, const permissions &perm)
  134. : m_mtx_wrapper()
  135. {
  136. named_mut_callbacks callbacks(m_mtx_wrapper);
  137. m_named_sync.open_or_create(DoCreate, name, perm, callbacks);
  138. }
  139. inline winapi_named_mutex::winapi_named_mutex
  140. (open_or_create_t, const wchar_t *name, const permissions &perm)
  141. : m_mtx_wrapper()
  142. {
  143. named_mut_callbacks callbacks(m_mtx_wrapper);
  144. m_named_sync.open_or_create(DoOpenOrCreate, name, perm, callbacks);
  145. }
  146. inline winapi_named_mutex::winapi_named_mutex(open_only_t, const wchar_t *name)
  147. : m_mtx_wrapper()
  148. {
  149. named_mut_callbacks callbacks(m_mtx_wrapper);
  150. m_named_sync.open_or_create(DoOpen, name, permissions(), callbacks);
  151. }
  152. inline void winapi_named_mutex::unlock()
  153. {
  154. m_mtx_wrapper.unlock();
  155. }
  156. inline void winapi_named_mutex::lock()
  157. {
  158. m_mtx_wrapper.lock();
  159. }
  160. inline bool winapi_named_mutex::try_lock()
  161. {
  162. return m_mtx_wrapper.try_lock();
  163. }
  164. inline bool winapi_named_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
  165. {
  166. return m_mtx_wrapper.timed_lock(abs_time);
  167. }
  168. inline bool winapi_named_mutex::remove(const char *name)
  169. {
  170. return windows_named_sync::remove(name);
  171. }
  172. inline bool winapi_named_mutex::remove(const wchar_t *name)
  173. {
  174. return windows_named_sync::remove(name);
  175. }
  176. } //namespace ipcdetail {
  177. } //namespace interprocess {
  178. } //namespace boost {
  179. #include <boost/interprocess/detail/config_end.hpp>
  180. #endif //BOOST_INTERPROCESS_WINDOWS_NAMED_MUTEX_HPP