diff --git a/src/runtime/darwin-os.c b/src/runtime/darwin-os.c index c0dce91..412a1e6 100644 --- a/src/runtime/darwin-os.c +++ b/src/runtime/darwin-os.c @@ -32,6 +32,10 @@ #include #include +#if __MAC_OS_X_VERSION_MIN_REQUIRED < 101200 +#include +#endif + #ifdef LISP_FEATURE_MACH_EXCEPTION_HANDLER #include #endif @@ -368,3 +372,39 @@ sb_nanosleep(time_t sec, int nsec) { } } } + +#if __MAC_OS_X_VERSION_MIN_REQUIRED < 101200 + +int clock_gettime (int clock_id, struct timespec *ts) +{ + int returnValue = -1; + + if (clock_id == CLOCK_MONOTONIC || clock_id == CLOCK_REALTIME) { + clock_serv_t cclock; + mach_timespec_t mts; + + host_get_clock_service(mach_host_self(), REALTIME_CLOCK, &cclock); + if (clock_get_time(cclock, &mts) == 0) { + mach_port_deallocate(mach_task_self(), cclock); + ts->tv_sec = mts.tv_sec; + ts->tv_nsec = mts.tv_nsec; + + returnValue = 0; + } + + } else if (clock_id == CLOCK_PROCESS_CPUTIME_ID) { + struct rusage usage; + + if (getrusage(RUSAGE_SELF, &usage) == 0) { + ts->tv_sec = usage.ru_utime.tv_sec + usage.ru_stime.tv_sec; + ts->tv_nsec = (usage.ru_utime.tv_usec + usage.ru_stime.tv_usec) * + 1000; + + returnValue = 0; + } + } + + return returnValue; +} + +#endif diff --git a/src/runtime/darwin-os.h b/src/runtime/darwin-os.h index ac7b9ac..18839b2 100644 --- a/src/runtime/darwin-os.h +++ b/src/runtime/darwin-os.h @@ -42,4 +42,12 @@ void darwin_init(void); typedef dispatch_semaphore_t os_sem_t; #endif +#if __MAC_OS_X_VERSION_MIN_REQUIRED < 101200 + +#define CLOCK_REALTIME 0 +#define CLOCK_MONOTONIC 6 +#define CLOCK_PROCESS_CPUTIME_ID 12 + +#endif + #endif /* _DARWIN_OS_H */