Skip to content

Commit

Permalink
Merge pull request #104 from SIPp/osx_monotonic
Browse files Browse the repository at this point in the history
OSX fixes (the lack of CLOCK_MONOTONIC)

Reported by: dmpanch
Tested by: Ben Klang, Stexxen
  • Loading branch information
wdoekes committed Oct 3, 2014
2 parents 7ced056 + c64e82d commit 8923379
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
#include <time.h>
#include <sys/time.h>
#include <unistd.h>
#ifdef __MACH__
#include <mach/clock.h>
#include <mach/mach.h>
#endif
#include "time.hpp"
#include "sipp.hpp"
#define MICROSECONDS_PER_SECOND 1000000LL
Expand All @@ -53,10 +57,21 @@ unsigned long long getmicroseconds()
unsigned long long microseconds;
static unsigned long long start_time = 0;

#ifdef __MACH__
// OS X does not have clock_gettime, use clock_get_time
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);
time.tv_sec = mts.tv_sec;
time.tv_nsec = mts.tv_nsec;
#else
#if defined(CLOCK_MONOTONIC_COARSE)
clock_gettime(CLOCK_MONOTONIC_COARSE, &time);
#else
clock_gettime(CLOCK_MONOTONIC, &time);
#endif
#endif
microseconds = (MICROSECONDS_PER_SECOND * time.tv_sec) + (time.tv_nsec / NANOSECONDS_PER_MICROSECOND);
if (start_time == 0) {
Expand Down

0 comments on commit 8923379

Please sign in to comment.