/Src/Dependencies/Boost/boost/interprocess/sync/emulation/recursive_mutex.hpp

http://hadesmem.googlecode.com/ · C++ Header · 167 lines · 124 code · 15 blank · 28 comment · 14 complexity · 4d11859dbf87e2ea2995af25dfa86b3d MD5 · raw file

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