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.



884
885
886
887
888
889
890
# File 'lib/daedalus.rb', line 884

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

  calculate_max(max)
end

Class Method Details

.detect_cpusObject



912
913
914
915
916
917
918
919
920
921
922
923
924
925
# File 'lib/daedalus.rb', line 912

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



892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
# File 'lib/daedalus.rb', line 892

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



933
934
935
936
937
# File 'lib/daedalus.rb', line 933

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

#perform_tasks(tasks) ⇒ Object



939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
# File 'lib/daedalus.rb', line 939

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



978
979
980
981
982
983
984
985
986
987
# File 'lib/daedalus.rb', line 978

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



927
928
929
930
931
# File 'lib/daedalus.rb', line 927

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