Class: Daedalus::TaskRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/daedalus.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(compiler, tasks, max = nil) ⇒ TaskRunner

Returns a new instance of TaskRunner.



804
805
806
807
808
809
810
# File 'lib/daedalus.rb', line 804

def initialize(compiler, tasks, max=nil)
  @max = TaskRunner.detect_cpus
  @tasks = tasks
  @compiler = compiler

  calculate_max(max)
end

Class Method Details

.detect_cpusObject



832
833
834
835
836
837
838
839
840
841
842
843
844
845
# File 'lib/daedalus.rb', line 832

def self.detect_cpus
  if RUBY_PLATFORM =~ /windows/
    return 1
  else
    if RUBY_PLATFORM =~ /bsd/
      key = 'NPROCESSORS_CONF'
    else
      key = '_NPROCESSORS_CONF'
    end
    count = `getconf #{key} 2>&1`.to_i
    return 1 if $?.exitstatus != 0
    return count
  end
end

Instance Method Details

#calculate_max(max) ⇒ Object



812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
# File 'lib/daedalus.rb', line 812

def calculate_max(max)
  cpus = TaskRunner.detect_cpus

  case max
  when nil # auto
    case cpus
    when 1, 2
      @max = cpus
    when 4
      @max = 3
    else
      @max = 4
    end
  when Fixnum
    @max = max
  when "cpu"
    @max = cpus
  end
end

#linear_tasks(tasks) ⇒ Object



853
854
855
856
857
# File 'lib/daedalus.rb', line 853

def linear_tasks(tasks)
  tasks.each do |task|
    task.build @compiler
  end
end

#perform_tasks(tasks) ⇒ Object



859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
# File 'lib/daedalus.rb', line 859

def perform_tasks(tasks)
  count = tasks.size

  puts "Running #{count} tasks using #{@max} parallel threads"
  start = Time.now

  queue = Queue.new
  threads = []

  @max.times do
    threads << Thread.new {
      while true
        task = queue.pop
        break unless task
        task.build @compiler
      end
    }
  end

  sync = []

  queue_tasks(queue, tasks, sync)

  # Kill off the builders
  threads.each do |t|
    queue << nil
  end

  threads.each do |t|
    t.join
  end

  sync.each do |task|
    task.build @compiler
  end

  puts "Build time: #{Time.now - start} seconds"
end

#queue_tasks(queue, tasks, sync) ⇒ Object



898
899
900
901
902
903
904
905
906
907
# File 'lib/daedalus.rb', line 898

def queue_tasks(queue, tasks, sync)
  tasks.each do |task|
    if task.kind_of? Array
      queue_tasks queue, task[1..-1], sync
      sync << task[0]
    else
      queue.push task
    end
  end
end

#startObject



847
848
849
850
851
# File 'lib/daedalus.rb', line 847

def start
  linear_tasks @tasks.pre
  perform_tasks @tasks.default
  linear_tasks @tasks.post
end