Class: Wire
- Inherits:
-
Thread
- Object
- Thread
- Wire
- Defined in:
- lib/wire.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(args, &block) ⇒ Wire
constructor
A new instance of Wire.
- #runner ⇒ Object
Constructor Details
#initialize(args, &block) ⇒ Wire
Returns a new instance of Wire.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/wire.rb', line 9 def initialize(args, &block) args.keys.each { |name| instance_variable_set "@" + name.to_s, args[name] } if @max.to_i <= 0 or @wait.nil? warn "Both max and wait needs to be passed, where max > 0. Using default values" @max = 10 if @max.to_i <= 0 @wait ||= 1 end @block = block @counter = Wire.counter @counter.synchronize do @counter.cond.wait_until { @counter.i < @max } @counter.inc end if @counter.last and (t = Time.now.to_f - @counter.last) < @wait sleep (@wait - t) end super { runner } end |
Class Method Details
.counter ⇒ Object
5 6 7 |
# File 'lib/wire.rb', line 5 def self.counter @counter ||= Counter.new end |
Instance Method Details
#runner ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/wire.rb', line 33 def runner @block.call(*@vars) rescue => error raise error ensure @counter.synchronize do if @max == @counter.i or @counter.last @counter.last = Time.now.to_f end @counter.dec @counter.cond.signal end end |