basic_recursive_mutex.hpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #ifndef BOOST_BASIC_RECURSIVE_MUTEX_WIN32_HPP
  2. #define BOOST_BASIC_RECURSIVE_MUTEX_WIN32_HPP
  3. // basic_recursive_mutex.hpp
  4. //
  5. // (C) Copyright 2006-8 Anthony Williams
  6. // (C) Copyright 2011-2012,2017-2018 Vicente J. Botet Escriba
  7. //
  8. // Distributed under the Boost Software License, Version 1.0. (See
  9. // accompanying file LICENSE_1_0.txt or copy at
  10. // http://www.boost.org/LICENSE_1_0.txt)
  11. #include <boost/thread/win32/thread_primitives.hpp>
  12. #include <boost/thread/win32/basic_timed_mutex.hpp>
  13. #ifdef BOOST_THREAD_USES_CHRONO
  14. #include <boost/chrono/system_clocks.hpp>
  15. #include <boost/chrono/ceil.hpp>
  16. #endif
  17. #include <boost/config/abi_prefix.hpp>
  18. namespace boost
  19. {
  20. namespace detail
  21. {
  22. template<typename underlying_mutex_type>
  23. struct basic_recursive_mutex_impl
  24. {
  25. long recursion_count;
  26. long locking_thread_id;
  27. underlying_mutex_type mutex;
  28. void initialize()
  29. {
  30. recursion_count=0;
  31. locking_thread_id=0;
  32. mutex.initialize();
  33. }
  34. void destroy()
  35. {
  36. mutex.destroy();
  37. }
  38. bool try_lock() BOOST_NOEXCEPT
  39. {
  40. long const current_thread_id=boost::winapi::GetCurrentThreadId();
  41. return try_recursive_lock(current_thread_id) || try_basic_lock(current_thread_id);
  42. }
  43. void lock()
  44. {
  45. long const current_thread_id=boost::winapi::GetCurrentThreadId();
  46. if(!try_recursive_lock(current_thread_id))
  47. {
  48. mutex.lock();
  49. BOOST_INTERLOCKED_EXCHANGE(&locking_thread_id,current_thread_id);
  50. recursion_count=1;
  51. }
  52. }
  53. #if defined BOOST_THREAD_USES_DATETIME
  54. bool timed_lock(::boost::system_time const& target)
  55. {
  56. long const current_thread_id=boost::winapi::GetCurrentThreadId();
  57. return try_recursive_lock(current_thread_id) || try_timed_lock(current_thread_id,target);
  58. }
  59. template<typename Duration>
  60. bool timed_lock(Duration const& target)
  61. {
  62. long const current_thread_id=boost::winapi::GetCurrentThreadId();
  63. return try_recursive_lock(current_thread_id) || try_timed_lock(current_thread_id,target);
  64. }
  65. #endif
  66. #ifdef BOOST_THREAD_USES_CHRONO
  67. template <class Rep, class Period>
  68. bool try_lock_for(const chrono::duration<Rep, Period>& rel_time)
  69. {
  70. long const current_thread_id=boost::winapi::GetCurrentThreadId();
  71. return try_recursive_lock(current_thread_id) || try_timed_lock_for(current_thread_id,rel_time);
  72. }
  73. template <class Clock, class Duration>
  74. bool try_lock_until(const chrono::time_point<Clock, Duration>& t)
  75. {
  76. long const current_thread_id=boost::winapi::GetCurrentThreadId();
  77. return try_recursive_lock(current_thread_id) || try_timed_lock_until(current_thread_id,t);
  78. }
  79. #endif
  80. void unlock()
  81. {
  82. if(!--recursion_count)
  83. {
  84. BOOST_INTERLOCKED_EXCHANGE(&locking_thread_id,0);
  85. mutex.unlock();
  86. }
  87. }
  88. private:
  89. bool try_recursive_lock(long current_thread_id) BOOST_NOEXCEPT
  90. {
  91. if(::boost::detail::interlocked_read_acquire(&locking_thread_id)==current_thread_id)
  92. {
  93. ++recursion_count;
  94. return true;
  95. }
  96. return false;
  97. }
  98. bool try_basic_lock(long current_thread_id) BOOST_NOEXCEPT
  99. {
  100. if(mutex.try_lock())
  101. {
  102. BOOST_INTERLOCKED_EXCHANGE(&locking_thread_id,current_thread_id);
  103. recursion_count=1;
  104. return true;
  105. }
  106. return false;
  107. }
  108. #if defined BOOST_THREAD_USES_DATETIME
  109. bool try_timed_lock(long current_thread_id,::boost::system_time const& target)
  110. {
  111. if(mutex.timed_lock(target))
  112. {
  113. BOOST_INTERLOCKED_EXCHANGE(&locking_thread_id,current_thread_id);
  114. recursion_count=1;
  115. return true;
  116. }
  117. return false;
  118. }
  119. template<typename Duration>
  120. bool try_timed_lock(long current_thread_id,Duration const& target)
  121. {
  122. if(mutex.timed_lock(target))
  123. {
  124. BOOST_INTERLOCKED_EXCHANGE(&locking_thread_id,current_thread_id);
  125. recursion_count=1;
  126. return true;
  127. }
  128. return false;
  129. }
  130. #endif
  131. template <typename TP>
  132. bool try_timed_lock_until(long current_thread_id,TP const& target)
  133. {
  134. if(mutex.try_lock_until(target))
  135. {
  136. BOOST_INTERLOCKED_EXCHANGE(&locking_thread_id,current_thread_id);
  137. recursion_count=1;
  138. return true;
  139. }
  140. return false;
  141. }
  142. template <typename D>
  143. bool try_timed_lock_for(long current_thread_id,D const& target)
  144. {
  145. if(mutex.try_lock_for(target))
  146. {
  147. BOOST_INTERLOCKED_EXCHANGE(&locking_thread_id,current_thread_id);
  148. recursion_count=1;
  149. return true;
  150. }
  151. return false;
  152. }
  153. };
  154. typedef basic_recursive_mutex_impl<basic_timed_mutex> basic_recursive_mutex;
  155. typedef basic_recursive_mutex_impl<basic_timed_mutex> basic_recursive_timed_mutex;
  156. }
  157. }
  158. #define BOOST_BASIC_RECURSIVE_MUTEX_INITIALIZER {0}
  159. #include <boost/config/abi_suffix.hpp>
  160. #endif