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.



886
887
888
889
890
891
892
# File 'lib/daedalus.rb', line 886

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

  calculate_max(max)
end

Class Method Details

.detect_cpusObject



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

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



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

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



935
936
937
938
939
# File 'lib/daedalus.rb', line 935

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

#perform_tasks(tasks) ⇒ Object



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
977
978
# File 'lib/daedalus.rb', line 941

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



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

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



929
930
931
932
933
# File 'lib/daedalus.rb', line 929

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