class HyperThread::Pool

Attributes

queue[R]

Public Class Methods

new(max: 2) click to toggle source
# File lib/hyper_thread/pool.rb, line 5
def initialize(max: 2)
  @threads = []
  @mutex   = Mutex.new  
  @queue   = Queue.new  
  @max     = max.to_i   
end

Public Instance Methods

<<(thread) click to toggle source
# File lib/hyper_thread/pool.rb, line 118
def <<(thread)
  nsync do
    return false if @threads.count == max
    @threads << thread
  end
end
async(forever: false, count: 1, &block) click to toggle source
# File lib/hyper_thread/pool.rb, line 55
def async(forever: false, count: 1, &block)
  raise "Required block syntax" unless block_given?
  count.times do
    if @threads.count == max
      @queue << block
      next
    end
    self << Thread.new do
      if forever
        loop do
          nsync do 
            block.call
          end
        end
      else
        nsync do 
          block.call
        end
      end
    end
  end
  return @queue if @threads.count == max
  @threads
end
max() click to toggle source
# File lib/hyper_thread/pool.rb, line 23
def max
  @max
end
max=(value) click to toggle source
# File lib/hyper_thread/pool.rb, line 12
def max=(value)
  nsync do
    if value > @max
      dead = @threads.sample(value).map(&:exit)
      @threads -= dead
    end
    @max = value
    return true
  end
end
nsync(&block) click to toggle source
# File lib/hyper_thread/pool.rb, line 47
def nsync(&block)
  @mutex.synchronize do
    break if @shutdown
    block.call
  end
  return if @shutdown
end
qsync(count: 1, &block) click to toggle source
# File lib/hyper_thread/pool.rb, line 99
def qsync(count: 1, &block)
  nsync do 
    raise "Required block syntax" unless block_given?
    Thread.new do
      count.times do
        @queue << block
      end
    end
    true
  end
end
reap() click to toggle source
# File lib/hyper_thread/pool.rb, line 40
def reap
  dead = @threads.reject(&:alive?)
  dead.map(&:kill)
  @threads -= dead
  return true
end
shutdown() click to toggle source
# File lib/hyper_thread/pool.rb, line 34
def shutdown
  @shutdown = true
  dead = @threads.map(&:exit)
  @threads -= dead
end
threads() { |thread| ... } click to toggle source
# File lib/hyper_thread/pool.rb, line 27
def threads
  return @threads unless block_given?
  @threads.each do |thread|
    yield thread
  end
end
todo(forever: false, count: false) { |pop| ... } click to toggle source
# File lib/hyper_thread/pool.rb, line 80
def todo(forever: false, count: false)
  raise "Required block syntax" unless block_given?
  if count
    count.times do
      yield @queue.pop
    end
  else
    if forever
      loop do  
        while @queue.size > 0
          yield @queue.pop
        end
      end
    else
      yield @queue.pop
    end
  end
end
todo?() click to toggle source
# File lib/hyper_thread/pool.rb, line 111
def todo?
  nsync do 
    return true if @queue.size > 0
    false
  end
end