Class: Frypan::Signal::InputThread

Inherits:
Frypan::Signal show all
Defined in:
lib/frypan.rb

Instance Method Summary collapse

Methods inherited from Frypan::Signal

#__pull, #__pull_deps, #__same, async_input, const, foldp, #foldp, input, #lift, lift, #method_missing

Constructor Details

#initialize(buf_size = 1, &proc) ⇒ InputThread

Returns a new instance of InputThread.



113
114
115
116
# File 'lib/frypan.rb', line 113

def initialize(buf_size=1, &proc)
  @buf_size = buf_size
  @proc = proc
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Frypan::Signal

Instance Method Details

#__calc(memo0, memo1, memo2) ⇒ Object



148
149
150
151
152
153
154
155
156
# File 'lib/frypan.rb', line 148

def __calc(memo0, memo1, memo2)
  unless memo1.has_key?(self)
    memo0.merge(self => [[], make_thread])
  else
    inputs = get_inputs(memo1[self][1])
    memo1[self][1].run
    return memo0.merge(self => [inputs, memo1[self][1]])
  end
end

#atom(thread, &block) ⇒ Object



118
119
120
# File 'lib/frypan.rb', line 118

def atom(thread, &block)
  thread[:mutex].synchronize(&block)
end

#get_inputs(thread) ⇒ Object



140
141
142
143
144
145
146
# File 'lib/frypan.rb', line 140

def get_inputs(thread)
  atom(thread){
    inputs = thread[:inputs]
    thread[:inputs] = []
    return inputs
  }
end

#make_threadObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/frypan.rb', line 122

def make_thread
  thread = ::Thread.new(@proc) do |proc| 
    sleep
    while true
      input = proc.call
      size = atom(Thread.current){
        Thread.current[:inputs] << input
        Thread.current[:inputs].size
      }
      sleep if size >= @buf_size
    end
  end
  thread[:mutex] = Mutex.new
  thread[:inputs] = []
  thread.run
  return thread
end