interprocess_mutex.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. #include <boost/interprocess/sync/detail/common_algorithms.hpp>
  30. #if !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_POSIX_PROCESS_SHARED)
  31. #include <boost/interprocess/sync/posix/mutex.hpp>
  32. #define BOOST_INTERPROCESS_MUTEX_USE_POSIX
  33. #elif !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_WINDOWS)
  34. //Experimental...
  35. #define BOOST_INTERPROCESS_MUTEX_USE_WINAPI
  36. #include <boost/interprocess/sync/windows/mutex.hpp>
  37. #else
  38. //spin_mutex is used
  39. #include <boost/interprocess/sync/spin/mutex.hpp>
  40. namespace boost {
  41. namespace interprocess {
  42. namespace ipcdetail{
  43. namespace robust_emulation_helpers {
  44. template<class T>
  45. class mutex_traits;
  46. }}}}
  47. #endif
  48. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  49. //!\file
  50. //!Describes a mutex class that can be placed in memory shared by
  51. //!several processes.
  52. namespace boost {
  53. namespace interprocess {
  54. class interprocess_condition;
  55. //!Wraps a interprocess_mutex that can be placed in shared memory and can be
  56. //!shared between processes. Allows timed lock tries
  57. class interprocess_mutex
  58. {
  59. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  60. //Non-copyable
  61. interprocess_mutex(const interprocess_mutex &);
  62. interprocess_mutex &operator=(const interprocess_mutex &);
  63. friend class interprocess_condition;
  64. public:
  65. #if defined(BOOST_INTERPROCESS_MUTEX_USE_POSIX)
  66. typedef ipcdetail::posix_mutex internal_mutex_type;
  67. #elif defined(BOOST_INTERPROCESS_MUTEX_USE_WINAPI)
  68. typedef ipcdetail::winapi_mutex internal_mutex_type;
  69. #else
  70. typedef ipcdetail::spin_mutex internal_mutex_type;
  71. private:
  72. friend class ipcdetail::robust_emulation_helpers::mutex_traits<interprocess_mutex>;
  73. void take_ownership(){ m_mutex.take_ownership(); }
  74. public:
  75. #endif
  76. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  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. //!Requires: The calling thread does not own the mutex.
  85. //!
  86. //!Effects: The calling thread tries to obtain ownership of the mutex, and
  87. //! if another thread has ownership of the mutex, it waits until it can
  88. //! obtain the ownership. If a thread takes ownership of the mutex the
  89. //! mutex must be unlocked by the same mutex.
  90. //!Throws: interprocess_exception on error.
  91. //!
  92. //!Note: A program may deadlock if the thread that has ownership calls
  93. //! this function. If the implementation can detect the deadlock,
  94. //! an exception could be thrown.
  95. void lock();
  96. //!Requires: The calling thread does not own the mutex.
  97. //!
  98. //!Effects: The calling thread tries to obtain ownership of the mutex, and
  99. //! if another thread has ownership of the mutex returns immediately.
  100. //!Returns: If the thread acquires ownership of the mutex, returns true, if
  101. //! the another thread has ownership of the mutex, returns false.
  102. //!Throws: interprocess_exception on error.
  103. //!
  104. //!Note: A program may deadlock if the thread that has ownership calls
  105. //! this function. If the implementation can detect the deadlock,
  106. //! an exception could be thrown.
  107. bool try_lock();
  108. //!Requires: The calling thread does not own the mutex.
  109. //!
  110. //!Effects: The calling thread will try to obtain exclusive ownership of the
  111. //! mutex if it can do so in until the specified time is reached. If the
  112. //! mutex supports recursive locking, the mutex must be unlocked the same
  113. //! number of times it is locked.
  114. //!Returns: If the thread acquires ownership of the mutex, returns true, if
  115. //! the timeout expires returns false.
  116. //!Throws: interprocess_exception on error.
  117. //!
  118. //!Note: A program may deadlock if the thread that has ownership calls
  119. //! this function. If the implementation can detect the deadlock,
  120. //! an exception could be thrown.
  121. bool timed_lock(const boost::posix_time::ptime &abs_time);
  122. //!Effects: The calling thread releases the exclusive ownership of the mutex.
  123. //!Throws: interprocess_exception on error.
  124. void unlock();
  125. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  126. internal_mutex_type &internal_mutex()
  127. { return m_mutex; }
  128. const internal_mutex_type &internal_mutex() const
  129. { return m_mutex; }
  130. private:
  131. internal_mutex_type m_mutex;
  132. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  133. };
  134. } //namespace interprocess {
  135. } //namespace boost {
  136. namespace boost {
  137. namespace interprocess {
  138. inline interprocess_mutex::interprocess_mutex(){}
  139. inline interprocess_mutex::~interprocess_mutex(){}
  140. inline void interprocess_mutex::lock()
  141. { ipcdetail::timeout_when_locking_aware_lock(m_mutex); }
  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