Pyrogenesis  trunk
counter.h
Go to the documentation of this file.
1 /* Copyright (c) 2010 Wildfire Games
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining
4  * a copy of this software and associated documentation files (the
5  * "Software"), to deal in the Software without restriction, including
6  * without limitation the rights to use, copy, modify, merge, publish,
7  * distribute, sublicense, and/or sell copies of the Software, and to
8  * permit persons to whom the Software is furnished to do so, subject to
9  * the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 /*
24  * Interface for counter implementations
25  */
26 
27 #ifndef INCLUDED_COUNTER
28 #define INCLUDED_COUNTER
29 
30 // derived implementations must be called CounterIMPL,
31 // where IMPL matches the WHRT_IMPL identifier. (see CREATE)
32 class ICounter
33 {
34 public:
35  // (compiled-generated) ctor only sets up the vptr
36  virtual ~ICounter() {}
37 
38  virtual const char* Name() const = 0;
39 
40  // Activate with an error return value is much cleaner+safer than
41  // throwing exceptions in the ctor.
42  virtual Status Activate() = 0;
43  virtual void Shutdown() = 0;
44 
45  virtual bool IsSafe() const = 0;
46 
47  /**
48  * @return the current value of the counter (all but the lower
49  * CounterBits() bits must be zero)
50  **/
51  virtual u64 Counter() const = 0;
52 
53  // note: implementations need not cache the following; that's taken
54  // care of by WHRT.
55 
56  /**
57  * @return the bit width of the counter (<= 64)
58  * WHRT uses this to ensure the counter (running at nominal frequency)
59  * doesn't overflow more than once during CALIBRATION_INTERVAL_MS.
60  **/
61  virtual size_t CounterBits() const = 0;
62 
63  /**
64  * initial measurement of the tick rate. not necessarily correct
65  * (e.g. when using TSC: os_cpu_ClockFrequency isn't exact).
66  **/
67  virtual double NominalFrequency() const = 0;
68 
69  /**
70  * actual resolution [s]. differs from 1/NominalFrequency if the
71  * timer adjustment is greater than 1 tick.
72  **/
73  virtual double Resolution() const = 0;
74 };
75 
76 
77 /**
78  * @return a newly created ICounter of type <id> or 0 iff the ID is invalid.
79  * @param id integer ID (0..N-1)
80  *
81  * there can only be one active counter at a time; the previous one must
82  * have been destroyed before creating another!
83  **/
84 extern ICounter* CreateCounter(size_t id);
85 
86 /**
87  * shut down the counter, free its resources and zero its pointer.
88  **/
89 extern void DestroyCounter(ICounter*& counter);
90 
91 #endif // #ifndef INCLUDED_COUNTER
virtual ~ICounter()
Definition: counter.h:36
virtual Status Activate()=0
static ICounter * counter
Definition: whrt.cpp:96
Definition: counter.h:32
virtual const char * Name() const =0
ICounter * CreateCounter(size_t id)
Definition: counter.cpp:82
uint64_t u64
Definition: types.h:40
virtual double NominalFrequency() const =0
initial measurement of the tick rate.
virtual double Resolution() const =0
actual resolution [s].
void DestroyCounter(ICounter *&counter)
shut down the counter, free its resources and zero its pointer.
Definition: counter.cpp:109
virtual void Shutdown()=0
i64 Status
Error handling system.
Definition: status.h:171
virtual u64 Counter() const =0
virtual bool IsSafe() const =0
virtual size_t CounterBits() const =0