Class: AtDo
- Inherits:
-
Object
- Object
- AtDo
- Defined in:
- lib/atdo.rb
Constant Summary collapse
- VERSION =
"0.3"
Instance Method Summary collapse
- #at(time, &action) ⇒ Object
-
#initialize ⇒ AtDo
constructor
A new instance of AtDo.
- #stop ⇒ Object
- #stop! ⇒ Object
- #thread ⇒ Object
Constructor Details
#initialize ⇒ AtDo
Returns a new instance of AtDo.
6 7 8 9 10 11 |
# File 'lib/atdo.rb', line 6 def initialize @events = [] ## option to use rbtree @mon = Monitor.new @cvar = @mon.new_cond @thread = nil end |
Instance Method Details
#at(time, &action) ⇒ Object
23 24 25 26 27 28 29 30 |
# File 'lib/atdo.rb', line 23 def at time, &action thread @mon.synchronize do @events << [time, action] t, a = @events.sort_by! {|t, a| t}.first @cvar.signal if t == time end end |
#stop ⇒ Object
13 14 15 16 17 |
# File 'lib/atdo.rb', line 13 def stop @mon.synchronize do @thread.kill if @thread end end |
#stop! ⇒ Object
19 20 21 |
# File 'lib/atdo.rb', line 19 def stop! @thread.kill if @thread end |
#thread ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/atdo.rb', line 32 def thread @thread ||= Thread.new do @mon.synchronize do loop do duration = loop do t, a = @events.first break nil if !t t_now = Time.now break t-t_now if t > t_now begin a.call rescue => ex ## ? end @events.shift end @cvar.wait *duration end end end end |