1 | /* $Id: time-r0drv-darwin.cpp 4781 2007-09-13 19:07:42Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * innotek Portable Runtime - Time, Ring-0 Driver, Darwin.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 innotek GmbH
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License as published by the Free Software Foundation,
|
---|
13 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
14 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
15 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 |
|
---|
19 | /*******************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *******************************************************************************/
|
---|
22 | #define LOG_GROUP RTLOGGROUP_TIME
|
---|
23 | #include "the-darwin-kernel.h"
|
---|
24 | #include <iprt/time.h>
|
---|
25 | #include <iprt/asm.h>
|
---|
26 |
|
---|
27 |
|
---|
28 | DECLINLINE(uint64_t) rtTimeGetSystemNanoTS(void)
|
---|
29 | {
|
---|
30 | static int8_t s_fSimple = -1;
|
---|
31 |
|
---|
32 | /* first call: check if life is simple or not. */
|
---|
33 | if (s_fSimple < 0)
|
---|
34 | {
|
---|
35 | struct mach_timebase_info Info;
|
---|
36 | clock_timebase_info(&Info);
|
---|
37 | ASMAtomicXchgS8((int8_t * volatile)&s_fSimple, Info.denom == 1 && Info.numer == 1);
|
---|
38 | }
|
---|
39 |
|
---|
40 | /* special case: absolute time is in nanoseconds */
|
---|
41 | if (s_fSimple)
|
---|
42 | return mach_absolute_time();
|
---|
43 |
|
---|
44 | /* general case: let mach do the mult/div for us. */
|
---|
45 | uint64_t u64;
|
---|
46 | absolutetime_to_nanoseconds(mach_absolute_time(), &u64);
|
---|
47 | return u64;
|
---|
48 | }
|
---|
49 |
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Gets the current nanosecond timestamp.
|
---|
53 | *
|
---|
54 | * @returns nanosecond timestamp.
|
---|
55 | */
|
---|
56 | RTDECL(uint64_t) RTTimeNanoTS(void)
|
---|
57 | {
|
---|
58 | return rtTimeGetSystemNanoTS();
|
---|
59 | }
|
---|
60 |
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Gets the current millisecond timestamp.
|
---|
64 | *
|
---|
65 | * @returns millisecond timestamp.
|
---|
66 | */
|
---|
67 | RTDECL(uint64_t) RTTimeMilliTS(void)
|
---|
68 | {
|
---|
69 | return rtTimeGetSystemNanoTS() / 1000000;
|
---|
70 | }
|
---|
71 |
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Gets the current nanosecond timestamp.
|
---|
75 | *
|
---|
76 | * This differs from RTTimeNanoTS in that it will use system APIs and not do any
|
---|
77 | * resolution or performance optimizations.
|
---|
78 | *
|
---|
79 | * @returns nanosecond timestamp.
|
---|
80 | */
|
---|
81 | RTDECL(uint64_t) RTTimeSystemNanoTS(void)
|
---|
82 | {
|
---|
83 | return rtTimeGetSystemNanoTS();
|
---|
84 | }
|
---|
85 |
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * Gets the current millisecond timestamp.
|
---|
89 | *
|
---|
90 | * This differs from RTTimeNanoTS in that it will use system APIs and not do any
|
---|
91 | * resolution or performance optimizations.
|
---|
92 | *
|
---|
93 | * @returns millisecond timestamp.
|
---|
94 | */
|
---|
95 | RTDECL(uint64_t) RTTimeSystemMilliTS(void)
|
---|
96 | {
|
---|
97 | return rtTimeGetSystemNanoTS() / 1000000;
|
---|
98 | }
|
---|
99 |
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * Gets the current system time.
|
---|
103 | *
|
---|
104 | * @returns pTime.
|
---|
105 | * @param pTime Where to store the time.
|
---|
106 | */
|
---|
107 | RTDECL(PRTTIMESPEC) RTTimeNow(PRTTIMESPEC pTime)
|
---|
108 | {
|
---|
109 | uint32_t u32Secs;
|
---|
110 | uint32_t u32Nanosecs;
|
---|
111 | clock_get_calendar_nanotime(&u32Secs, &u32Nanosecs);
|
---|
112 | return RTTimeSpecSetNano(pTime, (uint64_t)u32Secs * 1000000000 + u32Nanosecs);
|
---|
113 | }
|
---|
114 |
|
---|