ETL  0.04.19
_clock_base.h
Go to the documentation of this file.
1 
25 /* === S T A R T =========================================================== */
26 
27 #ifndef __ETL__CLOCK_H
28 #define __ETL__CLOCK_H
29 
30 /* === H E A D E R S ======================================================= */
31 
32 #ifndef WIN32
33 #include <unistd.h>
34 #else
35 inline void sleep(int i) { Sleep(i*1000); }
36 #endif
37 
38 /* === M A C R O S ========================================================= */
39 
40 /* === T Y P E D E F S ===================================================== */
41 
42 /* === C L A S S E S & S T R U C T S ======================================= */
43 
45 
46 inline void yield() { sleep(0); }
47 
54 template <class DESC>
55 class clock_base : public DESC
56 {
57 public:
58  typedef typename DESC::value_type value_type;
59 
60 private:
62  typedef typename DESC::timestamp timestamp;
63 
65 
66  using DESC::get_current_time;
67  using DESC::realtime;
68  using DESC::one_second;
69 public:
70 
71  clock_base() { reset(); }
72 
73  void reset()
74  { get_current_time(base_time); }
75 
77  { return this->timestamp_to_seconds(get_current_time()-base_time); }
78 
80  {
81  // Grab the old base time
82  timestamp old_time=base_time;
83 
84  // Put the current time into base_time
85  get_current_time(base_time);
86 
87  return this->timestamp_to_seconds(base_time-old_time);
88  }
89 
90  static void
91  sleep(const value_type &length)
92  {
93  if(!realtime())
94  ::sleep((int)(length+0.5));
95  else
96  {
97  _clock timer;
98  timer.reset();
99  value_type val;
100  for(val=timer();one_second()<length-val;val=timer())
101  ::sleep((int)((length-val)/2.0+0.4));
102  while(timer()<length)
103  ;
104  }
105 
106 
107  /* This is a different waiting mechanism that uses
108  ** the native timestamp type of the clock rather
109  ** than converting it to a double (or whatever).
110  ** You would think that this would be at least a
111  ** few microseconds faster, but a few tests on my
112  ** PowerBook G4 have proved otherwise. Indeed I loose
113  ** several microseconds using this "optimized" method.
114  ** Bizarre.
115  ** - darco (8-17-2002)
116  {
117  timestamp endtime=get_current_time()+seconds_to_timestamp(length);
118  timestamp loopendtime=get_current_time()+seconds_to_timestamp(length-1.0);
119  while(get_current_time()<loopendtime)
120  ::sleep((int)timestamp_to_seconds(loopendtime-get_current_time())/2.0);
121  while(get_current_time()<endtime);
122  }
123  */
124 
125  return;
126  }
127 };
128 
130 
131 /* === E N D =============================================================== */
132 
133 #endif