From 54cdc0b5e88c641e2bb6b1da4d70bf7823875abd Mon Sep 17 00:00:00 2001 From: falkTX Date: Tue, 16 May 2023 20:37:55 +0200 Subject: [PATCH] Introduce new header file for time-related utilities Signed-off-by: falkTX --- source/bridges-plugin/CarlaBridgePlugin.cpp | 2 + source/bridges-ui/CarlaBridgeFormat.cpp | 3 +- .../bridges-ui/CarlaBridgeToolkitNative.cpp | 3 +- source/modules/water/files/TemporaryFile.cpp | 4 +- source/utils/CarlaBridgeUtils.cpp | 3 +- source/utils/CarlaRunner.hpp | 2 +- source/utils/CarlaThread.hpp | 1 + source/utils/CarlaTimeUtils.hpp | 159 ++++++++++++++++++ source/utils/CarlaUtils.hpp | 37 ---- 9 files changed, 172 insertions(+), 42 deletions(-) create mode 100644 source/utils/CarlaTimeUtils.hpp diff --git a/source/bridges-plugin/CarlaBridgePlugin.cpp b/source/bridges-plugin/CarlaBridgePlugin.cpp index b91cc5a8c..2399ea15c 100644 --- a/source/bridges-plugin/CarlaBridgePlugin.cpp +++ b/source/bridges-plugin/CarlaBridgePlugin.cpp @@ -26,6 +26,8 @@ #include "CarlaBackendUtils.hpp" #include "CarlaJuceUtils.hpp" #include "CarlaMainLoop.hpp" +#include "CarlaTimeUtils.hpp" + #include "CarlaMIDI.h" #ifdef CARLA_OS_MAC diff --git a/source/bridges-ui/CarlaBridgeFormat.cpp b/source/bridges-ui/CarlaBridgeFormat.cpp index 3e347f22f..2703fa289 100644 --- a/source/bridges-ui/CarlaBridgeFormat.cpp +++ b/source/bridges-ui/CarlaBridgeFormat.cpp @@ -1,6 +1,6 @@ /* * Carla Bridge UI - * Copyright (C) 2011-2021 Filipe Coelho + * Copyright (C) 2011-2023 Filipe Coelho * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as @@ -20,6 +20,7 @@ #include "CarlaBase64Utils.hpp" #include "CarlaProcessUtils.hpp" +#include "CarlaTimeUtils.hpp" #include "CarlaMIDI.h" diff --git a/source/bridges-ui/CarlaBridgeToolkitNative.cpp b/source/bridges-ui/CarlaBridgeToolkitNative.cpp index 61933ad97..452598d8f 100644 --- a/source/bridges-ui/CarlaBridgeToolkitNative.cpp +++ b/source/bridges-ui/CarlaBridgeToolkitNative.cpp @@ -1,6 +1,6 @@ /* * Carla Bridge UI - * Copyright (C) 2014-2022 Filipe Coelho + * Copyright (C) 2014-2023 Filipe Coelho * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as @@ -20,6 +20,7 @@ #include "CarlaMainLoop.hpp" #include "CarlaPluginUI.hpp" +#include "CarlaTimeUtils.hpp" #include "CarlaUtils.h" #if defined(CARLA_OS_MAC) && defined(BRIDGE_COCOA) diff --git a/source/modules/water/files/TemporaryFile.cpp b/source/modules/water/files/TemporaryFile.cpp index 5dd5ed5df..76a235614 100644 --- a/source/modules/water/files/TemporaryFile.cpp +++ b/source/modules/water/files/TemporaryFile.cpp @@ -3,7 +3,7 @@ This file is part of the Water library. Copyright (c) 2016 ROLI Ltd. - Copyright (C) 2017 Filipe Coelho + Copyright (C) 2017-2023 Filipe Coelho Permission is granted to use this software under the terms of the ISC license http://www.isc.org/downloads/software-support-policy/isc-license/ @@ -26,6 +26,8 @@ #include "TemporaryFile.h" #include "../maths/Random.h" +#include "CarlaTimeUtils.hpp" + namespace water { static File createTempFile (const File& parentDirectory, String name, diff --git a/source/utils/CarlaBridgeUtils.cpp b/source/utils/CarlaBridgeUtils.cpp index 8bc24994d..74b376909 100644 --- a/source/utils/CarlaBridgeUtils.cpp +++ b/source/utils/CarlaBridgeUtils.cpp @@ -1,6 +1,6 @@ /* * Carla Bridge utils - * Copyright (C) 2013-2020 Filipe Coelho + * Copyright (C) 2013-2023 Filipe Coelho * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as @@ -17,6 +17,7 @@ #include "CarlaBridgeUtils.hpp" #include "CarlaShmUtils.hpp" +#include "CarlaTimeUtils.hpp" // must be last #include "jackbridge/JackBridge.hpp" diff --git a/source/utils/CarlaRunner.hpp b/source/utils/CarlaRunner.hpp index b6f4a3862..c22f1b031 100644 --- a/source/utils/CarlaRunner.hpp +++ b/source/utils/CarlaRunner.hpp @@ -1,6 +1,6 @@ /* * Carla Runner - * Copyright (C) 2022 Filipe Coelho + * Copyright (C) 2022-2023 Filipe Coelho * * Permission to use, copy, modify, and/or distribute this software for any purpose with * or without fee is hereby granted, provided that the above copyright notice and this diff --git a/source/utils/CarlaThread.hpp b/source/utils/CarlaThread.hpp index 2f376c3d2..b871cf9e4 100644 --- a/source/utils/CarlaThread.hpp +++ b/source/utils/CarlaThread.hpp @@ -21,6 +21,7 @@ #include "CarlaMutex.hpp" #include "CarlaString.hpp" #include "CarlaProcessUtils.hpp" +#include "CarlaTimeUtils.hpp" #ifdef CARLA_OS_WASM # error Threads do not work under wasm! diff --git a/source/utils/CarlaTimeUtils.hpp b/source/utils/CarlaTimeUtils.hpp new file mode 100644 index 000000000..abbb7f65f --- /dev/null +++ b/source/utils/CarlaTimeUtils.hpp @@ -0,0 +1,159 @@ +/* + * Carla time utils + * Copyright (C) 2011-2023 Filipe Coelho + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * For a full copy of the GNU General Public License see the doc/GPL.txt file. + */ + +#ifndef CARLA_TIME_UTILS_HPP_INCLUDED +#define CARLA_TIME_UTILS_HPP_INCLUDED + +#include "CarlaUtils.hpp" + +#include + +#ifdef CARLA_OS_WIN +# include +#endif + +// -------------------------------------------------------------------------------------------------------------------- +// carla_*sleep + +/* + * Sleep for 'secs' seconds. + */ +static inline +void carla_sleep(const uint secs) noexcept +{ + CARLA_SAFE_ASSERT_RETURN(secs > 0,); + + try { + #ifdef CARLA_OS_WIN + ::Sleep(secs * 1000); + #else + ::sleep(secs); + #endif + } CARLA_SAFE_EXCEPTION("carla_sleep"); +} + +/* + * Sleep for 'msecs' milliseconds. + */ +static inline +void carla_msleep(const uint msecs) noexcept +{ + CARLA_SAFE_ASSERT_RETURN(msecs > 0,); + + try { + #ifdef CARLA_OS_WIN + ::Sleep(msecs); + #else + ::usleep(msecs * 1000); + #endif + } CARLA_SAFE_EXCEPTION("carla_msleep"); +} + +// -------------------------------------------------------------------------------------------------------------------- +// carla_gettime_* + +/* + * Get a monotonically-increasing time in milliseconds. + */ +static inline +time_t carla_gettime_ms() noexcept +{ + #if defined(CARLA_OS_MAC) + static const time_t s = clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / 1000000; + return (clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / 1000000) - s; + #elif defined(CARLA_OS_WIN) + return static_cast(timeGetTime()); + #else + static struct { + timespec ts; + int r; + time_t ms; + } s = { {}, clock_gettime(CLOCK_MONOTONIC, &s.ts), s.ts.tv_sec * 1000 + s.ts.tv_nsec / 1000000 }; + + timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + return (ts.tv_sec * 1000 + ts.tv_nsec / 1000000) - s.ms; + #endif +} + +/* + * Get a monotonically-increasing time in microseconds. + */ +static inline +uint64_t carla_gettime_us() noexcept +{ + #if defined(CARLA_OS_MAC) + static const uint64_t s = clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / 1000; + return (clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / 1000) - s; + #elif defined(CARLA_OS_WIN) + static struct { + LARGE_INTEGER freq; + LARGE_INTEGER counter; + BOOL r1, r2; + } s = { {}, {}, QueryPerformanceFrequency(&s.freq), QueryPerformanceCounter(&s.counter) }; + + LARGE_INTEGER counter; + QueryPerformanceCounter(&counter); + return (counter.QuadPart - s.counter.QuadPart) * 1000000 / s.freq.QuadPart; + #else + static struct { + timespec ts; + int r; + uint64_t us; + } s = { {}, clock_gettime(CLOCK_MONOTONIC, &s.ts), s.ts.tv_sec * 1000000 + s.ts.tv_nsec / 1000 }; + + timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + return (ts.tv_sec * 1000000 + ts.tv_nsec / 1000) - s.us; + #endif +} + +/* + * Get a monotonically-increasing time in nanoseconds. + */ +static inline +uint64_t carla_gettime_ns() noexcept +{ + #if defined(CARLA_OS_MAC) + static const uint64_t s = clock_gettime_nsec_np(CLOCK_UPTIME_RAW); + return clock_gettime_nsec_np(CLOCK_UPTIME_RAW) - s; + #elif defined(CARLA_OS_WIN) + static struct { + LARGE_INTEGER freq; + LARGE_INTEGER counter; + BOOL r1, r2; + } s = { {}, {}, QueryPerformanceFrequency(&s.freq), QueryPerformanceCounter(&s.counter) }; + + LARGE_INTEGER counter; + QueryPerformanceCounter(&counter); + return (counter.QuadPart - s.counter.QuadPart) * 1000000000ULL / s.freq.QuadPart; + #else + static struct { + timespec ts; + int r; + uint64_t ns; + } s = { {}, clock_gettime(CLOCK_MONOTONIC, &s.ts), s.ts.tv_sec * 1000000000ULL + s.ts.tv_nsec }; + + timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + return (ts.tv_sec * 1000000000ULL + ts.tv_nsec) - s.ns; + #endif +} + +// -------------------------------------------------------------------------------------------------------------------- + +#endif // CARLA_TIME_UTILS_HPP_INCLUDED diff --git a/source/utils/CarlaUtils.hpp b/source/utils/CarlaUtils.hpp index 830aeebdb..9b563aa96 100644 --- a/source/utils/CarlaUtils.hpp +++ b/source/utils/CarlaUtils.hpp @@ -290,43 +290,6 @@ void carla_safe_exception(const char* const exception, const char* const file, c carla_stderr2("Carla exception caught: \"%s\" in file %s, line %i", exception, file, line); } -// -------------------------------------------------------------------------------------------------------------------- -// carla_*sleep - -/* - * Sleep for 'secs' seconds. - */ -static inline -void carla_sleep(const uint secs) noexcept -{ - CARLA_SAFE_ASSERT_RETURN(secs > 0,); - - try { -#ifdef CARLA_OS_WIN - ::Sleep(secs * 1000); -#else - ::sleep(secs); -#endif - } CARLA_SAFE_EXCEPTION("carla_sleep"); -} - -/* - * Sleep for 'msecs' milliseconds. - */ -static inline -void carla_msleep(const uint msecs) noexcept -{ - CARLA_SAFE_ASSERT_RETURN(msecs > 0,); - - try { -#ifdef CARLA_OS_WIN - ::Sleep(msecs); -#else - ::usleep(msecs * 1000); -#endif - } CARLA_SAFE_EXCEPTION("carla_msleep"); -} - // -------------------------------------------------------------------------------------------------------------------- // carla_setenv