interprocess_mutex.hpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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/assert.hpp>
  28. #include <boost/interprocess/sync/detail/common_algorithms.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_MUTEX_USE_POSIX
  32. #elif !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_WINDOWS)
  33. //Experimental...
  34. #define BOOST_INTERPROCESS_MUTEX_USE_WINAPI
  35. #include <boost/interprocess/sync/windows/mutex.hpp>
  36. #else
  37. //spin_mutex is used
  38. #include <boost/interprocess/sync/spin/mutex.hpp>
  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_MUTEX_USE_POSIX)
  65. typedef ipcdetail::posix_mutex internal_mutex_type;
  66. #elif defined(BOOST_INTERPROCESS_MUTEX_USE_WINAPI)
  67. typedef ipcdetail::winapi_mutex internal_mutex_type;
  68. #else
  69. typedef ipcdetail::spin_mutex internal_mutex_type;
  70. private:
  71. friend class ipcdetail::robust_emulation_helpers::mutex_traits<interprocess_mutex>;
  72. void take_ownership(){ m_mutex.take_ownership(); }
  73. public:
  74. #endif
  75. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  76. public:
  77. //!Constructor.
  78. //!Throws interprocess_exception on error.
  79. interprocess_mutex();
  80. //!Destructor. If any process uses the mutex after the destructor is called
  81. //!the result is undefined. Does not throw.
  82. ~interprocess_mutex();
  83. //!Requires: The calling thread does not own the mutex.
  84. //!
  85. //!Effects: The calling thread tries to obtain ownership of the mutex, and
  86. //! if another thread has ownership of the mutex, it waits until it can
  87. //! obtain the ownership. If a thread takes ownership of the mutex the
  88. //! mutex must be unlocked by the same mutex.
  89. //!Throws: interprocess_exception on error.
  90. //!
  91. //!Note: A program may deadlock if the thread that has ownership calls
  92. //! this function. If the implementation can detect the deadlock,
  93. //! an exception could be thrown.
  94. void lock();
  95. //!Requires: The calling thread does not own the mutex.
  96. //!
  97. //!Effects: The calling thread tries to obtain ownership of the mutex, and
  98. //! if another thread has ownership of the mutex returns immediately.
  99. //!Returns: If the thread acquires ownership of the mutex, returns true, if
  100. //! the another thread has ownership of the mutex, returns false.
  101. //!Throws: interprocess_exception on error.
  102. //!
  103. //!Note: A program may deadlock if the thread that has ownership calls
  104. //! this function. If the implementation can detect the deadlock,
  105. //! an exception could be thrown.
  106. bool try_lock();
  107. //!Requires: The calling thread does not own the mutex.
  108. //!
  109. //!Effects: The calling thread will try to obtain exclusive ownership of the
  110. //! mutex if it can do so in until the specified time is reached. If the
  111. //! mutex supports recursive locking, the mutex must be unlocked the same
  112. //! number of times it is locked.
  113. //!Returns: If the thread acquires ownership of the mutex, returns true, if
  114. //! the timeout expires returns false.
  115. //!Throws: interprocess_exception on error.
  116. //!
  117. //!Note: A program may deadlock if the thread that has ownership calls
  118. //! this function. If the implementation can detect the deadlock,
  119. //! an exception could be thrown.
  120. template<class TimePoint>
  121. bool timed_lock(const TimePoint &abs_time);
  122. //!Same as `timed_lock`, but this function is modeled after the
  123. //!standard library interface.
  124. template<class TimePoint> bool try_lock_until(const TimePoint &abs_time)
  125. { return this->timed_lock(abs_time); }
  126. //!Same as `timed_lock`, but this function is modeled after the
  127. //!standard library interface.
  128. template<class Duration> bool try_lock_for(const Duration &dur)
  129. { return this->timed_lock(ipcdetail::duration_to_ustime(dur)); }
  130. //!Effects: The calling thread releases the exclusive ownership of the mutex.
  131. //!Throws: interprocess_exception on error.
  132. void unlock();
  133. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  134. internal_mutex_type &internal_mutex()
  135. { return m_mutex; }
  136. const internal_mutex_type &internal_mutex() const
  137. { return m_mutex; }
  138. private:
  139. internal_mutex_type m_mutex;
  140. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  141. };
  142. } //namespace interprocess {
  143. } //namespace boost {
  144. namespace boost {
  145. namespace interprocess {
  146. inline interprocess_mutex::interprocess_mutex(){}
  147. inline interprocess_mutex::~interprocess_mutex(){}
  148. inline void interprocess_mutex::lock()
  149. { ipcdetail::timeout_when_locking_aware_lock(m_mutex); }
  150. inline bool interprocess_mutex::try_lock()
  151. { return m_mutex.try_lock(); }
  152. template <class TimePoint>
  153. inline bool interprocess_mutex::timed_lock(const TimePoint &abs_time)
  154. { return m_mutex.timed_lock(abs_time); }
  155. inline void interprocess_mutex::unlock()
  156. { m_mutex.unlock(); }
  157. } //namespace interprocess {
  158. } //namespace boost {
  159. #include <boost/interprocess/detail/config_end.hpp>
  160. #endif //BOOST_INTERPROCESS_MUTEX_HPP