Fix basic_waitable_timer's underlying implementation so that it can

handle any time_point value without overflowing the intermediate
duration objects.
This commit is contained in:
Christopher Kohlhoff 2013-05-15 15:41:21 +10:00
parent a23aed7aa5
commit 1aa960ee0e
2 changed files with 52 additions and 2 deletions

View File

@ -47,13 +47,63 @@ struct chrono_time_traits
// Add a duration to a time.
static time_type add(const time_type& t, const duration_type& d)
{
const time_type epoch;
if (t >= epoch)
{
if ((time_type::max)() - t < d)
return (time_type::max)();
}
else // t < epoch
{
if (-(t - (time_type::min)()) > d)
return (time_type::min)();
}
return t + d;
}
// Subtract one time from another.
static duration_type subtract(const time_type& t1, const time_type& t2)
{
return t1 - t2;
const time_type epoch;
if (t1 >= epoch)
{
if (t2 >= epoch)
{
return t1 - t2;
}
else if (t2 == (time_type::min)())
{
return (duration_type::max)();
}
else if ((time_type::max)() - t1 < epoch - t2)
{
return (duration_type::max)();
}
else
{
return t1 - t2;
}
}
else // t1 < epoch
{
if (t2 < epoch)
{
return t1 - t2;
}
else if (t1 == (time_type::min)())
{
return (duration_type::min)();
}
else if ((time_type::max)() - t2 < epoch - t1)
{
return (duration_type::min)();
}
else
{
return -(t2 - t1);
}
}
}
// Test whether one time is less than another.

View File

@ -300,7 +300,7 @@ void system_timer_custom_allocation_test()
for (int i = 50; i < 100; ++i)
{
timers[i].t.expires_at(asio::system_timer::time_point());
timers[i].t.expires_at(asio::system_timer::time_point::min());
timers[i].t.async_wait(custom_allocation_timer_handler(&allocation_count));
}