Class: Gem::Dependent::Parallel

Inherits:
Object
  • Object
show all
Defined in:
lib/rubygems/dependent/parallel.rb

Defined Under Namespace

Classes: ExceptionWrapper

Constant Summary collapse

VERSION =
'0.5.8'

Class Method Summary collapse

Class Method Details

.each(array, options = {}, &block) ⇒ Object



37
38
39
40
# File 'lib/rubygems/dependent/parallel.rb', line 37

def self.each(array, options={}, &block)
  map(array, options.merge(:preserve_results => false), &block)
  array
end

.each_with_index(array, options = {}, &block) ⇒ Object



42
43
44
# File 'lib/rubygems/dependent/parallel.rb', line 42

def self.each_with_index(array, options={}, &block)
  each(array, options.merge(:with_index => true), &block)
end

.in_processes(options = {}, &block) ⇒ Object



31
32
33
34
35
# File 'lib/rubygems/dependent/parallel.rb', line 31

def self.in_processes(options = {}, &block)
  count, options = extract_count_from_options(options)
  count ||= processor_count
  map(0...count, options.merge(:in_processes => count), &block)
end

.in_threads(options = {:count => 2}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rubygems/dependent/parallel.rb', line 14

def self.in_threads(options={:count => 2})
  count, options = extract_count_from_options(options)

  out = []
  threads = []

  count.times do |i|
    threads[i] = Thread.new do
      out[i] = yield(i)
    end
  end

  wait_for_threads(threads)

  out
end

.map(array, options = {}, &block) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rubygems/dependent/parallel.rb', line 46

def self.map(array, options = {}, &block)
  array = array.to_a # turn Range and other Enumerable-s into an Array

  if options[:in_threads]
    method = :in_threads
    size = options[method]
  else
    method = :in_processes
    size = options[method] || processor_count
  end
  size = [array.size, size].min

  return work_direct(array, options, &block) if size == 0

  if method == :in_threads
    work_in_threads(array, options.merge(:count => size), &block)
  else
    work_in_processes(array, options.merge(:count => size), &block)
  end
end

.map_with_index(array, options = {}, &block) ⇒ Object



67
68
69
# File 'lib/rubygems/dependent/parallel.rb', line 67

def self.map_with_index(array, options={}, &block)
  map(array, options.merge(:with_index => true), &block)
end

.processor_countObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rubygems/dependent/parallel.rb', line 71

def self.processor_count
  case RbConfig::CONFIG['host_os']
  when /darwin9/
    `hwprefs cpu_count`.to_i
  when /darwin/
    (hwprefs_available? ? `hwprefs thread_count` : `sysctl -n hw.ncpu`).to_i
  when /linux/
    `grep -c processor /proc/cpuinfo`.to_i
  when /freebsd/
    `sysctl -n hw.ncpu`.to_i
  when /mswin|mingw/
    require 'win32ole'
    wmi = WIN32OLE.connect("winmgmts://")
    cpu = wmi.ExecQuery("select NumberOfLogicalProcessors from Win32_Processor")
    cpu.to_enum.first.NumberOfLogicalProcessors
  end
end