Sha256: bbb0e46ae53342bab72eee91d661ee691891b6ee6fded2f89b779405cce761d6

Contents?: true

Size: 1.85 KB

Versions: 19

Compression:

Stored size: 1.85 KB

Contents

require 'thread'
begin
  require 'fastthread'
rescue LoadError
#  $stderr.puts "Using the ruby-core thread implementation"
end

class ThreadPool
  class Worker
    def initialize(thread_queue)
      @block = nil
      @mutex = Mutex.new
      @cv = ConditionVariable.new
      @queue = thread_queue
      @running = true
      @thread = Thread.new do
        @mutex.synchronize do
          while @running
            @cv.wait(@mutex)
            block = get_block
            if block
              @mutex.unlock
              block.call
              @mutex.lock
              reset_block
            end
            @queue << self
          end
        end
      end
    end

    def name
      @thread.inspect
    end

    def get_block
      @block
    end

    def set_block(block)
      @mutex.synchronize do
        raise RuntimeError, "Thread already busy." if @block
        @block = block
        # Signal the thread in this class, that there's a job to be done
        @cv.signal
      end
    end

    def reset_block
      @block = nil
    end

    def busy?
      @mutex.synchronize { !@block.nil? }
    end

    def stop
      @mutex.synchronize do
        @running = false
        @cv.signal
      end
      @thread.join
    end
  end

  attr_accessor :max_size

  def initialize(max_size = 10)
    @max_size = max_size
    @queue = Queue.new
    @workers = []
  end

  def size
    @workers.size
  end

  def busy?
    @queue.size < @workers.size
  end

  def shutdown
    @workers.each { |w| w.stop }
    @workers = []
  end

  alias :join :shutdown

  def process(block=nil,&blk)
    block = blk if block_given?
    worker = get_worker
    worker.set_block(block)
  end

  private

  def get_worker
    if !@queue.empty? or @workers.size == @max_size
      return @queue.pop
    else
      worker = Worker.new(@queue)
      @workers << worker
      worker
    end
  end

end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
tpkg-2.2.1 lib/tpkg/thread_pool.rb
tpkg-2.2.0 lib/tpkg/thread_pool.rb
tpkg-2.1.1 lib/tpkg/thread_pool.rb
tpkg-2.1.0 lib/tpkg/thread_pool.rb
tpkg-2.0.1 lib/tpkg/thread_pool.rb
tpkg-2.0.0 lib/tpkg/thread_pool.rb
tpkg-1.27.4 lib/tpkg/thread_pool.rb
tpkg-1.27.3 lib/tpkg/thread_pool.rb
tpkg-1.27.1 lib/tpkg/thread_pool.rb
tpkg-1.25.1 lib/tpkg/thread_pool.rb
tpkg-1.23.3 lib/tpkg/thread_pool.rb
tpkg-1.23.2 lib/tpkg/thread_pool.rb
tpkg-1.22.1 lib/tpkg/thread_pool.rb
tpkg-1.21.1 lib/tpkg/thread_pool.rb
tpkg-1.21.0 lib/tpkg/thread_pool.rb
tpkg-1.20.0 lib/tpkg/thread_pool.rb
tpkg-1.19.2 lib/tpkg/thread_pool.rb
tpkg-1.18.2 lib/tpkg/thread_pool.rb
tpkg-1.16.2 lib/tpkg/thread_pool.rb