Class: SynFlow

Inherits:
Object show all
Defined in:
lib/syn_flow.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

@@i =
-1
@@out =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(factory, initial_state) ⇒ SynFlow

Returns a new instance of SynFlow.



175
176
177
178
179
180
181
182
# File 'lib/syn_flow.rb', line 175

def initialize ( factory, initial_state )
  @i = (@@i += 1)
  super()
  @mutex = Mutex.new
  @condition = ConditionVariable.new
  @factory = factory
  @state = initial_state
end

Instance Attribute Details

#iObject (readonly)

Returns the value of attribute i.



172
173
174
# File 'lib/syn_flow.rb', line 172

def i
  @i
end

#stateObject (readonly)

Returns the value of attribute state.



171
172
173
# File 'lib/syn_flow.rb', line 171

def state
  @state
end

Class Method Details

.debug_outObject



236
237
238
# File 'lib/syn_flow.rb', line 236

def self.debug_out
  @@out
end

Instance Method Details

#destroyObject



229
230
231
# File 'lib/syn_flow.rb', line 229

def destroy
  @i = @mutex = @condition = @factory = @state = nil
end

#feed(label) ⇒ Object Also known as: advance, <<



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/syn_flow.rb', line 185

def feed ( label )
  D "#@i: Want read label #{label} (#@state)"
  @mutex.synchronize do
    while not @factory.include?(@state, label)
      begin
        @condition.wait(@mutex)
      rescue ThreadError
        raise Error,
          'Cannot wait for change state because only one thread is running'
      end
    end
    dest = @factory.delta(@state, label)
    D "#@i: Ok change state with label #{label} (#@state -> #{dest})"
    @state = dest
    @condition.broadcast
  end
end

#try_feed(label) ⇒ Object Also known as: try_advance



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/syn_flow.rb', line 207

def try_feed ( label )
  D "#@i: Try read label #{label} (#@state)"
  if @mutex.try_lock
    begin
      if @factory.include?(@state, label)
        dest = @factory.delta(@state, label)
        D "#@i: Ok change state with label #{label} (#@state -> #{dest})"
        @state = dest
        @condition.broadcast
        return true
      end
    ensure
      @mutex.unlock
    end
  end
  D "#@i: Ko you cannot change state"
  return false
end