1 #ifndef BOOST_BASIC_TIMED_MUTEX_WIN32_HPP
2 #define BOOST_BASIC_TIMED_MUTEX_WIN32_HPP
4 // basic_timed_mutex_win32.hpp
6 // (C) Copyright 2006-8 Anthony Williams
7 // (C) Copyright 2011-2012 Vicente J. Botet Escriba
9 // Distributed under the Boost Software License, Version 1.0. (See
10 // accompanying file LICENSE_1_0.txt or copy at
11 // http://www.boost.org/LICENSE_1_0.txt)
13 #include <boost/assert.hpp>
14 #include <boost/thread/win32/thread_primitives.hpp>
15 #include <boost/thread/win32/interlocked_read.hpp>
16 #include <boost/thread/thread_time.hpp>
17 #if defined BOOST_THREAD_USES_DATETIME
18 #include <boost/thread/xtime.hpp>
20 #include <boost/detail/interlocked.hpp>
21 #ifdef BOOST_THREAD_USES_CHRONO
22 #include <boost/chrono/system_clocks.hpp>
23 #include <boost/chrono/ceil.hpp>
25 #include <boost/config/abi_prefix.hpp>
31 struct basic_timed_mutex
33 BOOST_STATIC_CONSTANT(unsigned char,lock_flag_bit=31);
34 BOOST_STATIC_CONSTANT(unsigned char,event_set_flag_bit=30);
35 BOOST_STATIC_CONSTANT(long,lock_flag_value=1<<lock_flag_bit);
36 BOOST_STATIC_CONSTANT(long,event_set_flag_value=1<<event_set_flag_bit);
50 #pragma warning(disable:4312)
52 void* const old_event=BOOST_INTERLOCKED_EXCHANGE_POINTER(&event,0);
58 win32::CloseHandle(old_event);
63 bool try_lock() BOOST_NOEXCEPT
65 return !win32::interlocked_bit_test_and_set(&active_count,lock_flag_bit);
74 long old_count=active_count;
75 mark_waiting_and_try_lock(old_count);
77 if(old_count&lock_flag_value)
79 bool lock_acquired=false;
80 void* const sem=get_event();
84 unsigned const retval(win32::WaitForSingleObjectEx(sem, ::boost::detail::win32::infinite,0));
85 BOOST_VERIFY(0 == retval || ::boost::detail::win32::wait_abandoned == retval);
86 // BOOST_VERIFY(win32::WaitForSingleObject(
87 // sem,::boost::detail::win32::infinite)==0);
88 clear_waiting_and_try_lock(old_count);
89 lock_acquired=!(old_count&lock_flag_value);
91 while(!lock_acquired);
94 void mark_waiting_and_try_lock(long& old_count)
98 bool const was_locked=(old_count&lock_flag_value) ? true : false;
99 long const new_count=was_locked?(old_count+1):(old_count|lock_flag_value);
100 long const current=BOOST_INTERLOCKED_COMPARE_EXCHANGE(&active_count,new_count,old_count);
101 if(current==old_count)
111 void clear_waiting_and_try_lock(long& old_count)
113 old_count&=~lock_flag_value;
114 old_count|=event_set_flag_value;
117 long const new_count=((old_count&lock_flag_value)?old_count:((old_count-1)|lock_flag_value))&~event_set_flag_value;
118 long const current=BOOST_INTERLOCKED_COMPARE_EXCHANGE(&active_count,new_count,old_count);
119 if(current==old_count)
128 #if defined BOOST_THREAD_USES_DATETIME
129 bool timed_lock(::boost::system_time const& wait_until)
135 long old_count=active_count;
136 mark_waiting_and_try_lock(old_count);
138 if(old_count&lock_flag_value)
140 bool lock_acquired=false;
141 void* const sem=get_event();
145 if(win32::WaitForSingleObjectEx(sem,::boost::detail::get_milliseconds_until(wait_until),0)!=0)
147 BOOST_INTERLOCKED_DECREMENT(&active_count);
150 clear_waiting_and_try_lock(old_count);
151 lock_acquired=!(old_count&lock_flag_value);
153 while(!lock_acquired);
158 template<typename Duration>
159 bool timed_lock(Duration const& timeout)
161 return timed_lock(get_system_time()+timeout);
164 bool timed_lock(boost::xtime const& timeout)
166 return timed_lock(system_time(timeout));
169 #ifdef BOOST_THREAD_USES_CHRONO
170 template <class Rep, class Period>
171 bool try_lock_for(const chrono::duration<Rep, Period>& rel_time)
173 return try_lock_until(chrono::steady_clock::now() + rel_time);
175 template <class Clock, class Duration>
176 bool try_lock_until(const chrono::time_point<Clock, Duration>& t)
178 using namespace chrono;
179 system_clock::time_point s_now = system_clock::now();
180 typename Clock::time_point c_now = Clock::now();
181 return try_lock_until(s_now + ceil<system_clock::duration>(t - c_now));
183 template <class Duration>
184 bool try_lock_until(const chrono::time_point<chrono::system_clock, Duration>& t)
186 using namespace chrono;
187 typedef time_point<chrono::system_clock, chrono::system_clock::duration> sys_tmpt;
188 return try_lock_until(sys_tmpt(chrono::ceil<chrono::system_clock::duration>(t.time_since_epoch())));
190 bool try_lock_until(const chrono::time_point<chrono::system_clock, chrono::system_clock::duration>& tp)
196 long old_count=active_count;
197 mark_waiting_and_try_lock(old_count);
199 if(old_count&lock_flag_value)
201 bool lock_acquired=false;
202 void* const sem=get_event();
206 chrono::time_point<chrono::system_clock, chrono::system_clock::duration> now = chrono::system_clock::now();
208 BOOST_INTERLOCKED_DECREMENT(&active_count);
211 chrono::milliseconds rel_time= chrono::ceil<chrono::milliseconds>(tp-now);
213 if(win32::WaitForSingleObjectEx(sem,static_cast<unsigned long>(rel_time.count()),0)!=0)
215 BOOST_INTERLOCKED_DECREMENT(&active_count);
218 clear_waiting_and_try_lock(old_count);
219 lock_acquired=!(old_count&lock_flag_value);
221 while(!lock_acquired);
229 long const offset=lock_flag_value;
230 long const old_count=BOOST_INTERLOCKED_EXCHANGE_ADD(&active_count,lock_flag_value);
231 if(!(old_count&event_set_flag_value) && (old_count>offset))
233 if(!win32::interlocked_bit_test_and_set(&active_count,event_set_flag_bit))
235 win32::SetEvent(get_event());
243 void* current_event=::boost::detail::interlocked_read_acquire(&event);
247 void* const new_event=win32::create_anonymous_event(win32::auto_reset_event,win32::event_initially_reset);
249 #pragma warning(push)
250 #pragma warning(disable:4311)
251 #pragma warning(disable:4312)
253 void* const old_event=BOOST_INTERLOCKED_COMPARE_EXCHANGE_POINTER(&event,new_event,0);
259 win32::CloseHandle(new_event);
267 return current_event;
275 #define BOOST_BASIC_TIMED_MUTEX_INITIALIZER {0}
277 #include <boost/config/abi_suffix.hpp>