- Main Page
- Related Pages
- Classes
- Files

# _clock_base.h

Go to the documentation of this file.

    00001
    00025 /* === S T A R T =========================================================== */
    00026
    00027 #ifndef __ETL__CLOCK_H
    00028 #define __ETL__CLOCK_H
    00029
    00030 /* === H E A D E R S ======================================================= */
    00031
    00032 #ifndef WIN32
    00033 #include <unistd.h>
    00034 #else
    00035 inline void sleep(int i) { Sleep(i*1000); }
    00036 #endif
    00037
    00038 /* === M A C R O S ========================================================= */
    00039
    00040 /* === T Y P E D E F S ===================================================== */
    00041
    00042 /* === C L A S S E S & S T R U C T S ======================================= */
    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         // Grab the old base time
    00082         timestamp old_time=base_time;
    00083
    00084         // Put the current time into base_time
    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         /* This is a different waiting mechanism that uses
    00108         ** the native timestamp type of the clock rather
    00109         ** than converting it to a double (or whatever).
    00110         ** You would think that this would be at least a
    00111         ** few microseconds faster, but a few tests on my
    00112         ** PowerBook G4 have proved otherwise. Indeed I loose
    00113         ** several microseconds using this "optimized" method.
    00114         ** Bizarre.
    00115         **  - darco (8-17-2002)
    00116         {
    00117             timestamp endtime=get_current_time()+seconds_to_timestamp(length);
    00118             timestamp loopendtime=get_current_time()+seconds_to_timestamp(length-1.0);
    00119             while(get_current_time()<loopendtime)
    00120                 ::sleep((int)timestamp_to_seconds(loopendtime-get_current_time())/2.0);
    00121             while(get_current_time()<endtime);
    00122         }
    00123         */
    00124
    00125         return;
    00126     }
    00127 };
    00128
    00129 _ETL_END_NAMESPACE
    00130
    00131 /* === E N D =============================================================== */
    00132
    00133 #endif