named_sharable_mutex.hpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2005-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_NAMED_SHARABLE_MUTEX_HPP
  11. #define BOOST_INTERPROCESS_NAMED_SHARABLE_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/exceptions.hpp>
  19. #include <boost/interprocess/shared_memory_object.hpp>
  20. #include <boost/interprocess/detail/managed_open_or_create_impl.hpp>
  21. #include <boost/interprocess/sync/interprocess_sharable_mutex.hpp>
  22. #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
  23. #include <boost/interprocess/sync/shm/named_creation_functor.hpp>
  24. #include <boost/interprocess/permissions.hpp>
  25. //!\file
  26. //!Describes a named sharable mutex class for inter-process synchronization
  27. namespace boost {
  28. namespace interprocess {
  29. /// @cond
  30. namespace ipcdetail{ class interprocess_tester; }
  31. /// @endcond
  32. class named_condition;
  33. //!A sharable mutex with a global name, so it can be found from different
  34. //!processes. This mutex can't be placed in shared memory, and
  35. //!each process should have it's own named sharable mutex.
  36. class named_sharable_mutex
  37. {
  38. /// @cond
  39. //Non-copyable
  40. named_sharable_mutex();
  41. named_sharable_mutex(const named_sharable_mutex &);
  42. named_sharable_mutex &operator=(const named_sharable_mutex &);
  43. /// @endcond
  44. public:
  45. //!Creates a global sharable mutex with a name.
  46. //!If the sharable mutex can't be created throws interprocess_exception
  47. named_sharable_mutex(create_only_t create_only, const char *name, const permissions &perm = permissions());
  48. //!Opens or creates a global sharable mutex with a name.
  49. //!If the sharable mutex is created, this call is equivalent to
  50. //!named_sharable_mutex(create_only_t, ...)
  51. //!If the sharable mutex is already created, this call is equivalent to
  52. //!named_sharable_mutex(open_only_t, ... ).
  53. named_sharable_mutex(open_or_create_t open_or_create, const char *name, const permissions &perm = permissions());
  54. //!Opens a global sharable mutex with a name if that sharable mutex
  55. //!is previously.
  56. //!created. If it is not previously created this function throws
  57. //!interprocess_exception.
  58. named_sharable_mutex(open_only_t open_only, const char *name);
  59. //!Destroys *this and indicates that the calling process is finished using
  60. //!the resource. The destructor function will deallocate
  61. //!any system resources allocated by the system for use by this process for
  62. //!this resource. The resource can still be opened again calling
  63. //!the open constructor overload. To erase the resource from the system
  64. //!use remove().
  65. ~named_sharable_mutex();
  66. //Exclusive locking
  67. //!Effects: The calling thread tries to obtain exclusive ownership of the mutex,
  68. //! and if another thread has exclusive or sharable ownership of
  69. //! the mutex, it waits until it can obtain the ownership.
  70. //!Throws: interprocess_exception on error.
  71. void lock();
  72. //!Effects: The calling thread tries to acquire exclusive ownership of the mutex
  73. //! without waiting. If no other thread has exclusive or sharable
  74. //! ownership of the mutex this succeeds.
  75. //!Returns: If it can acquire exclusive ownership immediately returns true.
  76. //! If it has to wait, returns false.
  77. //!Throws: interprocess_exception on error.
  78. bool try_lock();
  79. //!Effects: The calling thread tries to acquire exclusive ownership of the mutex
  80. //! waiting if necessary until no other thread has exclusive, or sharable
  81. //! ownership of the mutex or abs_time is reached.
  82. //!Returns: If acquires exclusive ownership, returns true. Otherwise returns false.
  83. //!Throws: interprocess_exception on error.
  84. bool timed_lock(const boost::posix_time::ptime &abs_time);
  85. //!Precondition: The thread must have exclusive ownership of the mutex.
  86. //!Effects: The calling thread releases the exclusive ownership of the mutex.
  87. //!Throws: An exception derived from interprocess_exception on error.
  88. void unlock();
  89. //Sharable locking
  90. //!Effects: The calling thread tries to obtain sharable ownership of the mutex,
  91. //! and if another thread has exclusive ownership of the mutex,
  92. //! waits until it can obtain the ownership.
  93. //!Throws: interprocess_exception on error.
  94. void lock_sharable();
  95. //!Effects: The calling thread tries to acquire sharable ownership of the mutex
  96. //! without waiting. If no other thread has exclusive ownership
  97. //! of the mutex this succeeds.
  98. //!Returns: If it can acquire sharable ownership immediately returns true. If it
  99. //! has to wait, returns false.
  100. //!Throws: interprocess_exception on error.
  101. bool try_lock_sharable();
  102. //!Effects: The calling thread tries to acquire sharable ownership of the mutex
  103. //! waiting if necessary until no other thread has exclusive
  104. //! ownership of the mutex or abs_time is reached.
  105. //!Returns: If acquires sharable ownership, returns true. Otherwise returns false.
  106. //!Throws: interprocess_exception on error.
  107. bool timed_lock_sharable(const boost::posix_time::ptime &abs_time);
  108. //!Precondition: The thread must have sharable ownership of the mutex.
  109. //!Effects: The calling thread releases the sharable ownership of the mutex.
  110. //!Throws: An exception derived from interprocess_exception on error.
  111. void unlock_sharable();
  112. //!Erases a named sharable mutex from the system.
  113. //!Returns false on error. Never throws.
  114. static bool remove(const char *name);
  115. /// @cond
  116. private:
  117. friend class ipcdetail::interprocess_tester;
  118. void dont_close_on_destruction();
  119. interprocess_sharable_mutex *mutex() const
  120. { return static_cast<interprocess_sharable_mutex*>(m_shmem.get_user_address()); }
  121. typedef ipcdetail::managed_open_or_create_impl<shared_memory_object, 0, true, false> open_create_impl_t;
  122. open_create_impl_t m_shmem;
  123. typedef ipcdetail::named_creation_functor<interprocess_sharable_mutex> construct_func_t;
  124. /// @endcond
  125. };
  126. /// @cond
  127. inline named_sharable_mutex::~named_sharable_mutex()
  128. {}
  129. inline named_sharable_mutex::named_sharable_mutex
  130. (create_only_t, const char *name, const permissions &perm)
  131. : m_shmem (create_only
  132. ,name
  133. ,sizeof(interprocess_sharable_mutex) +
  134. open_create_impl_t::ManagedOpenOrCreateUserOffset
  135. ,read_write
  136. ,0
  137. ,construct_func_t(ipcdetail::DoCreate)
  138. ,perm)
  139. {}
  140. inline named_sharable_mutex::named_sharable_mutex
  141. (open_or_create_t, const char *name, const permissions &perm)
  142. : m_shmem (open_or_create
  143. ,name
  144. ,sizeof(interprocess_sharable_mutex) +
  145. open_create_impl_t::ManagedOpenOrCreateUserOffset
  146. ,read_write
  147. ,0
  148. ,construct_func_t(ipcdetail::DoOpenOrCreate)
  149. ,perm)
  150. {}
  151. inline named_sharable_mutex::named_sharable_mutex
  152. (open_only_t, const char *name)
  153. : m_shmem (open_only
  154. ,name
  155. ,read_write
  156. ,0
  157. ,construct_func_t(ipcdetail::DoOpen))
  158. {}
  159. inline void named_sharable_mutex::dont_close_on_destruction()
  160. { ipcdetail::interprocess_tester::dont_close_on_destruction(m_shmem); }
  161. inline void named_sharable_mutex::lock()
  162. { this->mutex()->lock(); }
  163. inline void named_sharable_mutex::unlock()
  164. { this->mutex()->unlock(); }
  165. inline bool named_sharable_mutex::try_lock()
  166. { return this->mutex()->try_lock(); }
  167. inline bool named_sharable_mutex::timed_lock
  168. (const boost::posix_time::ptime &abs_time)
  169. { return this->mutex()->timed_lock(abs_time); }
  170. inline void named_sharable_mutex::lock_sharable()
  171. { this->mutex()->lock_sharable(); }
  172. inline void named_sharable_mutex::unlock_sharable()
  173. { this->mutex()->unlock_sharable(); }
  174. inline bool named_sharable_mutex::try_lock_sharable()
  175. { return this->mutex()->try_lock_sharable(); }
  176. inline bool named_sharable_mutex::timed_lock_sharable
  177. (const boost::posix_time::ptime &abs_time)
  178. { return this->mutex()->timed_lock_sharable(abs_time); }
  179. inline bool named_sharable_mutex::remove(const char *name)
  180. { return shared_memory_object::remove(name); }
  181. /// @endcond
  182. } //namespace interprocess {
  183. } //namespace boost {
  184. #include <boost/interprocess/detail/config_end.hpp>
  185. #endif //BOOST_INTERPROCESS_NAMED_SHARABLE_MUTEX_HPP