Sha256: 2c281d2151de938f79438a9bb0d9698eb1c4c8f6c991f57b219ebdd988c99296

Contents?: true

Size: 1.4 KB

Versions: 48

Compression:

Stored size: 1.4 KB

Contents

require_relative '../common/ext/stdout'
require 'stringio'
require 'thread'
require 'monitor' # this should fix a missing require in concurrent gem
require 'concurrent/atomic/mutex_semaphore'

module Bake
  module Multithread

    class Jobs

      def self.incThread
        @@semaphore.acquire
      end
      def self.decThread
        @@semaphore.release
      end
      def self.init_semaphore
        @@semaphore = ::Concurrent::MutexSemaphore.new(Bake.options.threads)
      end

      def initialize(jobs, &block)
        nr_of_threads = [Bake.options.threads, jobs.length].min
        @jobs = jobs
        @threads = []
        nr_of_threads.times do
          @threads << ::Thread.new(Thread.current[:stdout]) do |outStr|
            Thread.current[:stdout] = outStr
            begin
              Jobs.incThread()
              block.call(self)
            ensure
              Jobs.decThread()
            end
          end
        end
      end

      def failed
        @failed ||= false
      end
      def set_failed
        @failed = true
      end

      def get_next_or_nil
        the_next = nil
        mutex.synchronize {
          the_next = @jobs.shift
        }
        the_next
      end
      def join
        @threads.each{|t| while not t.join(2) do end}
      end
      def mutex
        @mutex ||= Mutex.new
      end
    end

  end
end

Version data entries

48 entries across 48 versions & 1 rubygems

Version Path
bake-toolkit-2.72.0 lib/multithread/job.rb
bake-toolkit-2.71.0 lib/multithread/job.rb
bake-toolkit-2.70.1 lib/multithread/job.rb
bake-toolkit-2.70.0 lib/multithread/job.rb
bake-toolkit-2.69.0 lib/multithread/job.rb
bake-toolkit-2.68.1 lib/multithread/job.rb
bake-toolkit-2.68.0 lib/multithread/job.rb
bake-toolkit-2.67.0 lib/multithread/job.rb
bake-toolkit-2.66.0 lib/multithread/job.rb
bake-toolkit-2.65.2 lib/multithread/job.rb
bake-toolkit-2.65.1 lib/multithread/job.rb
bake-toolkit-2.65.0 lib/multithread/job.rb
bake-toolkit-2.64.4 lib/multithread/job.rb
bake-toolkit-2.64.3 lib/multithread/job.rb
bake-toolkit-2.64.2 lib/multithread/job.rb
bake-toolkit-2.64.1 lib/multithread/job.rb
bake-toolkit-2.64.0 lib/multithread/job.rb
bake-toolkit-2.63.2 lib/multithread/job.rb
bake-toolkit-2.63.1 lib/multithread/job.rb
bake-toolkit-2.63.0 lib/multithread/job.rb