5e3d2b4da8be1e5bcf09e9b1742310bbfa76f010
[ardour.git] / libs / backends / wavesaudio / wavesapi / MiscUtils / UMicroseconds.cpp
1 #ifdef PLATFORM_WINDOWS
2     #include "IncludeWindows.h"
3 #endif
4 #if defined(__linux__) || defined(__APPLE__)
5         #include <sys/time.h>
6 #endif
7
8 #include "UMicroseconds.h"
9
10 namespace wvNS { 
11 UMicroseconds& UMicroseconds::ReadTime()
12 {
13         // Note: g_get_monotonic_time() may be a viable alternative
14         // (it is on Linux and OSX); if not, this code should really go into libpbd
15 #ifdef PLATFORM_WINDOWS
16         LARGE_INTEGER Frequency, Count ;
17
18         QueryPerformanceFrequency(&Frequency) ;
19         QueryPerformanceCounter(&Count);
20         theTime = uint64_t((Count.QuadPart * 1000000.0 / Frequency.QuadPart));
21
22 #elif defined __MACH__ // OSX, BSD..
23
24         clock_serv_t cclock;
25         mach_timespec_t mts;
26         host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock);
27         clock_get_time(cclock, &mts);
28         mach_port_deallocate(mach_task_self(), cclock);
29         theTime = (uint64_t)mts.tv_sec * 1e6 + (uint64_t)mts.tv_nsec / 1000;
30
31 #else // Linux, POSIX
32
33         struct timespec *ts
34         clock_gettime(CLOCK_MONOTONIC, ts);
35         theTime = (uint64_t)ts.tv_sec * 1e6 + (uint64_t)buf.tv_nsec / 1000;
36
37 #endif
38
39         return *this;
40 }
41 /*
42  Removed in favor of the posix implementation. 
43 #ifdef __APPLE__
44         uint32_t UMicroseconds::hi() {return reinterpret_cast<UnsignedWide*>(&theTime)->hi;}
45         uint32_t UMicroseconds::lo() {return reinterpret_cast<UnsignedWide*>(&theTime)->lo;}
46 #endif
47 */
48 void UMicrosecondsAccumulator::Start()
49 {
50         m_start_time.ReadTime();
51 }
52
53 void UMicrosecondsAccumulator::Stop()
54 {
55         UMicroseconds stop_time;
56         
57         m_accumulator += stop_time.GetNativeTime() - m_start_time.GetNativeTime();
58 }
59
60 void UMicrosecondsAccumulator::Clear()
61 {
62         m_start_time = 0;
63         m_accumulator = 0;
64 }
65
66 UMicroseconds UMicrosecondsAccumulator::GetAccumulatedTime() const
67 {
68         return m_accumulator;
69 }
70
71 UMicrosecondsAccumulator& UMicrosecondsAccumulator::operator+=(const UMicrosecondsAccumulator& inaccum_to_add)
72 {
73         m_accumulator += inaccum_to_add.GetAccumulatedTime();
74         return *this;
75 }
76         
77 } // namespace wvNS {