VirtualBox

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

Last change on this file since 27613 was 26209, checked in by vboxsync, 15 years ago

IPRT: Added RTTimeSet.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.6 KB
Line 
1/* $Id: time-darwin.cpp 26209 2010-02-03 16:54:41Z vboxsync $ */
2/** @file
3 * IPRT - Time, Darwin.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 *
26 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31
32/*******************************************************************************
33* Header Files *
34*******************************************************************************/
35#define LOG_GROUP RTLOGGROUP_TIME
36#define RTTIME_INCL_TIMEVAL
37#include <mach/mach_time.h>
38#include <mach/kern_return.h>
39#include <sys/time.h>
40#include <time.h>
41
42#include <iprt/time.h>
43#include <iprt/assert.h>
44#include "internal/time.h"
45
46
47/*******************************************************************************
48* Global Variables *
49*******************************************************************************/
50static struct mach_timebase_info g_Info = { 0, 0 };
51static double g_rdFactor = 0.0;
52static bool g_fFailedToGetTimeBaseInfo = false;
53
54
55/**
56 * Perform lazy init (pray we're not racing anyone in a bad way).
57 */
58static void rtTimeDarwinLazyInit(void)
59{
60 struct mach_timebase_info Info;
61 if (mach_timebase_info(&Info) == KERN_SUCCESS)
62 {
63 g_rdFactor = (double)Info.numer / (double)Info.denom;
64 g_Info = Info;
65 }
66 else
67 {
68 g_fFailedToGetTimeBaseInfo = true;
69 Assert(g_Info.denom == 0 && g_Info.numer == 0 && g_rdFactor == 0.0);
70 }
71}
72
73
74/**
75 * Internal worker.
76 * @returns Nanosecond timestamp.
77 */
78DECLINLINE(uint64_t) rtTimeGetSystemNanoTS(void)
79{
80 /* Lazy init. */
81 if (RT_UNLIKELY(g_Info.denom == 0 && !g_fFailedToGetTimeBaseInfo))
82 rtTimeDarwinLazyInit();
83
84 /* special case: absolute time is in nanoseconds */
85 if (g_Info.denom == 1 && g_Info.numer == 1)
86 return mach_absolute_time();
87
88 /* general case: multiply by factor to get nanoseconds. */
89 if (g_rdFactor != 0.0)
90 return mach_absolute_time() * g_rdFactor;
91
92 /* worst case: fallback to gettimeofday(). */
93 struct timeval tv;
94 gettimeofday(&tv, NULL);
95 return (uint64_t)tv.tv_sec * (uint64_t)(1000 * 1000 * 1000)
96 + (uint64_t)(tv.tv_usec * 1000);
97}
98
99
100RTDECL(uint64_t) RTTimeSystemNanoTS(void)
101{
102 return rtTimeGetSystemNanoTS();
103}
104
105
106RTDECL(uint64_t) RTTimeSystemMilliTS(void)
107{
108 return rtTimeGetSystemNanoTS();
109}
110
111
112RTDECL(PRTTIMESPEC) RTTimeNow(PRTTIMESPEC pTime)
113{
114 /** @todo find nanosecond API for getting time of day. */
115 struct timeval tv;
116 gettimeofday(&tv, NULL);
117 return RTTimeSpecSetTimeval(pTime, &tv);
118}
119
Note: See TracBrowser for help on using the repository browser.

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