From 3506d115583282bc22c025818603a53dc7970659 Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Mon, 3 Aug 2020 11:37:19 -0500 Subject: [PATCH] Speed up osdTimeGetCurrent() on recent macOS Fixes https://github.com/epics-base/epics-base/issues/83 --- modules/libcom/src/osi/os/Darwin/osdTime.cpp | 39 +++++++++++++++----- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/modules/libcom/src/osi/os/Darwin/osdTime.cpp b/modules/libcom/src/osi/os/Darwin/osdTime.cpp index 88abd00d6..cb0c070d9 100644 --- a/modules/libcom/src/osi/os/Darwin/osdTime.cpp +++ b/modules/libcom/src/osi/os/Darwin/osdTime.cpp @@ -12,8 +12,25 @@ #include #include #include -#include -#include +#include + +#ifndef CLOCK_REALTIME + #include + #include + + 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);