nsprpub/pr/src/misc/prinrval.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/nsprpub/pr/src/misc/prinrval.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,118 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +/*
    1.10 + * file:			prinrval.c
    1.11 + * description:		implementation for the kernel interval timing functions
    1.12 + */
    1.13 +
    1.14 +#include "primpl.h"
    1.15 +
    1.16 +/*
    1.17 + *-----------------------------------------------------------------------
    1.18 + *
    1.19 + * _PR_InitClock --
    1.20 + *
    1.21 + *
    1.22 + *-----------------------------------------------------------------------
    1.23 + */
    1.24 +
    1.25 +void _PR_InitClock(void)
    1.26 +{
    1.27 +    _PR_MD_INTERVAL_INIT();
    1.28 +#ifdef DEBUG
    1.29 +    {
    1.30 +        PRIntervalTime ticksPerSec = PR_TicksPerSecond();
    1.31 +
    1.32 +        PR_ASSERT(ticksPerSec >= PR_INTERVAL_MIN);
    1.33 +        PR_ASSERT(ticksPerSec <= PR_INTERVAL_MAX);
    1.34 +    }
    1.35 +#endif /* DEBUG */
    1.36 +}
    1.37 +
    1.38 +PR_IMPLEMENT(PRIntervalTime) PR_IntervalNow(void)
    1.39 +{
    1.40 +    if (!_pr_initialized) _PR_ImplicitInitialization();
    1.41 +    return _PR_MD_GET_INTERVAL();
    1.42 +}  /* PR_IntervalNow */
    1.43 +
    1.44 +PR_EXTERN(PRUint32) PR_TicksPerSecond(void)
    1.45 +{
    1.46 +    if (!_pr_initialized) _PR_ImplicitInitialization();
    1.47 +    return _PR_MD_INTERVAL_PER_SEC();
    1.48 +}  /* PR_TicksPerSecond */
    1.49 +
    1.50 +PR_IMPLEMENT(PRIntervalTime) PR_SecondsToInterval(PRUint32 seconds)
    1.51 +{
    1.52 +    return seconds * PR_TicksPerSecond();
    1.53 +}  /* PR_SecondsToInterval */
    1.54 +
    1.55 +PR_IMPLEMENT(PRIntervalTime) PR_MillisecondsToInterval(PRUint32 milli)
    1.56 +{
    1.57 +    PRIntervalTime ticks;
    1.58 +    PRUint64 tock, tps, msecPerSec, rounding;
    1.59 +    LL_UI2L(tock, milli);
    1.60 +    LL_I2L(msecPerSec, PR_MSEC_PER_SEC);
    1.61 +    LL_I2L(rounding, (PR_MSEC_PER_SEC >> 1));
    1.62 +    LL_I2L(tps, PR_TicksPerSecond());
    1.63 +    LL_MUL(tock, tock, tps);
    1.64 +    LL_ADD(tock, tock, rounding);
    1.65 +    LL_DIV(tock, tock, msecPerSec);
    1.66 +    LL_L2UI(ticks, tock);
    1.67 +    return ticks;
    1.68 +}  /* PR_MillisecondsToInterval */
    1.69 +
    1.70 +PR_IMPLEMENT(PRIntervalTime) PR_MicrosecondsToInterval(PRUint32 micro)
    1.71 +{
    1.72 +    PRIntervalTime ticks;
    1.73 +    PRUint64 tock, tps, usecPerSec, rounding;
    1.74 +    LL_UI2L(tock, micro);
    1.75 +    LL_I2L(usecPerSec, PR_USEC_PER_SEC);
    1.76 +    LL_I2L(rounding, (PR_USEC_PER_SEC >> 1));
    1.77 +    LL_I2L(tps, PR_TicksPerSecond());
    1.78 +    LL_MUL(tock, tock, tps);
    1.79 +    LL_ADD(tock, tock, rounding);
    1.80 +    LL_DIV(tock, tock, usecPerSec);
    1.81 +    LL_L2UI(ticks, tock);
    1.82 +    return ticks;
    1.83 +}  /* PR_MicrosecondsToInterval */
    1.84 +
    1.85 +PR_IMPLEMENT(PRUint32) PR_IntervalToSeconds(PRIntervalTime ticks)
    1.86 +{
    1.87 +    return ticks / PR_TicksPerSecond();
    1.88 +}  /* PR_IntervalToSeconds */
    1.89 +
    1.90 +PR_IMPLEMENT(PRUint32) PR_IntervalToMilliseconds(PRIntervalTime ticks)
    1.91 +{
    1.92 +    PRUint32 milli;
    1.93 +    PRUint64 tock, tps, msecPerSec, rounding;
    1.94 +    LL_UI2L(tock, ticks);
    1.95 +    LL_I2L(msecPerSec, PR_MSEC_PER_SEC);
    1.96 +    LL_I2L(tps, PR_TicksPerSecond());
    1.97 +    LL_USHR(rounding, tps, 1);
    1.98 +    LL_MUL(tock, tock, msecPerSec);
    1.99 +    LL_ADD(tock, tock, rounding);
   1.100 +    LL_DIV(tock, tock, tps);
   1.101 +    LL_L2UI(milli, tock);
   1.102 +    return milli;
   1.103 +}  /* PR_IntervalToMilliseconds */
   1.104 +
   1.105 +PR_IMPLEMENT(PRUint32) PR_IntervalToMicroseconds(PRIntervalTime ticks)
   1.106 +{
   1.107 +    PRUint32 micro;
   1.108 +    PRUint64 tock, tps, usecPerSec, rounding;
   1.109 +    LL_UI2L(tock, ticks);
   1.110 +    LL_I2L(usecPerSec, PR_USEC_PER_SEC);
   1.111 +    LL_I2L(tps, PR_TicksPerSecond());
   1.112 +    LL_USHR(rounding, tps, 1);
   1.113 +    LL_MUL(tock, tock, usecPerSec);
   1.114 +    LL_ADD(tock, tock, rounding);
   1.115 +    LL_DIV(tock, tock, tps);
   1.116 +    LL_L2UI(micro, tock);
   1.117 +    return micro;
   1.118 +}  /* PR_IntervalToMicroseconds */
   1.119 +
   1.120 +/* prinrval.c */
   1.121 +

mercurial