Class: Dynflow::Semaphores::Aggregating

Inherits:
Abstract
  • Object
show all
Defined in:
lib/dynflow/semaphores/aggregating.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(children) ⇒ Aggregating

Returns a new instance of Aggregating.



8
9
10
11
# File 'lib/dynflow/semaphores/aggregating.rb', line 8

def initialize(children)
  @children = children
  @waiting  = []
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



6
7
8
# File 'lib/dynflow/semaphores/aggregating.rb', line 6

def children
  @children
end

#waitingObject (readonly)

Returns the value of attribute waiting.



6
7
8
# File 'lib/dynflow/semaphores/aggregating.rb', line 6

def waiting
  @waiting
end

Instance Method Details

#drainObject



44
45
46
47
48
# File 'lib/dynflow/semaphores/aggregating.rb', line 44

def drain
  available = free
  @children.values.each { |child| child.get available }
  available
end

#freeObject



50
51
52
# File 'lib/dynflow/semaphores/aggregating.rb', line 50

def free
  @children.values.map(&:free).reduce { |acc, cur| cur < acc ? cur : acc }
end

#get(n = 1) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/dynflow/semaphores/aggregating.rb', line 34

def get(n = 1)
  available = free
  if n > available
    drain
  else
    @children.values.each { |child| child.get n }
    n
  end
end

#get_waitingObject



22
23
24
# File 'lib/dynflow/semaphores/aggregating.rb', line 22

def get_waiting
  @waiting.shift
end

#has_waiting?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/dynflow/semaphores/aggregating.rb', line 26

def has_waiting?
  !@waiting.empty?
end

#release(n = 1, key = nil) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/dynflow/semaphores/aggregating.rb', line 54

def release(n = 1, key = nil)
  if key.nil?
    @children.values.each { |child| child.release n }
  else
    @children[key].release n
  end
end

#saveObject



30
31
32
# File 'lib/dynflow/semaphores/aggregating.rb', line 30

def save
  @children.values.each(&:save)
end

#wait(thing) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/dynflow/semaphores/aggregating.rb', line 13

def wait(thing)
  if get > 0
    true
  else
    @waiting << thing
    false
  end
end