zenilib  0.5.3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Timer_HQ.cpp
Go to the documentation of this file.
1 /* This file is part of the Zenipex Library (zenilib).
2  * Copyright (C) 2011 Mitchell Keith Bloch (bazald).
3  *
4  * zenilib is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * zenilib 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 Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with zenilib. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <zeni.h>
19 
20 #if defined(_DEBUG) && defined(_WINDOWS)
21 #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
22 #define new DEBUG_NEW
23 #endif
24 
25 #include <Zeni/Chronometer.hxx>
26 #include <Zeni/Singleton.hxx>
27 
28 #ifdef _MACOSX
29 #include <mach/mach_time.h>
30 #endif
31 
32 namespace Zeni {
33 
34 #ifndef _WINDOWS
35  timespec subtract(const timespec &lhs, const timespec &rhs) {
36  timespec retval = lhs;
37 
38  retval.tv_sec -= rhs.tv_sec;
39 
40  if(retval.tv_nsec > rhs.tv_nsec)
41  retval.tv_nsec -= rhs.tv_nsec;
42  else {
43  --retval.tv_sec;
44  retval.tv_nsec = 1000000000 - (rhs.tv_nsec - retval.tv_nsec);
45  }
46 
47  return retval;
48  }
49 
50  long double to_seconds(const timespec &ticks) {
51  return ticks.tv_sec + ticks.tv_nsec / 1000000000.0;
52  }
53 
54  long double to_useconds(const timespec &ticks) {
55  return 1000000000.0 * ticks.tv_sec + ticks.tv_nsec;
56  }
57 
58 #ifdef _MACOSX
59  timespec orwl_gettime(void) {
60  static const double ORWL_NANO = +1.0e-9;
61  static const uint64_t ORWL_GIGA = 1000000000;
62 
63  static double orwl_timebase = 0.0;
64  static uint64_t orwl_timestart = 0;
65 
66  // be more careful in a multithreaded environement
67  if(!orwl_timestart) {
68  mach_timebase_info_data_t tb = {0, 0};
69  mach_timebase_info(&tb);
70  orwl_timebase = tb.numer;
71  orwl_timebase /= tb.denom;
72  orwl_timestart = mach_absolute_time();
73  }
74 
75  struct timespec t;
76  double diff = (mach_absolute_time() - orwl_timestart) * orwl_timebase;
77  t.tv_sec = diff * ORWL_NANO;
78  t.tv_nsec = diff - (t.tv_sec * ORWL_GIGA);
79  return t;
80  }
81 #endif
82 #endif
83 
85  : m_ticks(ticks),
86  m_ticks_per_second(get_Timer_HQ().get_ticks_per_second())
87  {
88  }
89 
90  Time_HQ::Time_HQ(const HQ_Tick_Type &ticks, const HQ_Tick_Type &ticks_per_second)
91  : m_ticks(ticks),
92  m_ticks_per_second(ticks_per_second)
93  {
94  }
95 
97  : m_ticks(get_Timer_HQ().get_ticks()),
98  m_ticks_per_second(get_Timer_HQ().get_ticks_per_second())
99  {
100  }
101 
102  template class Singleton<Timer_HQ>;
103  template class Chronometer<Time_HQ>;
104 
105  Timer_HQ * Timer_HQ::create() {
106  return new Timer_HQ;
107  }
108 
109  Timer_HQ::Timer_HQ()
110  : m_ticks_per_second(1000)
111  {
112 #ifdef _WINDOWS
113  LARGE_INTEGER lpFrequency;
114  if(QueryPerformanceFrequency(&lpFrequency) == 0)
115  throw Timer_HQ_Init_Failure();
116 
117  m_ticks_per_second = unsigned long(lpFrequency.QuadPart);
118 #else
119  m_ticks_per_second = 1000000000;
120 #endif
121  }
122 
124  return Timer_HQ::get();
125  }
126 
127 }
A High Quality Timer Singleton.
Definition: Timer_HQ.h:115
long double to_seconds(const timespec &ticks)
Definition: Timer_HQ.cpp:50
long double HQ_Tick_Type
Definition: Timer_HQ.h:69
unsigned long long uint64_t
GLdouble GLdouble t
Definition: glew.h:1384
Timer_HQ & get_Timer_HQ()
Get access to the singleton.
Definition: Timer_HQ.cpp:123
long double to_useconds(const timespec &ticks)
Definition: Timer_HQ.cpp:54
static Timer_HQ & get()
A stoppable running timer.
Definition: Chronometer.h:40
timespec subtract(const timespec &lhs, const timespec &rhs)
Definition: Timer_HQ.cpp:35
Time_HQ()
Initialize to the current time.
Definition: Timer_HQ.cpp:96