named_mutex.hpp 6.8 KB

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