Panda3D
trueClock.h
1 // Filename: trueClock.h
2 // Created by: drose (04Jul00)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #ifndef TRUECLOCK_H
16 #define TRUECLOCK_H
17 
18 #include "pandabase.h"
19 #include "typedef.h"
20 #include "pdeque.h"
21 #include "mutexImpl.h"
22 #include "config_express.h"
23 
24 ////////////////////////////////////////////////////////////////////
25 // Class : TrueClock
26 // Description : An interface to whatever real-time clock we might
27 // have available in the current environment. There is
28 // only one TrueClock in existence, and it constructs
29 // itself.
30 //
31 // The TrueClock returns elapsed real time in seconds
32 // since some undefined epoch. Since it is not defined
33 // at what time precisely the clock indicates zero, this
34 // value can only be meaningfully used to measure
35 // elapsed time, by sampling it at two different times
36 // and subtracting.
37 ////////////////////////////////////////////////////////////////////
38 class EXPCL_PANDAEXPRESS TrueClock {
39 PUBLISHED:
40  // get_long_time() returns the most accurate timer we have over a
41  // long interval. It may not be very precise for measuring short
42  // intervals, but it should not drift substantially over the long
43  // haul.
44  double get_long_time();
45 
46  // get_short_time() returns the most precise timer we have over a
47  // short interval. It may tend to drift over the long haul, but it
48  // should have lots of digits to measure short intervals very
49  // precisely.
50  INLINE double get_short_time();
51 
52  // get_short_raw_time() is like get_short_time(), but does not apply
53  // any corrections (e.g. paranoid-clock) to the result returned by
54  // the OS.
55  double get_short_raw_time();
56 
57  INLINE int get_error_count() const;
58 
59  INLINE static TrueClock *get_global_ptr();
60 
61  bool set_cpu_affinity(PN_uint32 mask) const;
62 
63 protected:
64  TrueClock();
65  INLINE ~TrueClock();
66 
67  int _error_count;
68 
69  static TrueClock *_global_ptr;
70 
71 #ifdef WIN32
72  double correct_time(double time);
73  void set_time_scale(double time, double new_time_scale);
74 
75  bool _has_high_res;
76  PN_int64 _init_count;
77  double _frequency, _recip_frequency;
78  int _init_tc;
79  PN_uint64 _init_tod;
80 
81  // The rest of the data structures in this block are strictly for
82  // implementing paranoid_clock: they are designed to allow us to
83  // cross-check the high-resolution clock against the time-of-day
84  // clock, and smoothly correct for deviations.
85  class Timestamp {
86  public:
87  Timestamp(double time, double tod) : _time(time), _tod(tod) { }
88  double _time;
89  double _tod;
90  };
91  typedef pdeque<Timestamp> Timestamps;
92  Timestamps _timestamps;
93  double _time_scale;
94  double _time_offset;
95  double _tod_offset;
96  int _num_jump_errors;
97  bool _time_scale_changed;
98  double _last_reported_time_scale;
99  double _report_time_scale_time;
100  enum ChaseClock {
101  CC_slow_down,
102  CC_keep_even,
103  CC_speed_up,
104  };
105  ChaseClock _chase_clock;
106  MutexImpl _lock;
107 #endif // WIN32
108 };
109 
110 #include "trueClock.I"
111 
112 #endif
This is our own Panda specialization on the default STL deque.
Definition: pdeque.h:38
An interface to whatever real-time clock we might have available in the current environment.
Definition: trueClock.h:38
A fake mutex implementation for single-threaded applications that don&#39;t need any synchronization cont...