1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* ***** BEGIN LICENSE BLOCK *****
3
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
5
* The contents of this file are subject to the Mozilla Public License Version
6
* 1.1 (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
8
* http://www.mozilla.org/MPL/
10
* Software distributed under the License is distributed on an "AS IS" basis,
11
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12
* for the specific language governing rights and limitations under the
15
* The Original Code is the Netscape Portable Runtime (NSPR).
17
* The Initial Developer of the Original Code is
18
* Netscape Communications Corporation.
19
* Portions created by the Initial Developer are Copyright (C) 1998-2000
20
* the Initial Developer. All Rights Reserved.
24
* Alternatively, the contents of this file may be used under the terms of
25
* either the GNU General Public License Version 2 or later (the "GPL"), or
26
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27
* in which case the provisions of the GPL or the LGPL are applicable instead
28
* of those above. If you wish to allow use of your version of this file only
29
* under the terms of either the GPL or the LGPL, and not to allow others to
30
* use your version of this file under the terms of the MPL, indicate your
31
* decision by deleting the provisions above and replace them with the notice
32
* and other provisions required by the GPL or the LGPL. If you do not delete
33
* the provisions above, a recipient may use your version of this file under
34
* the terms of any one of the MPL, the GPL or the LGPL.
36
* ***** END LICENSE BLOCK ***** */
40
* description: implementation for the kernel interval timing functions
46
*-----------------------------------------------------------------------
51
*-----------------------------------------------------------------------
54
void _PR_InitClock(void)
56
_PR_MD_INTERVAL_INIT();
59
PRIntervalTime ticksPerSec = PR_TicksPerSecond();
61
PR_ASSERT(ticksPerSec >= PR_INTERVAL_MIN);
62
PR_ASSERT(ticksPerSec <= PR_INTERVAL_MAX);
68
* This version of interval times is based on the time of day
69
* capability offered by system. This isn't valid for two reasons:
70
* 1) The time of day is neither linear nor montonically increasing
71
* 2) The units here are milliseconds. That's not appropriate for our use.
74
PR_IMPLEMENT(PRIntervalTime) PR_IntervalNow(void)
76
if (!_pr_initialized) _PR_ImplicitInitialization();
77
return _PR_MD_GET_INTERVAL();
78
} /* PR_IntervalNow */
80
PR_EXTERN(PRUint32) PR_TicksPerSecond(void)
82
if (!_pr_initialized) _PR_ImplicitInitialization();
83
return _PR_MD_INTERVAL_PER_SEC();
84
} /* PR_TicksPerSecond */
86
PR_IMPLEMENT(PRIntervalTime) PR_SecondsToInterval(PRUint32 seconds)
88
return seconds * PR_TicksPerSecond();
89
} /* PR_SecondsToInterval */
91
PR_IMPLEMENT(PRIntervalTime) PR_MillisecondsToInterval(PRUint32 milli)
94
PRUint64 tock, tps, msecPerSec, rounding;
96
LL_I2L(msecPerSec, PR_MSEC_PER_SEC);
97
LL_I2L(rounding, (PR_MSEC_PER_SEC >> 1));
98
LL_I2L(tps, PR_TicksPerSecond());
99
LL_MUL(tock, tock, tps);
100
LL_ADD(tock, tock, rounding);
101
LL_DIV(tock, tock, msecPerSec);
102
LL_L2UI(ticks, tock);
104
} /* PR_MillisecondsToInterval */
106
PR_IMPLEMENT(PRIntervalTime) PR_MicrosecondsToInterval(PRUint32 micro)
108
PRIntervalTime ticks;
109
PRUint64 tock, tps, usecPerSec, rounding;
110
LL_UI2L(tock, micro);
111
LL_I2L(usecPerSec, PR_USEC_PER_SEC);
112
LL_I2L(rounding, (PR_USEC_PER_SEC >> 1));
113
LL_I2L(tps, PR_TicksPerSecond());
114
LL_MUL(tock, tock, tps);
115
LL_ADD(tock, tock, rounding);
116
LL_DIV(tock, tock, usecPerSec);
117
LL_L2UI(ticks, tock);
119
} /* PR_MicrosecondsToInterval */
121
PR_IMPLEMENT(PRUint32) PR_IntervalToSeconds(PRIntervalTime ticks)
123
return ticks / PR_TicksPerSecond();
124
} /* PR_IntervalToSeconds */
126
PR_IMPLEMENT(PRUint32) PR_IntervalToMilliseconds(PRIntervalTime ticks)
129
PRUint64 tock, tps, msecPerSec, rounding;
130
LL_UI2L(tock, ticks);
131
LL_I2L(msecPerSec, PR_MSEC_PER_SEC);
132
LL_I2L(tps, PR_TicksPerSecond());
133
LL_USHR(rounding, tps, 1);
134
LL_MUL(tock, tock, msecPerSec);
135
LL_ADD(tock, tock, rounding);
136
LL_DIV(tock, tock, tps);
137
LL_L2UI(milli, tock);
139
} /* PR_IntervalToMilliseconds */
141
PR_IMPLEMENT(PRUint32) PR_IntervalToMicroseconds(PRIntervalTime ticks)
144
PRUint64 tock, tps, usecPerSec, rounding;
145
LL_UI2L(tock, ticks);
146
LL_I2L(usecPerSec, PR_USEC_PER_SEC);
147
LL_I2L(tps, PR_TicksPerSecond());
148
LL_USHR(rounding, tps, 1);
149
LL_MUL(tock, tock, usecPerSec);
150
LL_ADD(tock, tock, rounding);
151
LL_DIV(tock, tock, tps);
152
LL_L2UI(micro, tock);
154
} /* PR_IntervalToMicroseconds */