Skip to content

Commit

Permalink
macOS 10.10 - 10.11 clock_gettime replacement
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristopherHX committed Sep 26, 2020
1 parent 54a1173 commit 6b46b28
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
#endif
#ifdef __APPLE__
#include <xlocale.h>
#if __MAC_OS_X_VERSION_MIN_REQUIRED < 101200
// for macOS 10.10 - 10.11
#include <mach/clock.h>
#include <mach/mach.h>
#endif
#endif
#include <inttypes.h>

Expand Down Expand Up @@ -146,7 +151,28 @@ ssize_t shim::pwrite(int fd, const void *buf, size_t len, bionic::off_t off) {
#endif

int shim::clock_gettime(bionic::clock_type clock, struct timespec *ts) {
#if defined(__APPLE__) && __MAC_OS_X_VERSION_MIN_REQUIRED < 101200
if(::clock_gettime != NULL) {
#endif
return ::clock_gettime(bionic::to_host_clock_type(clock), ts);
#if defined(__APPLE__) && __MAC_OS_X_VERSION_MIN_REQUIRED < 101200
} else {
// fallback if weak symbol is nullptr < macOS 10.12
clock_serv_t cclock;
mach_timespec_t mts;
if (host_get_clock_service(mach_host_self(), clock == bionic::clock_type::MONOTONIC ? SYSTEM_CLOCK : CALENDAR_CLOCK, &cclock) != KERN_SUCCESS) {
return -1;
}
kern_return_t r = clock_get_time(cclock, &mts);
mach_port_deallocate(mach_task_self(), cclock);
if (r != KERN_SUCCESS) {
return -1;
}
ts->tv_sec = mts.tv_sec;
ts->tv_nsec = mts.tv_nsec;
return 0;
}
#endif
}

void* shim::memalign(size_t alignment, size_t size) {
Expand Down
8 changes: 8 additions & 0 deletions src/semaphore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
#ifdef __APPLE__
#include <errno.h>
#include <mach/mach.h>
#if __MAC_OS_X_VERSION_MIN_REQUIRED < 101200
// for shim::clock_gettime 10.10 - 10.12
#include "common.h"
#endif
#endif

using namespace shim;
Expand Down Expand Up @@ -56,7 +60,11 @@ int shim::sem_post(host_sem_t *sem) {

int shim::sem_timedwait(host_sem_t *sem, const struct timespec *abs_timeout) {
struct timespec now;
#if __MAC_OS_X_VERSION_MIN_REQUIRED < 101200
shim::clock_gettime(shim::bionic::clock_type::REALTIME, &now);
#else
clock_gettime(CLOCK_REALTIME, &now);
#endif
mach_timespec_t ts;
if (abs_timeout->tv_sec > now.tv_sec ||
(abs_timeout->tv_sec == now.tv_sec && abs_timeout->tv_nsec > now.tv_nsec)) {
Expand Down

0 comments on commit 6b46b28

Please sign in to comment.