Sha256: 5d839f8718d8a75bded14e37a86617ca412b63bcd95ae0dcb92d30bec770459c

Contents?: true

Size: 1.74 KB

Versions: 2

Compression:

Stored size: 1.74 KB

Contents

require 'common/ext/stdout'
require 'stringio'
require 'thread'

module Bake
  module Multithread

    class Jobs

      @@mutex_sempaphore = Mutex.new
      @@running_threads = 0
      @@waiting_threads = 0
      @@cv = ConditionVariable.new

      def self.incThread
        @@mutex_sempaphore.synchronize do
          if @@running_threads >= Bake.options.threads
            @@waiting_threads += 1
            @@cv.wait(@@mutex_sempaphore)
            @@waiting_threads -= 1
            @@running_threads += 1
          else
            @@running_threads += 1
          end
        end
      end
      def self.decThread
        @@mutex_sempaphore.synchronize do
          @@running_threads -= 1
          if @@waiting_threads > 0
            @@cv.signal
          end
        end
      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], Thread.current[:errorStream]) do |outStr, errStr|
            Thread.current[:stdout] = outStr
            Thread.current[:errorStream] = errStr
            Jobs.incThread()
            block.call(self)
            Jobs.decThread()
          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

2 entries across 2 versions & 1 rubygems

Version Path
bake-toolkit-2.34.1 lib/multithread/job.rb
bake-toolkit-2.34.0 lib/multithread/job.rb