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

ruby-changes:61636

From: nicholas <ko1@a...>
Date: Tue, 9 Jun 2020 18:57:31 +0900 (JST)
Subject: [ruby-changes:61636] b6bae0ec44 (master): Return seconds slept after Thread.scheduler wait_sleep

https://git.ruby-lang.org/ruby.git/commit/?id=b6bae0ec44

From b6bae0ec4433bcea6ba94a50de344c2e3b90fee9 Mon Sep 17 00:00:00 2001
From: "nicholas a. evans" <nicholas.evans@g...>
Date: Tue, 9 Jun 2020 00:30:43 -0400
Subject: Return seconds slept after Thread.scheduler wait_sleep

Kernel#sleep should still return seconds slept, even when using a
Thread.scheduler. The return value of Scheduler#wait_sleep can be
ignored.

diff --git a/process.c b/process.c
index e8a786d..bb30a86 100644
--- a/process.c
+++ b/process.c
@@ -4920,20 +4920,20 @@ rb_f_spawn(int argc, VALUE *argv, VALUE _) https://github.com/ruby/ruby/blob/trunk/process.c#L4920
 static VALUE
 rb_f_sleep(int argc, VALUE *argv, VALUE _)
 {
+    time_t beg = time(0);
     VALUE scheduler = rb_current_thread_scheduler();
 
     if (scheduler != Qnil) {
-        VALUE result = rb_funcallv(scheduler, rb_intern("wait_sleep"), argc, argv);
-        return RTEST(result);
-    }
-
-    time_t beg = time(0);
-    if (argc == 0) {
-	rb_thread_sleep_forever();
+        rb_funcallv(scheduler, rb_intern("wait_sleep"), argc, argv);
     }
     else {
-	rb_check_arity(argc, 0, 1);
-	rb_thread_wait_for(rb_time_interval(argv[0]));
+        if (argc == 0) {
+            rb_thread_sleep_forever();
+        }
+        else {
+            rb_check_arity(argc, 0, 1);
+            rb_thread_wait_for(rb_time_interval(argv[0]));
+        }
     }
 
     time_t end = time(0) - beg;
diff --git a/test/fiber/test_sleep.rb b/test/fiber/test_sleep.rb
index bfb892e..4dadbb5 100644
--- a/test/fiber/test_sleep.rb
+++ b/test/fiber/test_sleep.rb
@@ -14,7 +14,7 @@ class TestFiberSleep < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/fiber/test_sleep.rb#L14
 
       5.times do |i|
         Fiber do
-          sleep(i/100.0)
+          assert(sleep(i/100.0) >= 0)
           items << i
         end
       end
@@ -27,4 +27,21 @@ class TestFiberSleep < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/fiber/test_sleep.rb#L27
 
     assert_equal ITEMS, items
   end
+
+  def test_sleep_returns_seconds_slept
+    seconds = nil
+
+    thread = Thread.new do
+      scheduler = Scheduler.new
+      Thread.current.scheduler = scheduler
+      Fiber do
+        seconds = sleep(2)
+      end
+    end
+
+    thread.join
+
+    assert(seconds >= 2, "actual: %p" % seconds)
+  end
+
 end
-- 
cgit v0.10.2


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

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