interprocess_mutex.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. #ifndef BOOST_INTERPROCESS_MUTEX_HPP
  15. #define BOOST_INTERPROCESS_MUTEX_HPP
  16. /// @cond
  17. #if (defined _MSC_VER) && (_MSC_VER >= 1200)
  18. # pragma once
  19. #endif
  20. #include <boost/interprocess/detail/config_begin.hpp>
  21. #include <boost/interprocess/exceptions.hpp>
  22. #include <boost/interprocess/detail/workaround.hpp>
  23. #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
  24. #include <boost/assert.hpp>
  25. #if !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_POSIX_PROCESS_SHARED)
  26. #include <boost/interprocess/sync/posix/mutex.hpp>
  27. #define BOOST_INTERPROCESS_USE_POSIX
  28. //Experimental...
  29. #elif !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_WINDOWS)
  30. #include <boost/interprocess/sync/windows/mutex.hpp>
  31. #define BOOST_INTERPROCESS_USE_WINDOWS
  32. #elif !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  33. #include <boost/interprocess/sync/spin/mutex.hpp>
  34. #define BOOST_INTERPROCESS_USE_GENERIC_EMULATION
  35. namespace boost {
  36. namespace interprocess {
  37. namespace ipcdetail{
  38. namespace robust_emulation_helpers {
  39. template<class T>
  40. class mutex_traits;
  41. }}}}
  42. #endif
  43. /// @endcond
  44. //!\file
  45. //!Describes a mutex class that can be placed in memory shared by
  46. //!several processes.
  47. namespace boost {
  48. namespace interprocess {
  49. class interprocess_condition;
  50. //!Wraps a interprocess_mutex that can be placed in shared memory and can be
  51. //!shared between processes. Allows timed lock tries
  52. class interprocess_mutex
  53. {
  54. /// @cond
  55. //Non-copyable
  56. interprocess_mutex(const interprocess_mutex &);
  57. interprocess_mutex &operator=(const interprocess_mutex &);
  58. friend class interprocess_condition;
  59. public:
  60. #if defined(BOOST_INTERPROCESS_USE_GENERIC_EMULATION)
  61. #undef BOOST_INTERPROCESS_USE_GENERIC_EMULATION
  62. typedef ipcdetail::spin_mutex internal_mutex_type;
  63. private:
  64. friend class ipcdetail::robust_emulation_helpers::mutex_traits<interprocess_mutex>;
  65. void take_ownership(){ m_mutex.take_ownership(); }
  66. public:
  67. #elif defined(BOOST_INTERPROCESS_USE_POSIX)
  68. #undef BOOST_INTERPROCESS_USE_POSIX
  69. typedef ipcdetail::posix_mutex internal_mutex_type;
  70. #elif defined(BOOST_INTERPROCESS_USE_WINDOWS)
  71. #undef BOOST_INTERPROCESS_USE_WINDOWS
  72. typedef ipcdetail::windows_mutex internal_mutex_type;
  73. #else
  74. #error "Unknown platform for interprocess_mutex"
  75. #endif
  76. /// @endcond
  77. public:
  78. //!Constructor.
  79. //!Throws interprocess_exception on error.
  80. interprocess_mutex();
  81. //!Destructor. If any process uses the mutex after the destructor is called
  82. //!the result is undefined. Does not throw.
  83. ~interprocess_mutex();
  84. //!Effects: The calling thread tries to obtain ownership of the mutex, and
  85. //! if another thread has ownership of the mutex, it waits until it can
  86. //! obtain the ownership. If a thread takes ownership of the mutex the
  87. //! mutex must be unlocked by the same mutex.
  88. //!Throws: interprocess_exception on error.
  89. void lock();
  90. //!Effects: The calling thread tries to obtain ownership of the mutex, and
  91. //! if another thread has ownership of the mutex returns immediately.
  92. //!Returns: If the thread acquires ownership of the mutex, returns true, if
  93. //! the another thread has ownership of the mutex, returns false.
  94. //!Throws: interprocess_exception on error.
  95. bool try_lock();
  96. //!Effects: The calling thread will try to obtain exclusive ownership of the
  97. //! mutex if it can do so in until the specified time is reached. If the
  98. //! mutex supports recursive locking, the mutex must be unlocked the same
  99. //! number of times it is locked.
  100. //!Returns: If the thread acquires ownership of the mutex, returns true, if
  101. //! the timeout expires returns false.
  102. //!Throws: interprocess_exception on error.
  103. bool timed_lock(const boost::posix_time::ptime &abs_time);
  104. //!Effects: The calling thread releases the exclusive ownership of the mutex.
  105. //!Throws: interprocess_exception on error.
  106. void unlock();
  107. /// @cond
  108. internal_mutex_type &internal_mutex()
  109. { return m_mutex; }
  110. const internal_mutex_type &internal_mutex() const
  111. { return m_mutex; }
  112. private:
  113. internal_mutex_type m_mutex;
  114. /// @endcond
  115. };
  116. } //namespace interprocess {
  117. } //namespace boost {
  118. namespace boost {
  119. namespace interprocess {
  120. inline interprocess_mutex::interprocess_mutex(){}
  121. inline interprocess_mutex::~interprocess_mutex(){}
  122. inline void interprocess_mutex::lock()
  123. {
  124. #ifdef BOOST_INTERPROCESS_ENABLE_TIMEOUT_WHEN_LOCKING
  125. boost::posix_time::ptime wait_time
  126. = boost::posix_time::microsec_clock::universal_time()
  127. + boost::posix_time::milliseconds(BOOST_INTERPROCESS_TIMEOUT_WHEN_LOCKING_DURATION_MS);
  128. if (!m_mutex.timed_lock(wait_time))
  129. {
  130. throw interprocess_exception(timeout_when_locking_error
  131. , "Interprocess mutex timeout when locking. Possible deadlock: "
  132. "owner died without unlocking?");
  133. }
  134. #else
  135. m_mutex.lock();
  136. #endif
  137. }
  138. inline bool interprocess_mutex::try_lock()
  139. { return m_mutex.try_lock(); }
  140. inline bool interprocess_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
  141. { return m_mutex.timed_lock(abs_time); }
  142. inline void interprocess_mutex::unlock()
  143. { m_mutex.unlock(); }
  144. } //namespace interprocess {
  145. } //namespace boost {
  146. #include <boost/interprocess/detail/config_end.hpp>
  147. #endif //BOOST_INTERPROCESS_MUTEX_HPP