Class: AtDo
- Inherits:
-
Object
- Object
- AtDo
- Defined in:
- lib/atdo.rb
Constant Summary collapse
- VERSION =
0.2
Instance Method Summary collapse
- #at(time, &action) ⇒ Object
-
#initialize ⇒ AtDo
constructor
A new instance of AtDo.
- #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 @mutex = Mutex.new @cvar = ConditionVariable.new @thread = nil end |
Instance Method Details
#at(time, &action) ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/atdo.rb', line 17 def at time, &action thread @mutex.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 |
# File 'lib/atdo.rb', line 13 def stop @thread.kill if @thread end |
#thread ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/atdo.rb', line 26 def thread @thread ||= Thread.new do @mutex.synchronize do loop do t_now = Time.now duration = loop do t, a = @events.first break nil if !t break t-t_now if t > t_now a.call @events.shift end @cvar.wait @mutex, *duration end end end end |