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

http://hadesmem.googlecode.com/ · C++ Header · 235 lines · 136 code · 49 blank · 50 comment · 5 complexity · 8a7720fde65a5c2940986535ca9f0aa5 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_MUTEX_HPP
  11. #define BOOST_INTERPROCESS_NAMED_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/sync/emulation/named_creation_functor.hpp>
  20. #include <boost/interprocess/detail/interprocess_tester.hpp>
  21. #include <boost/interprocess/permissions.hpp>
  22. #if defined(BOOST_INTERPROCESS_NAMED_MUTEX_USES_POSIX_SEMAPHORES)
  23. #include <boost/interprocess/sync/posix/semaphore_wrapper.hpp>
  24. #else
  25. #include <boost/interprocess/shared_memory_object.hpp>
  26. #include <boost/interprocess/sync/interprocess_mutex.hpp>
  27. #include <boost/interprocess/detail/managed_open_or_create_impl.hpp>
  28. #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
  29. #endif
  30. //!\file
  31. //!Describes a named mutex class for inter-process synchronization
  32. namespace boost {
  33. namespace interprocess {
  34. class named_condition;
  35. //!A mutex with a global name, so it can be found from different
  36. //!processes. This mutex can't be placed in shared memory, and
  37. //!each process should have it's own named_mutex.
  38. class named_mutex
  39. {
  40. /// @cond
  41. //Non-copyable
  42. named_mutex();
  43. named_mutex(const named_mutex &);
  44. named_mutex &operator=(const named_mutex &);
  45. friend class named_condition;
  46. /// @endcond
  47. public:
  48. //!Creates a global interprocess_mutex with a name.
  49. //!Throws interprocess_exception on error.
  50. named_mutex(create_only_t create_only, const char *name, const permissions &perm = permissions());
  51. //!Opens or creates a global mutex with a name.
  52. //!If the mutex is created, this call is equivalent to
  53. //!named_mutex(create_only_t, ... )
  54. //!If the mutex is already created, this call is equivalent
  55. //!named_mutex(open_only_t, ... )
  56. //!Does not throw
  57. named_mutex(open_or_create_t open_or_create, const char *name, const permissions &perm = permissions());
  58. //!Opens a global mutex with a name if that mutex is previously
  59. //!created. If it is not previously created this function throws
  60. //!interprocess_exception.
  61. named_mutex(open_only_t open_only, const char *name);
  62. //!Destroys *this and indicates that the calling process is finished using
  63. //!the resource. The destructor function will deallocate
  64. //!any system resources allocated by the system for use by this process for
  65. //!this resource. The resource can still be opened again calling
  66. //!the open constructor overload. To erase the resource from the system
  67. //!use remove().
  68. ~named_mutex();
  69. //!Unlocks a previously locked
  70. //!interprocess_mutex.
  71. void unlock();
  72. //!Locks interprocess_mutex, sleeps when interprocess_mutex is already locked.
  73. //!Throws interprocess_exception if a severe error is found
  74. void lock();
  75. //!Tries to lock the interprocess_mutex, returns false when interprocess_mutex
  76. //!is already locked, returns true when success.
  77. //!Throws interprocess_exception if a severe error is found
  78. bool try_lock();
  79. //!Tries to lock the interprocess_mutex until time abs_time,
  80. //!Returns false when timeout expires, returns true when locks.
  81. //!Throws interprocess_exception if a severe error is found
  82. bool timed_lock(const boost::posix_time::ptime &abs_time);
  83. //!Erases a named mutex from the system.
  84. //!Returns false on error. Never throws.
  85. static bool remove(const char *name);
  86. /// @cond
  87. private:
  88. friend class detail::interprocess_tester;
  89. void dont_close_on_destruction();
  90. #if defined(BOOST_INTERPROCESS_NAMED_MUTEX_USES_POSIX_SEMAPHORES)
  91. detail::named_semaphore_wrapper m_sem;
  92. #else
  93. interprocess_mutex *mutex() const
  94. { return static_cast<interprocess_mutex*>(m_shmem.get_user_address()); }
  95. detail::managed_open_or_create_impl<shared_memory_object> m_shmem;
  96. typedef detail::named_creation_functor<interprocess_mutex> construct_func_t;
  97. #endif
  98. /// @endcond
  99. };
  100. /// @cond
  101. #if defined(BOOST_INTERPROCESS_NAMED_MUTEX_USES_POSIX_SEMAPHORES)
  102. inline named_mutex::named_mutex(create_only_t, const char *name, const permissions &perm)
  103. : m_sem(detail::DoCreate, name, 1, perm)
  104. {}
  105. inline named_mutex::named_mutex(open_or_create_t, const char *name, const permissions &perm)
  106. : m_sem(detail::DoOpenOrCreate, name, 1, perm)
  107. {}
  108. inline named_mutex::named_mutex(open_only_t, const char *name)
  109. : m_sem(detail::DoOpen, name, 1, permissions())
  110. {}
  111. inline void named_mutex::dont_close_on_destruction()
  112. { detail::interprocess_tester::dont_close_on_destruction(m_sem); }
  113. inline named_mutex::~named_mutex()
  114. {}
  115. inline void named_mutex::lock()
  116. { m_sem.wait(); }
  117. inline void named_mutex::unlock()
  118. { m_sem.post(); }
  119. inline bool named_mutex::try_lock()
  120. { return m_sem.try_wait(); }
  121. inline bool named_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
  122. {
  123. if(abs_time == boost::posix_time::pos_infin){
  124. this->lock();
  125. return true;
  126. }
  127. return m_sem.timed_wait(abs_time);
  128. }
  129. inline bool named_mutex::remove(const char *name)
  130. { return detail::named_semaphore_wrapper::remove(name); }
  131. #else
  132. inline void named_mutex::dont_close_on_destruction()
  133. { detail::interprocess_tester::dont_close_on_destruction(m_shmem); }
  134. inline named_mutex::~named_mutex()
  135. {}
  136. inline named_mutex::named_mutex(create_only_t, const char *name, const permissions &perm)
  137. : m_shmem (create_only
  138. ,name
  139. ,sizeof(interprocess_mutex) +
  140. detail::managed_open_or_create_impl<shared_memory_object>::
  141. ManagedOpenOrCreateUserOffset
  142. ,read_write
  143. ,0
  144. ,construct_func_t(detail::DoCreate)
  145. ,perm)
  146. {}
  147. inline named_mutex::named_mutex(open_or_create_t, const char *name, const permissions &perm)
  148. : m_shmem (open_or_create
  149. ,name
  150. ,sizeof(interprocess_mutex) +
  151. detail::managed_open_or_create_impl<shared_memory_object>::
  152. ManagedOpenOrCreateUserOffset
  153. ,read_write
  154. ,0
  155. ,construct_func_t(detail::DoOpenOrCreate)
  156. ,perm)
  157. {}
  158. inline named_mutex::named_mutex(open_only_t, const char *name)
  159. : m_shmem (open_only
  160. ,name
  161. ,read_write
  162. ,0
  163. ,construct_func_t(detail::DoOpen))
  164. {}
  165. inline void named_mutex::lock()
  166. { this->mutex()->lock(); }
  167. inline void named_mutex::unlock()
  168. { this->mutex()->unlock(); }
  169. inline bool named_mutex::try_lock()
  170. { return this->mutex()->try_lock(); }
  171. inline bool named_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
  172. {
  173. if(abs_time == boost::posix_time::pos_infin){
  174. this->lock();
  175. return true;
  176. }
  177. return this->mutex()->timed_lock(abs_time);
  178. }
  179. inline bool named_mutex::remove(const char *name)
  180. { return shared_memory_object::remove(name); }
  181. #endif
  182. /// @endcond
  183. } //namespace interprocess {
  184. } //namespace boost {
  185. #include <boost/interprocess/detail/config_end.hpp>
  186. #endif //BOOST_INTERPROCESS_NAMED_MUTEX_HPP