cmake: Install host-plugin/native-plugin/standalone pkgconfig files
[carla.git] / source / utils / CarlaTimeUtils.hpp
blobe3913dadb2d6a032065a338c181ee6cfdc29db96
1 /*
2 * Carla time utils
3 * Copyright (C) 2011-2023 Filipe Coelho <falktx@falktx.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of
8 * the License, or any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * For a full copy of the GNU General Public License see the doc/GPL.txt file.
18 #ifndef CARLA_TIME_UTILS_HPP_INCLUDED
19 #define CARLA_TIME_UTILS_HPP_INCLUDED
21 #include "CarlaUtils.hpp"
23 #include <ctime>
25 #ifdef CARLA_OS_WIN
26 # include <mmsystem.h>
27 #endif
29 // --------------------------------------------------------------------------------------------------------------------
30 // carla_*sleep
33 * Sleep for 'secs' seconds.
35 static inline
36 void carla_sleep(const uint secs) noexcept
38 CARLA_SAFE_ASSERT_RETURN(secs > 0,);
40 try {
41 #ifdef CARLA_OS_WIN
42 ::Sleep(secs * 1000);
43 #else
44 ::sleep(secs);
45 #endif
46 } CARLA_SAFE_EXCEPTION("carla_sleep");
50 * Sleep for 'msecs' milliseconds.
52 static inline
53 void carla_msleep(const uint msecs) noexcept
55 CARLA_SAFE_ASSERT_RETURN(msecs > 0,);
57 try {
58 #ifdef CARLA_OS_WIN
59 ::Sleep(msecs);
60 #else
61 ::usleep(msecs * 1000);
62 #endif
63 } CARLA_SAFE_EXCEPTION("carla_msleep");
66 // --------------------------------------------------------------------------------------------------------------------
67 // carla_gettime_*
70 * Get a monotonically-increasing time in milliseconds.
72 static inline
73 uint32_t carla_gettime_ms() noexcept
75 #if defined(CARLA_OS_MAC)
76 static const time_t s = clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / 1000000;
77 return (clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / 1000000) - s;
78 #elif defined(CARLA_OS_WIN)
79 return static_cast<uint32_t>(timeGetTime());
80 #else
81 static struct {
82 timespec ts;
83 int r;
84 uint32_t ms;
85 } s = { {}, clock_gettime(CLOCK_MONOTONIC, &s.ts), static_cast<uint32_t>(s.ts.tv_sec * 1000 +
86 s.ts.tv_nsec / 1000000) };
87 timespec ts;
88 clock_gettime(CLOCK_MONOTONIC, &ts);
89 return (ts.tv_sec * 1000 + ts.tv_nsec / 1000000) - s.ms;
90 #endif
94 * Get a monotonically-increasing time in microseconds.
96 static inline
97 uint64_t carla_gettime_us() noexcept
99 #if defined(CARLA_OS_MAC)
100 static const uint64_t s = clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / 1000;
101 return (clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / 1000) - s;
102 #elif defined(CARLA_OS_WIN)
103 static struct {
104 LARGE_INTEGER freq;
105 LARGE_INTEGER counter;
106 BOOL r1, r2;
107 } s = { {}, {}, QueryPerformanceFrequency(&s.freq), QueryPerformanceCounter(&s.counter) };
109 LARGE_INTEGER counter;
110 QueryPerformanceCounter(&counter);
111 return (counter.QuadPart - s.counter.QuadPart) * 1000000 / s.freq.QuadPart;
112 #else
113 static struct {
114 timespec ts;
115 int r;
116 uint64_t us;
117 } s = { {}, clock_gettime(CLOCK_MONOTONIC, &s.ts), static_cast<uint64_t>(s.ts.tv_sec * 1000000 +
118 s.ts.tv_nsec / 1000) };
119 timespec ts;
120 clock_gettime(CLOCK_MONOTONIC, &ts);
121 return (ts.tv_sec * 1000000 + ts.tv_nsec / 1000) - s.us;
122 #endif
126 * Get a monotonically-increasing time in nanoseconds.
128 static inline
129 uint64_t carla_gettime_ns() noexcept
131 #if defined(CARLA_OS_MAC)
132 static const uint64_t s = clock_gettime_nsec_np(CLOCK_UPTIME_RAW);
133 return clock_gettime_nsec_np(CLOCK_UPTIME_RAW) - s;
134 #elif defined(CARLA_OS_WIN)
135 static struct {
136 LARGE_INTEGER freq;
137 LARGE_INTEGER counter;
138 BOOL r1, r2;
139 } s = { {}, {}, QueryPerformanceFrequency(&s.freq), QueryPerformanceCounter(&s.counter) };
141 LARGE_INTEGER counter;
142 QueryPerformanceCounter(&counter);
143 return (counter.QuadPart - s.counter.QuadPart) * 1000000000ULL / s.freq.QuadPart;
144 #else
145 static struct {
146 timespec ts;
147 int r;
148 uint64_t ns;
149 } s = { {}, clock_gettime(CLOCK_MONOTONIC, &s.ts), static_cast<uint64_t>(s.ts.tv_sec * 1000000000ULL +
150 s.ts.tv_nsec) };
151 timespec ts;
152 clock_gettime(CLOCK_MONOTONIC, &ts);
153 return (ts.tv_sec * 1000000000ULL + ts.tv_nsec) - s.ns;
154 #endif
157 // --------------------------------------------------------------------------------------------------------------------
159 #endif // CARLA_TIME_UTILS_HPP_INCLUDED