00001
00025
00026
00027 #ifndef __ETL__CLOCK_H
00028 #define __ETL__CLOCK_H
00029
00030
00031
00032 #ifndef WIN32
00033 #include <unistd.h>
00034 #else
00035 inline void sleep(int i) { Sleep(i*1000); }
00036 #endif
00037
00038
00039
00040
00041
00042
00043
00044 _ETL_BEGIN_NAMESPACE
00045
00046 inline void yield() { sleep(0); }
00047
00054 template <class DESC>
00055 class clock_base : public DESC
00056 {
00057 public:
00058 typedef typename DESC::value_type value_type;
00059
00060 private:
00061 typedef clock_base<DESC> _clock;
00062 typedef typename DESC::timestamp timestamp;
00063
00064 timestamp base_time;
00065
00066 using DESC::get_current_time;
00067 using DESC::realtime;
00068 using DESC::one_second;
00069 public:
00070
00071 clock_base() { reset(); }
00072
00073 void reset()
00074 { get_current_time(base_time); }
00075
00076 value_type operator()()const
00077 { return timestamp_to_seconds(get_current_time()-base_time); }
00078
00079 value_type pop_time()
00080 {
00081
00082 timestamp old_time=base_time;
00083
00084
00085 get_current_time(base_time);
00086
00087 return timestamp_to_seconds(base_time-old_time);
00088 }
00089
00090 static void
00091 sleep(const value_type &length)
00092 {
00093 if(!realtime())
00094 ::sleep((int)(length+0.5));
00095 else
00096 {
00097 _clock timer;
00098 timer.reset();
00099 value_type val;
00100 for(val=timer();one_second()<length-val;val=timer())
00101 ::sleep((int)((length-val)/2.0+0.4));
00102 while(timer()<length);
00103 }
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124 return;
00125 }
00126 };
00127
00128 _ETL_END_NAMESPACE
00129
00130
00131
00132 #endif