VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/darwin/time-darwin.cpp@ 99108

最後變更 在這個檔案從99108是 99108,由 vboxsync 提交於 2 年 前

Runtime/darwin/time-darwin.cpp: Use clock_gettime_nsec_np(CLOCK_UPTIME_RAW) on arm64 as mach_absolute_time() is deprecated now

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Id Revision
檔案大小: 4.2 KB
 
1/* $Id: time-darwin.cpp 99108 2023-03-22 09:31:15Z vboxsync $ */
2/** @file
3 * IPRT - Time, Darwin.
4 */
5
6/*
7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.alldomusa.eu.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#define LOG_GROUP RTLOGGROUP_TIME
42#define RTTIME_INCL_TIMEVAL
43#include <mach/mach_time.h>
44#include <mach/kern_return.h>
45#include <sys/time.h>
46#include <time.h>
47
48#include <iprt/time.h>
49#include <iprt/assert.h>
50#include "internal/time.h"
51
52
53/*********************************************************************************************************************************
54* Global Variables *
55*********************************************************************************************************************************/
56#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
57static struct mach_timebase_info g_Info = { 0, 0 };
58static double g_rdFactor = 0.0;
59static bool g_fFailedToGetTimeBaseInfo = false;
60
61
62/**
63 * Perform lazy init (pray we're not racing anyone in a bad way).
64 */
65static void rtTimeDarwinLazyInit(void)
66{
67 struct mach_timebase_info Info;
68 if (mach_timebase_info(&Info) == KERN_SUCCESS)
69 {
70 g_rdFactor = (double)Info.numer / (double)Info.denom;
71 g_Info = Info;
72 }
73 else
74 {
75 g_fFailedToGetTimeBaseInfo = true;
76 Assert(g_Info.denom == 0 && g_Info.numer == 0 && g_rdFactor == 0.0);
77 }
78}
79#endif
80
81
82/**
83 * Internal worker.
84 * @returns Nanosecond timestamp.
85 */
86DECLINLINE(uint64_t) rtTimeGetSystemNanoTS(void)
87{
88#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
89 /* Lazy init. */
90 if (RT_UNLIKELY(g_Info.denom == 0 && !g_fFailedToGetTimeBaseInfo))
91 rtTimeDarwinLazyInit();
92
93 /* special case: absolute time is in nanoseconds */
94 if (g_Info.denom == 1 && g_Info.numer == 1)
95 return mach_absolute_time();
96
97 /* general case: multiply by factor to get nanoseconds. */
98 if (g_rdFactor != 0.0)
99 return mach_absolute_time() * g_rdFactor;
100
101 /* worst case: fallback to gettimeofday(). */
102 struct timeval tv;
103 gettimeofday(&tv, NULL);
104 return (uint64_t)tv.tv_sec * RT_NS_1SEC_64
105 + (uint64_t)(tv.tv_usec * RT_NS_1US);
106#else
107 return clock_gettime_nsec_np(CLOCK_UPTIME_RAW);
108#endif
109}
110
111
112RTDECL(uint64_t) RTTimeSystemNanoTS(void)
113{
114 return rtTimeGetSystemNanoTS();
115}
116
117
118RTDECL(uint64_t) RTTimeSystemMilliTS(void)
119{
120 return rtTimeGetSystemNanoTS() / RT_NS_1MS;
121}
122
123
124RTDECL(PRTTIMESPEC) RTTimeNow(PRTTIMESPEC pTime)
125{
126 /** @todo find nanosecond API for getting time of day. */
127 struct timeval tv;
128 gettimeofday(&tv, NULL);
129 return RTTimeSpecSetTimeval(pTime, &tv);
130}
131
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette