VirtualBox

Changeset 15870 in vbox for trunk/src/VBox


Ignore:
Timestamp:
Jan 8, 2009 3:08:24 PM (16 years ago)
Author:
vboxsync
Message:

IPRT/darwin: Fixed bugs in RTTimeGetSystem*TS: wrong mach_timebase_info check and gcc messing up static variables in inlined functions.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/r3/darwin/time-darwin.cpp

    r8245 r15870  
    4141
    4242#include <iprt/time.h>
     43#include <iprt/assert.h>
    4344#include "internal/time.h"
    4445
    4546
     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 */
    4678DECLINLINE(uint64_t) rtTimeGetSystemNanoTS(void)
    4779{
    48     static struct mach_timebase_info    s_Info = { 0, 0 };
    49     static double                       s_rdFactor = 0.0;
    50 
    51     /* get the factors the first time (pray we're not racing anyone) */
    52     if (s_Info.denom == 0)
    53     {
    54         static bool s_fFailedToGetTimeBaseInfo = false;
    55         if (!s_fFailedToGetTimeBaseInfo)
    56         {
    57             struct mach_timebase_info Info;
    58             if (mach_timebase_info(&Info) != KERN_SUCCESS)
    59             {
    60                 s_rdFactor = (double)Info.numer / (double)Info.denom;
    61                 s_Info = Info;
    62             }
    63             else
    64             {
    65                 s_Info.denom = s_Info.numer = 0;
    66                 s_fFailedToGetTimeBaseInfo = true;
    67             }
    68         }
    69     }
     80    /* Lazy init. */
     81    if (RT_UNLIKELY(g_Info.denom == 0 && !g_fFailedToGetTimeBaseInfo))
     82        rtTimeDarwinLazyInit();
    7083
    7184    /* special case: absolute time is in nanoseconds */
    72     if (s_Info.denom == 1 && s_Info.numer == 1)
     85    if (g_Info.denom == 1 && g_Info.numer == 1)
    7386        return mach_absolute_time();
    7487
    7588    /* general case: multiply by factor to get nanoseconds. */
    76     if (s_rdFactor != 0.0)
    77         return mach_absolute_time() * s_rdFactor;
     89    if (g_rdFactor != 0.0)
     90        return mach_absolute_time() * g_rdFactor;
    7891
    7992    /* worst case: fallback to gettimeofday(). */
Note: See TracChangeset for help on using the changeset viewer.

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