1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 * description: implementation for the kernel interval timing functions
14 *-----------------------------------------------------------------------
19 *-----------------------------------------------------------------------
22 void _PR_InitClock(void) {
23 _PR_MD_INTERVAL_INIT();
26 PRIntervalTime ticksPerSec
= PR_TicksPerSecond();
28 PR_ASSERT(ticksPerSec
>= PR_INTERVAL_MIN
);
29 PR_ASSERT(ticksPerSec
<= PR_INTERVAL_MAX
);
34 PR_IMPLEMENT(PRIntervalTime
) PR_IntervalNow(void) {
35 if (!_pr_initialized
) {
36 _PR_ImplicitInitialization();
38 return _PR_MD_GET_INTERVAL();
39 } /* PR_IntervalNow */
41 PR_EXTERN(PRUint32
) PR_TicksPerSecond(void) {
42 if (!_pr_initialized
) {
43 _PR_ImplicitInitialization();
45 return _PR_MD_INTERVAL_PER_SEC();
46 } /* PR_TicksPerSecond */
48 PR_IMPLEMENT(PRIntervalTime
) PR_SecondsToInterval(PRUint32 seconds
) {
49 return seconds
* PR_TicksPerSecond();
50 } /* PR_SecondsToInterval */
52 PR_IMPLEMENT(PRIntervalTime
) PR_MillisecondsToInterval(PRUint32 milli
) {
54 PRUint64 tock
, tps
, msecPerSec
, rounding
;
56 LL_I2L(msecPerSec
, PR_MSEC_PER_SEC
);
57 LL_I2L(rounding
, (PR_MSEC_PER_SEC
>> 1));
58 LL_I2L(tps
, PR_TicksPerSecond());
59 LL_MUL(tock
, tock
, tps
);
60 LL_ADD(tock
, tock
, rounding
);
61 LL_DIV(tock
, tock
, msecPerSec
);
64 } /* PR_MillisecondsToInterval */
66 PR_IMPLEMENT(PRIntervalTime
) PR_MicrosecondsToInterval(PRUint32 micro
) {
68 PRUint64 tock
, tps
, usecPerSec
, rounding
;
70 LL_I2L(usecPerSec
, PR_USEC_PER_SEC
);
71 LL_I2L(rounding
, (PR_USEC_PER_SEC
>> 1));
72 LL_I2L(tps
, PR_TicksPerSecond());
73 LL_MUL(tock
, tock
, tps
);
74 LL_ADD(tock
, tock
, rounding
);
75 LL_DIV(tock
, tock
, usecPerSec
);
78 } /* PR_MicrosecondsToInterval */
80 PR_IMPLEMENT(PRUint32
) PR_IntervalToSeconds(PRIntervalTime ticks
) {
81 return ticks
/ PR_TicksPerSecond();
82 } /* PR_IntervalToSeconds */
84 PR_IMPLEMENT(PRUint32
) PR_IntervalToMilliseconds(PRIntervalTime ticks
) {
86 PRUint64 tock
, tps
, msecPerSec
, rounding
;
88 LL_I2L(msecPerSec
, PR_MSEC_PER_SEC
);
89 LL_I2L(tps
, PR_TicksPerSecond());
90 LL_USHR(rounding
, tps
, 1);
91 LL_MUL(tock
, tock
, msecPerSec
);
92 LL_ADD(tock
, tock
, rounding
);
93 LL_DIV(tock
, tock
, tps
);
96 } /* PR_IntervalToMilliseconds */
98 PR_IMPLEMENT(PRUint32
) PR_IntervalToMicroseconds(PRIntervalTime ticks
) {
100 PRUint64 tock
, tps
, usecPerSec
, rounding
;
101 LL_UI2L(tock
, ticks
);
102 LL_I2L(usecPerSec
, PR_USEC_PER_SEC
);
103 LL_I2L(tps
, PR_TicksPerSecond());
104 LL_USHR(rounding
, tps
, 1);
105 LL_MUL(tock
, tock
, usecPerSec
);
106 LL_ADD(tock
, tock
, rounding
);
107 LL_DIV(tock
, tock
, tps
);
108 LL_L2UI(micro
, tock
);
110 } /* PR_IntervalToMicroseconds */