13
0

macOS 10.12 finally started defining CLOCK_{REALTIME,MONOTONIC} and I think this is the correct way to test for that

This commit is contained in:
Paul Davis 2021-03-29 11:22:26 -06:00
parent 3256350ef8
commit 3076d22987

View File

@ -917,6 +917,60 @@ ARDOUR::get_available_sync_options ()
return ret;
}
/** Return a monotonic value for the number of microseconds that have elapsed
* since an arbitrary zero origin.
*/
#ifdef __APPLE__
#if __MAC_OS_X_VERSION_MAX_ALLOWED < __MAC_10_12
/* Thanks Apple for not implementing this basic SUSv2, POSIX.1-2001 function
*/
#include <mach/mach_time.h>
#define CLOCK_REALTIME 0
#define CLOCK_MONOTONIC 0
int
clock_gettime (int /*clk_id*/, struct timespec* t)
{
static bool initialized = false;
static mach_timebase_info_data_t timebase;
if (!initialized) {
mach_timebase_info (&timebase);
initialized = true;
}
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;
}
#endif
#endif
microseconds_t
ARDOUR::get_microseconds ()
{
#ifdef PLATFORM_WINDOWS
microseconds_t ret = 0;
LARGE_INTEGER freq, time;
if (QueryPerformanceFrequency (&freq))
if (QueryPerformanceCounter (&time))
ret = (microseconds_t) ((time.QuadPart * 1000000) / freq.QuadPart);
return ret;
#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
}
/** Return the number of bits per sample for a given sample format.
*
* This is closely related to sndfile_data_width() but does NOT