Class: Dataflow::Variable

Inherits:
Object show all
Defined in:
lib/vendor/dataflow/dataflow.rb

Overview

Note that this class uses instance variables directly rather than nicely initialized instance variables in get/set methods for memory and performance reasons

Constant Summary collapse

LOCK =
Monitor.new

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Variable

Returns a new instance of Variable.



69
# File 'lib/vendor/dataflow/dataflow.rb', line 69

def initialize(&block) @__trigger__ = block if block_given? end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



108
109
110
111
112
# File 'lib/vendor/dataflow/dataflow.rb', line 108

def method_missing(name, *args, &block)
  return "#<Dataflow::Variable:#{__id__} unbound>" if !@__bound__ && name == :inspect
  __wait__
  @__value__.__send__(name, *args, &block)
end

Instance Method Details

#__activate_trigger__Object



90
91
92
93
94
# File 'lib/vendor/dataflow/dataflow.rb', line 90

def __activate_trigger__
  @__value__ = @__trigger__.call
  @__bound__ = true
  @__trigger__ = nil # GC
end

#__binding_condition__Object

Lazy-load conditions to be nice on memory usage



72
# File 'lib/vendor/dataflow/dataflow.rb', line 72

def __binding_condition__() @__binding_condition__ ||= LOCK.new_cond end

#__dataflow__?Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/vendor/dataflow/dataflow.rb', line 114

def __dataflow__?
  true
end

#__unify__(value) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/vendor/dataflow/dataflow.rb', line 74

def __unify__(value)
  LOCK.synchronize do
    __activate_trigger__ if @__trigger__
    if @__bound__
      return @__value__.__unify__(value) if @__value__.__dataflow__? rescue nil
      raise UnificationError, "#{@__value__.inspect} != #{value.inspect}" if self != value
    else
      @__value__ = value
      @__bound__ = true
      __binding_condition__.broadcast # wakeup all method callers
      @__binding_condition__ = nil # GC
    end
  end
  @__value__
end

#__wait__Object



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/vendor/dataflow/dataflow.rb', line 96

def __wait__
  LOCK.synchronize do
    unless @__bound__
      if @__trigger__
        __activate_trigger__
      else
        __binding_condition__.wait
      end
    end
  end unless @__bound__
end