| Class | SynFlow |
| In: |
lib/syn_flow.rb
|
| Parent: | Object |
| i | [R] | |
| state | [R] |
# 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
# 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
# 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