VirtualBox

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

Last change on this file since 4750 was 4750, checked in by vboxsync, 17 years ago

eol style native

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.2 KB
Line 
1/* $Id: time-darwin.cpp 4750 2007-09-13 07:05:03Z vboxsync $ */
2/** @file
3 * innotek Portable Runtime - Time, 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#define RTTIME_INCL_TIMEVAL
24#include <mach/mach_time.h>
25#include <mach/kern_return.h>
26#include <sys/time.h>
27#include <time.h>
28
29#include <iprt/time.h>
30#include "internal/time.h"
31
32
33DECLINLINE(uint64_t) rtTimeGetSystemNanoTS(void)
34{
35 static struct mach_timebase_info s_Info = { 0, 0 };
36 static double s_rdFactor = 0.0;
37
38 /* get the factors the first time (pray we're not racing anyone) */
39 if (s_Info.denom == 0)
40 {
41 static bool s_fFailedToGetTimeBaseInfo = false;
42 if (!s_fFailedToGetTimeBaseInfo)
43 {
44 struct mach_timebase_info Info;
45 if (mach_timebase_info(&Info) != KERN_SUCCESS)
46 {
47 s_rdFactor = (double)Info.numer / (double)Info.denom;
48 s_Info = Info;
49 }
50 else
51 {
52 s_Info.denom = s_Info.numer = 0;
53 s_fFailedToGetTimeBaseInfo = true;
54 }
55 }
56 }
57
58 /* special case: absolute time is in nanoseconds */
59 if (s_Info.denom == 1 && s_Info.numer == 1)
60 return mach_absolute_time();
61
62 /* general case: multiply by factor to get nanoseconds. */
63 if (s_rdFactor != 0.0)
64 return mach_absolute_time() * s_rdFactor;
65
66 /* worst case: fallback to gettimeofday(). */
67 struct timeval tv;
68 gettimeofday(&tv, NULL);
69 return (uint64_t)tv.tv_sec * (uint64_t)(1000 * 1000 * 1000)
70 + (uint64_t)(tv.tv_usec * 1000);
71}
72
73
74/**
75 * Gets the current nanosecond timestamp.
76 *
77 * This differs from RTTimeNanoTS in that it will use system APIs and not do any
78 * resolution or performance optimizations.
79 *
80 * @returns nanosecond timestamp.
81 */
82RTDECL(uint64_t) RTTimeSystemNanoTS(void)
83{
84 return rtTimeGetSystemNanoTS();
85}
86
87
88/**
89 * Gets the current millisecond timestamp.
90 *
91 * This differs from RTTimeNanoTS in that it will use system APIs and not do any
92 * resolution or performance optimizations.
93 *
94 * @returns millisecond timestamp.
95 */
96RTDECL(uint64_t) RTTimeSystemMilliTS(void)
97{
98 return rtTimeGetSystemNanoTS();
99}
100
101
102/**
103 * Gets the current system time.
104 *
105 * @returns pTime.
106 * @param pTime Where to store the time.
107 */
108RTDECL(PRTTIMESPEC) RTTimeNow(PRTTIMESPEC pTime)
109{
110 /** @todo find nanosecond API for getting time of day. */
111 struct timeval tv;
112 gettimeofday(&tv, NULL);
113 return RTTimeSpecSetTimeval(pTime, &tv);
114}
115
Note: See TracBrowser for help on using the repository browser.

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