summaryrefslogtreecommitdiff
path: root/boost/thread/win32/basic_timed_mutex.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'boost/thread/win32/basic_timed_mutex.hpp')
-rw-r--r--boost/thread/win32/basic_timed_mutex.hpp150
1 files changed, 84 insertions, 66 deletions
diff --git a/boost/thread/win32/basic_timed_mutex.hpp b/boost/thread/win32/basic_timed_mutex.hpp
index b579d50530..b332dab752 100644
--- a/boost/thread/win32/basic_timed_mutex.hpp
+++ b/boost/thread/win32/basic_timed_mutex.hpp
@@ -22,6 +22,8 @@
#include <boost/chrono/system_clocks.hpp>
#include <boost/chrono/ceil.hpp>
#endif
+#include <boost/thread/detail/platform_time.hpp>
+
#include <boost/config/abi_prefix.hpp>
namespace boost
@@ -59,7 +61,7 @@ namespace boost
}
}
-
+ // Take the lock flag if it's available
bool try_lock() BOOST_NOEXCEPT
{
return !win32::interlocked_bit_test_and_set(&active_count,lock_flag_bit);
@@ -76,21 +78,21 @@ namespace boost
if(old_count&lock_flag_value)
{
- bool lock_acquired=false;
void* const sem=get_event();
do
{
- unsigned const retval(winapi::WaitForSingleObjectEx(sem, ::boost::detail::win32::infinite,0));
- BOOST_VERIFY(0 == retval || ::boost::detail::win32::wait_abandoned == retval);
-// BOOST_VERIFY(winapi::WaitForSingleObject(
-// sem,::boost::detail::win32::infinite)==0);
- clear_waiting_and_try_lock(old_count);
- lock_acquired=!(old_count&lock_flag_value);
+ if(winapi::WaitForSingleObjectEx(sem,::boost::detail::win32::infinite,0)==0)
+ {
+ clear_waiting_and_try_lock(old_count);
+ }
}
- while(!lock_acquired);
+ while(old_count&lock_flag_value);
}
}
+
+ // Loop until the number of waiters has been incremented or we've taken the lock flag
+ // The loop is necessary since this function may be called by multiple threads simultaneously
void mark_waiting_and_try_lock(long& old_count)
{
for(;;)
@@ -102,12 +104,19 @@ namespace boost
{
if(was_locked)
old_count=new_count;
+ // else we've taken the lock flag
+ // don't update old_count so that the calling function can see that
+ // the old lock flag was 0 and know that we've taken the lock flag
break;
}
old_count=current;
}
}
+ // Loop until someone else has taken the lock flag and cleared the event set flag or
+ // until we've taken the lock flag and cleared the event set flag and decremented the
+ // number of waiters
+ // The loop is necessary since this function may be called by multiple threads simultaneously
void clear_waiting_and_try_lock(long& old_count)
{
old_count&=~lock_flag_value;
@@ -118,117 +127,124 @@ namespace boost
long const current=BOOST_INTERLOCKED_COMPARE_EXCHANGE(&active_count,new_count,old_count);
if(current==old_count)
{
+ // if someone else has taken the lock flag
+ // no need to update old_count since old_count == new_count (ignoring
+ // event_set_flag_value which the calling function doesn't care about)
+ // else we've taken the lock flag
+ // don't update old_count so that the calling function can see that
+ // the old lock flag was 0 and know that we've taken the lock flag
break;
}
old_count=current;
}
}
+ private:
+ unsigned long getMs(detail::platform_duration const& d)
+ {
+ return static_cast<unsigned long>(d.getMs());
+ }
+
+ template <typename Duration>
+ unsigned long getMs(Duration const& d)
+ {
+ return static_cast<unsigned long>(chrono::ceil<chrono::milliseconds>(d).count());
+ }
-#if defined BOOST_THREAD_USES_DATETIME
- bool timed_lock(::boost::system_time const& wait_until)
+ template <typename Clock, typename Timepoint, typename Duration>
+ bool do_lock_until(Timepoint const& t, Duration const& max)
{
if(try_lock())
{
return true;
}
+
long old_count=active_count;
mark_waiting_and_try_lock(old_count);
if(old_count&lock_flag_value)
{
- bool lock_acquired=false;
void* const sem=get_event();
+ // If the clock is the system clock, it may jump while this function
+ // is waiting. To compensate for this and time out near the correct
+ // time, we call WaitForSingleObjectEx() in a loop with a short
+ // timeout and recheck the time remaining each time through the loop.
do
{
- if(winapi::WaitForSingleObjectEx(sem,::boost::detail::get_milliseconds_until(wait_until),0)!=0)
+ Duration d(t - Clock::now());
+ if(d <= Duration::zero()) // timeout occurred
{
BOOST_INTERLOCKED_DECREMENT(&active_count);
return false;
}
- clear_waiting_and_try_lock(old_count);
- lock_acquired=!(old_count&lock_flag_value);
+ if(max != Duration::zero())
+ {
+ d = (std::min)(d, max);
+ }
+ if(winapi::WaitForSingleObjectEx(sem,getMs(d),0)==0)
+ {
+ clear_waiting_and_try_lock(old_count);
+ }
}
- while(!lock_acquired);
+ while(old_count&lock_flag_value);
}
return true;
}
+ public:
+
+#if defined BOOST_THREAD_USES_DATETIME
+ bool timed_lock(::boost::system_time const& wait_until)
+ {
+ const detail::real_platform_timepoint t(wait_until);
+ return do_lock_until<detail::real_platform_clock>(t, detail::platform_milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS));
+ }
template<typename Duration>
bool timed_lock(Duration const& timeout)
{
- return timed_lock(get_system_time()+timeout);
+ const detail::mono_platform_timepoint t(detail::mono_platform_clock::now() + detail::platform_duration(timeout));
+ // The reference clock is steady and so no need to poll periodically, thus 0 ms max (i.e. no max)
+ return do_lock_until<detail::mono_platform_clock>(t, detail::platform_duration::zero());
}
bool timed_lock(boost::xtime const& timeout)
{
- return timed_lock(system_time(timeout));
+ return timed_lock(boost::system_time(timeout));
}
#endif
#ifdef BOOST_THREAD_USES_CHRONO
template <class Rep, class Period>
bool try_lock_for(const chrono::duration<Rep, Period>& rel_time)
{
- return try_lock_until(chrono::steady_clock::now() + rel_time);
- }
- template <class Clock, class Duration>
- bool try_lock_until(const chrono::time_point<Clock, Duration>& t)
- {
- using namespace chrono;
- system_clock::time_point s_now = system_clock::now();
- typename Clock::time_point c_now = Clock::now();
- return try_lock_until(s_now + ceil<system_clock::duration>(t - c_now));
+ const chrono::steady_clock::time_point t(chrono::steady_clock::now() + rel_time);
+ typedef typename chrono::duration<Rep, Period> Duration;
+ typedef typename common_type<Duration, typename chrono::steady_clock::duration>::type common_duration;
+ // The reference clock is steady and so no need to poll periodically, thus 0 ms max (i.e. no max)
+ return do_lock_until<chrono::steady_clock>(t, common_duration::zero());
}
template <class Duration>
- bool try_lock_until(const chrono::time_point<chrono::system_clock, Duration>& t)
+ bool try_lock_until(const chrono::time_point<chrono::steady_clock, Duration>& t)
{
- using namespace chrono;
- typedef time_point<chrono::system_clock, chrono::system_clock::duration> sys_tmpt;
- return try_lock_until(sys_tmpt(chrono::ceil<chrono::system_clock::duration>(t.time_since_epoch())));
+ typedef typename common_type<Duration, typename chrono::steady_clock::duration>::type common_duration;
+ // The reference clock is steady and so no need to poll periodically, thus 0 ms max (i.e. no max)
+ return do_lock_until<chrono::steady_clock>(t, common_duration::zero());
}
- bool try_lock_until(const chrono::time_point<chrono::system_clock, chrono::system_clock::duration>& tp)
+ template <class Clock, class Duration>
+ bool try_lock_until(const chrono::time_point<Clock, Duration>& t)
{
- if(try_lock())
- {
- return true;
- }
- long old_count=active_count;
- mark_waiting_and_try_lock(old_count);
-
- if(old_count&lock_flag_value)
- {
- bool lock_acquired=false;
- void* const sem=get_event();
-
- do
- {
- chrono::time_point<chrono::system_clock, chrono::system_clock::duration> now = chrono::system_clock::now();
- if (tp<=now) {
- BOOST_INTERLOCKED_DECREMENT(&active_count);
- return false;
- }
- chrono::milliseconds rel_time= chrono::ceil<chrono::milliseconds>(tp-now);
-
- if(winapi::WaitForSingleObjectEx(sem,static_cast<unsigned long>(rel_time.count()),0)!=0)
- {
- BOOST_INTERLOCKED_DECREMENT(&active_count);
- return false;
- }
- clear_waiting_and_try_lock(old_count);
- lock_acquired=!(old_count&lock_flag_value);
- }
- while(!lock_acquired);
- }
- return true;
+ typedef typename common_type<Duration, typename Clock::duration>::type common_duration;
+ return do_lock_until<Clock>(t, common_duration(chrono::milliseconds(BOOST_THREAD_POLL_INTERVAL_MILLISECONDS)));
}
#endif
void unlock()
{
- long const offset=lock_flag_value;
+ // Clear the lock flag using atomic addition (works since long is always 32 bits on Windows)
long const old_count=BOOST_INTERLOCKED_EXCHANGE_ADD(&active_count,lock_flag_value);
- if(!(old_count&event_set_flag_value) && (old_count>offset))
+ // If someone is waiting to take the lock, set the event set flag and, if
+ // the event set flag hadn't already been set, send an event.
+ if(!(old_count&event_set_flag_value) && (old_count>lock_flag_value))
{
if(!win32::interlocked_bit_test_and_set(&active_count,event_set_flag_bit))
{
@@ -238,6 +254,8 @@ namespace boost
}
private:
+ // Create an event in a thread-safe way
+ // The first thread to create the event wins and all other thread will use that event
void* get_event()
{
void* current_event=::boost::detail::interlocked_read_acquire(&event);