2 * Copyright 2014 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "folly/detail/Clock.h"
21 #include <mach/mach_time.h>
23 static mach_timebase_info_data_t tb_info;
24 static bool tb_init = mach_timebase_info(&tb_info) == KERN_SUCCESS;
26 int clock_gettime(clockid_t clk_id, struct timespec* ts) {
32 uint64_t now_ticks = mach_absolute_time();
33 uint64_t now_ns = (now_ticks * tb_info.numer) / tb_info.denom;
34 ts->tv_sec = now_ns / 1000000000;
35 ts->tv_nsec = now_ns % 1000000000;
40 int clock_getres(clockid_t clk_id, struct timespec* ts) {
47 ts->tv_nsec = tb_info.numer / tb_info.denom;
52 // using winpthreads from mingw-w64
53 // <pthreads_time.h> has clock_gettime and friends
54 // make sure to include <pthread.h> as well for typedefs of timespec/etc
56 #error No clock_gettime(2) compatibility wrapper available for this platform.