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 | LARGE_INTEGER count;
|
---|
75 | LARGE_INTEGER frequency;
|
---|
76 |
|
---|
77 | QueryPerformanceCounter(&count);
|
---|
78 | QueryPerformanceFrequency(&frequency);
|
---|
79 |
|
---|
80 | ULONGLONG nanoseconds = count.QuadPart*(1000000000/frequency.QuadPart);
|
---|
81 | #if defined(BOOST_DATE_TIME_HAS_NANOSECONDS)
|
---|
82 | return time_type(boost::posix_time::nanosec(nanoseconds);
|
---|
83 | #else
|
---|
84 | return time_type(boost::posix_time::microsec(nanoseconds/1000));
|
---|
85 | #endif
|
---|
86 | #endif
|
---|
87 | }
|
---|
88 |
|
---|
89 | /// Add a duration to a time.
|
---|
90 | static time_type add(const time_type& t, const duration_type& d)
|
---|
91 | {
|
---|
92 | return t + d;
|
---|
93 | }
|
---|
94 |
|
---|
95 | /// Subtract one time from another.
|
---|
96 | static duration_type subtract(const time_type& t1, const time_type& t2)
|
---|
97 | {
|
---|
98 | return t1.m - t2.m;
|
---|
99 | }
|
---|
100 |
|
---|
101 | /// Test whether one time is less than another.
|
---|
102 | static bool less_than(const time_type& t1, const time_type& t2)
|
---|
103 | {
|
---|
104 | return t1.m < t2.m;
|
---|
105 | }
|
---|
106 |
|
---|
107 | /// Convert to POSIX duration type.
|
---|
108 | static boost::posix_time::time_duration to_posix_duration(
|
---|
109 | const duration_type& d)
|
---|
110 | {
|
---|
111 | return d;
|
---|
112 | }
|
---|
113 | };
|
---|
114 |
|
---|
115 | /// Typedef for the typical usage of timer.
|
---|
116 | typedef boost::asio::basic_deadline_timer<boost::asio::monotone_time::mtime> monotone_timer;
|
---|
117 |
|
---|
118 | } // namespace asio
|
---|
119 | } // namespace boost
|
---|
120 |
|
---|
121 | #include <boost/asio/detail/pop_options.hpp>
|
---|
122 |
|
---|
123 |
|
---|
124 |
|
---|
125 | #endif // BOOST_ASIO_MONOTONE_TIMER_HPP
|
---|