/Src/Dependencies/Boost/boost/interprocess/sync/named_upgradable_mutex.hpp

http://hadesmem.googlecode.com/ · C++ Header · 372 lines · 163 code · 72 blank · 137 comment · 7 complexity · a20e6bb21d629f321a3b86ff5af84604 MD5 · raw file

  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2005-2009. 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_upgradable_mutex_HPP
  11. #define BOOST_INTERPROCESS_named_upgradable_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_upgradable_mutex.hpp>
  22. #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
  23. #include <boost/interprocess/sync/emulation/named_creation_functor.hpp>
  24. #include <boost/interprocess/permissions.hpp>
  25. //!\file
  26. //!Describes a named upgradable mutex class for inter-process synchronization
  27. namespace boost {
  28. namespace interprocess {
  29. /// @cond
  30. namespace detail{ class interprocess_tester; }
  31. /// @endcond
  32. class named_condition;
  33. //!A upgradable 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 upgradable mutex.
  36. class named_upgradable_mutex
  37. {
  38. /// @cond
  39. //Non-copyable
  40. named_upgradable_mutex();
  41. named_upgradable_mutex(const named_upgradable_mutex &);
  42. named_upgradable_mutex &operator=(const named_upgradable_mutex &);
  43. friend class named_condition;
  44. /// @endcond
  45. public:
  46. //!Creates a global upgradable mutex with a name.
  47. //!If the upgradable mutex can't be created throws interprocess_exception
  48. named_upgradable_mutex(create_only_t create_only, const char *name, const permissions &perm = permissions());
  49. //!Opens or creates a global upgradable mutex with a name, and an initial count.
  50. //!If the upgradable mutex is created, this call is equivalent to
  51. //!named_upgradable_mutex(create_only_t, ...)
  52. //!If the upgradable mutex is already created, this call is equivalent to
  53. //!named_upgradable_mutex(open_only_t, ... ).
  54. named_upgradable_mutex(open_or_create_t open_or_create, const char *name, const permissions &perm = permissions());
  55. //!Opens a global upgradable mutex with a name if that upgradable mutex
  56. //!is previously.
  57. //!created. If it is not previously created this function throws
  58. //!interprocess_exception.
  59. named_upgradable_mutex(open_only_t open_only, const char *name);
  60. //!Destroys *this and indicates that the calling process is finished using
  61. //!the resource. The destructor function will deallocate
  62. //!any system resources allocated by the system for use by this process for
  63. //!this resource. The resource can still be opened again calling
  64. //!the open constructor overload. To erase the resource from the system
  65. //!use remove().
  66. ~named_upgradable_mutex();
  67. //Exclusive locking
  68. //!Effects: The calling thread tries to obtain exclusive ownership of the mutex,
  69. //! and if another thread has exclusive, sharable or upgradable ownership of
  70. //! the mutex, it waits until it can obtain the ownership.
  71. //!Throws: interprocess_exception on error.
  72. void lock();
  73. //!Effects: The calling thread tries to acquire exclusive ownership of the mutex
  74. //! without waiting. If no other thread has exclusive, sharable or upgradable
  75. //! ownership of the mutex this succeeds.
  76. //!Returns: If it can acquire exclusive ownership immediately returns true.
  77. //! If it has to wait, returns false.
  78. //!Throws: interprocess_exception on error.
  79. bool try_lock();
  80. //!Effects: The calling thread tries to acquire exclusive ownership of the mutex
  81. //! waiting if necessary until no other thread has has exclusive, sharable or
  82. //! upgradable ownership of the mutex or abs_time is reached.
  83. //!Returns: If acquires exclusive ownership, returns true. Otherwise returns false.
  84. //!Throws: interprocess_exception on error.
  85. bool timed_lock(const boost::posix_time::ptime &abs_time);
  86. //!Precondition: The thread must have exclusive ownership of the mutex.
  87. //!Effects: The calling thread releases the exclusive ownership of the mutex.
  88. //!Throws: An exception derived from interprocess_exception on error.
  89. void unlock();
  90. //Sharable locking
  91. //!Effects: The calling thread tries to obtain sharable ownership of the mutex,
  92. //! and if another thread has exclusive or upgradable ownership of the mutex,
  93. //! waits until it can obtain the ownership.
  94. //!Throws: interprocess_exception on error.
  95. void lock_sharable();
  96. //!Effects: The calling thread tries to acquire sharable ownership of the mutex
  97. //! without waiting. If no other thread has has exclusive or upgradable ownership
  98. //! of the mutex this succeeds.
  99. //!Returns: If it can acquire sharable ownership immediately returns true. If it
  100. //! has to wait, returns false.
  101. //!Throws: interprocess_exception on error.
  102. bool try_lock_sharable();
  103. //!Effects: The calling thread tries to acquire sharable ownership of the mutex
  104. //! waiting if necessary until no other thread has has exclusive or upgradable
  105. //! ownership of the mutex or abs_time is reached.
  106. //!Returns: If acquires sharable ownership, returns true. Otherwise returns false.
  107. //!Throws: interprocess_exception on error.
  108. bool timed_lock_sharable(const boost::posix_time::ptime &abs_time);
  109. //!Precondition: The thread must have sharable ownership of the mutex.
  110. //!Effects: The calling thread releases the sharable ownership of the mutex.
  111. //!Throws: An exception derived from interprocess_exception on error.
  112. void unlock_sharable();
  113. //Upgradable locking
  114. //!Effects: The calling thread tries to obtain upgradable ownership of the mutex,
  115. //! and if another thread has exclusive or upgradable ownership of the mutex,
  116. //! waits until it can obtain the ownership.
  117. //!Throws: interprocess_exception on error.
  118. void lock_upgradable();
  119. //!Effects: The calling thread tries to acquire upgradable ownership of the mutex
  120. //! without waiting. If no other thread has has exclusive or upgradable ownership
  121. //! of the mutex this succeeds.
  122. //!Returns: If it can acquire upgradable ownership immediately returns true.
  123. //! If it has to wait, returns false.
  124. //!Throws: interprocess_exception on error.
  125. bool try_lock_upgradable();
  126. //!Effects: The calling thread tries to acquire upgradable ownership of the mutex
  127. //! waiting if necessary until no other thread has has exclusive or upgradable
  128. //! ownership of the mutex or abs_time is reached.
  129. //!Returns: If acquires upgradable ownership, returns true. Otherwise returns false.
  130. //!Throws: interprocess_exception on error.
  131. bool timed_lock_upgradable(const boost::posix_time::ptime &abs_time);
  132. //!Precondition: The thread must have upgradable ownership of the mutex.
  133. //!Effects: The calling thread releases the upgradable ownership of the mutex.
  134. //!Throws: An exception derived from interprocess_exception on error.
  135. void unlock_upgradable();
  136. //Demotions
  137. //!Precondition: The thread must have exclusive ownership of the mutex.
  138. //!Effects: The thread atomically releases exclusive ownership and acquires
  139. //! upgradable ownership. This operation is non-blocking.
  140. //!Throws: An exception derived from interprocess_exception on error.
  141. void unlock_and_lock_upgradable();
  142. //!Precondition: The thread must have exclusive ownership of the mutex.
  143. //!Effects: The thread atomically releases exclusive ownership and acquires
  144. //! sharable ownership. This operation is non-blocking.
  145. //!Throws: An exception derived from interprocess_exception on error.
  146. void unlock_and_lock_sharable();
  147. //!Precondition: The thread must have upgradable ownership of the mutex.
  148. //!Effects: The thread atomically releases upgradable ownership and acquires
  149. //! sharable ownership. This operation is non-blocking.
  150. //!Throws: An exception derived from interprocess_exception on error.
  151. void unlock_upgradable_and_lock_sharable();
  152. //Promotions
  153. //!Precondition: The thread must have upgradable ownership of the mutex.
  154. //!Effects: The thread atomically releases upgradable ownership and acquires
  155. //! exclusive ownership. This operation will block until all threads with
  156. //! sharable ownership release it.
  157. //!Throws: An exception derived from interprocess_exception on error.
  158. void unlock_upgradable_and_lock();
  159. //!Precondition: The thread must have upgradable ownership of the mutex.
  160. //!Effects: The thread atomically releases upgradable ownership and tries to
  161. //! acquire exclusive ownership. This operation will fail if there are threads
  162. //! with sharable ownership, but it will maintain upgradable ownership.
  163. //!Returns: If acquires exclusive ownership, returns true. Otherwise returns false.
  164. //!Throws: An exception derived from interprocess_exception on error.
  165. bool try_unlock_upgradable_and_lock();
  166. //!Precondition: The thread must have upgradable ownership of the mutex.
  167. //!Effects: The thread atomically releases upgradable ownership and tries to acquire
  168. //! exclusive ownership, waiting if necessary until abs_time. This operation will
  169. //! fail if there are threads with sharable ownership or timeout reaches, but it
  170. //! will maintain upgradable ownership.
  171. //!Returns: If acquires exclusive ownership, returns true. Otherwise returns false.
  172. //!Throws: An exception derived from interprocess_exception on error.
  173. bool timed_unlock_upgradable_and_lock(const boost::posix_time::ptime &abs_time);
  174. //!Precondition: The thread must have sharable ownership of the mutex.
  175. //!Effects: The thread atomically releases sharable ownership and tries to acquire
  176. //! exclusive ownership. This operation will fail if there are threads with sharable
  177. //! or upgradable ownership, but it will maintain sharable ownership.
  178. //!Returns: If acquires exclusive ownership, returns true. Otherwise returns false.
  179. //!Throws: An exception derived from interprocess_exception on error.
  180. bool try_unlock_sharable_and_lock();
  181. bool try_unlock_sharable_and_lock_upgradable();
  182. //!Erases a named upgradable mutex from the system.
  183. //!Returns false on error. Never throws.
  184. static bool remove(const char *name);
  185. /// @cond
  186. private:
  187. friend class detail::interprocess_tester;
  188. void dont_close_on_destruction();
  189. interprocess_upgradable_mutex *mutex() const
  190. { return static_cast<interprocess_upgradable_mutex*>(m_shmem.get_user_address()); }
  191. detail::managed_open_or_create_impl<shared_memory_object> m_shmem;
  192. typedef detail::named_creation_functor<interprocess_upgradable_mutex> construct_func_t;
  193. /// @endcond
  194. };
  195. /// @cond
  196. inline named_upgradable_mutex::~named_upgradable_mutex()
  197. {}
  198. inline named_upgradable_mutex::named_upgradable_mutex
  199. (create_only_t, const char *name, const permissions &perm)
  200. : m_shmem (create_only
  201. ,name
  202. ,sizeof(interprocess_upgradable_mutex) +
  203. detail::managed_open_or_create_impl<shared_memory_object>::
  204. ManagedOpenOrCreateUserOffset
  205. ,read_write
  206. ,0
  207. ,construct_func_t(detail::DoCreate)
  208. ,perm)
  209. {}
  210. inline named_upgradable_mutex::named_upgradable_mutex
  211. (open_or_create_t, const char *name, const permissions &perm)
  212. : m_shmem (open_or_create
  213. ,name
  214. ,sizeof(interprocess_upgradable_mutex) +
  215. detail::managed_open_or_create_impl<shared_memory_object>::
  216. ManagedOpenOrCreateUserOffset
  217. ,read_write
  218. ,0
  219. ,construct_func_t(detail::DoOpenOrCreate)
  220. ,perm)
  221. {}
  222. inline named_upgradable_mutex::named_upgradable_mutex
  223. (open_only_t, const char *name)
  224. : m_shmem (open_only
  225. ,name
  226. ,read_write
  227. ,0
  228. ,construct_func_t(detail::DoOpen))
  229. {}
  230. inline void named_upgradable_mutex::dont_close_on_destruction()
  231. { detail::interprocess_tester::dont_close_on_destruction(m_shmem); }
  232. inline void named_upgradable_mutex::lock()
  233. { this->mutex()->lock(); }
  234. inline void named_upgradable_mutex::unlock()
  235. { this->mutex()->unlock(); }
  236. inline bool named_upgradable_mutex::try_lock()
  237. { return this->mutex()->try_lock(); }
  238. inline bool named_upgradable_mutex::timed_lock
  239. (const boost::posix_time::ptime &abs_time)
  240. {
  241. if(abs_time == boost::posix_time::pos_infin){
  242. this->lock();
  243. return true;
  244. }
  245. return this->mutex()->timed_lock(abs_time);
  246. }
  247. inline void named_upgradable_mutex::lock_upgradable()
  248. { this->mutex()->lock_upgradable(); }
  249. inline void named_upgradable_mutex::unlock_upgradable()
  250. { this->mutex()->unlock_upgradable(); }
  251. inline bool named_upgradable_mutex::try_lock_upgradable()
  252. { return this->mutex()->try_lock_upgradable(); }
  253. inline bool named_upgradable_mutex::timed_lock_upgradable
  254. (const boost::posix_time::ptime &abs_time)
  255. {
  256. if(abs_time == boost::posix_time::pos_infin){
  257. this->lock_upgradable();
  258. return true;
  259. }
  260. return this->mutex()->timed_lock_upgradable(abs_time);
  261. }
  262. inline void named_upgradable_mutex::lock_sharable()
  263. { this->mutex()->lock_sharable(); }
  264. inline void named_upgradable_mutex::unlock_sharable()
  265. { this->mutex()->unlock_sharable(); }
  266. inline bool named_upgradable_mutex::try_lock_sharable()
  267. { return this->mutex()->try_lock_sharable(); }
  268. inline bool named_upgradable_mutex::timed_lock_sharable
  269. (const boost::posix_time::ptime &abs_time)
  270. {
  271. if(abs_time == boost::posix_time::pos_infin){
  272. this->lock_sharable();
  273. return true;
  274. }
  275. return this->mutex()->timed_lock_sharable(abs_time);
  276. }
  277. inline void named_upgradable_mutex::unlock_and_lock_upgradable()
  278. { this->mutex()->unlock_and_lock_upgradable(); }
  279. inline void named_upgradable_mutex::unlock_and_lock_sharable()
  280. { this->mutex()->unlock_and_lock_sharable(); }
  281. inline void named_upgradable_mutex::unlock_upgradable_and_lock_sharable()
  282. { this->mutex()->unlock_upgradable_and_lock_sharable(); }
  283. inline void named_upgradable_mutex::unlock_upgradable_and_lock()
  284. { this->mutex()->unlock_upgradable_and_lock(); }
  285. inline bool named_upgradable_mutex::try_unlock_upgradable_and_lock()
  286. { return this->mutex()->try_unlock_upgradable_and_lock(); }
  287. inline bool named_upgradable_mutex::timed_unlock_upgradable_and_lock
  288. (const boost::posix_time::ptime &abs_time)
  289. { return this->mutex()->timed_unlock_upgradable_and_lock(abs_time); }
  290. inline bool named_upgradable_mutex::try_unlock_sharable_and_lock()
  291. { return this->mutex()->try_unlock_sharable_and_lock(); }
  292. inline bool named_upgradable_mutex::try_unlock_sharable_and_lock_upgradable()
  293. { return this->mutex()->try_unlock_sharable_and_lock_upgradable(); }
  294. inline bool named_upgradable_mutex::remove(const char *name)
  295. { return shared_memory_object::remove(name); }
  296. /// @endcond
  297. } //namespace interprocess {
  298. } //namespace boost {
  299. #include <boost/interprocess/detail/config_end.hpp>
  300. #endif //BOOST_INTERPROCESS_named_upgradable_mutex_HPP