Sha256: 5690fd9cc830535027704119d4cade98a62bdf6eefb9e6b4a7ee1c8631b177ee

Contents?: true

Size: 1.04 KB

Versions: 11

Compression:

Stored size: 1.04 KB

Contents

require 'thread'

module Concurrent

  class FixedThreadPool

    class Worker

      def initialize(queue, parent)
        @queue = queue
        @parent = parent
        @mutex = Mutex.new
      end

      def dead?
        return @mutex.synchronize do
          @thread.nil? ? false : ! @thread.alive?
        end
      end

      def kill
        @mutex.synchronize do
          Thread.kill(@thread) unless @thread.nil?
          @thread = nil
        end
      end

      def run(thread = Thread.current)
        @mutex.synchronize do
          raise StandardError.new('already running') unless @thread.nil?
          @thread = thread
        end

        loop do
          task = @queue.pop
          if task == :stop
            @thread = nil
            @parent.on_worker_exit(self)
            break
          end

          @parent.on_start_task(self)
          begin
            task.last.call(*task.first)
          rescue
            # let it fail
          ensure
            @parent.on_end_task(self)
          end
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
concurrent-ruby-0.5.0 lib/concurrent/fixed_thread_pool/worker.rb
concurrent-ruby-0.5.0.pre.1 lib/concurrent/fixed_thread_pool/worker.rb
concurrent-ruby-0.4.1 lib/concurrent/fixed_thread_pool/worker.rb
concurrent-ruby-0.4.0 lib/concurrent/fixed_thread_pool/worker.rb
concurrent-ruby-0.3.2 lib/concurrent/fixed_thread_pool/worker.rb
concurrent-ruby-0.3.1 lib/concurrent/fixed_thread_pool/worker.rb
concurrent-ruby-0.3.1.pre.2 lib/concurrent/fixed_thread_pool/worker.rb
concurrent-ruby-0.3.1.pre.1 lib/concurrent/fixed_thread_pool/worker.rb
concurrent-ruby-0.3.0 lib/concurrent/fixed_thread_pool/worker.rb
concurrent-ruby-0.3.0.pre.3 lib/concurrent/fixed_thread_pool/worker.rb
concurrent-ruby-0.3.0.pre.2 lib/concurrent/fixed_thread_pool/worker.rb