basic_timed_mutex.hpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. #ifndef BOOST_BASIC_TIMED_MUTEX_WIN32_HPP
  2. #define BOOST_BASIC_TIMED_MUTEX_WIN32_HPP
  3. // basic_timed_mutex_win32.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/assert.hpp>
  12. #include <boost/thread/win32/thread_primitives.hpp>
  13. #include <boost/thread/win32/interlocked_read.hpp>
  14. #include <boost/thread/thread_time.hpp>
  15. #if defined BOOST_THREAD_USES_DATETIME
  16. #include <boost/thread/xtime.hpp>
  17. #endif
  18. #include <boost/detail/interlocked.hpp>
  19. #ifdef BOOST_THREAD_USES_CHRONO
  20. #include <boost/chrono/system_clocks.hpp>
  21. #include <boost/chrono/ceil.hpp>
  22. #endif
  23. #include <boost/config/abi_prefix.hpp>
  24. namespace boost
  25. {
  26. namespace detail
  27. {
  28. struct basic_timed_mutex
  29. {
  30. BOOST_STATIC_CONSTANT(unsigned char,lock_flag_bit=31);
  31. BOOST_STATIC_CONSTANT(unsigned char,event_set_flag_bit=30);
  32. BOOST_STATIC_CONSTANT(long,lock_flag_value=1<<lock_flag_bit);
  33. BOOST_STATIC_CONSTANT(long,event_set_flag_value=1<<event_set_flag_bit);
  34. long active_count;
  35. void* event;
  36. void initialize()
  37. {
  38. active_count=0;
  39. event=0;
  40. }
  41. void destroy()
  42. {
  43. #ifdef BOOST_MSVC
  44. #pragma warning(push)
  45. #pragma warning(disable:4312)
  46. #endif
  47. void* const old_event=BOOST_INTERLOCKED_EXCHANGE_POINTER(&event,0);
  48. #ifdef BOOST_MSVC
  49. #pragma warning(pop)
  50. #endif
  51. if(old_event)
  52. {
  53. win32::CloseHandle(old_event);
  54. }
  55. }
  56. bool try_lock() BOOST_NOEXCEPT
  57. {
  58. return !win32::interlocked_bit_test_and_set(&active_count,lock_flag_bit);
  59. }
  60. void lock()
  61. {
  62. if(try_lock())
  63. {
  64. return;
  65. }
  66. long old_count=active_count;
  67. mark_waiting_and_try_lock(old_count);
  68. if(old_count&lock_flag_value)
  69. {
  70. bool lock_acquired=false;
  71. void* const sem=get_event();
  72. do
  73. {
  74. unsigned const retval(win32::WaitForSingleObject(sem, ::boost::detail::win32::infinite));
  75. BOOST_VERIFY(0 == retval || ::boost::detail::win32::wait_abandoned == retval);
  76. // BOOST_VERIFY(win32::WaitForSingleObject(
  77. // sem,::boost::detail::win32::infinite)==0);
  78. clear_waiting_and_try_lock(old_count);
  79. lock_acquired=!(old_count&lock_flag_value);
  80. }
  81. while(!lock_acquired);
  82. }
  83. }
  84. void mark_waiting_and_try_lock(long& old_count)
  85. {
  86. for(;;)
  87. {
  88. bool const was_locked=(old_count&lock_flag_value) ? true : false;
  89. long const new_count=was_locked?(old_count+1):(old_count|lock_flag_value);
  90. long const current=BOOST_INTERLOCKED_COMPARE_EXCHANGE(&active_count,new_count,old_count);
  91. if(current==old_count)
  92. {
  93. if(was_locked)
  94. old_count=new_count;
  95. break;
  96. }
  97. old_count=current;
  98. }
  99. }
  100. void clear_waiting_and_try_lock(long& old_count)
  101. {
  102. old_count&=~lock_flag_value;
  103. old_count|=event_set_flag_value;
  104. for(;;)
  105. {
  106. long const new_count=((old_count&lock_flag_value)?old_count:((old_count-1)|lock_flag_value))&~event_set_flag_value;
  107. long const current=BOOST_INTERLOCKED_COMPARE_EXCHANGE(&active_count,new_count,old_count);
  108. if(current==old_count)
  109. {
  110. break;
  111. }
  112. old_count=current;
  113. }
  114. }
  115. #if defined BOOST_THREAD_USES_DATETIME
  116. bool timed_lock(::boost::system_time const& wait_until)
  117. {
  118. if(try_lock())
  119. {
  120. return true;
  121. }
  122. long old_count=active_count;
  123. mark_waiting_and_try_lock(old_count);
  124. if(old_count&lock_flag_value)
  125. {
  126. bool lock_acquired=false;
  127. void* const sem=get_event();
  128. do
  129. {
  130. if(win32::WaitForSingleObject(sem,::boost::detail::get_milliseconds_until(wait_until))!=0)
  131. {
  132. BOOST_INTERLOCKED_DECREMENT(&active_count);
  133. return false;
  134. }
  135. clear_waiting_and_try_lock(old_count);
  136. lock_acquired=!(old_count&lock_flag_value);
  137. }
  138. while(!lock_acquired);
  139. }
  140. return true;
  141. }
  142. template<typename Duration>
  143. bool timed_lock(Duration const& timeout)
  144. {
  145. return timed_lock(get_system_time()+timeout);
  146. }
  147. bool timed_lock(boost::xtime const& timeout)
  148. {
  149. return timed_lock(system_time(timeout));
  150. }
  151. #endif
  152. #ifdef BOOST_THREAD_USES_CHRONO
  153. template <class Rep, class Period>
  154. bool try_lock_for(const chrono::duration<Rep, Period>& rel_time)
  155. {
  156. return try_lock_until(chrono::steady_clock::now() + rel_time);
  157. }
  158. template <class Clock, class Duration>
  159. bool try_lock_until(const chrono::time_point<Clock, Duration>& t)
  160. {
  161. using namespace chrono;
  162. system_clock::time_point s_now = system_clock::now();
  163. typename Clock::time_point c_now = Clock::now();
  164. return try_lock_until(s_now + ceil<system_clock::duration>(t - c_now));
  165. }
  166. template <class Duration>
  167. bool try_lock_until(const chrono::time_point<chrono::system_clock, Duration>& t)
  168. {
  169. using namespace chrono;
  170. typedef time_point<chrono::system_clock, chrono::system_clock::duration> sys_tmpt;
  171. return try_lock_until(sys_tmpt(chrono::ceil<chrono::system_clock::duration>(t.time_since_epoch())));
  172. }
  173. bool try_lock_until(const chrono::time_point<chrono::system_clock, chrono::system_clock::duration>& tp)
  174. {
  175. if(try_lock())
  176. {
  177. return true;
  178. }
  179. long old_count=active_count;
  180. mark_waiting_and_try_lock(old_count);
  181. if(old_count&lock_flag_value)
  182. {
  183. bool lock_acquired=false;
  184. void* const sem=get_event();
  185. do
  186. {
  187. chrono::milliseconds rel_time= chrono::ceil<chrono::milliseconds>(tp-chrono::system_clock::now());
  188. if(win32::WaitForSingleObject(sem,static_cast<unsigned long>(rel_time.count()))!=0)
  189. {
  190. BOOST_INTERLOCKED_DECREMENT(&active_count);
  191. return false;
  192. }
  193. clear_waiting_and_try_lock(old_count);
  194. lock_acquired=!(old_count&lock_flag_value);
  195. }
  196. while(!lock_acquired);
  197. }
  198. return true;
  199. }
  200. #endif
  201. void unlock()
  202. {
  203. long const offset=lock_flag_value;
  204. long const old_count=BOOST_INTERLOCKED_EXCHANGE_ADD(&active_count,lock_flag_value);
  205. if(!(old_count&event_set_flag_value) && (old_count>offset))
  206. {
  207. if(!win32::interlocked_bit_test_and_set(&active_count,event_set_flag_bit))
  208. {
  209. win32::SetEvent(get_event());
  210. }
  211. }
  212. }
  213. private:
  214. void* get_event()
  215. {
  216. void* current_event=::boost::detail::interlocked_read_acquire(&event);
  217. if(!current_event)
  218. {
  219. void* const new_event=win32::create_anonymous_event(win32::auto_reset_event,win32::event_initially_reset);
  220. #ifdef BOOST_MSVC
  221. #pragma warning(push)
  222. #pragma warning(disable:4311)
  223. #pragma warning(disable:4312)
  224. #endif
  225. void* const old_event=BOOST_INTERLOCKED_COMPARE_EXCHANGE_POINTER(&event,new_event,0);
  226. #ifdef BOOST_MSVC
  227. #pragma warning(pop)
  228. #endif
  229. if(old_event!=0)
  230. {
  231. win32::CloseHandle(new_event);
  232. return old_event;
  233. }
  234. else
  235. {
  236. return new_event;
  237. }
  238. }
  239. return current_event;
  240. }
  241. };
  242. }
  243. }
  244. #define BOOST_BASIC_TIMED_MUTEX_INITIALIZER {0}
  245. #include <boost/config/abi_suffix.hpp>
  246. #endif