2021-06-26 20:30:11 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2021 Paul Davis <paul@linuxaudiosystems.com>
|
|
|
|
* Copyright (C) 2013-2015 Tim Mayberry <mojofunk@gmail.com>
|
|
|
|
* Copyright (C) 2014-2015 John Emmas <john@creativepost.co.uk>
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* (at your option) 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.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#ifdef PLATFORM_WINDOWS
|
2021-06-27 14:48:29 -04:00
|
|
|
#include <windows.h>
|
2021-06-26 20:30:11 -04:00
|
|
|
#endif
|
|
|
|
|
2021-06-26 20:33:27 -04:00
|
|
|
#include "pbd/error.h"
|
2021-06-26 20:30:11 -04:00
|
|
|
#include "pbd/microseconds.h"
|
2021-06-26 20:43:17 -04:00
|
|
|
#include "pbd/i18n.h"
|
2021-06-26 20:30:11 -04:00
|
|
|
|
|
|
|
#ifdef PLATFORM_WINDOWS
|
|
|
|
static double timer_rate_usecs = 0.0;
|
|
|
|
#endif
|
|
|
|
|
2021-06-26 23:09:07 -04:00
|
|
|
#ifdef __MACH__
|
2021-06-27 10:24:59 -04:00
|
|
|
#include <mach/mach_time.h>
|
2021-06-27 12:39:51 -04:00
|
|
|
|
2021-06-27 10:24:59 -04:00
|
|
|
#ifndef CLOCK_REALTIME
|
2021-06-27 12:39:51 -04:00
|
|
|
#define MACH_NEED_MICROSECONDS_TIMEBASE
|
|
|
|
|
|
|
|
static mach_timebase_info_data_t timebase;
|
|
|
|
|
2021-06-26 23:09:07 -04:00
|
|
|
/* Thanks Apple for not implementing this basic SUSv2, POSIX.1-2001 function
|
|
|
|
*/
|
|
|
|
#define CLOCK_REALTIME 0
|
|
|
|
#define CLOCK_MONOTONIC 0
|
2021-06-27 12:39:51 -04:00
|
|
|
|
2021-06-26 23:09:07 -04:00
|
|
|
int
|
|
|
|
clock_gettime (int /*clk_id*/, struct timespec* t)
|
|
|
|
{
|
|
|
|
uint64_t time;
|
|
|
|
time = mach_absolute_time ();
|
|
|
|
double nseconds = ((double)time * (double)timebase.numer) / ((double)timebase.denom);
|
|
|
|
double seconds = ((double)time * (double)timebase.numer) / ((double)timebase.denom * 1e9);
|
|
|
|
t->tv_sec = seconds;
|
|
|
|
t->tv_nsec = nseconds;
|
|
|
|
return 0;
|
|
|
|
}
|
2021-06-27 12:39:51 -04:00
|
|
|
|
|
|
|
#endif /* CLOCK_REALTIME */
|
|
|
|
#endif /* __MACH__ */
|
2021-06-26 23:09:07 -04:00
|
|
|
|
2021-06-26 20:30:11 -04:00
|
|
|
void
|
|
|
|
PBD::microsecond_timer_init ()
|
|
|
|
{
|
|
|
|
#ifdef PLATFORM_WINDOWS
|
|
|
|
LARGE_INTEGER freq;
|
|
|
|
if (!QueryPerformanceFrequency(&freq) || freq.QuadPart < 1) {
|
|
|
|
info << X_("Failed to determine frequency of QPC\n") << endmsg;
|
2021-06-26 20:53:28 -04:00
|
|
|
timer_rate_usecs = 0;
|
2021-06-26 20:30:11 -04:00
|
|
|
} else {
|
2021-06-26 20:53:28 -04:00
|
|
|
timer_rate_usecs = 1000000.0 / freq.QuadPart;
|
2021-06-26 20:30:11 -04:00
|
|
|
}
|
|
|
|
#endif
|
2021-06-27 12:39:51 -04:00
|
|
|
#ifdef MACH_NEED_MICROSECONDS_TIMEBASE
|
2021-06-27 10:16:46 -04:00
|
|
|
mach_timebase_info (&timebase);
|
|
|
|
#endif
|
2021-06-26 20:30:11 -04:00
|
|
|
}
|
|
|
|
|
2021-06-27 12:28:53 -04:00
|
|
|
/** Return a monotonic value for the number of microseconds that have elapsed
|
|
|
|
* since an arbitrary zero origin.
|
|
|
|
*/
|
2021-06-26 20:30:11 -04:00
|
|
|
PBD::microseconds_t
|
|
|
|
PBD::get_microseconds ()
|
|
|
|
{
|
|
|
|
#ifdef PLATFORM_WINDOWS
|
|
|
|
LARGE_INTEGER time;
|
|
|
|
|
2021-06-26 20:35:14 -04:00
|
|
|
if (timer_rate_usecs) {
|
2021-06-27 12:53:31 -04:00
|
|
|
if (QueryPerformanceCounter (&time)) {
|
2021-06-26 20:53:28 -04:00
|
|
|
return (microseconds_t) (time.QuadPart * timer_rate_usecs);
|
2021-06-26 20:30:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (microseconds_t) 0;
|
|
|
|
#else
|
|
|
|
struct timespec ts;
|
|
|
|
if (clock_gettime (CLOCK_MONOTONIC, &ts) != 0) {
|
|
|
|
/* EEEK! */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return (microseconds_t)ts.tv_sec * 1000000 + (ts.tv_nsec / 1000);
|
|
|
|
#endif
|
|
|
|
}
|