Back to index

lightning-sunbird  0.9+nobinonly
GUSITimer.h
Go to the documentation of this file.
00001 // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
00002 // % Project  :      GUSI                        -      Grand Unified Socket Interface                    
00003 // % File            :      GUSITimer.nw         -      Timing functions                              
00004 // % Author   :      Matthias Neeracher                                           
00005 // % Language :      C++                                                        
00006 // %                                                                       
00007 // % $Log: GUSITimer.h,v $
00008 // % Revision 1.1  2001/03/11 22:38:42  sgehani%netscape.com
00009 // % First Checked In.
00010 // %                                                
00011 // % Revision 1.12  2001/01/17 08:48:04  neeri                             
00012 // % Introduce Expired(), Reset()                                          
00013 // %                                                                       
00014 // % Revision 1.11  2000/10/29 18:36:32  neeri                             
00015 // % Fix time_t signedness issues                                          
00016 // %                                                                       
00017 // % Revision 1.10  2000/06/12 04:24:50  neeri                             
00018 // % Fix time, localtime, gmtime                                           
00019 // %                                                                       
00020 // % Revision 1.9  2000/05/23 07:24:58  neeri                              
00021 // % Improve formatting                                                    
00022 // %                                                                       
00023 // % Revision 1.8  2000/03/15 07:22:07  neeri                              
00024 // % Enforce alignment choices                                             
00025 // %                                                                       
00026 // % Revision 1.7  1999/11/15 07:20:18  neeri                              
00027 // % Safe context setup                                                    
00028 // %                                                                       
00029 // % Revision 1.6  1999/08/26 05:45:10  neeri                              
00030 // % Fixes for literate edition of source code                             
00031 // %                                                                       
00032 // % Revision 1.5  1999/08/02 07:02:46  neeri                              
00033 // % Support for asynchronous errors and other socket options              
00034 // %                                                                       
00035 // % Revision 1.4  1999/07/07 04:17:43  neeri                              
00036 // % Final tweaks for 2.0b3                                                
00037 // %                                                                       
00038 // % Revision 1.3  1999/06/28 06:08:46  neeri                              
00039 // % Support flexible timer classes                                        
00040 // %                                                                       
00041 // % Revision 1.2  1999/05/30 03:06:21  neeri                              
00042 // % Fixed various bugs in cleanup and wakeup                              
00043 // %                                                                       
00044 // % Revision 1.1  1999/03/17 09:05:14  neeri                              
00045 // % Added GUSITimer, expanded docs                                        
00046 // %                                                                       
00047 // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
00048 //                                                                         
00049 // \chapter{Timing functions}                                              
00050 //                                                                         
00051 // This section defines mechanisms to measure time. The basic mechanism is 
00052 // [[GUSITimer]] which can wake up a [[GUSIContext]] at some later time.   
00053 //                                                                         
00054 // <GUSITimer.h>=                                                          
00055 #ifndef _GUSITimer_
00056 #define _GUSITimer_
00057 
00058 #ifndef GUSI_SOURCE
00059 
00060 typedef struct GUSITimer GUSITimer;
00061 
00062 #else
00063 #include "GUSISpecific.h"
00064 
00065 #include <errno.h>
00066 #include <sys/cdefs.h>
00067 #include <sys/types.h>
00068 #include <sys/time.h>
00069 #include <inttypes.h>
00070 
00071 #include <MacTypes.h>
00072 #include <Timer.h>
00073 #include <Math64.h>
00074 
00075 #include <ConditionalMacros.h>
00076 
00077 #if PRAGMA_STRUCT_ALIGN
00078 #pragma options align=native
00079 #endif
00080 
00081 // \section{Definition of timing}                                          
00082 //                                                                         
00083 // [[GUSITime]] is an universal (if somewhat costly) format for            
00084 // the large variety of timing formats used in MacOS and POSIX.            
00085 //                                                                         
00086 // <Definition of class [[GUSITime]]>=                                     
00087 class GUSITime {
00088 public:
00089        enum Format {seconds, ticks, msecs, usecs, nsecs};
00090        
00091 #if !TYPE_LONGLONG
00092        GUSITime(int32_t  val, Format format);
00093        GUSITime(uint32_t val, Format format);
00094 #endif
00095        GUSITime(int64_t val, Format format=nsecs)       { Construct(val, format); }
00096        GUSITime(const timeval & tv);
00097        GUSITime(const timespec & ts);
00098        GUSITime(const tm & t);
00099        GUSITime() {}
00100        
00101        int32_t              Get(Format format)          { return S32Set(Get64(format));           }
00102        uint32_t      UGet(Format format)         
00103                                                  { return U32SetU(SInt64ToUInt64(Get64(format))); }
00104        int64_t              Get64(Format format);
00105        
00106        operator int64_t()          {      return fTime; }
00107        operator timeval();
00108        operator timespec();
00109        operator tm();
00110        
00111        GUSITime GM2LocalTime();
00112        GUSITime Local2GMTime();
00113        
00114        GUSITime & operator +=(const GUSITime & other) 
00115               { fTime = S64Add(fTime, other.fTime); return *this; }
00116        GUSITime & operator -=(const GUSITime & other) 
00117               { fTime = S64Subtract(fTime, other.fTime); return *this; }
00118        
00119        
00120        static GUSITime      Now();
00121        static timezone      &      Zone();
00122 private:
00123        void   Construct(int64_t val, Format format);
00124        time_t Deconstruct(int64_t & remainder);
00125        
00126        int64_t       fTime;
00127        
00128        static int64_t              sTimeOffset;
00129        static timezone      sTimeZone;
00130 };
00131 
00132 inline GUSITime operator+(const GUSITime & a, const GUSITime & b)
00133                                    { GUSITime t(a); return t+=b; }
00134 inline GUSITime operator-(const GUSITime & a, const GUSITime & b)
00135                                    { GUSITime t(a); return t-=b; }
00136 // A [[GUSITimer]] is a time manager task that wakes up a [[GUSIContext]]. 
00137 //                                                                         
00138 // <Definition of class [[GUSITimer]]>=                                    
00139 #if PRAGMA_STRUCT_ALIGN
00140 #pragma options align=mac68k
00141 #endif
00142 class GUSIContext;
00143 
00144 extern "C" void GUSIKillTimers(void * timers);
00145 
00146 class GUSITimer : public TMTask {
00147 public:
00148        GUSITimer(bool wakeup = true, GUSIContext * context = 0);
00149        virtual ~GUSITimer();
00150        
00151        void                 Sleep(long ms, bool driftFree = false);
00152        void                 MicroSleep(long us, bool driftFree = false)
00153                                                                       { Sleep(-us, driftFree);                         }
00154        GUSIContext * Context()                   { return fQueue->fContext;                       }
00155        GUSITimer *          Next()                      { return fNext;                                                }
00156        bool                 Primed()                    { return (qType&kTMTaskActive) != 0;      }
00157        bool                 Expired()                   { return !(qType&kTMTaskActive);          }
00158        virtual void  Wakeup();
00159        void                 Kill();
00160        void                 Reset();
00161        
00162        struct Queue {
00163               GUSITimer     *      fTimer;
00164               GUSIContext * fContext;
00165               
00166               Queue() : fTimer(0) {}
00167        };
00168        
00169        QElem *       Elem()        {      return reinterpret_cast<QElem *>(&this->qLink); }
00170 protected:
00171        Queue *                     fQueue;
00172        GUSITimer *   fNext;
00173        
00174        class TimerQueue : public GUSISpecificData<Queue,GUSIKillTimers> {
00175        public:
00176               ~TimerQueue();
00177        };
00178        
00179        static TimerQueue    sTimerQueue;
00180        static TimerUPP             sTimerProc;
00181 };
00182 #if PRAGMA_STRUCT_ALIGN
00183 #pragma options align=reset
00184 #endif
00185 
00186 #if PRAGMA_STRUCT_ALIGN
00187 #pragma options align=reset
00188 #endif
00189 
00190 #endif /* GUSI_SOURCE */
00191 
00192 #endif /* _GUSITimer_ */