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"
26 # include <mmsystem.h>
29 // --------------------------------------------------------------------------------------------------------------------
33 * Sleep for 'secs' seconds.
36 void carla_sleep(const uint secs
) noexcept
38 CARLA_SAFE_ASSERT_RETURN(secs
> 0,);
46 } CARLA_SAFE_EXCEPTION("carla_sleep");
50 * Sleep for 'msecs' milliseconds.
53 void carla_msleep(const uint msecs
) noexcept
55 CARLA_SAFE_ASSERT_RETURN(msecs
> 0,);
61 ::usleep(msecs
* 1000);
63 } CARLA_SAFE_EXCEPTION("carla_msleep");
66 // --------------------------------------------------------------------------------------------------------------------
70 * Get a monotonically-increasing time in milliseconds.
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());
85 } s
= { {}, clock_gettime(CLOCK_MONOTONIC
, &s
.ts
), static_cast<uint32_t>(s
.ts
.tv_sec
* 1000 +
86 s
.ts
.tv_nsec
/ 1000000) };
88 clock_gettime(CLOCK_MONOTONIC
, &ts
);
89 return (ts
.tv_sec
* 1000 + ts
.tv_nsec
/ 1000000) - s
.ms
;
94 * Get a monotonically-increasing time in microseconds.
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)
105 LARGE_INTEGER counter
;
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
;
117 } s
= { {}, clock_gettime(CLOCK_MONOTONIC
, &s
.ts
), static_cast<uint64_t>(s
.ts
.tv_sec
* 1000000 +
118 s
.ts
.tv_nsec
/ 1000) };
120 clock_gettime(CLOCK_MONOTONIC
, &ts
);
121 return (ts
.tv_sec
* 1000000 + ts
.tv_nsec
/ 1000) - s
.us
;
126 * Get a monotonically-increasing time in nanoseconds.
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)
137 LARGE_INTEGER counter
;
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
;
149 } s
= { {}, clock_gettime(CLOCK_MONOTONIC
, &s
.ts
), static_cast<uint64_t>(s
.ts
.tv_sec
* 1000000000ULL +
152 clock_gettime(CLOCK_MONOTONIC
, &ts
);
153 return (ts
.tv_sec
* 1000000000ULL + ts
.tv_nsec
) - s
.ns
;
157 // --------------------------------------------------------------------------------------------------------------------
159 #endif // CARLA_TIME_UTILS_HPP_INCLUDED