recursive_mutex.hpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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/posix_time_types_wrk.hpp>
  38. #include <boost/interprocess/detail/os_thread_functions.hpp>
  39. #include <boost/interprocess/exceptions.hpp>
  40. #include <boost/interprocess/detail/atomic.hpp>
  41. #include <boost/cstdint.hpp>
  42. #include <boost/interprocess/detail/os_thread_functions.hpp>
  43. #include <boost/interprocess/sync/spin/mutex.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. bool timed_lock(const boost::posix_time::ptime &abs_time);
  58. void unlock();
  59. void take_ownership();
  60. private:
  61. spin_mutex m_mutex;
  62. unsigned int m_nLockCount;
  63. volatile ipcdetail::OS_systemwide_thread_id_t m_nOwner;
  64. volatile boost::uint32_t m_s;
  65. };
  66. inline spin_recursive_mutex::spin_recursive_mutex()
  67. : m_nLockCount(0), m_nOwner(ipcdetail::get_invalid_systemwide_thread_id()){}
  68. inline spin_recursive_mutex::~spin_recursive_mutex(){}
  69. inline void spin_recursive_mutex::lock()
  70. {
  71. typedef ipcdetail::OS_systemwide_thread_id_t handle_t;
  72. const handle_t thr_id(ipcdetail::get_current_systemwide_thread_id());
  73. handle_t old_id;
  74. ipcdetail::systemwide_thread_id_copy(m_nOwner, old_id);
  75. if(ipcdetail::equal_systemwide_thread_id(thr_id , old_id)){
  76. if((unsigned int)(m_nLockCount+1) == 0){
  77. //Overflow, throw an exception
  78. throw interprocess_exception("boost::interprocess::spin_recursive_mutex recursive lock overflow");
  79. }
  80. ++m_nLockCount;
  81. }
  82. else{
  83. m_mutex.lock();
  84. ipcdetail::systemwide_thread_id_copy(thr_id, m_nOwner);
  85. m_nLockCount = 1;
  86. }
  87. }
  88. inline bool spin_recursive_mutex::try_lock()
  89. {
  90. typedef ipcdetail::OS_systemwide_thread_id_t handle_t;
  91. handle_t thr_id(ipcdetail::get_current_systemwide_thread_id());
  92. handle_t old_id;
  93. ipcdetail::systemwide_thread_id_copy(m_nOwner, old_id);
  94. if(ipcdetail::equal_systemwide_thread_id(thr_id , old_id)) { // we own it
  95. if((unsigned int)(m_nLockCount+1) == 0){
  96. //Overflow, throw an exception
  97. throw interprocess_exception("boost::interprocess::spin_recursive_mutex recursive lock overflow");
  98. }
  99. ++m_nLockCount;
  100. return true;
  101. }
  102. if(m_mutex.try_lock()){
  103. ipcdetail::systemwide_thread_id_copy(thr_id, m_nOwner);
  104. m_nLockCount = 1;
  105. return true;
  106. }
  107. return false;
  108. }
  109. inline bool spin_recursive_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
  110. {
  111. typedef ipcdetail::OS_systemwide_thread_id_t handle_t;
  112. const handle_t thr_id(ipcdetail::get_current_systemwide_thread_id());
  113. handle_t old_id;
  114. ipcdetail::systemwide_thread_id_copy(m_nOwner, old_id);
  115. if(ipcdetail::equal_systemwide_thread_id(thr_id , old_id)) { // we own it
  116. if((unsigned int)(m_nLockCount+1) == 0){
  117. //Overflow, throw an exception
  118. throw interprocess_exception("boost::interprocess::spin_recursive_mutex recursive lock overflow");
  119. }
  120. ++m_nLockCount;
  121. return true;
  122. }
  123. //m_mutex supports abs_time so no need to check it
  124. if(m_mutex.timed_lock(abs_time)){
  125. ipcdetail::systemwide_thread_id_copy(thr_id, m_nOwner);
  126. m_nLockCount = 1;
  127. return true;
  128. }
  129. return false;
  130. }
  131. inline void spin_recursive_mutex::unlock()
  132. {
  133. typedef ipcdetail::OS_systemwide_thread_id_t handle_t;
  134. handle_t old_id;
  135. ipcdetail::systemwide_thread_id_copy(m_nOwner, old_id);
  136. const handle_t thr_id(ipcdetail::get_current_systemwide_thread_id());
  137. (void)old_id;
  138. (void)thr_id;
  139. BOOST_ASSERT(ipcdetail::equal_systemwide_thread_id(thr_id, old_id));
  140. --m_nLockCount;
  141. if(!m_nLockCount){
  142. const handle_t new_id(ipcdetail::get_invalid_systemwide_thread_id());
  143. ipcdetail::systemwide_thread_id_copy(new_id, m_nOwner);
  144. m_mutex.unlock();
  145. }
  146. }
  147. inline void spin_recursive_mutex::take_ownership()
  148. {
  149. typedef ipcdetail::OS_systemwide_thread_id_t handle_t;
  150. this->m_nLockCount = 1;
  151. const handle_t thr_id(ipcdetail::get_current_systemwide_thread_id());
  152. ipcdetail::systemwide_thread_id_copy(thr_id, m_nOwner);
  153. }
  154. } //namespace ipcdetail {
  155. } //namespace interprocess {
  156. } //namespace boost {
  157. #include <boost/interprocess/detail/config_end.hpp>
  158. #endif //BOOST_INTERPROCESS_DETAIL_SPIN_RECURSIVE_MUTEX_HPP