class Dynflow::Semaphores::Stateful

Attributes

free[R]
meta[R]
tickets[R]
waiting[R]

Public Class Methods

new(tickets, free = tickets, meta = {}) click to toggle source
# File lib/dynflow/semaphores/stateful.rb, line 8
def initialize(tickets, free = tickets, meta = {})
  @tickets = tickets
  @free = free
  @waiting = []
  @meta = meta
end
new_from_hash(hash) click to toggle source
# File lib/dynflow/semaphores/stateful.rb, line 66
def self.new_from_hash(hash)
  self.new(*hash.values_at(:tickets, :free, :meta))
end

Public Instance Methods

drain() click to toggle source
# File lib/dynflow/semaphores/stateful.rb, line 51
def drain
  @free.tap do
    @free = 0
    save
  end
end
get(n = 1) click to toggle source
# File lib/dynflow/semaphores/stateful.rb, line 41
def get(n = 1)
  if n > @free
    drain
  else
    @free -= n
    save
    n
  end
end
get_waiting() click to toggle source
# File lib/dynflow/semaphores/stateful.rb, line 24
def get_waiting
  @waiting.shift
end
has_waiting?() click to toggle source
# File lib/dynflow/semaphores/stateful.rb, line 28
def has_waiting?
  !@waiting.empty?
end
release(n = 1) click to toggle source
# File lib/dynflow/semaphores/stateful.rb, line 32
def release(n = 1)
  @free += n
  @free = @tickets unless @tickets.nil? || @free <= @tickets
  save
end
save() click to toggle source
# File lib/dynflow/semaphores/stateful.rb, line 38
def save
end
to_hash() click to toggle source
# File lib/dynflow/semaphores/stateful.rb, line 58
def to_hash
  {
    :tickets => @tickets,
    :free => @free,
    :meta => @meta
  }
end
wait(thing) click to toggle source
# File lib/dynflow/semaphores/stateful.rb, line 15
def wait(thing)
  if get > 0
    true
  else
    @waiting << thing
    false
  end
end