Dusk Audio Framework
Loading...
Searching...
No Matches
Time.hpp
1/*
2 * DISTRHO Plugin Framework (DPF)
3 * Copyright (C) 2012-2024 Filipe Coelho <falktx@falktx.com>
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any purpose with
6 * or without fee is hereby granted, provided that the above copyright notice and this
7 * permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
10 * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
11 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
12 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
13 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17#ifndef DAF_TIME_HPP_INCLUDED
18#define DAF_TIME_HPP_INCLUDED
19
20#include "../DafUtils.hpp"
21
22#ifdef DAF_OS_WINDOWS
23# include <winsock2.h>
24# include <windows.h>
25# include <mmsystem.h>
26#else
27# include <ctime>
28#endif
29
31
32// -----------------------------------------------------------------------------------------------------------
33// d_gettime_*
34
35/*
36 * Get a monotonically-increasing time in milliseconds.
37 */
38static inline
39uint32_t d_gettime_ms() noexcept
40{
41 #if defined(DAF_OS_MAC)
42 static const time_t s = clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / 1000000;
43 return (clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / 1000000) - s;
44 #elif defined(DAF_OS_WINDOWS)
45 return static_cast<uint32_t>(timeGetTime());
46 #else
47 static struct {
48 timespec ts;
49 int r;
50 uint32_t ms;
51 } s = { {}, clock_gettime(CLOCK_MONOTONIC, &s.ts), static_cast<uint32_t>(s.ts.tv_sec * 1000 +
52 s.ts.tv_nsec / 1000000) };
53 timespec ts;
54 clock_gettime(CLOCK_MONOTONIC, &ts);
55 return (ts.tv_sec * 1000 + ts.tv_nsec / 1000000) - s.ms;
56 #endif
57}
58
59/*
60 * Get a monotonically-increasing time in microseconds.
61 */
62static inline
63uint64_t d_gettime_us() noexcept
64{
65 #if defined(DAF_OS_MAC)
66 static const uint64_t s = clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / 1000;
67 return (clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / 1000) - s;
68 #elif defined(DAF_OS_WINDOWS)
69 static struct {
70 LARGE_INTEGER freq;
71 LARGE_INTEGER counter;
72 BOOL r1, r2;
73 } s = { {}, {}, QueryPerformanceFrequency(&s.freq), QueryPerformanceCounter(&s.counter) };
74
75 LARGE_INTEGER counter;
76 QueryPerformanceCounter(&counter);
77 return (counter.QuadPart - s.counter.QuadPart) * 1000000 / s.freq.QuadPart;
78 #else
79 static struct {
80 timespec ts;
81 int r;
82 uint64_t us;
83 } s = { {}, clock_gettime(CLOCK_MONOTONIC, &s.ts), static_cast<uint64_t>(s.ts.tv_sec * 1000000 +
84 s.ts.tv_nsec / 1000) };
85 timespec ts;
86 clock_gettime(CLOCK_MONOTONIC, &ts);
87 return (ts.tv_sec * 1000000 + ts.tv_nsec / 1000) - s.us;
88 #endif
89}
90
91/*
92 * Get a monotonically-increasing time in nanoseconds.
93 */
94static inline
95uint64_t d_gettime_ns() noexcept
96{
97 #if defined(DAF_OS_MAC)
98 static const uint64_t s = clock_gettime_nsec_np(CLOCK_UPTIME_RAW);
99 return clock_gettime_nsec_np(CLOCK_UPTIME_RAW) - s;
100 #elif defined(DAF_OS_WINDOWS)
101 static struct {
102 LARGE_INTEGER freq;
103 LARGE_INTEGER counter;
104 BOOL r1, r2;
105 } s = { {}, {}, QueryPerformanceFrequency(&s.freq), QueryPerformanceCounter(&s.counter) };
106
107 LARGE_INTEGER counter;
108 QueryPerformanceCounter(&counter);
109 return (counter.QuadPart - s.counter.QuadPart) * 1000000000ULL / s.freq.QuadPart;
110 #else
111 static struct {
112 timespec ts;
113 int r;
114 uint64_t ns;
115 } s = { {}, clock_gettime(CLOCK_MONOTONIC, &s.ts), static_cast<uint64_t>(s.ts.tv_sec * 1000000000ULL +
116 s.ts.tv_nsec) };
117 timespec ts;
118 clock_gettime(CLOCK_MONOTONIC, &ts);
119 return (ts.tv_sec * 1000000000ULL + ts.tv_nsec) - s.ns;
120 #endif
121}
122
123// -----------------------------------------------------------------------------------------------------------
124
126
127#endif // DAF_TIME_HPP_INCLUDED
#define END_NAMESPACE_DAF
Definition DafInfo.hpp:996
#define START_NAMESPACE_DAF
Definition DafInfo.hpp:990