Introduce new header file for time-related utilities

Signed-off-by: falkTX <falktx@falktx.com>
This commit is contained in:
falkTX 2023-05-16 20:37:55 +02:00
parent f272c0ef11
commit 54cdc0b5e8
No known key found for this signature in database
GPG Key ID: CDBAA37ABC74FBA0
9 changed files with 172 additions and 42 deletions

View File

@ -26,6 +26,8 @@
#include "CarlaBackendUtils.hpp"
#include "CarlaJuceUtils.hpp"
#include "CarlaMainLoop.hpp"
#include "CarlaTimeUtils.hpp"
#include "CarlaMIDI.h"
#ifdef CARLA_OS_MAC

View File

@ -1,6 +1,6 @@
/*
* Carla Bridge UI
* Copyright (C) 2011-2021 Filipe Coelho <falktx@falktx.com>
* Copyright (C) 2011-2023 Filipe Coelho <falktx@falktx.com>
*
* 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"

View File

@ -1,6 +1,6 @@
/*
* Carla Bridge UI
* Copyright (C) 2014-2022 Filipe Coelho <falktx@falktx.com>
* Copyright (C) 2014-2023 Filipe Coelho <falktx@falktx.com>
*
* 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)

View File

@ -3,7 +3,7 @@
This file is part of the Water library.
Copyright (c) 2016 ROLI Ltd.
Copyright (C) 2017 Filipe Coelho <falktx@falktx.com>
Copyright (C) 2017-2023 Filipe Coelho <falktx@falktx.com>
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,

View File

@ -1,6 +1,6 @@
/*
* Carla Bridge utils
* Copyright (C) 2013-2020 Filipe Coelho <falktx@falktx.com>
* Copyright (C) 2013-2023 Filipe Coelho <falktx@falktx.com>
*
* 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"

View File

@ -1,6 +1,6 @@
/*
* Carla Runner
* Copyright (C) 2022 Filipe Coelho <falktx@falktx.com>
* Copyright (C) 2022-2023 Filipe Coelho <falktx@falktx.com>
*
* 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

View File

@ -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!

View File

@ -0,0 +1,159 @@
/*
* Carla time utils
* Copyright (C) 2011-2023 Filipe Coelho <falktx@falktx.com>
*
* 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 <ctime>
#ifdef CARLA_OS_WIN
# include <mmsystem.h>
#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<time_t>(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

View File

@ -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