named_mutex.hpp 4.9 KB

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