| 1 | //
 | 
|---|
| 2 | // monotone_timer.hpp
 | 
|---|
| 3 | // ~~~~~~~~~~~~~~~~~~
 | 
|---|
| 4 | //
 | 
|---|
| 5 | // Copyright (c) 2009
 | 
|---|
| 6 | //
 | 
|---|
| 7 | // Distributed under the Boost Software License, Version 1.0. (See accompanying
 | 
|---|
| 8 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
 | 
|---|
| 9 | //
 | 
|---|
| 10 | 
 | 
|---|
| 11 | #ifndef BOOST_ASIO_MONOTONE_TIMER_HPP
 | 
|---|
| 12 | #define BOOST_ASIO_MONOTONE_TIMER_HPP
 | 
|---|
| 13 | 
 | 
|---|
| 14 | #if defined(_MSC_VER) && (_MSC_VER >= 1200)
 | 
|---|
| 15 | # pragma once
 | 
|---|
| 16 | #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
 | 
|---|
| 17 | #include <boost/asio/detail/push_options.hpp>
 | 
|---|
| 18 | 
 | 
|---|
| 19 | #include <boost/asio/detail/socket_types.hpp> // Must come before posix_time.
 | 
|---|
| 20 | #include <boost/asio/detail/push_options.hpp>
 | 
|---|
| 21 | #include <boost/date_time/posix_time/posix_time_types.hpp>
 | 
|---|
| 22 | #include <boost/asio/detail/pop_options.hpp>
 | 
|---|
| 23 | 
 | 
|---|
| 24 | #include <boost/asio/basic_deadline_timer.hpp>
 | 
|---|
| 25 | 
 | 
|---|
| 26 | #include <boost/thread/mutex.hpp>
 | 
|---|
| 27 | #include <boost/thread/locks.hpp>
 | 
|---|
| 28 | 
 | 
|---|
| 29 | 
 | 
|---|
| 30 | namespace boost
 | 
|---|
| 31 | {
 | 
|---|
| 32 | namespace asio
 | 
|---|
| 33 | {
 | 
|---|
| 34 | 
 | 
|---|
| 35 | namespace monotone_time
 | 
|---|
| 36 | {
 | 
|---|
| 37 | 
 | 
|---|
| 38 | class mtime
 | 
|---|
| 39 | {
 | 
|---|
| 40 | public:
 | 
|---|
| 41 |         mtime() {}
 | 
|---|
| 42 |         explicit mtime(const boost::posix_time::time_duration &d) :     m(d){}
 | 
|---|
| 43 |         boost::posix_time::time_duration m;
 | 
|---|
| 44 |         mtime operator+(const boost::posix_time::time_duration &d) const { return mtime(m + d); }
 | 
|---|
| 45 |         mtime operator-(const boost::posix_time::time_duration &d) const { return mtime(m - d); }
 | 
|---|
| 46 |         boost::posix_time::time_duration operator-(const mtime &t) const { return m - t.m; }
 | 
|---|
| 47 | };
 | 
|---|
| 48 | 
 | 
|---|
| 49 | } // namespace monotone_time
 | 
|---|
| 50 | 
 | 
|---|
| 51 | 
 | 
|---|
| 52 | /// Time traits specialized for monotone_time
 | 
|---|
| 53 | template <>
 | 
|---|
| 54 | struct time_traits<monotone_time::mtime>
 | 
|---|
| 55 | {
 | 
|---|
| 56 |         /// The time type.
 | 
|---|
| 57 |         typedef boost::asio::monotone_time::mtime time_type;
 | 
|---|
| 58 | 
 | 
|---|
| 59 |         /// The duration type.
 | 
|---|
| 60 |         typedef boost::posix_time::time_duration duration_type;
 | 
|---|
| 61 | 
 | 
|---|
| 62 |         /// Get the current time.
 | 
|---|
| 63 |         static time_type now()
 | 
|---|
| 64 |         {
 | 
|---|
| 65 | #if defined(_POSIX_TIMERS) && ( _POSIX_TIMERS > 0 ) && defined(_POSIX_MONOTONIC_CLOCK)
 | 
|---|
| 66 |                 struct timespec ts;
 | 
|---|
| 67 |                 clock_gettime(CLOCK_MONOTONIC, &ts);
 | 
|---|
| 68 | #if defined(BOOST_DATE_TIME_HAS_NANOSECONDS)
 | 
|---|
| 69 |                 return time_type(boost::posix_time::seconds(ts.tv_sec) +boost::posix_time::nanosec(ts.tv_nsec));
 | 
|---|
| 70 | #else
 | 
|---|
| 71 |                 return time_type(boost::posix_time::seconds(ts.tv_sec) +boost::posix_time::microsec(ts.tv_nsec / 1000));
 | 
|---|
| 72 | #endif
 | 
|---|
| 73 | #elif defined(BOOST_WINDOWS)
 | 
|---|
| 74 |                 static LARGE_INTEGER c;
 | 
|---|
| 75 |                 static boost::mutex mx;
 | 
|---|
| 76 |                 boost::lock_guard<boost::mutex> lock(mx);
 | 
|---|
| 77 |                 uint32_t n = GetTickCount();
 | 
|---|
| 78 |                 if (n < (c.LowPart)) c.HighPart++;
 | 
|---|
| 79 |                 c.LowPart = n;
 | 
|---|
| 80 |                 return time_type(boost::posix_time::millisec(c.QuadPart));
 | 
|---|
| 81 | #endif
 | 
|---|
| 82 |         }
 | 
|---|
| 83 | 
 | 
|---|
| 84 |         /// Add a duration to a time.
 | 
|---|
| 85 |         static time_type add(const time_type& t, const duration_type& d)
 | 
|---|
| 86 |         {
 | 
|---|
| 87 |                 return t + d;
 | 
|---|
| 88 |         }
 | 
|---|
| 89 | 
 | 
|---|
| 90 |         /// Subtract one time from another.
 | 
|---|
| 91 |         static duration_type subtract(const time_type& t1, const time_type& t2)
 | 
|---|
| 92 |         {
 | 
|---|
| 93 |                 return t1.m - t2.m;
 | 
|---|
| 94 |         }
 | 
|---|
| 95 | 
 | 
|---|
| 96 |         /// Test whether one time is less than another.
 | 
|---|
| 97 |         static bool less_than(const time_type& t1, const time_type& t2)
 | 
|---|
| 98 |         {
 | 
|---|
| 99 |                 return t1.m < t2.m;
 | 
|---|
| 100 |         }
 | 
|---|
| 101 | 
 | 
|---|
| 102 |         /// Convert to POSIX duration type.
 | 
|---|
| 103 |         static boost::posix_time::time_duration to_posix_duration(
 | 
|---|
| 104 |                 const duration_type& d)
 | 
|---|
| 105 |         {
 | 
|---|
| 106 |                 return d;
 | 
|---|
| 107 |         }
 | 
|---|
| 108 | };
 | 
|---|
| 109 | 
 | 
|---|
| 110 | /// Typedef for the typical usage of timer.
 | 
|---|
| 111 | typedef boost::asio::basic_deadline_timer<boost::asio::monotone_time::mtime> monotone_timer;
 | 
|---|
| 112 | 
 | 
|---|
| 113 | } // namespace asio
 | 
|---|
| 114 | } // namespace boost
 | 
|---|
| 115 | 
 | 
|---|
| 116 | #include <boost/asio/detail/pop_options.hpp>
 | 
|---|
| 117 | 
 | 
|---|
| 118 | 
 | 
|---|
| 119 | 
 | 
|---|
| 120 | #endif // BOOST_ASIO_MONOTONE_TIMER_HPP
 | 
|---|