andre@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ andre@0: /* This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: andre@0: /* andre@0: * file: prinrval.c andre@0: * description: implementation for the kernel interval timing functions andre@0: */ andre@0: andre@0: #include "primpl.h" andre@0: andre@0: /* andre@0: *----------------------------------------------------------------------- andre@0: * andre@0: * _PR_InitClock -- andre@0: * andre@0: * andre@0: *----------------------------------------------------------------------- andre@0: */ andre@0: andre@0: void _PR_InitClock(void) andre@0: { andre@0: _PR_MD_INTERVAL_INIT(); andre@0: #ifdef DEBUG andre@0: { andre@0: PRIntervalTime ticksPerSec = PR_TicksPerSecond(); andre@0: andre@0: PR_ASSERT(ticksPerSec >= PR_INTERVAL_MIN); andre@0: PR_ASSERT(ticksPerSec <= PR_INTERVAL_MAX); andre@0: } andre@0: #endif /* DEBUG */ andre@0: } andre@0: andre@0: PR_IMPLEMENT(PRIntervalTime) PR_IntervalNow(void) andre@0: { andre@0: if (!_pr_initialized) _PR_ImplicitInitialization(); andre@0: return _PR_MD_GET_INTERVAL(); andre@0: } /* PR_IntervalNow */ andre@0: andre@0: PR_EXTERN(PRUint32) PR_TicksPerSecond(void) andre@0: { andre@0: if (!_pr_initialized) _PR_ImplicitInitialization(); andre@0: return _PR_MD_INTERVAL_PER_SEC(); andre@0: } /* PR_TicksPerSecond */ andre@0: andre@0: PR_IMPLEMENT(PRIntervalTime) PR_SecondsToInterval(PRUint32 seconds) andre@0: { andre@0: return seconds * PR_TicksPerSecond(); andre@0: } /* PR_SecondsToInterval */ andre@0: andre@0: PR_IMPLEMENT(PRIntervalTime) PR_MillisecondsToInterval(PRUint32 milli) andre@0: { andre@0: PRIntervalTime ticks; andre@0: PRUint64 tock, tps, msecPerSec, rounding; andre@0: LL_UI2L(tock, milli); andre@0: LL_I2L(msecPerSec, PR_MSEC_PER_SEC); andre@0: LL_I2L(rounding, (PR_MSEC_PER_SEC >> 1)); andre@0: LL_I2L(tps, PR_TicksPerSecond()); andre@0: LL_MUL(tock, tock, tps); andre@0: LL_ADD(tock, tock, rounding); andre@0: LL_DIV(tock, tock, msecPerSec); andre@0: LL_L2UI(ticks, tock); andre@0: return ticks; andre@0: } /* PR_MillisecondsToInterval */ andre@0: andre@0: PR_IMPLEMENT(PRIntervalTime) PR_MicrosecondsToInterval(PRUint32 micro) andre@0: { andre@0: PRIntervalTime ticks; andre@0: PRUint64 tock, tps, usecPerSec, rounding; andre@0: LL_UI2L(tock, micro); andre@0: LL_I2L(usecPerSec, PR_USEC_PER_SEC); andre@0: LL_I2L(rounding, (PR_USEC_PER_SEC >> 1)); andre@0: LL_I2L(tps, PR_TicksPerSecond()); andre@0: LL_MUL(tock, tock, tps); andre@0: LL_ADD(tock, tock, rounding); andre@0: LL_DIV(tock, tock, usecPerSec); andre@0: LL_L2UI(ticks, tock); andre@0: return ticks; andre@0: } /* PR_MicrosecondsToInterval */ andre@0: andre@0: PR_IMPLEMENT(PRUint32) PR_IntervalToSeconds(PRIntervalTime ticks) andre@0: { andre@0: return ticks / PR_TicksPerSecond(); andre@0: } /* PR_IntervalToSeconds */ andre@0: andre@0: PR_IMPLEMENT(PRUint32) PR_IntervalToMilliseconds(PRIntervalTime ticks) andre@0: { andre@0: PRUint32 milli; andre@0: PRUint64 tock, tps, msecPerSec, rounding; andre@0: LL_UI2L(tock, ticks); andre@0: LL_I2L(msecPerSec, PR_MSEC_PER_SEC); andre@0: LL_I2L(tps, PR_TicksPerSecond()); andre@0: LL_USHR(rounding, tps, 1); andre@0: LL_MUL(tock, tock, msecPerSec); andre@0: LL_ADD(tock, tock, rounding); andre@0: LL_DIV(tock, tock, tps); andre@0: LL_L2UI(milli, tock); andre@0: return milli; andre@0: } /* PR_IntervalToMilliseconds */ andre@0: andre@0: PR_IMPLEMENT(PRUint32) PR_IntervalToMicroseconds(PRIntervalTime ticks) andre@0: { andre@0: PRUint32 micro; andre@0: PRUint64 tock, tps, usecPerSec, rounding; andre@0: LL_UI2L(tock, ticks); andre@0: LL_I2L(usecPerSec, PR_USEC_PER_SEC); andre@0: LL_I2L(tps, PR_TicksPerSecond()); andre@0: LL_USHR(rounding, tps, 1); andre@0: LL_MUL(tock, tock, usecPerSec); andre@0: LL_ADD(tock, tock, rounding); andre@0: LL_DIV(tock, tock, tps); andre@0: LL_L2UI(micro, tock); andre@0: return micro; andre@0: } /* PR_IntervalToMicroseconds */ andre@0: andre@0: /* prinrval.c */ andre@0: