recursive_mutex.hpp 6.2 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. //
  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_DETAIL_SPIN_RECURSIVE_MUTEX_HPP
  27. #define BOOST_INTERPROCESS_DETAIL_SPIN_RECURSIVE_MUTEX_HPP
  28. #ifndef BOOST_CONFIG_HPP
  29. # include <boost/config.hpp>
  30. #endif
  31. #
  32. #if defined(BOOST_HAS_PRAGMA_ONCE)
  33. # pragma once
  34. #endif
  35. #include <boost/interprocess/detail/config_begin.hpp>
  36. #include <boost/interprocess/detail/workaround.hpp>
  37. #include <boost/interprocess/detail/os_thread_functions.hpp>
  38. #include <boost/interprocess/exceptions.hpp>
  39. #include <boost/interprocess/detail/atomic.hpp>
  40. #include <boost/cstdint.hpp>
  41. #include <boost/interprocess/detail/os_thread_functions.hpp>
  42. #include <boost/interprocess/sync/spin/mutex.hpp>
  43. #include <boost/assert.hpp>
  44. namespace boost {
  45. namespace interprocess {
  46. namespace ipcdetail {
  47. class spin_recursive_mutex
  48. {
  49. spin_recursive_mutex(const spin_recursive_mutex &);
  50. spin_recursive_mutex &operator=(const spin_recursive_mutex &);
  51. public:
  52. spin_recursive_mutex();
  53. ~spin_recursive_mutex();
  54. void lock();
  55. bool try_lock();
  56. template<class TimePoint>
  57. bool timed_lock(const TimePoint &abs_time);
  58. template<class TimePoint> bool try_lock_until(const TimePoint &abs_time)
  59. { return this->timed_lock(abs_time); }
  60. template<class Duration> bool try_lock_for(const Duration &dur)
  61. { return this->timed_lock(duration_to_ustime(dur)); }
  62. void unlock();
  63. void take_ownership();
  64. private:
  65. spin_mutex m_mutex;
  66. unsigned int m_nLockCount;
  67. volatile ipcdetail::OS_systemwide_thread_id_t m_nOwner;
  68. volatile boost::uint32_t m_s;
  69. };
  70. inline spin_recursive_mutex::spin_recursive_mutex()
  71. : m_nLockCount(0), m_nOwner(ipcdetail::get_invalid_systemwide_thread_id()){}
  72. inline spin_recursive_mutex::~spin_recursive_mutex(){}
  73. inline void spin_recursive_mutex::lock()
  74. {
  75. typedef ipcdetail::OS_systemwide_thread_id_t handle_t;
  76. const handle_t thr_id(ipcdetail::get_current_systemwide_thread_id());
  77. handle_t old_id;
  78. ipcdetail::systemwide_thread_id_copy(m_nOwner, old_id);
  79. if(ipcdetail::equal_systemwide_thread_id(thr_id , old_id)){
  80. if((unsigned int)(m_nLockCount+1) == 0){
  81. //Overflow, throw an exception
  82. throw interprocess_exception("boost::interprocess::spin_recursive_mutex recursive lock overflow");
  83. }
  84. ++m_nLockCount;
  85. }
  86. else{
  87. m_mutex.lock();
  88. ipcdetail::systemwide_thread_id_copy(thr_id, m_nOwner);
  89. m_nLockCount = 1;
  90. }
  91. }
  92. inline bool spin_recursive_mutex::try_lock()
  93. {
  94. typedef ipcdetail::OS_systemwide_thread_id_t handle_t;
  95. handle_t thr_id(ipcdetail::get_current_systemwide_thread_id());
  96. handle_t old_id;
  97. ipcdetail::systemwide_thread_id_copy(m_nOwner, old_id);
  98. if(ipcdetail::equal_systemwide_thread_id(thr_id , old_id)) { // we own it
  99. if((unsigned int)(m_nLockCount+1) == 0){
  100. //Overflow, throw an exception
  101. throw interprocess_exception("boost::interprocess::spin_recursive_mutex recursive lock overflow");
  102. }
  103. ++m_nLockCount;
  104. return true;
  105. }
  106. if(m_mutex.try_lock()){
  107. ipcdetail::systemwide_thread_id_copy(thr_id, m_nOwner);
  108. m_nLockCount = 1;
  109. return true;
  110. }
  111. return false;
  112. }
  113. template<class TimePoint>
  114. inline bool spin_recursive_mutex::timed_lock(const TimePoint &abs_time)
  115. {
  116. typedef ipcdetail::OS_systemwide_thread_id_t handle_t;
  117. const handle_t thr_id(ipcdetail::get_current_systemwide_thread_id());
  118. handle_t old_id;
  119. ipcdetail::systemwide_thread_id_copy(m_nOwner, old_id);
  120. if(ipcdetail::equal_systemwide_thread_id(thr_id , old_id)) { // we own it
  121. if((unsigned int)(m_nLockCount+1) == 0){
  122. //Overflow, throw an exception
  123. throw interprocess_exception("boost::interprocess::spin_recursive_mutex recursive lock overflow");
  124. }
  125. ++m_nLockCount;
  126. return true;
  127. }
  128. //m_mutex supports abs_time so no need to check it
  129. if(m_mutex.timed_lock(abs_time)){
  130. ipcdetail::systemwide_thread_id_copy(thr_id, m_nOwner);
  131. m_nLockCount = 1;
  132. return true;
  133. }
  134. return false;
  135. }
  136. inline void spin_recursive_mutex::unlock()
  137. {
  138. typedef ipcdetail::OS_systemwide_thread_id_t handle_t;
  139. handle_t old_id;
  140. ipcdetail::systemwide_thread_id_copy(m_nOwner, old_id);
  141. const handle_t thr_id(ipcdetail::get_current_systemwide_thread_id());
  142. (void)old_id;
  143. (void)thr_id;
  144. BOOST_ASSERT(ipcdetail::equal_systemwide_thread_id(thr_id, old_id));
  145. --m_nLockCount;
  146. if(!m_nLockCount){
  147. const handle_t new_id(ipcdetail::get_invalid_systemwide_thread_id());
  148. ipcdetail::systemwide_thread_id_copy(new_id, m_nOwner);
  149. m_mutex.unlock();
  150. }
  151. }
  152. inline void spin_recursive_mutex::take_ownership()
  153. {
  154. typedef ipcdetail::OS_systemwide_thread_id_t handle_t;
  155. this->m_nLockCount = 1;
  156. const handle_t thr_id(ipcdetail::get_current_systemwide_thread_id());
  157. ipcdetail::systemwide_thread_id_copy(thr_id, m_nOwner);
  158. }
  159. } //namespace ipcdetail {
  160. } //namespace interprocess {
  161. } //namespace boost {
  162. #include <boost/interprocess/detail/config_end.hpp>
  163. #endif //BOOST_INTERPROCESS_DETAIL_SPIN_RECURSIVE_MUTEX_HPP