Class: Async::Actor::Variable
- Inherits:
-
Object
- Object
- Async::Actor::Variable
- Defined in:
- lib/async/actor/variable.rb
Class Method Summary collapse
Instance Method Summary collapse
- #fail(error) ⇒ Object
- #get ⇒ Object
-
#initialize ⇒ Variable
constructor
A new instance of Variable.
- #set(value) ⇒ Object
Constructor Details
#initialize ⇒ Variable
Returns a new instance of Variable.
20 21 22 23 24 25 26 |
# File 'lib/async/actor/variable.rb', line 20 def initialize @set = nil @value = nil @guard = Thread::Mutex.new @condition = Thread::ConditionVariable.new end |
Class Method Details
.fulfill(variable) ⇒ Object
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/async/actor/variable.rb', line 9 def self.fulfill(variable) variable.set(yield) variable = nil rescue => error variable&.fail(error) variable = nil ensure # throw, etc: variable&.fail(RuntimeError.new("Invalid flow control!")) end |
Instance Method Details
#fail(error) ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'lib/async/actor/variable.rb', line 38 def fail(error) @guard.synchronize do raise "Variable already set!" unless @set.nil? @set = false @error = error @condition.broadcast end end |
#get ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/async/actor/variable.rb', line 48 def get @guard.synchronize do while @set.nil? @condition.wait(@guard) end if @set return @value else raise @error end end end |
#set(value) ⇒ Object
28 29 30 31 32 33 34 35 36 |
# File 'lib/async/actor/variable.rb', line 28 def set(value) @guard.synchronize do raise "Variable already set!" unless @set.nil? @set = true @value = value @condition.broadcast end end |