summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-10-02 03:23:26 +0000
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-10-02 03:23:26 +0000
commit7b25bd8ba247ba7f0c1b3542b89aac220f0564ed (patch)
tree36433f05b9e1eb57fc8b9da1b1e64bb1aff46900
parent57da36ebcfafa68b988a2b5b23e645f2aecc724c (diff)
* ext/etc/etc.c (etc_nprocessors): New method.
Accepted by matz at RubyKaigi 2014. [ruby-core:65142] [Feature #10267] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@47761 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog6
-rw-r--r--ext/etc/etc.c33
-rw-r--r--test/etc/test_etc.rb5
3 files changed, 44 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog
index b59a09eb771..9dd8fac86b4 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+Thu Oct 2 12:21:52 2014 Tanaka Akira <akr@fsij.org>
+
+ * ext/etc/etc.c (etc_nprocessors): New method.
+ Accepted by matz at RubyKaigi 2014.
+ [ruby-core:65142] [Feature #10267]
+
Thu Oct 2 07:56:49 2014 Eric Wong <e@80x24.org>
* iseq.c (rb_iseq_line_trace_each): explicit cast
diff --git a/ext/etc/etc.c b/ext/etc/etc.c
index 6168ed15106..25465607137 100644
--- a/ext/etc/etc.c
+++ b/ext/etc/etc.c
@@ -891,6 +891,38 @@ io_pathconf(VALUE io, VALUE arg)
#define io_pathconf rb_f_notimplement
#endif
+#if defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
+/*
+ * Returns the number of online processors.
+ *
+ * The result is intended as the number of processes to
+ * use all available processors.
+ *
+ * This method is implemented as:
+ * - sysconf(_SC_NPROCESSORS_ONLN): GNU/Linux, NetBSD, FreeBSD, OpenBSD, DragonFly BSD, OpenIndiana, Mac OS X, AIX
+ *
+ * Example:
+ *
+ * require 'etc'
+ * p Etc.nprocessors #=> 4
+ *
+ */
+static VALUE
+etc_nprocessors(VALUE obj)
+{
+ long ret;
+
+ errno = 0;
+ ret = sysconf(_SC_NPROCESSORS_ONLN);
+ if (ret == -1) {
+ rb_sys_fail("sysconf(_SC_NPROCESSORS_ONLN)");
+ }
+ return LONG2NUM(ret);
+}
+#else
+#define etc_nprocessors rb_f_notimplement
+#endif
+
/*
* The Etc module provides access to information typically stored in
* files in the /etc directory on Unix systems.
@@ -946,6 +978,7 @@ Init_etc(void)
rb_define_module_function(mEtc, "sysconf", etc_sysconf, 1);
rb_define_module_function(mEtc, "confstr", etc_confstr, 1);
rb_define_method(rb_cIO, "pathconf", io_pathconf, 1);
+ rb_define_module_function(mEtc, "nprocessors", etc_nprocessors, 0);
sPasswd = rb_struct_define_under(mEtc, "Passwd",
"name",
diff --git a/test/etc/test_etc.rb b/test/etc/test_etc.rb
index 4a099e7df56..2f61ac7919b 100644
--- a/test/etc/test_etc.rb
+++ b/test/etc/test_etc.rb
@@ -159,4 +159,9 @@ class TestEtc < Test::Unit::TestCase
}
end if defined?(Etc::PC_PIPE_BUF)
+ def test_nprocessors
+ n = Etc.nprocessors
+ assert_operator(1, :<=, n)
+ end
+
end