interprocess_mutex.hpp 6.2 KB

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