Skip to content

Commit

Permalink
Don't use clock_gettime() on macOS before 10.12.
Browse files Browse the repository at this point in the history
  • Loading branch information
icculus committed Aug 24, 2019
1 parent 288971f commit 5df2be0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
38 changes: 33 additions & 5 deletions altrace_common.c
Expand Up @@ -64,27 +64,55 @@ char *sprintf_alloc(const char *fmt, ...)
return retval;
}

static struct timespec starttime;

static uint64 starttime = 0;

uint32 now(void)
{
#ifdef __APPLE__
if (clock_gettime == NULL) { // not available until 10.12, use gettimeofday if necessary.
struct timeval tv;
if (gettimeofday(&tv, NULL) == -1) {
fprintf(stderr, "%s: Failed to get current clock time: %s\n", GAppName, strerror(errno));
return 0;
}
return (uint32) ( ( (((uint64) tv.tv_sec) * 1000) + (((uint64) tv.tv_usec) / 1000) ) - starttime );
}
#endif

struct timespec ts;
if (clock_gettime(CLOCK_MONOTONIC_RAW, &ts) == -1) {
fprintf(stderr, "%s: Failed to get current clock time: %s\n", GAppName, strerror(errno));
return 0;
}

return (uint32)
( ( (((uint64) ts.tv_sec) * 1000) + (((uint64) ts.tv_nsec) / 1000000) ) -
( (((uint64) starttime.tv_sec) * 1000) + (((uint64) starttime.tv_nsec) / 1000000) ) );
return (uint32) ( ( (((uint64) ts.tv_sec) * 1000) + (((uint64) ts.tv_nsec) / 1000000) ) - starttime );
}

int init_clock(void)
{
if (clock_gettime(CLOCK_MONOTONIC_RAW, &starttime) == -1) {
#ifdef __APPLE__
if (clock_gettime == NULL) { // not available until 10.12, use gettimeofday if necessary.
struct timeval tv;
if (gettimeofday(&tv, NULL) == -1) {
fprintf(stderr, "%s: Failed to get current clock time: %s\n", GAppName, strerror(errno));
return 0;
}
usleep(1000); // just so now() is (hopefully) never 0
starttime = (((uint64) tv.tv_sec) * 1000) + (((uint64) tv.tv_usec) / 1000);
return 1;
}
#endif

struct timespec ts;
if (clock_gettime(CLOCK_MONOTONIC_RAW, &ts) == -1) {
fprintf(stderr, "%s: Failed to get current clock time: %s\n", GAppName, strerror(errno));
return 0;
}
usleep(1000); // just so now() is (hopefully) never 0

starttime = (((uint64) ts.tv_sec) * 1000) + (((uint64) ts.tv_nsec) / 1000000);

return 1;
}

Expand Down
1 change: 1 addition & 0 deletions altrace_common.h
Expand Up @@ -16,6 +16,7 @@
#include <stdarg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <fcntl.h>
#include <pthread.h>
#include <dlfcn.h>
Expand Down

0 comments on commit 5df2be0

Please sign in to comment.