Module: Einhorn::Compat
- Defined in:
- lib/einhorn/compat.rb
Class Method Summary collapse
- .accept_nonblock(server) ⇒ Object
- .cloexec!(fd, enable) ⇒ Object
- .cloexec?(fd) ⇒ Boolean
-
.exec(script, args, opts = {}) ⇒ Object
Opts are ignored in Ruby 1.8.
-
.pipe ⇒ Object
In Ruby 2.1.0 (and possibly earlier), IO.pipe sets cloexec on the descriptors.
- .processor_count ⇒ Object
- .unixserver_new(path) ⇒ Object
Class Method Details
.accept_nonblock(server) ⇒ Object
42 43 44 45 46 |
# File 'lib/einhorn/compat.rb', line 42 def self.accept_nonblock(server) sock = server.accept_nonblock cloexec!(sock, false) sock end |
.cloexec!(fd, enable) ⇒ Object
12 13 14 15 16 17 18 19 20 |
# File 'lib/einhorn/compat.rb', line 12 def self.cloexec!(fd, enable) original = fd.fcntl(Fcntl::F_GETFD) if enable new = original | Fcntl::FD_CLOEXEC else new = original & (-Fcntl::FD_CLOEXEC-1) end fd.fcntl(Fcntl::F_SETFD, new) end |
.cloexec?(fd) ⇒ Boolean
22 23 24 |
# File 'lib/einhorn/compat.rb', line 22 def self.cloexec?(fd) fd.fcntl(Fcntl::F_GETFD) & Fcntl::FD_CLOEXEC end |
.exec(script, args, opts = {}) ⇒ Object
Opts are ignored in Ruby 1.8
27 28 29 30 31 32 33 34 |
# File 'lib/einhorn/compat.rb', line 27 def self.exec(script, args, opts={}) cmd = [script, script] begin Kernel.exec(cmd, *(args + [opts])) rescue TypeError Kernel.exec(cmd, *args) end end |
.pipe ⇒ Object
In Ruby 2.1.0 (and possibly earlier), IO.pipe sets cloexec on the descriptors.
5 6 7 8 9 10 |
# File 'lib/einhorn/compat.rb', line 5 def self.pipe readable, writeable = IO.pipe cloexec!(readable, false) cloexec!(writeable, false) [readable, writeable] end |
.processor_count ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/einhorn/compat.rb', line 48 def self.processor_count # jruby if defined? Java::Java return Java::Java.lang.Runtime.getRuntime.availableProcessors end # linux / friends begin return File.read('/proc/cpuinfo').scan(/^processor\s*:/).count rescue Errno::ENOENT end # OS X if RUBY_PLATFORM =~ /darwin/ return Integer(`sysctl -n hw.logicalcpu`) end # windows / friends begin require 'win32ole' rescue LoadError else wmi = WIN32OLE.connect("winmgmts://") wmi.ExecQuery("select * from Win32_ComputerSystem").each do |system| begin processors = system.NumberOfLogicalProcessors rescue processors = 0 end return [system.NumberOfProcessors, processors].max end end raise "Failed to detect number of CPUs" end |
.unixserver_new(path) ⇒ Object
36 37 38 39 40 |
# File 'lib/einhorn/compat.rb', line 36 def self.unixserver_new(path) server = UNIXServer.new(path) cloexec!(server, false) server end |