check: Don't try to include CoreServices.h on iOS
[platform/upstream/gstreamer.git] / libs / gst / check / libcheck / libcompat / clock_gettime.c
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 # if TARGET_OS_MAC
29 #  include <CoreServices/CoreServices.h>
30 # endif
31 #include <unistd.h>
32 #endif
33
34 #define NANOSECONDS_PER_SECOND 1000000000
35
36
37
38 int
39 clock_gettime (clockid_t clk_id CK_ATTRIBUTE_UNUSED, struct timespec *ts)
40 {
41
42 #ifdef __APPLE__
43   /* OS X does not have clock_gettime, use mach_absolute_time */
44
45   static mach_timebase_info_data_t sTimebaseInfo;
46   uint64_t rawTime;
47   uint64_t nanos;
48
49   rawTime = mach_absolute_time ();
50
51   /*
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.
56    */
57
58   /*
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. 
62    */
63   if (sTimebaseInfo.denom == 0) {
64     (void) mach_timebase_info (&sTimebaseInfo);
65   }
66
67   /* 
68    * Do the conversion. We hope that the multiplication doesn't 
69    * overflow; the price you pay for working in fixed point.
70    */
71   nanos = rawTime * sTimebaseInfo.numer / sTimebaseInfo.denom;
72
73   /* 
74    * Fill in the timespec container 
75    */
76   ts->tv_sec = nanos / NANOSECONDS_PER_SECOND;
77   ts->tv_nsec = nanos - (ts->tv_sec * NANOSECONDS_PER_SECOND);
78 #else
79   /* 
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. 
82    */
83   ts->tv_sec = 0;
84   ts->tv_nsec = 0;
85 #endif
86
87   return 0;
88 }