Codemod: use #include angle brackets in folly and thrift
[folly.git] / folly / detail / Clock.cpp
1 /*
2  * Copyright 2014 Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <folly/detail/Clock.h>
18
19 #if __MACH__
20 #include <errno.h>
21 #include <mach/mach_time.h>
22
23 static mach_timebase_info_data_t tb_info;
24 static bool tb_init = mach_timebase_info(&tb_info) == KERN_SUCCESS;
25
26 int clock_gettime(clockid_t clk_id, struct timespec* ts) {
27   if (!tb_init) {
28     errno = EINVAL;
29     return -1;
30   }
31
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;
36
37   return 0;
38 }
39
40 int clock_getres(clockid_t clk_id, struct timespec* ts) {
41   if (!tb_init) {
42     errno = EINVAL;
43     return -1;
44   }
45
46   ts->tv_sec = 0;
47   ts->tv_nsec = tb_info.numer / tb_info.denom;
48
49   return 0;
50 }
51 #elif _MSC_VER
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
55 #else
56 #error No clock_gettime(2) compatibility wrapper available for this platform.
57 #endif