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.



826
827
828
829
830
831
832
# File 'lib/daedalus.rb', line 826

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

  calculate_max(max)
end

Class Method Details

.detect_cpusObject



854
855
856
857
858
859
860
861
862
863
864
865
866
867
# File 'lib/daedalus.rb', line 854

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



834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
# File 'lib/daedalus.rb', line 834

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



875
876
877
878
879
# File 'lib/daedalus.rb', line 875

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

#perform_tasks(tasks) ⇒ Object



881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
# File 'lib/daedalus.rb', line 881

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



920
921
922
923
924
925
926
927
928
929
# File 'lib/daedalus.rb', line 920

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



869
870
871
872
873
# File 'lib/daedalus.rb', line 869

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