Class: SynFlow
Defined Under Namespace
Classes: Error
Constant Summary collapse
- @@i =
-1
- @@out =
[]
Instance Attribute Summary collapse
-
#i ⇒ Object
readonly
Returns the value of attribute i.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
Class Method Summary collapse
Instance Method Summary collapse
- #destroy ⇒ Object
- #feed(label) ⇒ Object (also: #advance, #<<)
-
#initialize(factory, initial_state) ⇒ SynFlow
constructor
A new instance of SynFlow.
- #try_feed(label) ⇒ Object (also: #try_advance)
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
#i ⇒ Object (readonly)
Returns the value of attribute i.
172 173 174 |
# File 'lib/syn_flow.rb', line 172 def i @i end |
#state ⇒ Object (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_out ⇒ Object
236 237 238 |
# File 'lib/syn_flow.rb', line 236 def self.debug_out @@out end |
Instance Method Details
#destroy ⇒ Object
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 |