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