Panda3D

trueClock.h

00001 // Filename: trueClock.h
00002 // Created by:  drose (04Jul00)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #ifndef TRUECLOCK_H
00016 #define TRUECLOCK_H
00017 
00018 #include "pandabase.h"
00019 #include "typedef.h"
00020 #include "pdeque.h"
00021 #include "mutexImpl.h"
00022 #include "config_express.h"
00023 
00024 ////////////////////////////////////////////////////////////////////
00025 //       Class : TrueClock
00026 // Description : An interface to whatever real-time clock we might
00027 //               have available in the current environment.  There is
00028 //               only one TrueClock in existence, and it constructs
00029 //               itself.
00030 //
00031 //               The TrueClock returns elapsed real time in seconds
00032 //               since some undefined epoch.  Since it is not defined
00033 //               at what time precisely the clock indicates zero, this
00034 //               value can only be meaningfully used to measure
00035 //               elapsed time, by sampling it at two different times
00036 //               and subtracting.
00037 ////////////////////////////////////////////////////////////////////
00038 class EXPCL_PANDAEXPRESS TrueClock {
00039 PUBLISHED:
00040   // get_long_time() returns the most accurate timer we have over a
00041   // long interval.  It may not be very precise for measuring short
00042   // intervals, but it should not drift substantially over the long
00043   // haul.
00044   double get_long_time();
00045 
00046   // get_short_time() returns the most precise timer we have over a
00047   // short interval.  It may tend to drift over the long haul, but it
00048   // should have lots of digits to measure short intervals very
00049   // precisely.
00050   INLINE double get_short_time();
00051 
00052   // get_short_raw_time() is like get_short_time(), but does not apply
00053   // any corrections (e.g. paranoid-clock) to the result returned by
00054   // the OS.
00055   double get_short_raw_time();
00056 
00057   INLINE int get_error_count() const;
00058 
00059   INLINE static TrueClock *get_global_ptr();
00060 
00061   bool set_cpu_affinity(PN_uint32 mask) const;
00062 
00063 protected:
00064   TrueClock();
00065   INLINE ~TrueClock();
00066 
00067   int _error_count;
00068 
00069   static TrueClock *_global_ptr;
00070 
00071 #ifdef WIN32
00072   double correct_time(double time);
00073   void set_time_scale(double time, double new_time_scale);
00074 
00075   bool _has_high_res;
00076   PN_int64 _init_count;
00077   double _frequency, _recip_frequency;
00078   int _init_tc;
00079   PN_uint64 _init_tod;
00080 
00081   // The rest of the data structures in this block are strictly for
00082   // implementing paranoid_clock: they are designed to allow us to
00083   // cross-check the high-resolution clock against the time-of-day
00084   // clock, and smoothly correct for deviations.
00085   class Timestamp {
00086   public:
00087     Timestamp(double time, double tod) : _time(time), _tod(tod) { }
00088     double _time;
00089     double _tod;
00090   };
00091   typedef pdeque<Timestamp> Timestamps;
00092   Timestamps _timestamps;
00093   double _time_scale;
00094   double _time_offset;
00095   double _tod_offset;
00096   int _num_jump_errors;
00097   bool _time_scale_changed;
00098   double _last_reported_time_scale;
00099   double _report_time_scale_time;
00100   enum ChaseClock {
00101     CC_slow_down,
00102     CC_keep_even,
00103     CC_speed_up,
00104   };
00105   ChaseClock _chase_clock;
00106   MutexImpl _lock;
00107 #endif  // WIN32
00108 };
00109 
00110 #include "trueClock.I"
00111 
00112 #endif
 All Classes Functions Variables Enumerations