Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r84347 - in trunk: boost/asio/detail libs/asio/test
From: chris_at_[hidden]
Date: 2013-05-18 08:02:00


Author: chris_kohlhoff
Date: 2013-05-18 08:01:59 EDT (Sat, 18 May 2013)
New Revision: 84347
URL: http://svn.boost.org/trac/boost/changeset/84347

Log:
Fix basic_waitable_timer's underlying implementation so that it can
handle any time_point value without overflowing the intermediate
duration objects.

Text files modified:
   trunk/boost/asio/detail/chrono_time_traits.hpp | 52 +++++++++++++++++++++++++++++++++++++++
   trunk/libs/asio/test/system_timer.cpp | 2
   2 files changed, 52 insertions(+), 2 deletions(-)

Modified: trunk/boost/asio/detail/chrono_time_traits.hpp
==============================================================================
--- trunk/boost/asio/detail/chrono_time_traits.hpp (original)
+++ trunk/boost/asio/detail/chrono_time_traits.hpp 2013-05-18 08:01:59 EDT (Sat, 18 May 2013)
@@ -48,13 +48,63 @@
   // 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.

Modified: trunk/libs/asio/test/system_timer.cpp
==============================================================================
--- trunk/libs/asio/test/system_timer.cpp (original)
+++ trunk/libs/asio/test/system_timer.cpp 2013-05-18 08:01:59 EDT (Sat, 18 May 2013)
@@ -300,7 +300,7 @@
 
   for (int i = 50; i < 100; ++i)
   {
- timers[i].t.expires_at(boost::asio::system_timer::time_point());
+ timers[i].t.expires_at(boost::asio::system_timer::time_point::min());
     timers[i].t.async_wait(custom_allocation_timer_handler(&allocation_count));
   }
 


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk