class Fibril::Guard

Attributes

guard_seq[RW]
break_condition[RW]
depleted[RW]
depleted_at[RW]
fibril[RW]
id[RW]
result[RW]
timeout_period[RW]

Public Class Methods

create(fibril, counter=1) click to toggle source

Create a new guard for a given fibril and add a reference to it to this same fibril.

# File lib/fibril/guard.rb, line 15
def self.create(fibril, counter=1)
  self.guard_seq += 1
  guard = Fibril::Guard.new(self.guard_seq, counter, fibril)
  fibril.guards << guard
  return guard
end
new(id, counter, fibril) click to toggle source

Create a new guard object. A guard can have a break condition which is either

  1. A counter, guard will deplete when it has been visited this many times

  2. A break condition, guard will deplete when this proc/lambda returns true

# File lib/fibril/guard.rb, line 48
def initialize(id, counter, fibril)
  self.id = id
  self.fibril =  fibril
  self.break_condition = counter
  self.result = NoResult
end

Public Instance Methods

await() click to toggle source

Continue to process fibrils until this guard is depleted.

# File lib/fibril/guard.rb, line 32
def await
  Fibril.current.tick while !self.depleted
end
cancel() click to toggle source

Schedule this guard to deplete the next time it is visited

# File lib/fibril/guard.rb, line 39
def cancel
  self.break_condition = 1
end
deplete(result) click to toggle source

Deplete the guard. The guard has served its purpose

# File lib/fibril/guard.rb, line 87
def deplete(result)
  self.result = result
  self.depleted = true
  self.depleted_at = Time.now
  Fibril.deplete_guard(self, result)
end
depleted?() click to toggle source
# File lib/fibril/guard.rb, line 26
def depleted?
  depleted
end
loop(break_condition=-1, timeout=0, &blck) click to toggle source

Loop the fibril associated with a guard either a set number of times or until a block evaluates to true

# File lib/fibril/guard.rb, line 98
def loop(break_condition=-1, timeout=0, &blck)
  self.break_condition = block_given? ? blck : break_condition
  self.timeout_period = timeout
  self
end
result?() click to toggle source
# File lib/fibril/guard.rb, line 22
def result?
  self.result != NoResult
end
until(*guards, &blk) click to toggle source

Equivalent of loop

# File lib/fibril/guard.rb, line 114
def until(*guards, &blk)
  if block_given?
    loop{ blk[] }
  else
    loop{
      guards.map{|guard| guard.kind_of?(Symbol) ? Fibril.guard.send(guard) : guard}.all?(&:depleted?)
    }
  end
end
visit(result=nil) click to toggle source

Visit this guard. This is called everytime the fibril associated with this guard completes. If the guard does not deplete the fibril resets and runs again

# File lib/fibril/guard.rb, line 59
def visit(result=nil)
  case self.break_condition
  when Proc
    if self.break_condition[]
      self.deplete(result)
    else
      self.fibril    = self.fibril.reset(self)
    end
  else
    self.break_condition -= 1
    if self.break_condition.zero?
      self.deplete(result)
    else
      unless timeout_period.zero?
        if timeout_period > 0.1
          async.sleep(timeout_period)
        else
          sleep(timeout_period)
        end
      end
      self.fibril = self.fibril.reset(self)
    end
  end
end
while(&blk) click to toggle source

The inverse of loop. Loop until a block evalutes to true

# File lib/fibril/guard.rb, line 107
def while(&blk)
  loop{ !blk[] }
end