named_mutex.hpp 6.6 KB

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