]>
Commit | Line | Data |
---|---|---|
34e8f829 A |
1 | /* |
2 | * Copyright (c) 2008 Apple Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
6 | * This file contains Original Code and/or Modifications of Original Code | |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. Please obtain a copy of the License at | |
10 | * http://www.opensource.apple.com/apsl/ and read it before using this | |
11 | * file. | |
12 | * | |
13 | * The Original Code and all software distributed under the License are | |
14 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
15 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
16 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
17 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. | |
18 | * Please see the License for the specific language governing rights and | |
19 | * limitations under the License. | |
20 | * | |
21 | * @APPLE_LICENSE_HEADER_END@ | |
22 | */ | |
b5d655f7 A |
23 | #include <time.h> |
24 | #include <tzfile.h> | |
25 | #include <sys/time.h> | |
26 | #include <errno.h> | |
27 | #include <sys/syscall.h> | |
28 | #include <unistd.h> | |
29 | #include <mach/clock_types.h> | |
30 | #include <mach/mach.h> | |
31 | #include <mach/mach_time.h> | |
32 | #include <machine/cpu_capabilities.h> | |
33 | ||
34 | int __commpage_gettimeofday(struct timeval *); | |
35 | ||
36 | ||
37 | #define TIME_ADD(rsecs, secs, rfrac, frac, unit) \ | |
38 | { \ | |
39 | (rfrac) += (frac); \ | |
40 | while ((rfrac) >= (unit)) { \ | |
41 | (rfrac) -= (unit); \ | |
42 | (rsecs) += 1; \ | |
43 | } \ | |
44 | (rsecs) += (secs); \ | |
45 | } | |
46 | ||
47 | ||
48 | int __commpage_gettimeofday(struct timeval *tp) { | |
49 | commpage_timeofday_data_t *commpage_timeofday_datap; | |
50 | uint64_t tbr; | |
51 | uint64_t t64; | |
52 | uint64_t TimeBase; | |
53 | uint32_t TimeStamp_usec; | |
54 | uint32_t TimeStamp_sec; | |
55 | uint32_t TimeBaseTicks_per_sec; | |
56 | uint64_t TimeBase_magic; | |
57 | uint32_t TimeBase_add; | |
58 | uint32_t TimeBase_shift; | |
59 | uint32_t x, q; | |
60 | ||
61 | ||
62 | commpage_timeofday_datap = (commpage_timeofday_data_t *)_COMM_PAGE_TIMEOFDAY_DATA; | |
63 | ||
64 | do { | |
65 | TimeBase = commpage_timeofday_datap->TimeBase; | |
66 | TimeStamp_sec = commpage_timeofday_datap->TimeStamp_sec; | |
67 | TimeStamp_usec = commpage_timeofday_datap->TimeStamp_usec; | |
68 | TimeBaseTicks_per_sec = commpage_timeofday_datap->TimeBaseTicks_per_sec; | |
69 | TimeBase_magic = commpage_timeofday_datap->TimeBase_magic; | |
70 | TimeBase_add = commpage_timeofday_datap->TimeBase_add; | |
71 | TimeBase_shift = commpage_timeofday_datap->TimeBase_shift; | |
72 | } while (TimeBase != commpage_timeofday_datap->TimeBase); | |
73 | ||
74 | if (TimeBase == 0) | |
75 | return(1); | |
76 | ||
77 | tbr = mach_absolute_time(); | |
78 | ||
79 | t64 = tbr - TimeBase; | |
80 | ||
81 | if (t64 >= (uint64_t)TimeBaseTicks_per_sec) | |
82 | return(1); | |
83 | ||
84 | x = (uint32_t)t64; | |
85 | q = ((uint64_t)x * (uint32_t)(TimeBase_magic)) >> 32; | |
86 | tp->tv_usec = TimeBase_add ? (((x - q) >> 1) + q) >> (TimeBase_shift - 1) : q >> TimeBase_shift; | |
87 | tp->tv_sec = 0; | |
88 | ||
89 | TIME_ADD(tp->tv_sec, TimeStamp_sec, tp->tv_usec, TimeStamp_usec, USEC_PER_SEC); | |
90 | ||
91 | return(0); | |
92 | } |