named_sharable_mutex.hpp 9.0 KB

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