Speed up osdTimeGetCurrent() on recent macOS

Fixes https://github.com/epics-base/epics-base/issues/83
This commit is contained in:
Andrew Johnson
2020-08-03 11:37:19 -05:00
parent 29bdca3b84
commit 3506d11558

View File

@@ -12,8 +12,25 @@
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <mach/mach.h>
#include <mach/clock.h>
#include <sys/time.h>
#ifndef CLOCK_REALTIME
#include <mach/mach.h>
#include <mach/clock.h>
static clock_serv_t host_clock;
#define HOST_GETCLOCK host_get_clock_service(mach_host_self(), \
CALENDAR_CLOCK, &host_clock)
#define TIMESPEC mach_timespec_t
#define CLOCK_GETTIME(ts) clock_get_time(host_clock, ts)
#define TP_NAME "MachTime"
#else
#define HOST_GETCLOCK
#define TIMESPEC struct timespec
#define CLOCK_GETTIME(ts) clock_gettime(CLOCK_REALTIME, ts)
#define TP_NAME "OS Clock"
#endif
#define EPICS_EXPOSE_LIBCOM_MONOTONIC_PRIVATE
#include "osiSock.h"
@@ -22,17 +39,19 @@
#include "epicsTime.h"
#include "generalTimeSup.h"
static clock_serv_t host_clock;
extern "C" {
int osdTimeGetCurrent (epicsTimeStamp *pDest)
{
mach_timespec_t mts;
struct timespec ts;
clock_get_time(host_clock, &mts);
#ifndef CLOCK_REALTIME
TIMESPEC mts;
CLOCK_GETTIME(&mts);
ts.tv_sec = mts.tv_sec;
ts.tv_nsec = mts.tv_nsec;
#else
CLOCK_GETTIME(&ts);
#endif
*pDest = epicsTime(ts);
return epicsTimeOK;
}
@@ -41,9 +60,9 @@ int osdTimeGetCurrent (epicsTimeStamp *pDest)
static int timeRegister(void)
{
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &host_clock);
HOST_GETCLOCK;
generalTimeCurrentTpRegister("MachTime", \
generalTimeCurrentTpRegister(TP_NAME, \
LAST_RESORT_PRIORITY, osdTimeGetCurrent);
osdMonotonicInit();
@@ -67,7 +86,7 @@ int epicsTime_localtime(const time_t *clock, struct tm *result)
extern "C" LIBCOM_API void
convertDoubleToWakeTime(double timeout, struct timespec *wakeTime)
{
mach_timespec_t now;
TIMESPEC now;
struct timespec wait;
if (timeout < 0.0)
@@ -75,7 +94,7 @@ convertDoubleToWakeTime(double timeout, struct timespec *wakeTime)
else if (timeout > 60 * 60 * 24 * 3652.5)
timeout = 60 * 60 * 24 * 3652.5; /* 10 years */
clock_get_time(host_clock, &now);
CLOCK_GETTIME(&now);
wait.tv_sec = static_cast< time_t >(timeout);
wait.tv_nsec = static_cast< long >((timeout - (double)wait.tv_sec) * 1e9);