class Frypan::Signal::InputThread

Public Class Methods

new(buf_size=1, &proc) click to toggle source
# File lib/frypan.rb, line 113
def initialize(buf_size=1, &proc)
  @buf_size = buf_size
  @proc = proc
end

Public Instance Methods

__calc(memo0, memo1, memo2) click to toggle source
# 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) click to toggle source
# File lib/frypan.rb, line 118
def atom(thread, &block)
  thread[:mutex].synchronize(&block)
end
get_inputs(thread) click to toggle source
# File lib/frypan.rb, line 140
def get_inputs(thread)
  atom(thread){
    inputs = thread[:inputs]
    thread[:inputs] = []
    return inputs
  }
end
make_thread() click to toggle source
# 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