AsyncSourceAsyncVariable

class Variable

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

Definitions

def initialize(condition = Condition.new)

Create a new variable.

Signature

parameter condition Condition

The condition to use for synchronization.

Implementation

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

def resolve(value = true)

Resolve the value.

Signals all waiting tasks.

Signature

parameter value Object

The value to resolve.

Implementation

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

def resolved?

Whether the value has been resolved.

Signature

returns Boolean

Whether the value has been resolved.

Implementation

def resolved?
	@condition.nil?
end

def wait

Wait for the value to be resolved.

Signature

returns Object

The resolved value.

Implementation

def wait
	@condition&.wait
	return @value
end

Discussion