reSIProcate/rutil  9694
Time.hxx
Go to the documentation of this file.
00001 #if !defined(RESIP_TIME_HXX)
00002 #define RESIP_TIME_HXX
00003 
00004 #include "rutil/Mutex.hxx"
00005 #include <limits.h>
00006 #include <cassert>
00007 
00008 namespace resip
00009 {
00010 
00050 class ResipClock
00051 {
00052    public:
00053       ResipClock(void);
00054       ~ResipClock(void);
00055 
00058       static UInt64 getTimeMicroSec()
00059       {
00060          return getSystemTime();
00061       }
00062 
00063 
00066       static UInt64 getTimeMs()
00067       {
00068          return getSystemTime()/1000LL;
00069       }
00070 
00073       static UInt64 getTimeSecs()
00074       {
00075          return getSystemTime()/1000000LL;
00076       }
00077 
00081       static UInt64 getRandomFutureTimeMs( UInt64 futureMs );
00084       static UInt64 getForever();
00085 
00089       static unsigned getMaxSystemTimeWaitMs(void)
00090       {
00091          return mMaxSystemTimeWaitMs;
00092       }     
00093 
00099       static void queryTimerInfo(unsigned &minRes, unsigned &maxRes, unsigned &actualRes, bool &isMonotonic);
00100 
00101    private:
00105       static UInt64 getSystemTime();
00106 
00107       static unsigned mMaxSystemTimeWaitMs;
00108 
00109 #ifdef WIN32
00110    private:
00115       class WinMonoClock
00116       {
00117          public:
00118             WinMonoClock();        
00119 
00123             static UInt64 GetClock64(void)
00124             {
00125                return mGTC64();
00126             }                     
00127 
00128          private:
00129 
00130             static void Initialize(void);
00133             typedef UInt64 (*PGTC64)(void);
00134 
00139             class GTCInterlocked
00140             {
00141                public:
00142 
00143                   static UInt64 GTC64(void);
00144 
00148                   static UInt32 GetMaxWaitMs(void)
00149                   {
00150                      //Since the base time isn't updated on every call, need to ensure that it's updated once every 49.7 days.
00151                      //The base time will lag behind the current tick count, which means the lag time must be used
00152                      //to determine the max wait.
00153                      //Also need to add a cushion to this calculaton because ::timeGetTime may not be accurate to 1ms.
00154                      __int64 maxWait = (__int64)UINT_MAX - mBaseTimeUpdateInterval - mBaseTimeCushion;
00155                      if (maxWait <= 0)
00156                      {
00157                         assert(0);
00158                         const_cast<UInt32 &>(mBaseTimeUpdateInterval) = 60000;
00159                         const_cast<UInt32 &>(mBaseTimeCushion) = 120000;
00160                         return UINT_MAX - mBaseTimeUpdateInterval - mBaseTimeCushion;
00161                      }
00162                      return static_cast<UInt32>(maxWait);
00163                   }
00164 
00165                private:
00169                   static _declspec(align(128)) volatile UInt64 mBaseTime;
00173                   static const UInt32 mBaseTimeUpdateInterval = 60000;
00174                   static const UInt32 mBaseTimeCushion = 120000; 
00175             };
00176 
00182             class GTCLockDuringRange
00183             {
00184                public:
00185 
00186                   static UInt64 GTC64(void);
00187 
00188                   static UInt32 GetMaxWaitMs(void)
00189                   {
00190                      return 120000;
00191                   }
00192 
00193                private:
00198                   static UInt32 mWrapCounter;
00199 
00202                   static DWORD mPrevTick;
00203 
00206                   static Mutex mWrapCounterMutex;
00207 
00208             };
00209 
00213             class GTCLock
00214             {
00215                public:
00216                   static UInt64 GTC64(void);
00217 
00218                private:
00219                   static ULARGE_INTEGER mBaseTime;
00220 
00223                   static Mutex mMutex;
00224             };
00225 
00226             static PGTC64 mGTC64;
00227       };
00228 #endif
00229 };
00230 
00231 
00232 }//namespace resip
00233 
00234 #endif
00235 /* ====================================================================
00236  * The Vovida Software License, Version 1.0
00237  *
00238  * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
00239  *
00240  * Redistribution and use in source and binary forms, with or without
00241  * modification, are permitted provided that the following conditions
00242  * are met:
00243  *
00244  * 1. Redistributions of source code must retain the above copyright
00245  *    notice, this list of conditions and the following disclaimer.
00246  *
00247  * 2. Redistributions in binary form must reproduce the above copyright
00248  *    notice, this list of conditions and the following disclaimer in
00249  *    the documentation and/or other materials provided with the
00250  *    distribution.
00251  *
00252  * 3. The names "VOCAL", "Vovida Open Communication Application Library",
00253  *    and "Vovida Open Communication Application Library (VOCAL)" must
00254  *    not be used to endorse or promote products derived from this
00255  *    software without prior written permission. For written
00256  *    permission, please contact vocal@vovida.org.
00257  *
00258  * 4. Products derived from this software may not be called "VOCAL", nor
00259  *    may "VOCAL" appear in their name, without prior written
00260  *    permission of Vovida Networks, Inc.
00261  *
00262  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
00263  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00264  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
00265  * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
00266  * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
00267  * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
00268  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00269  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00270  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
00271  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00272  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00273  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00274  * DAMAGE.
00275  *
00276  * ====================================================================
00277  *
00278  * This software consists of voluntary contributions made by Vovida
00279  * Networks, Inc. and many individuals on behalf of Vovida Networks,
00280  * Inc.  For more information on Vovida Networks, Inc., please see
00281  * <http://www.vovida.org/>.
00282  *
00283  * vi: set shiftwidth=3 expandtab:
00284  */