Back to index

lightning-sunbird  0.9+nobinonly
prinrval.c
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /*
00039  * file:                    prinrval.c
00040  * description:             implementation for the kernel interval timing functions
00041  */
00042 
00043 #include "primpl.h"
00044 
00045 /*
00046  *-----------------------------------------------------------------------
00047  *
00048  * _PR_InitClock --
00049  *
00050  *
00051  *-----------------------------------------------------------------------
00052  */
00053 
00054 void _PR_InitClock(void)
00055 {
00056     _PR_MD_INTERVAL_INIT();
00057 #ifdef DEBUG
00058     {
00059         PRIntervalTime ticksPerSec = PR_TicksPerSecond();
00060 
00061         PR_ASSERT(ticksPerSec >= PR_INTERVAL_MIN);
00062         PR_ASSERT(ticksPerSec <= PR_INTERVAL_MAX);
00063     }
00064 #endif /* DEBUG */
00065 }
00066 
00067 /*
00068  * This version of interval times is based on the time of day
00069  * capability offered by system. This isn't valid for two reasons:
00070  * 1) The time of day is neither linear nor montonically increasing
00071  * 2) The units here are milliseconds. That's not appropriate for our use.
00072  */
00073 
00074 PR_IMPLEMENT(PRIntervalTime) PR_IntervalNow(void)
00075 {
00076     if (!_pr_initialized) _PR_ImplicitInitialization();
00077     return _PR_MD_GET_INTERVAL();
00078 }  /* PR_IntervalNow */
00079 
00080 PR_EXTERN(PRUint32) PR_TicksPerSecond(void)
00081 {
00082     if (!_pr_initialized) _PR_ImplicitInitialization();
00083     return _PR_MD_INTERVAL_PER_SEC();
00084 }  /* PR_TicksPerSecond */
00085 
00086 PR_IMPLEMENT(PRIntervalTime) PR_SecondsToInterval(PRUint32 seconds)
00087 {
00088     return seconds * PR_TicksPerSecond();
00089 }  /* PR_SecondsToInterval */
00090 
00091 PR_IMPLEMENT(PRIntervalTime) PR_MillisecondsToInterval(PRUint32 milli)
00092 {
00093     PRIntervalTime ticks;
00094     PRUint64 tock, tps, msecPerSec, rounding;
00095     LL_UI2L(tock, milli);
00096     LL_I2L(msecPerSec, PR_MSEC_PER_SEC);
00097     LL_I2L(rounding, (PR_MSEC_PER_SEC >> 1));
00098     LL_I2L(tps, PR_TicksPerSecond());
00099     LL_MUL(tock, tock, tps);
00100     LL_ADD(tock, tock, rounding);
00101     LL_DIV(tock, tock, msecPerSec);
00102     LL_L2UI(ticks, tock);
00103     return ticks;
00104 }  /* PR_MillisecondsToInterval */
00105 
00106 PR_IMPLEMENT(PRIntervalTime) PR_MicrosecondsToInterval(PRUint32 micro)
00107 {
00108     PRIntervalTime ticks;
00109     PRUint64 tock, tps, usecPerSec, rounding;
00110     LL_UI2L(tock, micro);
00111     LL_I2L(usecPerSec, PR_USEC_PER_SEC);
00112     LL_I2L(rounding, (PR_USEC_PER_SEC >> 1));
00113     LL_I2L(tps, PR_TicksPerSecond());
00114     LL_MUL(tock, tock, tps);
00115     LL_ADD(tock, tock, rounding);
00116     LL_DIV(tock, tock, usecPerSec);
00117     LL_L2UI(ticks, tock);
00118     return ticks;
00119 }  /* PR_MicrosecondsToInterval */
00120 
00121 PR_IMPLEMENT(PRUint32) PR_IntervalToSeconds(PRIntervalTime ticks)
00122 {
00123     return ticks / PR_TicksPerSecond();
00124 }  /* PR_IntervalToSeconds */
00125 
00126 PR_IMPLEMENT(PRUint32) PR_IntervalToMilliseconds(PRIntervalTime ticks)
00127 {
00128     PRUint32 milli;
00129     PRUint64 tock, tps, msecPerSec, rounding;
00130     LL_UI2L(tock, ticks);
00131     LL_I2L(msecPerSec, PR_MSEC_PER_SEC);
00132     LL_I2L(tps, PR_TicksPerSecond());
00133     LL_USHR(rounding, tps, 1);
00134     LL_MUL(tock, tock, msecPerSec);
00135     LL_ADD(tock, tock, rounding);
00136     LL_DIV(tock, tock, tps);
00137     LL_L2UI(milli, tock);
00138     return milli;
00139 }  /* PR_IntervalToMilliseconds */
00140 
00141 PR_IMPLEMENT(PRUint32) PR_IntervalToMicroseconds(PRIntervalTime ticks)
00142 {
00143     PRUint32 micro;
00144     PRUint64 tock, tps, usecPerSec, rounding;
00145     LL_UI2L(tock, ticks);
00146     LL_I2L(usecPerSec, PR_USEC_PER_SEC);
00147     LL_I2L(tps, PR_TicksPerSecond());
00148     LL_USHR(rounding, tps, 1);
00149     LL_MUL(tock, tock, usecPerSec);
00150     LL_ADD(tock, tock, rounding);
00151     LL_DIV(tock, tock, tps);
00152     LL_L2UI(micro, tock);
00153     return micro;
00154 }  /* PR_IntervalToMicroseconds */
00155 
00156 /* prinrval.c */
00157