interprocess_recursive_mutex.hpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. //
  11. // Parts of the pthread code come from Boost Threads code:
  12. //
  13. //////////////////////////////////////////////////////////////////////////////
  14. //
  15. // Copyright (C) 2001-2003
  16. // William E. Kempf
  17. //
  18. // Permission to use, copy, modify, distribute and sell this software
  19. // and its documentation for any purpose is hereby granted without fee,
  20. // provided that the above copyright notice appear in all copies and
  21. // that both that copyright notice and this permission notice appear
  22. // in supporting documentation. William E. Kempf makes no representations
  23. // about the suitability of this software for any purpose.
  24. // It is provided "as is" without express or implied warranty.
  25. //////////////////////////////////////////////////////////////////////////////
  26. #ifndef BOOST_INTERPROCESS_RECURSIVE_MUTEX_HPP
  27. #define BOOST_INTERPROCESS_RECURSIVE_MUTEX_HPP
  28. /// @cond
  29. #if (defined _MSC_VER) && (_MSC_VER >= 1200)
  30. # pragma once
  31. #endif
  32. #include <boost/interprocess/detail/config_begin.hpp>
  33. #include <boost/interprocess/detail/workaround.hpp>
  34. #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
  35. #include <boost/assert.hpp>
  36. #if !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && \
  37. (defined(BOOST_INTERPROCESS_POSIX_PROCESS_SHARED) && defined (BOOST_INTERPROCESS_POSIX_RECURSIVE_MUTEXES))
  38. #include <boost/interprocess/sync/posix/recursive_mutex.hpp>
  39. #define BOOST_INTERPROCESS_USE_POSIX
  40. //Experimental...
  41. #elif !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_WINDOWS)
  42. #include <boost/interprocess/sync/windows/recursive_mutex.hpp>
  43. #define BOOST_INTERPROCESS_USE_WINDOWS
  44. #elif !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  45. #include <boost/interprocess/sync/spin/recursive_mutex.hpp>
  46. #define BOOST_INTERPROCESS_USE_GENERIC_EMULATION
  47. #endif
  48. #if defined (BOOST_INTERPROCESS_USE_GENERIC_EMULATION)
  49. namespace boost {
  50. namespace interprocess {
  51. namespace ipcdetail{
  52. namespace robust_emulation_helpers {
  53. template<class T>
  54. class mutex_traits;
  55. }}}}
  56. #endif
  57. /// @endcond
  58. //!\file
  59. //!Describes interprocess_recursive_mutex and shared_recursive_try_mutex classes
  60. namespace boost {
  61. namespace interprocess {
  62. //!Wraps a interprocess_mutex that can be placed in shared memory and can be
  63. //!shared between processes. Allows several locking calls by the same
  64. //!process. Allows timed lock tries
  65. class interprocess_recursive_mutex
  66. {
  67. /// @cond
  68. //Non-copyable
  69. interprocess_recursive_mutex(const interprocess_recursive_mutex &);
  70. interprocess_recursive_mutex &operator=(const interprocess_recursive_mutex &);
  71. /// @endcond
  72. public:
  73. //!Constructor.
  74. //!Throws interprocess_exception on error.
  75. interprocess_recursive_mutex();
  76. //!Destructor. If any process uses the mutex after the destructor is called
  77. //!the result is undefined. Does not throw.
  78. ~interprocess_recursive_mutex();
  79. //!Effects: The calling thread tries to obtain ownership of the mutex, and
  80. //! if another thread has ownership of the mutex, it waits until it can
  81. //! obtain the ownership. If a thread takes ownership of the mutex the
  82. //! mutex must be unlocked by the same mutex. The mutex must be unlocked
  83. //! the same number of times it is locked.
  84. //!Throws: interprocess_exception on error.
  85. void lock();
  86. //!Tries to lock the interprocess_mutex, returns false when interprocess_mutex
  87. //!is already locked, returns true when success. The mutex must be unlocked
  88. //!the same number of times it is locked.
  89. //!Throws: interprocess_exception if a severe error is found
  90. bool try_lock();
  91. //!Tries to lock the interprocess_mutex, if interprocess_mutex can't be locked before
  92. //!abs_time time, returns false. The mutex must be unlocked
  93. //! the same number of times it is locked.
  94. //!Throws: interprocess_exception if a severe error is found
  95. bool timed_lock(const boost::posix_time::ptime &abs_time);
  96. //!Effects: The calling thread releases the exclusive ownership of the mutex.
  97. //! If the mutex supports recursive locking, the mutex must be unlocked the
  98. //! same number of times it is locked.
  99. //!Throws: interprocess_exception on error.
  100. void unlock();
  101. /// @cond
  102. private:
  103. #if defined (BOOST_INTERPROCESS_USE_GENERIC_EMULATION)
  104. #undef BOOST_INTERPROCESS_USE_GENERIC_EMULATION
  105. void take_ownership(){ mutex.take_ownership(); }
  106. friend class ipcdetail::robust_emulation_helpers::mutex_traits<interprocess_recursive_mutex>;
  107. ipcdetail::spin_recursive_mutex mutex;
  108. #elif defined(BOOST_INTERPROCESS_USE_POSIX)
  109. #undef BOOST_INTERPROCESS_USE_POSIX
  110. ipcdetail::posix_recursive_mutex mutex;
  111. #elif defined(BOOST_INTERPROCESS_USE_WINDOWS)
  112. #undef BOOST_INTERPROCESS_USE_WINDOWS
  113. ipcdetail::windows_recursive_mutex mutex;
  114. #else
  115. #error "Unknown platform for interprocess_mutex"
  116. #endif
  117. /// @endcond
  118. };
  119. } //namespace interprocess {
  120. } //namespace boost {
  121. namespace boost {
  122. namespace interprocess {
  123. inline interprocess_recursive_mutex::interprocess_recursive_mutex(){}
  124. inline interprocess_recursive_mutex::~interprocess_recursive_mutex(){}
  125. inline void interprocess_recursive_mutex::lock()
  126. {
  127. #ifdef BOOST_INTERPROCESS_ENABLE_TIMEOUT_WHEN_LOCKING
  128. boost::posix_time::ptime wait_time
  129. = boost::posix_time::microsec_clock::universal_time()
  130. + boost::posix_time::milliseconds(BOOST_INTERPROCESS_TIMEOUT_WHEN_LOCKING_DURATION_MS);
  131. if (!mutex.timed_lock(wait_time)){
  132. throw interprocess_exception(timeout_when_locking_error, "Interprocess mutex timeout when locking. Possible deadlock: owner died without unlocking?");
  133. }
  134. #else
  135. mutex.lock();
  136. #endif
  137. }
  138. inline bool interprocess_recursive_mutex::try_lock()
  139. { return mutex.try_lock(); }
  140. inline bool interprocess_recursive_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
  141. { return mutex.timed_lock(abs_time); }
  142. inline void interprocess_recursive_mutex::unlock()
  143. { mutex.unlock(); }
  144. } //namespace interprocess {
  145. } //namespace boost {
  146. #include <boost/interprocess/detail/config_end.hpp>
  147. #endif //BOOST_INTERPROCESS_RECURSIVE_MUTEX_HPP