r42@gandalf: fugalh | 2006-06-07 17:08:39 -0600
[ardour.git] / libs / pbd3 / pbd / precision_timer.h
1 /*
2     Copyright (C) 1998-99 Paul Barton-Davis 
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18     $Id$
19 */
20
21 #ifndef __precision_timer_h__
22 #define __precision_timer_h__
23
24 #include <pbd/cycles.h>
25
26 typedef cycles_t precision_time_t;
27
28 class PrecisionTimer {
29   public:
30         PrecisionTimer ();
31
32         /* returns current time in microseconds since
33            the time base was created (which may be
34            the same as when the PrecisionTimer was
35            created or it may not).
36         */
37         
38 #ifdef PBD_HAVE_CYCLE_COUNTER
39
40         precision_time_t current () {
41                 return get_cycles() / cycles_per_usec;
42         }
43
44 #else /* !HAVE_CYCLE_COUNTER */
45
46         precision_time_t current () {
47                 struct timeval now;
48                 gettimeofday (&now, 0);
49                 return (precision_time_t) ((now.tv_sec * 1000000) + now.tv_usec);
50         }
51         
52 #endif /* HAVE_CYCLE_COUNTER */
53         
54   private:
55         int get_mhz();
56         static precision_time_t cycles_per_usec;
57 };
58
59 #endif // __precision_timer.h
60
61