Class: LibvirtAsync::Timer
- Inherits:
-
Object
- Object
- LibvirtAsync::Timer
- Includes:
- WithDbg
- Defined in:
- lib/libvirt_async/timer.rb
Defined Under Namespace
Classes: Monitor
Instance Attribute Summary collapse
-
#interval ⇒ Object
Returns the value of attribute interval.
-
#last_fired ⇒ Object
Returns the value of attribute last_fired.
-
#monitor ⇒ Object
readonly
Returns the value of attribute monitor.
-
#opaque ⇒ Object
readonly
Returns the value of attribute opaque.
-
#timer_id ⇒ Object
readonly
Returns the value of attribute timer_id.
Instance Method Summary collapse
-
#initialize(timer_id, interval, opaque) ⇒ Timer
constructor
A new instance of Timer.
- #inspect ⇒ Object
- #register ⇒ Object
- #to_s ⇒ Object
- #unregister ⇒ Object
- #wait_time ⇒ Object
Constructor Details
#initialize(timer_id, interval, opaque) ⇒ Timer
Returns a new instance of Timer.
46 47 48 49 50 51 52 53 54 |
# File 'lib/libvirt_async/timer.rb', line 46 def initialize(timer_id, interval, opaque) dbg { "#{self.class}#initialize timer_id=#{timer_id}, interval=#{interval}" } @timer_id = timer_id @interval = interval.to_f / 1000.to_f @opaque = opaque @last_fired = Time.now.to_f @monitor = nil end |
Instance Attribute Details
#interval ⇒ Object
Returns the value of attribute interval.
44 45 46 |
# File 'lib/libvirt_async/timer.rb', line 44 def interval @interval end |
#last_fired ⇒ Object
Returns the value of attribute last_fired.
44 45 46 |
# File 'lib/libvirt_async/timer.rb', line 44 def last_fired @last_fired end |
#monitor ⇒ Object (readonly)
Returns the value of attribute monitor.
43 44 45 |
# File 'lib/libvirt_async/timer.rb', line 43 def monitor @monitor end |
#opaque ⇒ Object (readonly)
Returns the value of attribute opaque.
43 44 45 |
# File 'lib/libvirt_async/timer.rb', line 43 def opaque @opaque end |
#timer_id ⇒ Object (readonly)
Returns the value of attribute timer_id.
43 44 45 |
# File 'lib/libvirt_async/timer.rb', line 43 def timer_id @timer_id end |
Instance Method Details
#inspect ⇒ Object
106 107 108 |
# File 'lib/libvirt_async/timer.rb', line 106 def inspect to_s end |
#register ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/libvirt_async/timer.rb', line 61 def register dbg { "#{self.class}#register starts timer_id=#{timer_id}, interval=#{interval}" } if wait_time.nil? dbg { "#{self.class}#register no wait time timer_id=#{timer_id}, interval=#{interval}" } return end task = Util.create_task do dbg { "#{self.class}#register async starts timer_id=#{timer_id}, interval=#{interval}" } now_time = Time.now.to_f timeout = wait_time > now_time ? wait_time - now_time : 0 @monitor = Monitor.new cancelled = wait_timer(timeout) if cancelled dbg { "#{self.class}#register async cancel timer_id=#{timer_id}, interval=#{interval}" } else dbg { "#{self.class}#register async ready timer_id=#{timer_id}, interval=#{interval}" } self.last_fired = Time.now.to_f dispatch end end dbg { "#{self.class}#register invokes fiber=0x#{task.fiber.object_id.to_s(16)} timer_id=#{timer_id}, interval=#{interval}" } task.run dbg { "#{self.class}#register ends timer_id=#{timer_id}, interval=#{interval}" } end |
#to_s ⇒ Object
102 103 104 |
# File 'lib/libvirt_async/timer.rb', line 102 def to_s "#<#{self.class}:0x#{object_id.to_s(16)} timer_id=#{timer_id} interval=#{interval} last_fired=#{last_fired} monitor=#{monitor}>" end |
#unregister ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/libvirt_async/timer.rb', line 90 def unregister dbg { "#{self.class}#unregister_timer timer_id=#{timer_id}, interval=#{interval}" } if @monitor.nil? dbg { "#{self.class}#unregister_timer already unregistered timer_id=#{timer_id}, interval=#{interval}" } return end @monitor.close @monitor = nil end |
#wait_time ⇒ Object
56 57 58 59 |
# File 'lib/libvirt_async/timer.rb', line 56 def wait_time return if interval < 0 last_fired + interval end |