recursive_mutex.hpp 6.1 KB

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