Class: Fibril::Guard

Inherits:
Object
  • Object
show all
Defined in:
lib/fibril/guard.rb

Defined Under Namespace

Classes: NoResult

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, counter, fibril) ⇒ Guard

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



48
49
50
51
52
53
# 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

Class Attribute Details

.guard_seqObject

Returns the value of attribute guard_seq.



3
4
5
# File 'lib/fibril/guard.rb', line 3

def guard_seq
  @guard_seq
end

Instance Attribute Details

#break_conditionObject

Returns the value of attribute break_condition.



6
7
8
# File 'lib/fibril/guard.rb', line 6

def break_condition
  @break_condition
end

#depletedObject

Returns the value of attribute depleted.



6
7
8
# File 'lib/fibril/guard.rb', line 6

def depleted
  @depleted
end

#depleted_atObject

Returns the value of attribute depleted_at.



6
7
8
# File 'lib/fibril/guard.rb', line 6

def depleted_at
  @depleted_at
end

#fibrilObject

Returns the value of attribute fibril.



6
7
8
# File 'lib/fibril/guard.rb', line 6

def fibril
  @fibril
end

#idObject

Returns the value of attribute id.



6
7
8
# File 'lib/fibril/guard.rb', line 6

def id
  @id
end

#resultObject

Returns the value of attribute result.



6
7
8
# File 'lib/fibril/guard.rb', line 6

def result
  @result
end

#timeout_periodObject

Returns the value of attribute timeout_period.



6
7
8
# File 'lib/fibril/guard.rb', line 6

def timeout_period
  @timeout_period
end

Class Method Details

.create(fibril, counter = 1) ⇒ Object

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



15
16
17
18
19
20
# 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

Instance Method Details

#awaitObject

Continue to process fibrils until this guard is depleted.



32
33
34
# File 'lib/fibril/guard.rb', line 32

def await
  Fibril.current.tick while !self.depleted
end

#cancelObject

Schedule this guard to deplete the next time it is visited



39
40
41
# File 'lib/fibril/guard.rb', line 39

def cancel
  self.break_condition = 1
end

#deplete(result) ⇒ Object

Deplete the guard. The guard has served its purpose



87
88
89
90
91
92
# 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?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/fibril/guard.rb', line 26

def depleted?
  depleted
end

#loop(break_condition = -1,, timeout = 0, &blck) ⇒ Object

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



98
99
100
101
102
# 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?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/fibril/guard.rb', line 22

def result?
  self.result != NoResult
end

#until(*guards, &blk) ⇒ Object

Equivalent of loop



114
115
116
117
118
119
120
121
122
# 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) ⇒ Object

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



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# 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) ⇒ Object

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



107
108
109
# File 'lib/fibril/guard.rb', line 107

def while(&blk)
  loop{ !blk[] }
end