class Dynflow::Semaphores::Aggregating

Attributes

children[R]
waiting[R]

Public Class Methods

new(children) click to toggle source
# File lib/dynflow/semaphores/aggregating.rb, line 7
def initialize(children)
  @children = children
  @waiting  = []
end

Public Instance Methods

drain() click to toggle source
# File lib/dynflow/semaphores/aggregating.rb, line 43
def drain
  available = free
  @children.values.each { |child| child.get available }
  available
end
free() click to toggle source
# File lib/dynflow/semaphores/aggregating.rb, line 49
def free
  @children.values.map(&:free).reduce { |acc, cur| cur < acc ? cur : acc }
end
get(n = 1) click to toggle source
# File lib/dynflow/semaphores/aggregating.rb, line 33
def get(n = 1)
  available = free
  if n > available
    drain
  else
    @children.values.each { |child| child.get n }
    n
  end
end
get_waiting() click to toggle source
# File lib/dynflow/semaphores/aggregating.rb, line 21
def get_waiting
  @waiting.shift
end
has_waiting?() click to toggle source
# File lib/dynflow/semaphores/aggregating.rb, line 25
def has_waiting?
  !@waiting.empty?
end
release(n = 1, key = nil) click to toggle source
# File lib/dynflow/semaphores/aggregating.rb, line 53
def release(n = 1, key = nil)
  if key.nil?
    @children.values.each { |child| child.release n }
  else
    @children[key].release n
  end
end
save() click to toggle source
# File lib/dynflow/semaphores/aggregating.rb, line 29
def save
  @children.values.each(&:save)
end
wait(thing) click to toggle source
# File lib/dynflow/semaphores/aggregating.rb, line 12
def wait(thing)
  if get > 0
    true
  else
    @waiting << thing
    false
  end
end