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.



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

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



104
105
106
107
108
# File 'lib/vendor/dataflow/dataflow.rb', line 104

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



86
87
88
89
90
# File 'lib/vendor/dataflow/dataflow.rb', line 86

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

#__binding_condition__Object

Lazy-load conditions to be nice on memory usage



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

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

#__dataflow__?Boolean

Returns:

  • (Boolean)


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

def __dataflow__?
  true
end

#__unify__(value) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/vendor/dataflow/dataflow.rb', line 70

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



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/vendor/dataflow/dataflow.rb', line 92

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