recursive_mutex.hpp 5.8 KB

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