[前][次][番号順一覧][スレッド一覧]

ruby-changes:35679

From: akr <ko1@a...>
Date: Thu, 2 Oct 2014 12:23:37 +0900 (JST)
Subject: [ruby-changes:35679] akr:r47761 (trunk): * ext/etc/etc.c (etc_nprocessors): New method.

akr	2014-10-02 12:23:26 +0900 (Thu, 02 Oct 2014)

  New Revision: 47761

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=47761

  Log:
    * ext/etc/etc.c (etc_nprocessors): New method.
      Accepted by matz at RubyKaigi 2014.
      [ruby-core:65142] [Feature #10267]

  Modified files:
    trunk/ChangeLog
    trunk/ext/etc/etc.c
    trunk/test/etc/test_etc.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 47760)
+++ ChangeLog	(revision 47761)
@@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Thu Oct  2 12:21:52 2014  Tanaka Akira  <akr@f...>
+
+	* 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@8...>
 
 	* iseq.c (rb_iseq_line_trace_each): explicit cast
Index: ext/etc/etc.c
===================================================================
--- ext/etc/etc.c	(revision 47760)
+++ ext/etc/etc.c	(revision 47761)
@@ -891,6 +891,38 @@ io_pathconf(VALUE io, VALUE arg) https://github.com/ruby/ruby/blob/trunk/ext/etc/etc.c#L891
 #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) https://github.com/ruby/ruby/blob/trunk/ext/etc/etc.c#L978
     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",
Index: test/etc/test_etc.rb
===================================================================
--- test/etc/test_etc.rb	(revision 47760)
+++ test/etc/test_etc.rb	(revision 47761)
@@ -159,4 +159,9 @@ class TestEtc < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/etc/test_etc.rb#L159
     }
   end if defined?(Etc::PC_PIPE_BUF)
 
+  def test_nprocessors
+    n = Etc.nprocessors
+    assert_operator(1, :<=, n)
+  end
+
 end

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]