Change MacOS detection logic for clock_gettime to hopefully cover all flavors

pull/519/head^2
Michael Black W9MDB 2021-01-26 15:05:43 -06:00
rodzic 9695828b19
commit 30eca94210
1 zmienionych plików z 45 dodań i 29 usunięć

Wyświetl plik

@ -57,48 +57,64 @@
#include "misc.h"
#include "serial.h"
#include "network.h"
#ifdef __APPLE__
#ifndef HAVE_CLOCK_GETTIME
#include <AvailabilityMacros.h>
#if AVAILABLE_MAC_OS_X_VERSION_10_8_AND_LATER
#else
# include <mach/mach_time.h>
# ifndef __clockid_t_defined
typedef enum clockid_t;
#define __clockid_t_defined 1
# endif /* __clockid_t_defined */
# ifndef __clockid_t_defined
typedef int clockid_t;
#define __clockid_t_defined 1
# endif /* __clockid_t_defined */
# define CLOCK_REALTIME 0
# define CLOCK_MONOTONIC 6
# define CLOCK_REALTIME 0
# define CLOCK_MONOTONIC 1
int clock_gettime(clockid_t clock_id, struct timespec* tp)
int clock_gettime(clockid_t clock_id, struct timespec *tp)
{
if (clock_id == CLOCK_REALTIME) {
struct timeval t;
if (gettimeofday(&t, NULL) != 0)
return -1;
tp->tv_sec = t.tv_sec;
tp->tv_nsec = t.tv_usec * 1000;
}
else if (clock_id == CLOCK_MONOTONIC) {
static mach_timebase_info_data_t info = { 0, 0 };
if (info.denom == 0)
mach_timebase_info(&info);
uint64_t t = mach_absolute_time() * info.numer / info.denom;
tp->tv_sec = t / 1000000000;
tp->tv_nsec = t % 1000000000;
}
else {
errno = EINVAL;
return -1;
}
if (clock_id == CLOCK_REALTIME)
{
struct timeval t;
return 0;
if (gettimeofday(&t, NULL) != 0)
{
return -1;
}
tp->tv_sec = t.tv_sec;
tp->tv_nsec = t.tv_usec * 1000;
}
else if (clock_id == CLOCK_MONOTONIC)
{
static mach_timebase_info_data_t info = { 0, 0 };
if (info.denom == 0)
{
mach_timebase_info(&info);
}
uint64_t t = mach_absolute_time() * info.numer / info.denom;
tp->tv_sec = t / 1000000000;
tp->tv_nsec = t % 1000000000;
}
else
{
errno = EINVAL;
return -1;
}
return 0;
}
#endif // !HAVE_CLOCK_GETTIME
#endif // __APPLE__
/**
* \brief Convert from binary to 4-bit BCD digits, little-endian
* \param bcd_data