/src/nspr/pr/src/misc/prinrval.c
Line | Count | Source (jump to first uncovered line) |
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/. */ |
5 | | |
6 | | /* |
7 | | * file: prinrval.c |
8 | | * description: implementation for the kernel interval timing functions |
9 | | */ |
10 | | |
11 | | #include "primpl.h" |
12 | | |
13 | | /* |
14 | | *----------------------------------------------------------------------- |
15 | | * |
16 | | * _PR_InitClock -- |
17 | | * |
18 | | * |
19 | | *----------------------------------------------------------------------- |
20 | | */ |
21 | | |
22 | | void _PR_InitClock(void) |
23 | 1 | { |
24 | 1 | _PR_MD_INTERVAL_INIT(); |
25 | 1 | #ifdef DEBUG |
26 | 1 | { |
27 | 1 | PRIntervalTime ticksPerSec = PR_TicksPerSecond(); |
28 | | |
29 | 1 | PR_ASSERT(ticksPerSec >= PR_INTERVAL_MIN); |
30 | 1 | PR_ASSERT(ticksPerSec <= PR_INTERVAL_MAX); |
31 | 1 | } |
32 | 1 | #endif /* DEBUG */ |
33 | 1 | } |
34 | | |
35 | | PR_IMPLEMENT(PRIntervalTime) PR_IntervalNow(void) |
36 | 0 | { |
37 | 0 | if (!_pr_initialized) { |
38 | 0 | _PR_ImplicitInitialization(); |
39 | 0 | } |
40 | 0 | return _PR_MD_GET_INTERVAL(); |
41 | 0 | } /* PR_IntervalNow */ |
42 | | |
43 | | PR_EXTERN(PRUint32) PR_TicksPerSecond(void) |
44 | 2 | { |
45 | 2 | if (!_pr_initialized) { |
46 | 0 | _PR_ImplicitInitialization(); |
47 | 0 | } |
48 | 2 | return _PR_MD_INTERVAL_PER_SEC(); |
49 | 2 | } /* PR_TicksPerSecond */ |
50 | | |
51 | | PR_IMPLEMENT(PRIntervalTime) PR_SecondsToInterval(PRUint32 seconds) |
52 | 0 | { |
53 | 0 | return seconds * PR_TicksPerSecond(); |
54 | 0 | } /* PR_SecondsToInterval */ |
55 | | |
56 | | PR_IMPLEMENT(PRIntervalTime) PR_MillisecondsToInterval(PRUint32 milli) |
57 | 1 | { |
58 | 1 | PRIntervalTime ticks; |
59 | 1 | PRUint64 tock, tps, msecPerSec, rounding; |
60 | 1 | LL_UI2L(tock, milli); |
61 | 1 | LL_I2L(msecPerSec, PR_MSEC_PER_SEC); |
62 | 1 | LL_I2L(rounding, (PR_MSEC_PER_SEC >> 1)); |
63 | 1 | LL_I2L(tps, PR_TicksPerSecond()); |
64 | 1 | LL_MUL(tock, tock, tps); |
65 | 1 | LL_ADD(tock, tock, rounding); |
66 | 1 | LL_DIV(tock, tock, msecPerSec); |
67 | 1 | LL_L2UI(ticks, tock); |
68 | 1 | return ticks; |
69 | 1 | } /* PR_MillisecondsToInterval */ |
70 | | |
71 | | PR_IMPLEMENT(PRIntervalTime) PR_MicrosecondsToInterval(PRUint32 micro) |
72 | 0 | { |
73 | 0 | PRIntervalTime ticks; |
74 | 0 | PRUint64 tock, tps, usecPerSec, rounding; |
75 | 0 | LL_UI2L(tock, micro); |
76 | 0 | LL_I2L(usecPerSec, PR_USEC_PER_SEC); |
77 | 0 | LL_I2L(rounding, (PR_USEC_PER_SEC >> 1)); |
78 | 0 | LL_I2L(tps, PR_TicksPerSecond()); |
79 | 0 | LL_MUL(tock, tock, tps); |
80 | 0 | LL_ADD(tock, tock, rounding); |
81 | 0 | LL_DIV(tock, tock, usecPerSec); |
82 | 0 | LL_L2UI(ticks, tock); |
83 | 0 | return ticks; |
84 | 0 | } /* PR_MicrosecondsToInterval */ |
85 | | |
86 | | PR_IMPLEMENT(PRUint32) PR_IntervalToSeconds(PRIntervalTime ticks) |
87 | 0 | { |
88 | 0 | return ticks / PR_TicksPerSecond(); |
89 | 0 | } /* PR_IntervalToSeconds */ |
90 | | |
91 | | PR_IMPLEMENT(PRUint32) PR_IntervalToMilliseconds(PRIntervalTime ticks) |
92 | 0 | { |
93 | 0 | PRUint32 milli; |
94 | 0 | PRUint64 tock, tps, msecPerSec, rounding; |
95 | 0 | LL_UI2L(tock, ticks); |
96 | 0 | LL_I2L(msecPerSec, PR_MSEC_PER_SEC); |
97 | 0 | LL_I2L(tps, PR_TicksPerSecond()); |
98 | 0 | LL_USHR(rounding, tps, 1); |
99 | 0 | LL_MUL(tock, tock, msecPerSec); |
100 | 0 | LL_ADD(tock, tock, rounding); |
101 | 0 | LL_DIV(tock, tock, tps); |
102 | 0 | LL_L2UI(milli, tock); |
103 | 0 | return milli; |
104 | 0 | } /* PR_IntervalToMilliseconds */ |
105 | | |
106 | | PR_IMPLEMENT(PRUint32) PR_IntervalToMicroseconds(PRIntervalTime ticks) |
107 | 0 | { |
108 | 0 | PRUint32 micro; |
109 | 0 | PRUint64 tock, tps, usecPerSec, rounding; |
110 | 0 | LL_UI2L(tock, ticks); |
111 | 0 | LL_I2L(usecPerSec, PR_USEC_PER_SEC); |
112 | 0 | LL_I2L(tps, PR_TicksPerSecond()); |
113 | 0 | LL_USHR(rounding, tps, 1); |
114 | 0 | LL_MUL(tock, tock, usecPerSec); |
115 | 0 | LL_ADD(tock, tock, rounding); |
116 | 0 | LL_DIV(tock, tock, tps); |
117 | 0 | LL_L2UI(micro, tock); |
118 | 0 | return micro; |
119 | 0 | } /* PR_IntervalToMicroseconds */ |
120 | | |
121 | | /* prinrval.c */ |
122 | | |