basic_timed_mutex.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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/thread/detail/platform_time.hpp>
  24. #include <boost/config/abi_prefix.hpp>
  25. namespace boost
  26. {
  27. namespace detail
  28. {
  29. struct basic_timed_mutex
  30. {
  31. BOOST_STATIC_CONSTANT(unsigned char,lock_flag_bit=31);
  32. BOOST_STATIC_CONSTANT(unsigned char,event_set_flag_bit=30);
  33. BOOST_STATIC_CONSTANT(long,lock_flag_value=1<<lock_flag_bit);
  34. BOOST_STATIC_CONSTANT(long,event_set_flag_value=1<<event_set_flag_bit);
  35. long active_count;
  36. void* event;
  37. void initialize()
  38. {
  39. active_count=0;
  40. event=0;
  41. }
  42. void destroy()
  43. {
  44. #ifdef BOOST_MSVC
  45. #pragma warning(push)
  46. #pragma warning(disable:4312)
  47. #endif
  48. void* const old_event=BOOST_INTERLOCKED_EXCHANGE_POINTER(&event,0);
  49. #ifdef BOOST_MSVC
  50. #pragma warning(pop)
  51. #endif
  52. if(old_event)
  53. {
  54. winapi::CloseHandle(old_event);
  55. }
  56. }
  57. // Take the lock flag if it's available
  58. bool try_lock() BOOST_NOEXCEPT
  59. {
  60. return !win32::interlocked_bit_test_and_set(&active_count,lock_flag_bit);
  61. }
  62. void lock()
  63. {
  64. if(try_lock())
  65. {
  66. return;
  67. }
  68. long old_count=active_count;
  69. mark_waiting_and_try_lock(old_count);
  70. if(old_count&lock_flag_value)
  71. {
  72. void* const sem=get_event();
  73. do
  74. {
  75. if(winapi::WaitForSingleObjectEx(sem,::boost::detail::win32::infinite,0)==0)
  76. {
  77. clear_waiting_and_try_lock(old_count);
  78. }
  79. }
  80. while(old_count&lock_flag_value);
  81. }
  82. }
  83. // Loop until the number of waiters has been incremented or we've taken the lock flag
  84. // The loop is necessary since this function may be called by multiple threads simultaneously
  85. void mark_waiting_and_try_lock(long& old_count)
  86. {
  87. for(;;)
  88. {
  89. bool const was_locked=(old_count&lock_flag_value) ? true : false;
  90. long const new_count=was_locked?(old_count+1):(old_count|lock_flag_value);
  91. long const current=BOOST_INTERLOCKED_COMPARE_EXCHANGE(&active_count,new_count,old_count);
  92. if(current==old_count)
  93. {
  94. if(was_locked)
  95. old_count=new_count;
  96. // else we've taken the lock flag
  97. // don't update old_count so that the calling function can see that
  98. // the old lock flag was 0 and know that we've taken the lock flag
  99. break;
  100. }
  101. old_count=current;
  102. }
  103. }
  104. // Loop until someone else has taken the lock flag and cleared the event set flag or
  105. // until we've taken the lock flag and cleared the event set flag and decremented the
  106. // number of waiters
  107. // The loop is necessary since this function may be called by multiple threads simultaneously
  108. void clear_waiting_and_try_lock(long& old_count)
  109. {
  110. old_count&=~lock_flag_value;
  111. old_count|=event_set_flag_value;
  112. for(;;)
  113. {
  114. long const new_count=((old_count&lock_flag_value)?old_count:((old_count-1)|lock_flag_value))&~event_set_flag_value;
  115. long const current=BOOST_INTERLOCKED_COMPARE_EXCHANGE(&active_count,new_count,old_count);
  116. if(current==old_count)
  117. {
  118. // if someone else has taken the lock flag
  119. // no need to update old_count since old_count == new_count (ignoring
  120. // event_set_flag_value which the calling function doesn't care about)
  121. // else we've taken the lock flag
  122. // don't update old_count so that the calling function can see that
  123. // the old lock flag was 0 and know that we've taken the lock flag
  124. break;
  125. }
  126. old_count=current;
  127. }
  128. }
  129. private:
  130. unsigned long getMs(detail::platform_duration const& d)
  131. {
  132. return static_cast<unsigned long>(d.getMs());
  133. }
  134. template <typename Duration>
  135. unsigned long getMs(Duration const& d)
  136. {
  137. return static_cast<unsigned long>(chrono::ceil<chrono::milliseconds>(d).count());
  138. }
  139. template <typename Clock, typename Timepoint, typename Duration>
  140. bool do_lock_until(Timepoint const& t, Duration const& max)
  141. {
  142. if(try_lock())
  143. {
  144. return true;
  145. }
  146. long old_count=active_count;
  147. mark_waiting_and_try_lock(old_count);
  148. if(old_count&lock_flag_value)
  149. {
  150. void* const sem=get_event();
  151. // If the clock is the system clock, it may jump while this function
  152. // is waiting. To compensate for this and time out near the correct
  153. // time, we call WaitForSingleObjectEx() in a loop with a short
  154. // timeout and recheck the time remaining each time through the loop.
  155. do
  156. {
  157. Duration d(t - Clock::now());
  158. if(d <= Duration::zero()) // timeout occurred
  159. {
  160. BOOST_INTERLOCKED_DECREMENT(&active_count);
  161. return false;
  162. }
  163. if(max != Duration::zero())
  164. {
  165. d = (std::min)(d, max);
  166. }
  167. if(winapi::WaitForSingleObjectEx(sem,getMs(d),0)==0)
  168. {
  169. clear_waiting_and_try_lock(old_count);
  170. }
  171. }
  172. while(old_count&lock_flag_value);
  173. }
  174. return true;
  175. }
  176. public:
  177. #if defined BOOST_THREAD_USES_DATETIME
  178. bool timed_lock(::boost::system_time const& wait_until)
  179. {
  180. const detail::real_platform_timepoint t(wait_until);
  181. return do_lock_until<detail::real_platform_clock>(t, detail::platform_milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS));
  182. }
  183. template<typename Duration>
  184. bool timed_lock(Duration const& timeout)
  185. {
  186. const detail::mono_platform_timepoint t(detail::mono_platform_clock::now() + detail::platform_duration(timeout));
  187. // The reference clock is steady and so no need to poll periodically, thus 0 ms max (i.e. no max)
  188. return do_lock_until<detail::mono_platform_clock>(t, detail::platform_duration::zero());
  189. }
  190. bool timed_lock(boost::xtime const& timeout)
  191. {
  192. return timed_lock(boost::system_time(timeout));
  193. }
  194. #endif
  195. #ifdef BOOST_THREAD_USES_CHRONO
  196. template <class Rep, class Period>
  197. bool try_lock_for(const chrono::duration<Rep, Period>& rel_time)
  198. {
  199. const chrono::steady_clock::time_point t(chrono::steady_clock::now() + rel_time);
  200. typedef typename chrono::duration<Rep, Period> Duration;
  201. typedef typename common_type<Duration, typename chrono::steady_clock::duration>::type common_duration;
  202. // The reference clock is steady and so no need to poll periodically, thus 0 ms max (i.e. no max)
  203. return do_lock_until<chrono::steady_clock>(t, common_duration::zero());
  204. }
  205. template <class Duration>
  206. bool try_lock_until(const chrono::time_point<chrono::steady_clock, Duration>& t)
  207. {
  208. typedef typename common_type<Duration, typename chrono::steady_clock::duration>::type common_duration;
  209. // The reference clock is steady and so no need to poll periodically, thus 0 ms max (i.e. no max)
  210. return do_lock_until<chrono::steady_clock>(t, common_duration::zero());
  211. }
  212. template <class Clock, class Duration>
  213. bool try_lock_until(const chrono::time_point<Clock, Duration>& t)
  214. {
  215. typedef typename common_type<Duration, typename Clock::duration>::type common_duration;
  216. return do_lock_until<Clock>(t, common_duration(chrono::milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS)));
  217. }
  218. #endif
  219. void unlock()
  220. {
  221. // Clear the lock flag using atomic addition (works since long is always 32 bits on Windows)
  222. long const old_count=BOOST_INTERLOCKED_EXCHANGE_ADD(&active_count,lock_flag_value);
  223. // If someone is waiting to take the lock, set the event set flag and, if
  224. // the event set flag hadn't already been set, send an event.
  225. if(!(old_count&event_set_flag_value) && (old_count>lock_flag_value))
  226. {
  227. if(!win32::interlocked_bit_test_and_set(&active_count,event_set_flag_bit))
  228. {
  229. winapi::SetEvent(get_event());
  230. }
  231. }
  232. }
  233. private:
  234. // Create an event in a thread-safe way
  235. // The first thread to create the event wins and all other thread will use that event
  236. void* get_event()
  237. {
  238. void* current_event=::boost::detail::interlocked_read_acquire(&event);
  239. if(!current_event)
  240. {
  241. void* const new_event=win32::create_anonymous_event(win32::auto_reset_event,win32::event_initially_reset);
  242. #ifdef BOOST_MSVC
  243. #pragma warning(push)
  244. #pragma warning(disable:4311)
  245. #pragma warning(disable:4312)
  246. #endif
  247. void* const old_event=BOOST_INTERLOCKED_COMPARE_EXCHANGE_POINTER(&event,new_event,0);
  248. #ifdef BOOST_MSVC
  249. #pragma warning(pop)
  250. #endif
  251. if(old_event!=0)
  252. {
  253. winapi::CloseHandle(new_event);
  254. return old_event;
  255. }
  256. else
  257. {
  258. return new_event;
  259. }
  260. }
  261. return current_event;
  262. }
  263. };
  264. }
  265. }
  266. #define BOOST_BASIC_TIMED_MUTEX_INITIALIZER {0}
  267. #include <boost/config/abi_suffix.hpp>
  268. #endif