From 26993d2d2b2be391adbfaa2ee53ee3e77f323b28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20G=C3=B6ttsche?= Date: Sun, 13 Dec 2020 13:16:44 +0100 Subject: Support clock_gettime() on OSX El Capitan and earlier --- Compat.c | 34 ++++++++++++++++++++++++++++++++++ Compat.h | 2 ++ ProcessList.c | 6 +++--- configure.ac | 2 ++ 4 files changed, 41 insertions(+), 3 deletions(-) diff --git a/Compat.c b/Compat.c index 43d02ec7..55be1b1d 100644 --- a/Compat.c +++ b/Compat.c @@ -11,12 +11,18 @@ in the source distribution for its full text. #include #include // IWYU pragma: keep +#include #include #include #include // IWYU pragma: keep #include "XUtils.h" // IWYU pragma: keep +#ifdef HAVE_HOST_GET_CLOCK_SERVICE +#include +#include +#endif + int Compat_faccessat(int dirfd, const char* pathname, @@ -117,3 +123,31 @@ ssize_t Compat_readlinkat(int dirfd, #endif } + +int Compat_clock_monotonic_gettime(struct timespec *tp) { + +#if defined(HAVE_CLOCK_GETTIME) + + return clock_gettime(CLOCK_MONOTONIC, tp); + +#elif defined(HAVE_HOST_GET_CLOCK_SERVICE) + + clock_serv_t cclock; + mach_timespec_t mts; + + host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock); + clock_get_time(cclock, &mts); + mach_port_deallocate(mach_task_self(), cclock); + + tp->tv_sec = mts.tv_sec; + tp->tv_nsec = mts.tv_nsec; + + return 0; + +#else + +#error No Compat_clock_monotonic_gettime() implementation! + +#endif + +} diff --git a/Compat.h b/Compat.h index 94c2ee2f..95947351 100644 --- a/Compat.h +++ b/Compat.h @@ -56,4 +56,6 @@ ssize_t Compat_readlinkat(int dirfd, char* buf, size_t bufsize); +int Compat_clock_monotonic_gettime(struct timespec *tp); + #endif /* HEADER_Compat */ diff --git a/ProcessList.c b/ProcessList.c index a12e8534..b7800e49 100644 --- a/ProcessList.c +++ b/ProcessList.c @@ -10,8 +10,8 @@ in the source distribution for its full text. #include #include #include -#include +#include "Compat.h" #include "CRT.h" #include "Hashtable.h" #include "Macros.h" @@ -538,8 +538,8 @@ void ProcessList_scan(ProcessList* this, bool pauseProcessUpdate) { if (!firstScanDone) { this->scanTs = 0; firstScanDone = true; - } else if (clock_gettime(CLOCK_MONOTONIC, &now) == 0) { - this->scanTs = now.tv_sec; + } else if (Compat_clock_monotonic_gettime(&now) == 0) { + this->scanTs = now.tv_sec + now.tv_nsec / 1000000000; } ProcessList_goThroughEntries(this, false); diff --git a/configure.ac b/configure.ac index a5b7093a..5c39b405 100644 --- a/configure.ac +++ b/configure.ac @@ -93,8 +93,10 @@ AC_TYPE_UINT64_T AC_FUNC_CLOSEDIR_VOID AC_FUNC_STAT AC_CHECK_FUNCS([\ + clock_gettime\ faccessat\ fstatat\ + host_get_clock_service\ openat\ readlinkat\ ]) -- cgit v1.2.3