interprocess_recursive_mutex.hpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  29. #ifndef BOOST_CONFIG_HPP
  30. # include <boost/config.hpp>
  31. #endif
  32. #
  33. #if defined(BOOST_HAS_PRAGMA_ONCE)
  34. # pragma once
  35. #endif
  36. #include <boost/interprocess/detail/config_begin.hpp>
  37. #include <boost/interprocess/detail/workaround.hpp>
  38. #include <boost/interprocess/sync/detail/common_algorithms.hpp>
  39. #include <boost/assert.hpp>
  40. #if !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && \
  41. defined(BOOST_INTERPROCESS_POSIX_PROCESS_SHARED) && \
  42. defined (BOOST_INTERPROCESS_POSIX_RECURSIVE_MUTEXES)
  43. #include <boost/interprocess/sync/posix/recursive_mutex.hpp>
  44. #define BOOST_INTERPROCESS_RECURSIVE_MUTEX_USE_POSIX
  45. #elif !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_WINDOWS)
  46. //Experimental...
  47. #include <boost/interprocess/sync/windows/recursive_mutex.hpp>
  48. #define BOOST_INTERPROCESS_RECURSIVE_MUTEX_USE_WINAPI
  49. #else
  50. //spin_recursive_mutex is used
  51. #include <boost/interprocess/sync/spin/recursive_mutex.hpp>
  52. namespace boost {
  53. namespace interprocess {
  54. namespace ipcdetail{
  55. namespace robust_emulation_helpers {
  56. template<class T>
  57. class mutex_traits;
  58. }}}}
  59. #endif
  60. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  61. //!\file
  62. //!Describes interprocess_recursive_mutex and shared_recursive_try_mutex classes
  63. namespace boost {
  64. namespace interprocess {
  65. //!Wraps a interprocess_mutex that can be placed in shared memory and can be
  66. //!shared between processes. Allows several locking calls by the same
  67. //!process. Allows timed lock tries
  68. class interprocess_recursive_mutex
  69. {
  70. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  71. //Non-copyable
  72. interprocess_recursive_mutex(const interprocess_recursive_mutex &);
  73. interprocess_recursive_mutex &operator=(const interprocess_recursive_mutex &);
  74. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  75. public:
  76. //!Constructor.
  77. //!Throws interprocess_exception on error.
  78. interprocess_recursive_mutex();
  79. //!Destructor. If any process uses the mutex after the destructor is called
  80. //!the result is undefined. Does not throw.
  81. ~interprocess_recursive_mutex();
  82. //!Effects: The calling thread tries to obtain ownership of the mutex, and
  83. //! if another thread has ownership of the mutex, it waits until it can
  84. //! obtain the ownership. If a thread takes ownership of the mutex the
  85. //! mutex must be unlocked by the same mutex. The mutex must be unlocked
  86. //! the same number of times it is locked.
  87. //!Throws: interprocess_exception on error.
  88. //!
  89. //!Note: A program shall not deadlock if the thread that has ownership calls
  90. //! this function.
  91. void lock();
  92. //!Tries to lock the interprocess_mutex, returns false when interprocess_mutex
  93. //!is already locked, returns true when success. The mutex must be unlocked
  94. //!the same number of times it is locked.
  95. //!Throws: interprocess_exception if a severe error is found
  96. //!
  97. //!Note: A program shall not deadlock if the thread that has ownership calls
  98. //! this function.
  99. bool try_lock();
  100. //!Tries to lock the interprocess_mutex, if interprocess_mutex can't be locked before
  101. //!abs_time time, returns false. The mutex must be unlocked
  102. //! the same number of times it is locked.
  103. //!Throws: interprocess_exception if a severe error is found
  104. //!
  105. //!Note: A program shall not deadlock if the thread that has ownership calls
  106. //! this function.
  107. template<class TimePoint>
  108. bool timed_lock(const TimePoint &abs_time);
  109. //!Same as `timed_lock`, but this function is modeled after the
  110. //!standard library interface.
  111. template<class TimePoint> bool try_lock_until(const TimePoint &abs_time)
  112. { return this->timed_lock(abs_time); }
  113. //!Same as `timed_lock`, but this function is modeled after the
  114. //!standard library interface.
  115. template<class Duration> bool try_lock_for(const Duration &dur)
  116. { return this->timed_lock(ipcdetail::duration_to_ustime(dur)); }
  117. //!Effects: The calling thread releases the exclusive ownership of the mutex.
  118. //! If the mutex supports recursive locking, the mutex must be unlocked the
  119. //! same number of times it is locked.
  120. //!Throws: interprocess_exception on error.
  121. void unlock();
  122. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  123. private:
  124. #if defined(BOOST_INTERPROCESS_RECURSIVE_MUTEX_USE_POSIX)
  125. ipcdetail::posix_recursive_mutex mutex;
  126. #elif defined(BOOST_INTERPROCESS_RECURSIVE_MUTEX_USE_WINAPI)
  127. ipcdetail::winapi_recursive_mutex mutex;
  128. #else
  129. void take_ownership(){ mutex.take_ownership(); }
  130. friend class ipcdetail::robust_emulation_helpers::mutex_traits<interprocess_recursive_mutex>;
  131. ipcdetail::spin_recursive_mutex mutex;
  132. #endif
  133. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  134. };
  135. } //namespace interprocess {
  136. } //namespace boost {
  137. namespace boost {
  138. namespace interprocess {
  139. inline interprocess_recursive_mutex::interprocess_recursive_mutex(){}
  140. inline interprocess_recursive_mutex::~interprocess_recursive_mutex(){}
  141. inline void interprocess_recursive_mutex::lock()
  142. { ipcdetail::timeout_when_locking_aware_lock(mutex); }
  143. inline bool interprocess_recursive_mutex::try_lock()
  144. { return mutex.try_lock(); }
  145. template<class TimePoint>
  146. inline bool interprocess_recursive_mutex::timed_lock(const TimePoint &abs_time)
  147. { return mutex.timed_lock(abs_time); }
  148. inline void interprocess_recursive_mutex::unlock()
  149. { mutex.unlock(); }
  150. } //namespace interprocess {
  151. } //namespace boost {
  152. #include <boost/interprocess/detail/config_end.hpp>
  153. #endif //BOOST_INTERPROCESS_RECURSIVE_MUTEX_HPP