1 /*! ========================================================================
2 ** Extended Template and Library
3 ** Clock Abstraction Implementation
6 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
8 ** This package is free software; you can redistribute it and/or
9 ** modify it under the terms of the GNU General Public License as
10 ** published by the Free Software Foundation; either version 2 of
11 ** the License, or (at your option) any later version.
13 ** This package is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 ** General Public License for more details.
18 ** === N O T E S ===========================================================
20 ** This is an internal header file, included by other ETL headers.
21 ** You should not attempt to use it directly.
23 ** ========================================================================= */
25 /* === S T A R T =========================================================== */
27 #ifndef __ETL__CLOCK_H
28 #define __ETL__CLOCK_H
30 /* === H E A D E R S ======================================================= */
35 inline void sleep(int i) { Sleep(i*1000); }
38 /* === M A C R O S ========================================================= */
40 /* === T Y P E D E F S ===================================================== */
42 /* === C L A S S E S & S T R U C T S ======================================= */
46 inline void yield() { sleep(0); }
48 /*! ========================================================================
50 ** \brief clock abstraction
52 ** A more detailed description needs to be written.
55 class clock_base : public DESC
58 typedef typename DESC::value_type value_type;
61 typedef clock_base<DESC> _clock;
62 typedef typename DESC::timestamp timestamp;
66 using DESC::get_current_time;
68 using DESC::one_second;
71 clock_base() { reset(); }
74 { get_current_time(base_time); }
76 value_type operator()()const
77 { return timestamp_to_seconds(get_current_time()-base_time); }
81 // Grab the old base time
82 timestamp old_time=base_time;
84 // Put the current time into base_time
85 get_current_time(base_time);
87 return timestamp_to_seconds(base_time-old_time);
91 sleep(const value_type &length)
94 ::sleep((int)(length+0.5));
100 for(val=timer();one_second()<length-val;val=timer())
101 ::sleep((int)((length-val)/2.0+0.4));
102 while(timer()<length)
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.
115 ** - darco (8-17-2002)
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);
131 /* === E N D =============================================================== */