| 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 | #include <sys/timeb.h>
|
|---|
| 17 | #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
|
|---|
| 18 | #include <boost/asio/detail/push_options.hpp>
|
|---|
| 19 |
|
|---|
| 20 | #include <boost/asio/detail/socket_types.hpp> // Must come before posix_time.
|
|---|
| 21 | #include <boost/asio/detail/push_options.hpp>
|
|---|
| 22 | #include <boost/date_time/posix_time/posix_time_types.hpp>
|
|---|
| 23 | #include <boost/asio/detail/pop_options.hpp>
|
|---|
| 24 |
|
|---|
| 25 | #include <boost/asio/basic_deadline_timer.hpp>
|
|---|
| 26 |
|
|---|
| 27 | #include <boost/thread/mutex.hpp>
|
|---|
| 28 | #include <boost/thread/locks.hpp>
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 | namespace boost
|
|---|
| 32 | {
|
|---|
| 33 | namespace asio
|
|---|
| 34 | {
|
|---|
| 35 |
|
|---|
| 36 | namespace monotone_time
|
|---|
| 37 | {
|
|---|
| 38 |
|
|---|
| 39 | class mtime
|
|---|
| 40 | {
|
|---|
| 41 | public:
|
|---|
| 42 | mtime() {}
|
|---|
| 43 | explicit mtime(const boost::posix_time::time_duration &d) : m(d){}
|
|---|
| 44 | boost::posix_time::time_duration m;
|
|---|
| 45 | mtime operator+(const boost::posix_time::time_duration &d) const { return mtime(m + d); }
|
|---|
| 46 | mtime operator-(const boost::posix_time::time_duration &d) const { return mtime(m - d); }
|
|---|
| 47 | boost::posix_time::time_duration operator-(const mtime &t) const { return m - t.m; }
|
|---|
| 48 | };
|
|---|
| 49 |
|
|---|
| 50 | } // namespace monotone_time
|
|---|
| 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 | struct _timeb ts;
|
|---|
| 75 | _ftime (&ts);
|
|---|
| 76 | #if defined(BOOST_DATE_TIME_HAS_NANOSECONDS)
|
|---|
| 77 | return time_type(boost::posix_time::seconds(ts.time) +boost::posix_time::nanosec(ts.millitm * 1000*1000));
|
|---|
| 78 | #else
|
|---|
| 79 | return time_type(boost::posix_time::seconds(ts.time) +boost::posix_time::microsec(ts.millitm * 1000));
|
|---|
| 80 | #endif
|
|---|
| 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
|
|---|