basic_recursive_mutex.hpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 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=win32::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=win32::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=win32::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& timeout)
  61. {
  62. return timed_lock(get_system_time()+timeout);
  63. }
  64. #endif
  65. #ifdef BOOST_THREAD_USES_CHRONO
  66. template <class Rep, class Period>
  67. bool try_lock_for(const chrono::duration<Rep, Period>& rel_time)
  68. {
  69. long const current_thread_id=win32::GetCurrentThreadId();
  70. return try_recursive_lock(current_thread_id) || try_timed_lock_for(current_thread_id,rel_time);
  71. }
  72. template <class Clock, class Duration>
  73. bool try_lock_until(const chrono::time_point<Clock, Duration>& t)
  74. {
  75. long const current_thread_id=win32::GetCurrentThreadId();
  76. return try_recursive_lock(current_thread_id) || try_timed_lock_until(current_thread_id,t);
  77. }
  78. #endif
  79. void unlock()
  80. {
  81. if(!--recursion_count)
  82. {
  83. BOOST_INTERLOCKED_EXCHANGE(&locking_thread_id,0);
  84. mutex.unlock();
  85. }
  86. }
  87. private:
  88. bool try_recursive_lock(long current_thread_id) BOOST_NOEXCEPT
  89. {
  90. if(::boost::detail::interlocked_read_acquire(&locking_thread_id)==current_thread_id)
  91. {
  92. ++recursion_count;
  93. return true;
  94. }
  95. return false;
  96. }
  97. bool try_basic_lock(long current_thread_id) BOOST_NOEXCEPT
  98. {
  99. if(mutex.try_lock())
  100. {
  101. BOOST_INTERLOCKED_EXCHANGE(&locking_thread_id,current_thread_id);
  102. recursion_count=1;
  103. return true;
  104. }
  105. return false;
  106. }
  107. #if defined BOOST_THREAD_USES_DATETIME
  108. bool try_timed_lock(long current_thread_id,::boost::system_time const& target)
  109. {
  110. if(mutex.timed_lock(target))
  111. {
  112. BOOST_INTERLOCKED_EXCHANGE(&locking_thread_id,current_thread_id);
  113. recursion_count=1;
  114. return true;
  115. }
  116. return false;
  117. }
  118. #endif
  119. template <typename TP>
  120. bool try_timed_lock_until(long current_thread_id,TP const& target)
  121. {
  122. if(mutex.try_lock_until(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. template <typename D>
  131. bool try_timed_lock_for(long current_thread_id,D const& target)
  132. {
  133. if(mutex.try_lock_for(target))
  134. {
  135. BOOST_INTERLOCKED_EXCHANGE(&locking_thread_id,current_thread_id);
  136. recursion_count=1;
  137. return true;
  138. }
  139. return false;
  140. }
  141. };
  142. typedef basic_recursive_mutex_impl<basic_timed_mutex> basic_recursive_mutex;
  143. typedef basic_recursive_mutex_impl<basic_timed_mutex> basic_recursive_timed_mutex;
  144. }
  145. }
  146. #define BOOST_BASIC_RECURSIVE_MUTEX_INITIALIZER {0}
  147. #include <boost/config/abi_suffix.hpp>
  148. #endif