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)
24 _PR_MD_INTERVAL_INIT();
27 PRIntervalTime ticksPerSec
= PR_TicksPerSecond();
29 PR_ASSERT(ticksPerSec
>= PR_INTERVAL_MIN
);
30 PR_ASSERT(ticksPerSec
<= PR_INTERVAL_MAX
);
35 PR_IMPLEMENT(PRIntervalTime
) PR_IntervalNow(void)
37 if (!_pr_initialized
) {
38 _PR_ImplicitInitialization();
40 return _PR_MD_GET_INTERVAL();
41 } /* PR_IntervalNow */
43 PR_EXTERN(PRUint32
) PR_TicksPerSecond(void)
45 if (!_pr_initialized
) {
46 _PR_ImplicitInitialization();
48 return _PR_MD_INTERVAL_PER_SEC();
49 } /* PR_TicksPerSecond */
51 PR_IMPLEMENT(PRIntervalTime
) PR_SecondsToInterval(PRUint32 seconds
)
53 return seconds
* PR_TicksPerSecond();
54 } /* PR_SecondsToInterval */
56 PR_IMPLEMENT(PRIntervalTime
) PR_MillisecondsToInterval(PRUint32 milli
)
59 PRUint64 tock
, tps
, msecPerSec
, rounding
;
61 LL_I2L(msecPerSec
, PR_MSEC_PER_SEC
);
62 LL_I2L(rounding
, (PR_MSEC_PER_SEC
>> 1));
63 LL_I2L(tps
, PR_TicksPerSecond());
64 LL_MUL(tock
, tock
, tps
);
65 LL_ADD(tock
, tock
, rounding
);
66 LL_DIV(tock
, tock
, msecPerSec
);
69 } /* PR_MillisecondsToInterval */
71 PR_IMPLEMENT(PRIntervalTime
) PR_MicrosecondsToInterval(PRUint32 micro
)
74 PRUint64 tock
, tps
, usecPerSec
, rounding
;
76 LL_I2L(usecPerSec
, PR_USEC_PER_SEC
);
77 LL_I2L(rounding
, (PR_USEC_PER_SEC
>> 1));
78 LL_I2L(tps
, PR_TicksPerSecond());
79 LL_MUL(tock
, tock
, tps
);
80 LL_ADD(tock
, tock
, rounding
);
81 LL_DIV(tock
, tock
, usecPerSec
);
84 } /* PR_MicrosecondsToInterval */
86 PR_IMPLEMENT(PRUint32
) PR_IntervalToSeconds(PRIntervalTime ticks
)
88 return ticks
/ PR_TicksPerSecond();
89 } /* PR_IntervalToSeconds */
91 PR_IMPLEMENT(PRUint32
) PR_IntervalToMilliseconds(PRIntervalTime ticks
)
94 PRUint64 tock
, tps
, msecPerSec
, rounding
;
96 LL_I2L(msecPerSec
, PR_MSEC_PER_SEC
);
97 LL_I2L(tps
, PR_TicksPerSecond());
98 LL_USHR(rounding
, tps
, 1);
99 LL_MUL(tock
, tock
, msecPerSec
);
100 LL_ADD(tock
, tock
, rounding
);
101 LL_DIV(tock
, tock
, tps
);
102 LL_L2UI(milli
, tock
);
104 } /* PR_IntervalToMilliseconds */
106 PR_IMPLEMENT(PRUint32
) PR_IntervalToMicroseconds(PRIntervalTime ticks
)
109 PRUint64 tock
, tps
, usecPerSec
, rounding
;
110 LL_UI2L(tock
, ticks
);
111 LL_I2L(usecPerSec
, PR_USEC_PER_SEC
);
112 LL_I2L(tps
, PR_TicksPerSecond());
113 LL_USHR(rounding
, tps
, 1);
114 LL_MUL(tock
, tock
, usecPerSec
);
115 LL_ADD(tock
, tock
, rounding
);
116 LL_DIV(tock
, tock
, tps
);
117 LL_L2UI(micro
, tock
);
119 } /* PR_IntervalToMicroseconds */