Sha256: f37486af1a01c0324dfe99b28ff0fa3d6184c109460d05e77c105507d8a53922

Contents?: true

Size: 1.47 KB

Versions: 4

Compression:

Stored size: 1.47 KB

Contents

module ForkAndReturn
  module Util	# :nodoc: Stuff copied from my personal library.
    def self.multi_core?
      @multi_core	||= cores > 1
    end

    def self.cores
      @cores	||= (l = cpu_info.length) < 1 ? 1 : l
    end

    def self.cpu_info	# TODO: Might not be correct, but it works for now.
      @cpu_info ||=
      begin
        cpus	= []

        if File.file?("/proc/cpuinfo")
          File.open("/proc/cpuinfo") do |f|
            while line = f.gets
              key, value	= line.chomp.split(/\s*:\s*/, 2)

              if key and value
                cpus << {}	if key == "processor"

                cpus[-1][key]	= value
              end
            end
          end
        end

        cpus
      end
    end

    def self.forkable?
      @forkable ||=
      begin
        Process.wait(
          Process.fork do
            Process.exit!
          end
        )

        true
      rescue NotImplementedError
        false
      end
    end

    def self.generate_counter(count=0)
      fun =
      lambda do
        Thread.exclusive do
          count	= count.succ
        end
      end

      class << fun
        alias next call
      end

      fun
    end

    @tempfile_counter	= Util.generate_counter

    def self.tempfile
      File.join(tempdir, "%s.%d.%d.tmp" % ["fork_and_return", $$, @tempfile_counter.next])
    end

    def self.tempdir
      [ENV["TMPDIR"], ENV["TMP"], ENV["TEMP"], "/tmp", "c:/temp"].compact.find{|dir| File.directory?(dir)}
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
forkandreturn-0.1.0 lib/forkandreturn/util.rb
forkandreturn-0.1.1 lib/forkandreturn/util.rb
forkandreturn-0.2.0 lib/forkandreturn/util.rb
forkandreturn-0.2.1 lib/forkandreturn/util.rb