2 * Check: a unit test framework for C
3 * Copyright (C) 2001, 2002 Arien Malec
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
21 #include "libcompat.h"
24 #include <mach/clock.h>
25 #include <mach/mach.h>
26 #include <mach/mach_time.h>
27 #include <TargetConditionals.h>
29 # include <CoreServices/CoreServices.h>
34 #define NANOSECONDS_PER_SECOND 1000000000
39 clock_gettime (clockid_t clk_id CK_ATTRIBUTE_UNUSED, struct timespec *ts)
43 /* OS X does not have clock_gettime, use mach_absolute_time */
45 static mach_timebase_info_data_t sTimebaseInfo;
49 rawTime = mach_absolute_time ();
52 * OS X has a function to convert abs time to nano seconds: AbsoluteToNanoseconds
53 * However, the function may not be available as we may not have
54 * access to CoreServices. Because of this, we convert the abs time
55 * to nano seconds manually.
59 * First grab the time base used on the system, if this is the first
60 * time we are being called. We can check if the value is uninitialized,
61 * as the denominator will be zero.
63 if (sTimebaseInfo.denom == 0) {
64 (void) mach_timebase_info (&sTimebaseInfo);
68 * Do the conversion. We hope that the multiplication doesn't
69 * overflow; the price you pay for working in fixed point.
71 nanos = rawTime * sTimebaseInfo.numer / sTimebaseInfo.denom;
74 * Fill in the timespec container
76 ts->tv_sec = nanos / NANOSECONDS_PER_SECOND;
77 ts->tv_nsec = nanos - (ts->tv_sec * NANOSECONDS_PER_SECOND);
80 * As there is no function to fall back onto to get the current
81 * time, zero out the time so the caller will have a sane value.