Class: Async::Variable

Inherits:
Object
  • Object
show all
Defined in:
lib/async/variable.rb

Overview

A synchronization primitive that allows one task to wait for another task to resolve a value.

Instance Method Summary collapse

Constructor Details

#initialize(condition = Condition.new) ⇒ Variable

Create a new variable.



14
15
16
17
# File 'lib/async/variable.rb', line 14

def initialize(condition = Condition.new)
	@condition = condition
	@value = nil
end

Instance Method Details

#resolve(value = true) ⇒ Object Also known as: value=

Resolve the value.

Signals all waiting tasks.



24
25
26
27
28
29
30
31
32
# File 'lib/async/variable.rb', line 24

def resolve(value = true)
	@value = value
	condition = @condition
	@condition = nil
	
	self.freeze
	
	condition.signal(value)
end

#resolved?Boolean

Whether the value has been resolved.

Returns:

  • (Boolean)


39
40
41
# File 'lib/async/variable.rb', line 39

def resolved?
	@condition.nil?
end

#waitObject Also known as: value

Wait for the value to be resolved.



46
47
48
49
# File 'lib/async/variable.rb', line 46

def wait
	@condition&.wait
	return @value
end