]> git.sesse.net Git - casparcg/blob - tbb30_20100406oss/include/tbb/tick_count.h
2.0.2: Updated to boost 1.48.
[casparcg] / tbb30_20100406oss / include / tbb / tick_count.h
1 /*
2     Copyright 2005-2010 Intel Corporation.  All Rights Reserved.
3
4     This file is part of Threading Building Blocks.
5
6     Threading Building Blocks is free software; you can redistribute it
7     and/or modify it under the terms of the GNU General Public License
8     version 2 as published by the Free Software Foundation.
9
10     Threading Building Blocks is distributed in the hope that it will be
11     useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12     of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with Threading Building Blocks; if not, write to the Free Software
17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
19     As a special exception, you may use this file as part of a free software
20     library without restriction.  Specifically, if other files instantiate
21     templates or use macros or inline functions from this file, or you compile
22     this file and link it with other files to produce an executable, this
23     file does not by itself cause the resulting executable to be covered by
24     the GNU General Public License.  This exception does not however
25     invalidate any other reasons why the executable file might be covered by
26     the GNU General Public License.
27 */
28
29 #ifndef __TBB_tick_count_H
30 #define __TBB_tick_count_H
31
32 #include "tbb_stddef.h"
33
34 #if _WIN32||_WIN64
35 #include <windows.h>
36 #elif __linux__
37 #include <ctime>
38 #else /* generic Unix */
39 #include <sys/time.h>
40 #endif /* (choice of OS) */
41
42 namespace tbb {
43
44 //! Absolute timestamp
45 /** @ingroup timing */
46 class tick_count {
47 public:
48     //! Relative time interval.
49     class interval_t {
50         long long value;
51         explicit interval_t( long long value_ ) : value(value_) {}
52     public:
53         //! Construct a time interval representing zero time duration
54         interval_t() : value(0) {};
55
56         //! Construct a time interval representing sec seconds time  duration
57         explicit interval_t( double sec );
58
59         //! Return the length of a time interval in seconds
60         double seconds() const;
61
62         friend class tbb::tick_count;
63
64         //! Extract the intervals from the tick_counts and subtract them.
65         friend interval_t operator-( const tick_count& t1, const tick_count& t0 );
66
67         //! Add two intervals.
68         friend interval_t operator+( const interval_t& i, const interval_t& j ) {
69             return interval_t(i.value+j.value);
70         }
71
72         //! Subtract two intervals.
73         friend interval_t operator-( const interval_t& i, const interval_t& j ) {
74             return interval_t(i.value-j.value);
75         }
76
77         //! Accumulation operator
78         interval_t& operator+=( const interval_t& i ) {value += i.value; return *this;}
79
80         //! Subtraction operator
81         interval_t& operator-=( const interval_t& i ) {value -= i.value; return *this;}
82     };
83     
84     //! Construct an absolute timestamp initialized to zero.
85     tick_count() : my_count(0) {};
86
87     //! Return current time.
88     static tick_count now();
89     
90     //! Subtract two timestamps to get the time interval between
91     friend interval_t operator-( const tick_count& t1, const tick_count& t0 );
92
93 private:
94     long long my_count;
95 };
96
97 inline tick_count tick_count::now() {
98     tick_count result;
99 #if _WIN32||_WIN64
100     LARGE_INTEGER qpcnt;
101     QueryPerformanceCounter(&qpcnt);
102     result.my_count = qpcnt.QuadPart;
103 #elif __linux__
104     struct timespec ts;
105 #if TBB_USE_ASSERT
106     int status = 
107 #endif /* TBB_USE_ASSERT */
108         clock_gettime( CLOCK_REALTIME, &ts );
109     __TBB_ASSERT( status==0, "CLOCK_REALTIME not supported" );
110     result.my_count = static_cast<long long>(1000000000UL)*static_cast<long long>(ts.tv_sec) + static_cast<long long>(ts.tv_nsec);
111 #else /* generic Unix */
112     struct timeval tv;
113 #if TBB_USE_ASSERT
114     int status = 
115 #endif /* TBB_USE_ASSERT */
116         gettimeofday(&tv, NULL);
117     __TBB_ASSERT( status==0, "gettimeofday failed" );
118     result.my_count = static_cast<long long>(1000000)*static_cast<long long>(tv.tv_sec) + static_cast<long long>(tv.tv_usec);
119 #endif /*(choice of OS) */
120     return result;
121 }
122
123 inline tick_count::interval_t::interval_t( double sec )
124 {
125 #if _WIN32||_WIN64
126     LARGE_INTEGER qpfreq;
127     QueryPerformanceFrequency(&qpfreq);
128     value = static_cast<long long>(sec*qpfreq.QuadPart);
129 #elif __linux__
130     value = static_cast<long long>(sec*1E9);
131 #else /* generic Unix */
132     value = static_cast<long long>(sec*1E6);
133 #endif /* (choice of OS) */
134 }
135
136 inline tick_count::interval_t operator-( const tick_count& t1, const tick_count& t0 ) {
137     return tick_count::interval_t( t1.my_count-t0.my_count );
138 }
139
140 inline double tick_count::interval_t::seconds() const {
141 #if _WIN32||_WIN64
142     LARGE_INTEGER qpfreq;
143     QueryPerformanceFrequency(&qpfreq);
144     return value/(double)qpfreq.QuadPart;
145 #elif __linux__
146     return value*1E-9;
147 #else /* generic Unix */
148     return value*1E-6;
149 #endif /* (choice of OS) */
150 }
151
152 } // namespace tbb
153
154 #endif /* __TBB_tick_count_H */
155