/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 | 1 | void _PR_InitClock(void) { |
23 | 1 | _PR_MD_INTERVAL_INIT(); |
24 | 1 | #ifdef DEBUG |
25 | 1 | { |
26 | 1 | PRIntervalTime ticksPerSec = PR_TicksPerSecond(); |
27 | | |
28 | 1 | PR_ASSERT(ticksPerSec >= PR_INTERVAL_MIN); |
29 | 1 | PR_ASSERT(ticksPerSec <= PR_INTERVAL_MAX); |
30 | 1 | } |
31 | 1 | #endif /* DEBUG */ |
32 | 1 | } |
33 | | |
34 | 0 | PR_IMPLEMENT(PRIntervalTime) PR_IntervalNow(void) { |
35 | 0 | if (!_pr_initialized) { |
36 | 0 | _PR_ImplicitInitialization(); |
37 | 0 | } |
38 | 0 | return _PR_MD_GET_INTERVAL(); |
39 | 0 | } /* PR_IntervalNow */ |
40 | | |
41 | 2 | PR_EXTERN(PRUint32) PR_TicksPerSecond(void) { |
42 | 2 | if (!_pr_initialized) { |
43 | 0 | _PR_ImplicitInitialization(); |
44 | 0 | } |
45 | 2 | return _PR_MD_INTERVAL_PER_SEC(); |
46 | 2 | } /* PR_TicksPerSecond */ |
47 | | |
48 | 0 | PR_IMPLEMENT(PRIntervalTime) PR_SecondsToInterval(PRUint32 seconds) { |
49 | 0 | return seconds * PR_TicksPerSecond(); |
50 | 0 | } /* PR_SecondsToInterval */ |
51 | | |
52 | 1 | PR_IMPLEMENT(PRIntervalTime) PR_MillisecondsToInterval(PRUint32 milli) { |
53 | 1 | PRIntervalTime ticks; |
54 | 1 | PRUint64 tock, tps, msecPerSec, rounding; |
55 | 1 | LL_UI2L(tock, milli); |
56 | 1 | LL_I2L(msecPerSec, PR_MSEC_PER_SEC); |
57 | 1 | LL_I2L(rounding, (PR_MSEC_PER_SEC >> 1)); |
58 | 1 | LL_I2L(tps, PR_TicksPerSecond()); |
59 | 1 | LL_MUL(tock, tock, tps); |
60 | 1 | LL_ADD(tock, tock, rounding); |
61 | 1 | LL_DIV(tock, tock, msecPerSec); |
62 | 1 | LL_L2UI(ticks, tock); |
63 | 1 | return ticks; |
64 | 1 | } /* PR_MillisecondsToInterval */ |
65 | | |
66 | 0 | PR_IMPLEMENT(PRIntervalTime) PR_MicrosecondsToInterval(PRUint32 micro) { |
67 | 0 | PRIntervalTime ticks; |
68 | 0 | PRUint64 tock, tps, usecPerSec, rounding; |
69 | 0 | LL_UI2L(tock, micro); |
70 | 0 | LL_I2L(usecPerSec, PR_USEC_PER_SEC); |
71 | 0 | LL_I2L(rounding, (PR_USEC_PER_SEC >> 1)); |
72 | 0 | LL_I2L(tps, PR_TicksPerSecond()); |
73 | 0 | LL_MUL(tock, tock, tps); |
74 | 0 | LL_ADD(tock, tock, rounding); |
75 | 0 | LL_DIV(tock, tock, usecPerSec); |
76 | 0 | LL_L2UI(ticks, tock); |
77 | 0 | return ticks; |
78 | 0 | } /* PR_MicrosecondsToInterval */ |
79 | | |
80 | 0 | PR_IMPLEMENT(PRUint32) PR_IntervalToSeconds(PRIntervalTime ticks) { |
81 | 0 | return ticks / PR_TicksPerSecond(); |
82 | 0 | } /* PR_IntervalToSeconds */ |
83 | | |
84 | 0 | PR_IMPLEMENT(PRUint32) PR_IntervalToMilliseconds(PRIntervalTime ticks) { |
85 | 0 | PRUint32 milli; |
86 | 0 | PRUint64 tock, tps, msecPerSec, rounding; |
87 | 0 | LL_UI2L(tock, ticks); |
88 | 0 | LL_I2L(msecPerSec, PR_MSEC_PER_SEC); |
89 | 0 | LL_I2L(tps, PR_TicksPerSecond()); |
90 | 0 | LL_USHR(rounding, tps, 1); |
91 | 0 | LL_MUL(tock, tock, msecPerSec); |
92 | 0 | LL_ADD(tock, tock, rounding); |
93 | 0 | LL_DIV(tock, tock, tps); |
94 | 0 | LL_L2UI(milli, tock); |
95 | 0 | return milli; |
96 | 0 | } /* PR_IntervalToMilliseconds */ |
97 | | |
98 | 0 | PR_IMPLEMENT(PRUint32) PR_IntervalToMicroseconds(PRIntervalTime ticks) { |
99 | 0 | PRUint32 micro; |
100 | 0 | PRUint64 tock, tps, usecPerSec, rounding; |
101 | 0 | LL_UI2L(tock, ticks); |
102 | 0 | LL_I2L(usecPerSec, PR_USEC_PER_SEC); |
103 | 0 | LL_I2L(tps, PR_TicksPerSecond()); |
104 | 0 | LL_USHR(rounding, tps, 1); |
105 | 0 | LL_MUL(tock, tock, usecPerSec); |
106 | 0 | LL_ADD(tock, tock, rounding); |
107 | 0 | LL_DIV(tock, tock, tps); |
108 | 0 | LL_L2UI(micro, tock); |
109 | 0 | return micro; |
110 | 0 | } /* PR_IntervalToMicroseconds */ |
111 | | |
112 | | /* prinrval.c */ |