| 1 |
/* |
| 2 |
* Check: a unit test framework for C |
| 3 |
* Copyright (C) 2001, 2002 Arien Malec |
| 4 |
* |
| 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. |
| 9 |
* |
| 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. |
| 14 |
* |
| 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, |
| 18 |
* MA 02110-1301, USA. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "libcompat.h" |
| 22 |
|
| 23 |
#ifdef __APPLE__ |
| 24 |
#include <mach/clock.h> |
| 25 |
#include <mach/mach.h> |
| 26 |
#include <mach/mach_time.h> |
| 27 |
#include <TargetConditionals.h> |
| 28 |
/* CoreServices.h is only available on macOS */ |
| 29 |
# if TARGET_OS_MAC && !TARGET_OS_IPHONE |
| 30 |
# include <CoreServices/CoreServices.h> |
| 31 |
# endif |
| 32 |
#include <unistd.h> |
| 33 |
#endif |
| 34 |
|
| 35 |
#define NANOSECONDS_PER_SECOND 1000000000 |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
int |
| 40 |
clock_gettime (clockid_t clk_id CK_ATTRIBUTE_UNUSED, struct timespec *ts) |
| 41 |
{ |
| 42 |
|
| 43 |
#ifdef __APPLE__ |
| 44 |
/* OS X does not have clock_gettime, use mach_absolute_time */ |
| 45 |
|
| 46 |
static mach_timebase_info_data_t sTimebaseInfo; |
| 47 |
uint64_t rawTime; |
| 48 |
uint64_t nanos; |
| 49 |
|
| 50 |
rawTime = mach_absolute_time (); |
| 51 |
|
| 52 |
/* |
| 53 |
* OS X has a function to convert abs time to nano seconds: AbsoluteToNanoseconds |
| 54 |
* However, the function may not be available as we may not have |
| 55 |
* access to CoreServices. Because of this, we convert the abs time |
| 56 |
* to nano seconds manually. |
| 57 |
*/ |
| 58 |
|
| 59 |
/* |
| 60 |
* First grab the time base used on the system, if this is the first |
| 61 |
* time we are being called. We can check if the value is uninitialized, |
| 62 |
* as the denominator will be zero. |
| 63 |
*/ |
| 64 |
if (sTimebaseInfo.denom == 0) { |
| 65 |
(void) mach_timebase_info (&sTimebaseInfo); |
| 66 |
} |
| 67 |
|
| 68 |
/* |
| 69 |
* Do the conversion. We hope that the multiplication doesn't |
| 70 |
* overflow; the price you pay for working in fixed point. |
| 71 |
*/ |
| 72 |
nanos = rawTime * sTimebaseInfo.numer / sTimebaseInfo.denom; |
| 73 |
|
| 74 |
/* |
| 75 |
* Fill in the timespec container |
| 76 |
*/ |
| 77 |
ts->tv_sec = nanos / NANOSECONDS_PER_SECOND; |
| 78 |
ts->tv_nsec = nanos - (ts->tv_sec * NANOSECONDS_PER_SECOND); |
| 79 |
#else |
| 80 |
/* |
| 81 |
* As there is no function to fall back onto to get the current |
| 82 |
* time, zero out the time so the caller will have a sane value. |
| 83 |
*/ |
| 84 |
ts->tv_sec = 0; |
| 85 |
ts->tv_nsec = 0; |
| 86 |
#endif |
| 87 |
|
| 88 |
return 0; |
| 89 |
} |