2 Copyright 2005-2011 Intel Corporation. All Rights Reserved.
4 This file is part of Threading Building Blocks.
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.
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.
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
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.
29 #ifndef __TBB_tick_count_H
30 #define __TBB_tick_count_H
32 #include "tbb_stddef.h"
35 #include "machine/windows_api.h"
38 #else /* generic Unix */
40 #endif /* (choice of OS) */
44 //! Absolute timestamp
45 /** @ingroup timing */
48 //! Relative time interval.
51 explicit interval_t( long long value_ ) : value(value_) {}
53 //! Construct a time interval representing zero time duration
54 interval_t() : value(0) {};
56 //! Construct a time interval representing sec seconds time duration
57 explicit interval_t( double sec );
59 //! Return the length of a time interval in seconds
60 double seconds() const;
62 friend class tbb::tick_count;
64 //! Extract the intervals from the tick_counts and subtract them.
65 friend interval_t operator-( const tick_count& t1, const tick_count& t0 );
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);
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);
77 //! Accumulation operator
78 interval_t& operator+=( const interval_t& i ) {value += i.value; return *this;}
80 //! Subtraction operator
81 interval_t& operator-=( const interval_t& i ) {value -= i.value; return *this;}
84 //! Construct an absolute timestamp initialized to zero.
85 tick_count() : my_count(0) {};
87 //! Return current time.
88 static tick_count now();
90 //! Subtract two timestamps to get the time interval between
91 friend interval_t operator-( const tick_count& t1, const tick_count& t0 );
97 inline tick_count tick_count::now() {
101 QueryPerformanceCounter(&qpcnt);
102 result.my_count = qpcnt.QuadPart;
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 */
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) */
123 inline tick_count::interval_t::interval_t( double sec )
126 LARGE_INTEGER qpfreq;
127 QueryPerformanceFrequency(&qpfreq);
128 value = static_cast<long long>(sec*qpfreq.QuadPart);
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) */
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 );
140 inline double tick_count::interval_t::seconds() const {
142 LARGE_INTEGER qpfreq;
143 QueryPerformanceFrequency(&qpfreq);
144 return value/(double)qpfreq.QuadPart;
147 #else /* generic Unix */
149 #endif /* (choice of OS) */
154 #endif /* __TBB_tick_count_H */