macos: avoid allocation when getting time

This commit is contained in:
Andrew Kelley 2015-09-11 10:20:46 -07:00
parent b6c0c84294
commit c62c2e4661
2 changed files with 7 additions and 3 deletions

View file

@ -127,6 +127,9 @@ static SYSTEM_INFO win32_system_info;
#else #else
static atomic_bool initialized = ATOMIC_VAR_INIT(false); static atomic_bool initialized = ATOMIC_VAR_INIT(false);
static pthread_mutex_t init_mutex = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t init_mutex = PTHREAD_MUTEX_INITIALIZER;
#if defined(__MACH__)
static clock_serv_t cclock;
#endif
#endif #endif
static int page_size; static int page_size;
@ -137,13 +140,10 @@ double soundio_os_get_time(void) {
QueryPerformanceCounter((LARGE_INTEGER*) &time); QueryPerformanceCounter((LARGE_INTEGER*) &time);
return time * win32_time_resolution; return time * win32_time_resolution;
#elif defined(__MACH__) #elif defined(__MACH__)
clock_serv_t cclock;
mach_timespec_t mts; mach_timespec_t mts;
host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock);
kern_return_t err = clock_get_time(cclock, &mts); kern_return_t err = clock_get_time(cclock, &mts);
assert(!err); assert(!err);
mach_port_deallocate(mach_task_self(), cclock);
double seconds = (double)mts.tv_sec; double seconds = (double)mts.tv_sec;
seconds += ((double)mts.tv_nsec) / 1000000000.0; seconds += ((double)mts.tv_nsec) / 1000000000.0;
@ -561,6 +561,9 @@ static int internal_init(void) {
page_size = win32_system_info.dwAllocationGranularity; page_size = win32_system_info.dwAllocationGranularity;
#else #else
page_size = getpagesize(); page_size = getpagesize();
#if defined(__MACH__)
host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock);
#endif
#endif #endif
return 0; return 0;
} }

View file

@ -15,6 +15,7 @@ static inline void ok_or_panic(int err) {
} }
static void test_os_get_time(void) { static void test_os_get_time(void) {
soundio_os_init();
double prev_time = soundio_os_get_time(); double prev_time = soundio_os_get_time();
for (int i = 0; i < 1000; i += 1) { for (int i = 0; i < 1000; i += 1) {
double time = soundio_os_get_time(); double time = soundio_os_get_time();