2 # Copyright (c) 2007 Jonas Pfenniger
4 # Permission is hereby granted, free of charge, to any person obtaining a copy
5 # of this software and associated documentation files (the "Software"), to deal
6 # in the Software without restriction, including without limitation the rights
7 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 # copies of the Software, and to permit persons to whom the Software is
9 # furnished to do so, subject to the following conditions:
11 # The above copyright notice and this permission notice shall be included in
12 # all copies or substantial portions of the Software.
14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 # Abstract implementation of a clock, inherited by each specific clocks.
28 # Returns the clock's current time in nanoseconds. The time is not
29 # correlated to the system's clock user representation.
30 def current_time; raise NotImplementedError, "abstract method not overridden"; end
32 # The resolution is a constant which indicated what the mimimum step of
35 # Returns nil if the information is not available.
36 def resolution; nil; end
38 # Returns how much times per second the clock is updated.
40 # Returns nil if the information is not available.
41 def frequency; 10e9; end
43 # Returns true if the clock is monotonic
44 def is_monotonic?; false; end
46 # Uses system clock ( gettimeofday )
49 class SystemClock < TimeBase
50 # See: TimeBase#current_time
53 now.sec * 1_000_000_000 + now.usec * 1_000
58 # See: TimeBase#resolution
59 def resolution; 1000; end
65 # Returns a hash of available clocks.
67 # All clocks are taken from the TimeBase namespace
70 if @_old_constants == c
74 @_available_clocks = c.map do |cname|
75 [cname, const_get(cname)]
77 obj[1].kind_of?(Class) and
78 obj[1].ancestors.include?(TimeBase)
79 end.inject(Hash.new) do |hash, obj|
80 hash[obj[0].sub(/Clock$/, '').downcase] = obj[1]
86 # Returns a Clock instance if it is found by the clock_name designation.
88 # If fallback is set to true and no clock has been found, it returns
90 def get_clock(name, fallback = false)
92 clock = self.available_clocks[name]
95 clock = self.available_clocks['system']
102 alias has_clock? get_clock
108 require 'timebase_ext'
110 warn "WARNING: timebase extension not found"